|
1 | 1 | import Postgres from require "pgmoon" |
2 | 2 |
|
3 | | -SOCKET_PATH = "/var/run/postgresql/.s.PGSQL.5432" |
4 | | - |
5 | | --- Basic test to verify Unix socket support loads correctly |
6 | | -describe "Unix socket support", -> |
7 | | - it "should choose nginx or luaposix when socket_path is provided", -> |
8 | | - pg = Postgres { |
9 | | - socket_path: "/tmp/test.sock" |
10 | | - user: "postgres" |
11 | | - database: "test" |
12 | | - } |
13 | | - |
14 | | - if ngx and ngx.get_phase! != "init" |
15 | | - assert.equal "nginx", pg.sock_type |
16 | | - else |
17 | | - assert.equal "luaposix", pg.sock_type |
18 | | - |
19 | | - it "should accept socket_path in configuration", -> |
20 | | - pg = Postgres { |
21 | | - socket_path: SOCKET_PATH |
22 | | - user: "postgres" |
23 | | - database: "test" |
24 | | - } |
25 | | - |
26 | | - assert.equal SOCKET_PATH, pg.config.socket_path |
27 | | - |
28 | | - it "should default to luasocket when no socket_path is provided", -> |
29 | | - pg = Postgres { |
30 | | - user: "postgres" |
31 | | - database: "test" |
32 | | - } |
33 | | - |
34 | | - -- Should not be luaposix since no socket_path provided |
35 | | - assert.is_not.equal "luaposix", pg.sock_type |
36 | | - |
37 | | - it "should have socket_path as nil in default config", -> |
38 | | - pg = Postgres {} |
39 | | - assert.is_nil pg.config.socket_path |
40 | | - |
41 | | - it "should override socket_type when socket_path is provided", -> |
42 | | - -- Even if we explicitly set socket_type to something else, |
43 | | - -- providing socket_path should force luaposix |
44 | | - pg = Postgres { |
45 | | - socket_type: "luasocket" |
46 | | - socket_path: "/tmp/test.sock" |
47 | | - user: "postgres" |
48 | | - database: "test" |
49 | | - } |
50 | | - |
51 | | - if ngx and ngx.get_phase! != "init" |
52 | | - assert.equal "nginx", pg.sock_type |
53 | | - else |
54 | | - assert.equal "luaposix", pg.sock_type |
55 | | - |
56 | | - -- This test would require an actual Unix socket PostgreSQL server |
57 | | - -- Keeping it commented out for now |
58 | | - it "should connect via Unix socket", -> |
59 | | - pg = Postgres { |
60 | | - socket_path: SOCKET_PATH |
61 | | - user: "postgres" |
62 | | - database: "postgres" |
63 | | - } |
64 | | - |
65 | | - success, err = pg\connect! |
66 | | - assert success, "Failed to connect: #{err}" |
67 | | - |
68 | | - result, err = pg\query "SELECT 1 as test" |
69 | | - assert result, "Failed to query: #{err}" |
70 | | - assert.equal 1, result[1].test |
71 | | - |
72 | | - pg\disconnect! |
| 3 | +import psql_unix, SOCKET_PATH, USER, PASSWORD, DB from require "spec.util" |
| 4 | + |
| 5 | +describe "pgmoon Unix socket with server", -> |
| 6 | + setup -> |
| 7 | + os.execute "spec/postgres.sh start unix" |
| 8 | + |
| 9 | + r = { psql_unix "drop database if exists #{DB}" } |
| 10 | + assert 0 == r[#r], "failed to execute psql_unix: drop database" |
| 11 | + |
| 12 | + r = { psql_unix "create database #{DB}" } |
| 13 | + assert 0 == r[#r], "failed to execute psql_unix: create database" |
| 14 | + |
| 15 | + teardown -> |
| 16 | + os.execute "spec/postgres.sh stop" |
| 17 | + |
| 18 | + -- Test that socket_path configuration selects the right socket type |
| 19 | + describe "socket type selection", -> |
| 20 | + it "should choose luaposix when socket_path is provided outside nginx", -> |
| 21 | + unless ngx |
| 22 | + pg = Postgres { |
| 23 | + socket_path: "/tmp/test.sock" |
| 24 | + user: "postgres" |
| 25 | + database: "test" |
| 26 | + } |
| 27 | + assert.equal "luaposix", pg.sock_type |
| 28 | + |
| 29 | + it "should accept socket_path in configuration", -> |
| 30 | + pg = Postgres { |
| 31 | + socket_path: SOCKET_PATH |
| 32 | + user: "postgres" |
| 33 | + database: "test" |
| 34 | + } |
| 35 | + assert.equal SOCKET_PATH, pg.config.socket_path |
| 36 | + |
| 37 | + it "should default to luasocket when no socket_path is provided", -> |
| 38 | + pg = Postgres { |
| 39 | + user: "postgres" |
| 40 | + database: "test" |
| 41 | + } |
| 42 | + -- Should not be luaposix since no socket_path provided |
| 43 | + assert.is_not.equal "luaposix", pg.sock_type |
| 44 | + |
| 45 | + it "should have socket_path as nil in default config", -> |
| 46 | + pg = Postgres {} |
| 47 | + assert.is_nil pg.config.socket_path |
| 48 | + |
| 49 | + it "should override socket_type when socket_path is provided", -> |
| 50 | + pg = Postgres { |
| 51 | + socket_type: "luasocket" |
| 52 | + socket_path: "/tmp/test.sock" |
| 53 | + user: "postgres" |
| 54 | + database: "test" |
| 55 | + } |
| 56 | + if ngx and ngx.get_phase! != "init" |
| 57 | + assert.equal "nginx", pg.sock_type |
| 58 | + else |
| 59 | + assert.equal "luaposix", pg.sock_type |
| 60 | + |
| 61 | + -- Actual connection tests via Unix socket |
| 62 | + describe "luaposix socket", -> |
| 63 | + local pg |
| 64 | + |
| 65 | + before_each -> |
| 66 | + pg = Postgres { |
| 67 | + socket_path: SOCKET_PATH |
| 68 | + user: USER |
| 69 | + password: PASSWORD |
| 70 | + database: DB |
| 71 | + } |
| 72 | + |
| 73 | + after_each -> |
| 74 | + pg\disconnect! if pg |
| 75 | + |
| 76 | + it "connects via Unix socket", -> |
| 77 | + success, err = pg\connect! |
| 78 | + assert success, "Failed to connect: #{err}" |
| 79 | + |
| 80 | + result, err = pg\query "SELECT 1 as test" |
| 81 | + assert result, "Failed to query: #{err}" |
| 82 | + assert.same { { test: 1 } }, result |
| 83 | + |
| 84 | + it "creates and queries table", -> |
| 85 | + assert pg\connect! |
| 86 | + |
| 87 | + res = assert pg\query [[ |
| 88 | + create table unix_test ( |
| 89 | + id serial not null, |
| 90 | + name text, |
| 91 | + primary key (id) |
| 92 | + ) |
| 93 | + ]] |
| 94 | + assert.same true, res |
| 95 | + |
| 96 | + res = assert pg\query [[ |
| 97 | + insert into unix_test (name) values ('hello') returning id, name |
| 98 | + ]] |
| 99 | + assert.same { |
| 100 | + affected_rows: 1 |
| 101 | + { id: 1, name: "hello" } |
| 102 | + }, res |
| 103 | + |
| 104 | + res = assert pg\query [[ |
| 105 | + select * from unix_test |
| 106 | + ]] |
| 107 | + assert.same { |
| 108 | + { id: 1, name: "hello" } |
| 109 | + }, res |
| 110 | + |
| 111 | + assert pg\query [[drop table unix_test]] |
| 112 | + |
| 113 | + it "handles errors correctly", -> |
| 114 | + assert pg\connect! |
| 115 | + |
| 116 | + status, err = pg\query "select * from nonexistent_table" |
| 117 | + assert.falsy status |
| 118 | + assert.truthy err\match "does not exist" |
| 119 | + |
| 120 | + it "can reconnect after disconnect", -> |
| 121 | + do return pending "luaposix socket does not support reconnect on same instance" |
| 122 | + assert pg\connect! |
| 123 | + assert pg\query "select 1" |
| 124 | + pg\disconnect! |
| 125 | + |
| 126 | + assert pg\connect! |
| 127 | + assert pg\query "select 1" |
0 commit comments