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