-
-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathpostgresql-container.test.ts
More file actions
152 lines (123 loc) · 4.62 KB
/
postgresql-container.test.ts
File metadata and controls
152 lines (123 loc) · 4.62 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
140
141
142
143
144
145
146
147
148
149
150
151
152
import path from "node:path";
import { Client } from "pg";
import { getImage } from "../../../testcontainers/src/utils/test-helper";
import { PostgreSqlContainer } from "./postgresql-container";
const IMAGE = getImage(__dirname);
const SSL_CA_CERT = path.resolve(__dirname, "test-certs/ca.crt");
const SSL_SERVER_CERT = path.resolve(__dirname, "test-certs/server.crt");
const SSL_SERVER_KEY = path.resolve(__dirname, "test-certs/server.key");
describe("PostgreSqlContainer", { timeout: 180_000 }, () => {
it("should connect and return a query result", async () => {
// pgConnect {
await using container = await new PostgreSqlContainer(IMAGE).start();
const client = new Client({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getPassword(),
});
await client.connect();
const result = await client.query("SELECT 1");
expect(result.rows[0]).toEqual({ "?column?": 1 });
await client.end();
// }
});
it("should work with database URI", async () => {
// pgUriConnect {
await using container = await new PostgreSqlContainer(IMAGE).start();
const client = new Client({
connectionString: container.getConnectionUri(),
});
// }
await client.connect();
const result = await client.query("SELECT 1");
expect(result.rows[0]).toEqual({ "?column?": 1 });
await client.end();
});
it("should set database", async () => {
// pgSetDatabase {
await using container = await new PostgreSqlContainer(IMAGE).withDatabase("customDatabase").start();
// }
const client = new Client({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getPassword(),
});
await client.connect();
const result = await client.query("SELECT current_database()");
expect(result.rows[0]).toEqual({ current_database: "customDatabase" });
await client.end();
});
it("should set username", async () => {
// pgSetUsername {
await using container = await new PostgreSqlContainer(IMAGE).withUsername("customUsername").start();
// }
const client = new Client({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getPassword(),
});
await client.connect();
const result = await client.query("SELECT current_user");
expect(result.rows[0]).toEqual({ current_user: "customUsername" });
await client.end();
});
it("should work with restarted container", async () => {
await using container = await new PostgreSqlContainer(IMAGE).start();
await container.restart();
const client = new Client({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getPassword(),
});
await client.connect();
const result = await client.query("SELECT 1");
expect(result.rows[0]).toEqual({ "?column?": 1 });
await client.end();
});
it("should allow custom healthcheck", async () => {
const container = new PostgreSqlContainer(IMAGE).withHealthCheck({
test: ["CMD-SHELL", "exit 1"],
interval: 100,
retries: 0,
timeout: 0,
});
await expect(() => container.start()).rejects.toThrow();
});
it("should connect with SSL", async () => {
// pgSslConnect {
await using container = await new PostgreSqlContainer(IMAGE)
.withSSLCert(SSL_CA_CERT, SSL_SERVER_CERT, SSL_SERVER_KEY)
.start();
const client = new Client({
host: container.getHost(),
port: container.getPort(),
database: container.getDatabase(),
user: container.getUsername(),
password: container.getPassword(),
ssl: {
rejectUnauthorized: false,
},
});
await client.connect();
const result = await client.query("SELECT ssl FROM pg_stat_ssl WHERE pid = pg_backend_pid()");
expect(result.rows[0]).toEqual({ ssl: true });
await client.end();
// }
});
it("should validate SSL certificate paths", () => {
const container = new PostgreSqlContainer(IMAGE);
expect(() => container.withSSL("", "server.key")).toThrow("SSL certificate file should not be empty.");
expect(() => container.withSSL("server.crt", "")).toThrow("SSL key file should not be empty.");
expect(() => container.withSSLCert("", "server.crt", "server.key")).toThrow(
"SSL CA certificate file should not be empty."
);
});
});