tests: add a test for the Nity sqlite3 module
[nit.git] / tests / test_sqlite3_nity.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Guillaume Auger <jeho@resist.ca>
4 # Copyright 2013-2014 Alexis Laferrière <alexis.laf@xymus.net>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 module test_sqlite3_nity
19
20 import sqlite3
21
22 var path = "test_nity.db"
23 if path.file_exists then path.file_delete
24
25 var db = new Sqlite3DB.open(path)
26 assert db.is_open else print db.error or else "no error?"
27
28 assert db.create_table("IF NOT EXISTS users (uname TEXT PRIMARY KEY, pass TEXT NOT NULL, activated INTEGER, perc FLOAT)") else
29 print db.error or else "no error?"
30 end
31
32 assert db.insert("INTO users VALUES('Bob', 'zzz', 1, 77.7)") else
33 print db.error or else "no error?"
34 end
35
36 assert db.insert("INTO users VALUES('Guillaume', 'xxx', 1, 88.8)") else
37 print db.error or else "no error?"
38 end
39
40 for row in db.select("* FROM users") do
41 print "####"
42
43 printn "{row[0].name}: "
44 print row[0]
45
46 printn "{row[1].name}: "
47 var val = row[1].value
48 assert val isa Text
49 print val.to_s
50
51 printn "{row[2].name}: "
52 print row[2].to_i
53
54 printn "{row[3].name}: "
55 print row[3].to_f
56 end
57
58 print db.is_open
59 db.close
60 print db.is_open