Merge: doc: fixed some typos and other misc. corrections
[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 import sqlite3
19
20 var path = "test_nity.db"
21 #alt1#path = "/../invalid_path.db"
22 if path.file_exists then path.file_delete
23
24 var db = new Sqlite3DB.open(path)
25 assert db.is_open else print db.error or else "no error?"
26
27 assert db.create_table("IF NOT EXISTS users (uname TEXT PRIMARY KEY, pass TEXT NOT NULL, activated INTEGER, perc FLOAT)") else
28 print db.error or else "no error?"
29 end
30
31 assert db.insert("INTO users VALUES('Bob', 'zzz', 1, 77.7)") else
32 print db.error or else "no error?"
33 end
34
35 assert db.insert("INTO users VALUES('Guillaume', 'xxx', 1, 88.8)") else
36 print db.error or else "no error?"
37 end
38
39 #alt2#assert db.insert("INTO notable VALUES('Alexis', 'asdf', 2, 99.9)") else
40 #alt2# print db.error or else "no error?"
41 #alt2#end
42
43 for row in db.select("* FROM users") do
44 print "####"
45
46 printn "{row[0].name}: "
47 print row[0]
48
49 printn "{row[1].name}: "
50 var val = row[1].value
51 assert val isa Text
52 print val.to_s
53
54 printn "{row[2].name}: "
55 print row[2].to_i
56
57 printn "{row[3].name}: "
58 print row[3].to_f
59 end
60
61 print "\nMap test:\n"
62
63 for row in db.select("* FROM users") do
64 var m = row.map
65 print "####"
66 for k, v in m do
67 print "{k} = {v or else "nil"}"
68 end
69 end
70 print ""
71
72 print db.is_open
73 db.close
74 print db.is_open