v2 / vlib / db / pg / pg_result_test.v
57 lines · 47 sloc · 1.41 KB · 8a488593d5d4a101e77952bb6fbf2941293e24f7
Raw
1// vtest build: started_postgres?
2module main
3
4import db.pg
5
6struct Info {
7 table_schema string
8 relname string
9 attname string
10 typename string
11 typealign string
12 typlen int
13}
14
15fn deref(val ?string) string {
16 return val or { panic('no value') }
17}
18
19fn row_mapper(res pg.Result, row pg.Row) !Info {
20 return Info{
21 table_schema: deref(row.vals[res.cols['table_schema']])
22 relname: deref(row.vals[res.cols['relname']])
23 attname: deref(row.vals[res.cols['attname']])
24 typename: deref(row.vals[res.cols['typename']])
25 typealign: deref(row.vals[res.cols['typealign']])
26 typlen: deref(row.vals[res.cols['typlen']]).int()
27 }
28}
29
30fn test_large_exec() {
31 $if !network ? {
32 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
33 eprintln('> This test requires a working postgres server running on localhost.')
34 return
35 }
36
37 db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
38 defer {
39 db.close() or {}
40 }
41
42 result := db.exec_result('
43SELECT ischema.table_schema, c.relname, a.attname, t.typname, t.typalign, t.typlen
44 FROM pg_class c
45 JOIN information_schema.tables ischema on ischema.table_name = c.relname
46 JOIN pg_attribute a ON (a.attrelid = c.oid)
47 JOIN pg_type t ON (t.oid = a.atttypid)
48WHERE
49 a.attnum >= 0
50 ')!
51
52 infos := result.as_structs(row_mapper)!
53
54 assert result.rows.len > 0 && infos.len == result.rows.len
55
56 // println(infos)
57}
58