2 # Copyright (C) 2006-2010, Parrot Foundation.
7 t/library/pg.t -- Postgres Tests
11 ./parrot t/library/pg.t
15 Test Parrot's libpg interface. The test is using the user's default
16 table, which should be created by your sysadmin.
20 .const int N_TESTS = 43
23 ## .include 'postgres.pasm'
24 .const int CONNECTION_OK = 0
26 .const int PGRES_COMMAND_OK = 1
27 .const int PGRES_TUPLES_OK = 2
30 load_bytecode 'Test/Builder.pir'
32 test = new [ 'Test'; 'Builder' ]
36 # TODO: fix when exception handling works again
42 load_bytecode 'postgres.pir'
44 test.'ok'(1, 'load_bytecode')
45 load_bytecode 'Pg.pir'
46 test.'ok'(1, 'load_bytecode Pg')
48 .local pmc cl, con, res
50 test.'ok'(1, 'Pg class exists')
51 con = cl.'connectdb'('') # assume table = user is present
52 $I0 = isa con, ['Pg'; 'Conn']
53 test.'ok'($I0, 'con isa Pg;Conn')
56 if $I0 goto have_connected
57 test.'skip'( 39, 'no Pg connection; skipping remaining tests' )
61 test.'ok'($I0, 'con is true after connect')
63 $I1 = iseq $I0, CONNECTION_OK
64 test.'ok'($I1, 'con.status() == CONNECTION_OK ')
67 $P1 = get_root_global ['parrot';'Pg'], 'PQstatus'
70 $I1 = iseq $I0, CONNECTION_OK
71 test.'ok'($I1, 'status(PGconn) == CONNECTION_OK ')
73 res = con.'exec'('BEGIN')
74 test.'ok'(1, 'exec BEGIN called')
75 $I0 = isa res, ['Pg'; 'Result']
76 test.'ok'($I0, 'res isa Pg;Result')
77 $I0 = res.'resultStatus'()
78 $I1 = iseq $I0, PGRES_COMMAND_OK
79 test.'ok'($I1, 'res.resultStatus() == PGRES_COMMAND_OK ')
81 # install a notice receiver to silent the CREATE
82 .const 'Sub' cb = 'notice'
83 $P0 = con.'setNoticeReceiver'(cb, test)
85 res = con.'exec'(<<'EOT')
86 CREATE TEMP TABLE parrot_tbl (
92 $I0 = res.'resultStatus'()
93 $I1 = iseq $I0, PGRES_COMMAND_OK
94 test.'ok'($I1, 'table created PGRES_COMMAND_OK ')
96 res = con.'exec'(<<'EOT')
97 INSERT INTO parrot_tbl (foo, bar) VALUES('a', 'b')
99 $I0 = res.'resultStatus'()
100 $I1 = iseq $I0, PGRES_COMMAND_OK
101 test.'ok'($I1, 'insert row PGRES_COMMAND_OK ')
103 res = con.'exec'(<<'EOT')
104 SELECT * FROM parrot_tbl
106 $I0 = res.'resultStatus'()
107 $I1 = iseq $I0, PGRES_TUPLES_OK
108 test.'ok'($I1, 'select * PGRES_TUPLES_OK ')
110 $I0 = res.'ntuples'()
112 test.'ok'($I1, 'res.ntuples == 1')
113 $I0 = res.'nfields'()
115 test.'ok'($I1, 'res.nfields == 3')
118 $I1 = iseq $S0, "foo"
119 test.'ok'($I1, 'res.fname(1) == "foo"')
121 $I1 = iseq $S0, "bar"
122 test.'ok'($I1, 'res.fname(1) == "bar"')
123 $I0 = res.'fnumber'('id')
125 test.'ok'($I1, 'res.fnumber("id") == 0')
126 $I0 = res.'fnumber'('bar')
128 test.'ok'($I1, 'res.fnumber("bar") == 2')
129 $I0 = res.'fnumber'('no_such_col_name')
131 test.'ok'($I1, 'res.fnumber("no_such_col_name") == -1')
133 $S0 = res.'getvalue'(0, 1)
135 test.'ok'($I1, 'getvalue(0, 1) == "a"')
136 $S0 = res.'getvalue'(0, 2)
138 test.'ok'($I1, 'getvalue(0, 2) == "b"')
141 res = con.'execParams'(<<'EOT', 'c', 'd')
142 INSERT INTO parrot_tbl (foo, bar) VALUES($1, $2)
144 $I0 = res.'resultStatus'()
145 $I1 = iseq $I0, PGRES_COMMAND_OK
146 test.'ok'($I1, 'insert w execParams PGRES_COMMAND_OK ')
147 res = con.'exec'(<<'EOT')
148 SELECT * FROM parrot_tbl
150 $I0 = res.'ntuples'()
152 test.'ok'($I1, 'res.ntuples == 2')
154 $S0 = res.'getvalue'(1, 1)
156 test.'ok'($I1, 'getvalue(1, 1) == "c"')
157 $S0 = res.'getvalue'(1, 2)
159 test.'ok'($I1, 'getvalue(1, 2) == "d"')
161 res = con.'prepare'('ins2', <<'EOT', 2)
162 INSERT INTO parrot_tbl (foo, bar) VALUES($1, $2)
164 $I0 = res.'resultStatus'()
165 $I1 = iseq $I0, PGRES_COMMAND_OK
166 test.'ok'($I1, 'prepare PGRES_COMMAND_OK ')
168 res = con.'execPrepared'('ins2', 'e', 'f')
169 $I0 = res.'resultStatus'()
170 $I1 = iseq $I0, PGRES_COMMAND_OK
171 test.'ok'($I1, 'execPrepared PGRES_COMMAND_OK ')
172 res = con.'exec'(<<'EOT')
173 SELECT * FROM parrot_tbl
175 $I0 = res.'ntuples'()
177 test.'ok'($I1, 'res.ntuples == 3')
179 res = con.'execPrepared'('ins2', 'g', 'h')
180 $I0 = res.'resultStatus'()
181 $I1 = iseq $I0, PGRES_COMMAND_OK
182 test.'ok'($I1, 'execPrepared PGRES_COMMAND_OK ')
183 res = con.'exec'(<<'EOT')
184 SELECT * FROM parrot_tbl
186 $I0 = res.'ntuples'()
188 test.'ok'($I1, 'res.ntuples == 4')
189 res = con.'exec'(<<'EOT')
190 INSERT INTO parrot_tbl (foo) VALUES('i')
192 $I0 = res.'resultStatus'()
193 $I1 = iseq $I0, PGRES_COMMAND_OK
194 test.'ok'($I1, 'insert row PGRES_COMMAND_OK ')
195 res = con.'exec'(<<'EOT')
196 SELECT * FROM parrot_tbl
198 $I0 = res.'ntuples'()
200 test.'ok'($I1, 'res.ntuples == 5')
201 $S0 = res.'getvalue'(4, 1)
203 test.'ok'($I1, 'getvalue(4, 1) == "i"')
204 $S0 = res.'getvalue'(4, 2)
206 test.'ok'($I1, 'getvalue(4, 2) == ""')
207 $I0 = res.'getisnull'(4, 1)
209 test.'ok'($I1, 'getisnull(4, 1) == 0')
210 $I0 = res.'getisnull'(4, 2)
212 test.'ok'($I1, 'getisnull(4, 2) == 1')
214 res = con.'exec'('ABORT')
215 $I0 = res.'resultStatus'()
216 $I1 = iseq $I0, PGRES_COMMAND_OK
217 test.'ok'($I1, 'ABORT succeeded')
219 # this calls __finalize, but there isn't a good way to test this
220 # because any references to the object would prevent destruction
224 test.'ok'(1, 'con.finish()')
226 test.'ok'($I0, 'con is false after finish')
238 # notice receiver callback function
242 test.'ok'(1, 'notice receiver called')
243 # res ought to be a PGresult struct
245 $I0 = $S0 == 'UnManagedStruct'
246 test.'ok'($I0, 'notice callback got a struct')
249 st = get_root_global ['parrot';'Pg'], 'PQresultStatus'
251 $I1 = iseq $I0, PGRES_COMMAND_OK
252 test.'ok'($I1, 'notice result is still ok')
259 # vim: expandtab shiftwidth=4 ft=pir: