Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_sqlite3_native.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Guillaume Auger <jeho@resist.ca>
4 # Copyright 2013 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_native
19
20 import sqlite3::native_sqlite3
21
22 var filename = "test.db"
23 filename.file_delete
24 var create_req = "CREATE TABLE IF NOT EXISTS users (uname TEXT PRIMARY KEY, pass TEXT NOT NULL, activated INTEGER)"
25 var insert_req_1 = "INSERT INTO users VALUES('Bob', 'zzz', 1)"
26 var insert_req_2 = "INSERT INTO users VALUES('Guillaume', 'xxx', 1)"
27 var select_req = "SELECT * FROM users"
28
29 var db = new NativeSqlite3.open(filename.to_cstring)
30 assert sqlite_open: db.error.is_ok
31
32 db.exec(create_req.to_cstring)
33 assert sqlite_create_table: db.error.is_ok
34
35 db.exec(insert_req_1.to_cstring)
36 assert sqlite_insert_1: db.error.is_ok
37
38 db.exec(insert_req_2.to_cstring)
39 assert sqlite_insert_2: db.error.is_ok
40
41 var stmt = db.prepare(select_req.to_cstring)
42 assert sqlite_select: db.error.is_ok
43 if stmt.address_is_null then
44 print "Prepared failed got: {db.error.to_s}"
45 abort
46 end
47
48 while stmt.step.is_row do
49 print stmt.column_text(0)
50 print stmt.column_text(1)
51 print stmt.column_text(2)
52 end
53
54 db.close
55
56 db = new NativeSqlite3.open(filename.to_cstring)
57 assert sqlite_reopen: db.error.is_ok
58
59 stmt = db.prepare(select_req.to_cstring)
60 assert sqlite_reselect: db.error.is_ok
61 assert not stmt.address_is_null
62 stmt.step
63 assert sqlite_column_0_0_reopened: stmt.column_text(0).to_s == "Bob"