Merge: doc: fixed some typos and other misc. corrections
[nit.git] / tests / test_postgres_native.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_native
18
19 import postgresql::native_postgres
20
21 var db_suffix = "NIT_TESTING_ID".environ
22 var db = new NativePostgres.connectdb("host=postgres user=postgres dbname=postgres")
23 assert postgres_open: db.status.is_ok else print_error db.error
24
25 var result = db.exec("CREATE TABLE IF NOT EXISTS animals_{db_suffix} (aname TEXT PRIMARY KEY, class TEXT NOT NULL, sex INTEGER)")
26 assert postgres_create_table: result.status.is_ok else print_error db.error
27
28 result = db.exec("INSERT INTO animals_{db_suffix} VALUES('Whale', 'mammal', 1)")
29 assert postgres_insert_1: result.status.is_ok else print_error db.error
30
31 result = db.exec("INSERT INTO animals_{db_suffix} VALUES('Snake', 'reptile', 0)")
32 assert postgres_insert_2: result.status.is_ok else print_error db.error
33
34 result = db.exec("SELECT * FROM animals_{db_suffix}")
35 assert postgres_select: result.status.is_ok else print_error db.error
36
37 assert postgres_ntuples: result.ntuples == 2 else print_error db.error
38 assert postgres_nfields: result.nfields == 3 else print_error db.error
39 assert postgres_fname: result.fname(0) == "aname" else print_error db.error
40 assert postgres_isnull: not result.is_null(0,0) else print_error db.error
41 assert postgres_value: result.value(0,0) == "Whale" else print_error db.error
42
43 var cols: Int = result.nfields
44 var rows: Int = result.ntuples
45 var fields: String = ""
46 for c in [0..cols[ do fields += result.fname(c) + " "
47 print fields
48 for i in [0..rows[ do
49 fields = ""
50 for j in [0..cols[ do fields += result.value(i, j) + " "
51 print fields
52 end
53
54 result = db.exec("DELETE FROM animals_{db_suffix} WHERE aname = 'Lioness'")
55 assert postgres_delete_1: result.status.is_ok else print_error db.error
56
57 result = db.exec("DELETE FROM animals_{db_suffix} WHERE aname = 'Snake'")
58 assert postgres_delete_2: result.status.is_ok else print_error db.error
59
60 result = db.prepare("PREPARED_INSERT", "INSERT INTO animals_{db_suffix}(aname, class, sex) VALUES ($1, $2, $3)", 3)
61 assert postgres_prepare: result.status.is_ok else print_error db.error
62
63 result = db.exec("DELETE FROM animals_{db_suffix} WHERE aname = 'Frog'")
64 assert postgres_delete_3: result.status.is_ok else print_error db.error
65
66 var values = ["Frog", "Anphibian", "1"]
67 var lengths = [values[0].length, values[1].length, values[2].length]
68 var formats = [0,0,0]
69 result = db.exec_prepared("PREPARED_INSERT", 3, values, lengths, formats,0)
70 assert postgres_exec_prepared: result.status.is_ok else print_error db.error
71
72 result = db.exec("DROP TABLE animals_{db_suffix}")
73 assert postgres_drop_table: result.status.is_ok else print_error db.error
74 db.finish