Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_postgres_nity.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2016 Guilherme Mansur <guilhermerpmansur@gmail.com>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 module test_postgres_nity
18
19 import postgresql::postgres
20
21 var db_suffix = "NIT_TESTING_ID".environ
22 var db = new Postgres.open("host=postgres user=postgres dbname=postgres")
23 assert open_db: not db.is_closed else print db.error
24
25 assert create_table: db.create_table("IF NOT EXISTS users_{db_suffix} (uname TEXT PRIMARY KEY, pass TEXT NOT NULL, activated INTEGER, perc FLOAT)") else
26 print db.error
27 end
28
29 assert insert1: db.insert("INTO users_{db_suffix} VALUES('Bob', 'zzz', 1, 77.7)") else
30 print db.error
31 end
32
33 assert insert2: db.insert("INTO users_{db_suffix} VALUES('Guilherme', 'xxx', 1, 88)") else
34 print db.error
35 end
36
37 var result = db.raw_execute("SELECT * FROM users_{db_suffix}")
38
39 assert raw_exec: result.is_ok else print db.error
40
41 assert postgres_nfields: result.nfields == 4 else print_error db.error
42 assert postgres_fname: result.fname(0) == "uname" else print_error db.error
43 assert postgres_isnull: not result.is_null(0,0) else print_error db.error
44 assert postgres_value: result.value(0,0) == "Bob" else print_error db.error
45
46 assert drop_table: db.execute("DROP TABLE users_{db_suffix}") else print db.error
47
48 db.finish
49
50 assert db.is_closed else print db.error