Repository navigation
Expand file tree
/
Copy pathpostgres_spec.lua
More file actions
127 lines (111 loc) · 4.04 KB
/
Copy pathpostgres_spec.lua
File metadata and controls
127 lines (111 loc) · 4.04 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
describe("postgres module #native", function()
local ok, pg = pcall(require, "lunet.postgres")
if not ok then
pending("lunet.postgres not built (xmake build lunet-postgres); if the build failed, check the libpq client library floor (libpq >= 17 required) — see ext/postgres/README.md", function() end)
return
end
it("exports the driver surface", function()
assert.are.equal("table", type(pg))
for _, fn in ipairs({ "open", "close", "checkout", "release", "query", "exec", "query_params", "exec_params" }) do
assert.are.equal("function", type(pg[fn]))
end
end)
describe("pool_size validation", function()
local function open_error(params)
local co = coroutine.create(function()
return pg.open(params)
end)
local resumed, err = coroutine.resume(co)
return resumed, err
end
it("rejects a non-integer pool_size", function()
local resumed, err = open_error({ host = "127.0.0.1", pool_size = "two" })
assert.is_false(resumed)
assert.matches("pool_size", tostring(err))
end)
it("rejects pool_size below 1", function()
local resumed, err = open_error({ host = "127.0.0.1", pool_size = 0 })
assert.is_false(resumed)
assert.matches("pool_size", tostring(err))
end)
it("rejects pool_size above the cap", function()
local resumed, err = open_error({ host = "127.0.0.1", pool_size = 65 })
assert.is_false(resumed)
assert.matches("pool_size", tostring(err))
end)
end)
describe("open passthrough validation", function()
local function open_error(params)
local co = coroutine.create(function()
return pg.open(params)
end)
local resumed, err = coroutine.resume(co)
return resumed, err
end
it("rejects a non-numeric connect_timeout", function()
local resumed, err = open_error({ host = "127.0.0.1", connect_timeout = "five" })
assert.is_false(resumed)
assert.matches("connect_timeout", tostring(err))
end)
it("rejects a non-positive connect_timeout", function()
local resumed, err = open_error({ host = "127.0.0.1", connect_timeout = 0 })
assert.is_false(resumed)
assert.matches("connect_timeout", tostring(err))
end)
it("rejects a non-string sslmode", function()
local resumed, err = open_error({ host = "127.0.0.1", sslmode = 42 })
assert.is_false(resumed)
assert.matches("sslmode", tostring(err))
end)
it("rejects an sslmode libpq does not accept", function()
local resumed, err = open_error({ host = "127.0.0.1", sslmode = "sometimes" })
assert.is_false(resumed)
assert.matches("sslmode", tostring(err))
end)
end)
describe("prepare validation", function()
it("requires calling prepare from a coroutine", function()
-- The coroutine guard fires before argument validation.
assert.has_error(function()
pg.prepare(nil, "SELECT 1")
end)
end)
it("rejects empty SQL", function()
local co = coroutine.create(function()
return pg.prepare(nil, "")
end)
local resumed, err = coroutine.resume(co)
assert.is_false(resumed)
assert.matches("prepare", tostring(err))
end)
end)
describe("checkout/release validation", function()
it("checkout requires a coroutine", function()
assert.has_error(function()
pg.checkout(nil)
end)
end)
it("checkout rejects a non-pool handle", function()
local co = coroutine.create(function()
return pg.checkout("not-a-pool")
end)
local resumed, r = coroutine.resume(co)
assert.is_false(resumed)
assert.is_truthy(r)
end)
it("release requires a coroutine", function()
assert.has_error(function()
pg.release(nil)
end)
end)
it("release rejects a non-conn handle", function()
local co = coroutine.create(function()
return pg.release("not-a-conn")
end)
local resumed, r, e = coroutine.resume(co)
assert.is_true(resumed)
assert.is_nil(r)
assert.is_truthy(e)
end)
end)
end)