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