v2 / vlib / db / pg / pg_test.v
146 lines · 123 sloc · 3.81 KB · 4118ee464d0c8849b18e6108f77f2871b29430e4
Raw
1// vtest build: started_postgres?
2module main
3
4import db.pg
5import orm
6
7fn test_large_exec() {
8 $if !network ? {
9 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
10 eprintln('> This test requires a working postgres server running on localhost.')
11 return
12 }
13
14 db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
15 defer {
16 db.close() or {}
17 }
18
19 assert db.validate()!
20
21 rows := db.exec('
22SELECT ischema.table_schema, c.relname, a.attname, t.typname, t.typalign, t.typlen
23 FROM pg_class c
24 JOIN information_schema.tables ischema on ischema.table_name = c.relname
25 JOIN pg_attribute a ON (a.attrelid = c.oid)
26 JOIN pg_type t ON (t.oid = a.atttypid)
27WHERE
28 a.attnum >= 0
29 ')!
30 for row in rows {
31 // We just need to access the memory to ensure it's properly allocated
32 row.str()
33 }
34}
35
36fn test_prepared() {
37 $if !network ? {
38 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
39 eprintln('> This test requires a working postgres server running on localhost.')
40 return
41 }
42 db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
43 defer {
44 db.close() or {}
45 }
46
47 db.prepare('test_prepared', 'SELECT NOW(), $1 AS NAME', 1) or { panic(err) }
48
49 result := db.exec_prepared('test_prepared', ['hello world']) or { panic(err) }
50
51 assert result.len == 1
52}
53
54fn test_transaction() {
55 $if !network ? {
56 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
57 eprintln('> This test requires a working postgres server running on localhost.')
58 return
59 }
60
61 mut db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
62 defer {
63 db.close() or {}
64 }
65 db.exec('drop table if exists users')!
66 db.exec('create table if not exists users (
67 id SERIAL PRIMARY KEY,
68 username TEXT,
69 last_name TEXT NULL DEFAULT NULL
70 )')!
71 mut conn := orm.TransactionalConnection(db)
72 mut tx := orm.begin(mut conn)!
73 tx.transaction[int](fn (mut tx orm.Tx) !int {
74 return 1
75 })!
76 tx.commit()!
77 db.begin()!
78 db.exec("insert into users (username) values ('jackson')")!
79 db.savepoint('savepoint1')!
80 db.exec("insert into users (username) values ('kitty')")!
81 db.rollback_to('savepoint1')!
82 db.exec("insert into users (username) values ('mars')")!
83 db.commit()!
84 rows := db.exec('select * from users')!
85 for row in rows {
86 // We just need to access the memory to ensure it's properly allocated
87 dump(row.str())
88 }
89}
90
91fn test_listen_notify() {
92 $if !network ? {
93 eprintln('> Skipping test ${@FN}, since `-d network` is not passed.')
94 eprintln('> This test requires a working postgres server running on localhost.')
95 return
96 }
97
98 db := pg.connect(pg.Config{ user: 'postgres', password: '12345678', dbname: 'postgres' })!
99 defer {
100 db.close() or {}
101 }
102
103 // Test listen
104 db.listen('test_channel')!
105
106 // Test notify with payload
107 db.notify('test_channel', 'hello world')!
108
109 // Consume input to process the notification
110 db.consume_input()!
111
112 // Get the notification
113 if notification := db.get_notification() {
114 assert notification.channel == 'test_channel'
115 assert notification.payload == 'hello world'
116 assert notification.pid > 0
117 } else {
118 assert false, 'Expected a notification but got none'
119 }
120
121 // Test notify without payload
122 db.notify('test_channel', '')!
123 db.consume_input()!
124
125 if notification := db.get_notification() {
126 assert notification.channel == 'test_channel'
127 assert notification.payload == ''
128 } else {
129 assert false, 'Expected a notification but got none'
130 }
131
132 // Test that no more notifications are pending
133 assert db.get_notification() == none
134
135 // Test unlisten
136 db.unlisten('test_channel')!
137
138 // Test unlisten_all
139 db.listen('channel1')!
140 db.listen('channel2')!
141 db.unlisten_all()!
142
143 // Test socket (should return valid fd)
144 socket_fd := db.socket()
145 assert socket_fd >= 0
146}
147