|
1
|
const mockConnect = jest.fn() |
|
2
|
const mockQuery = jest.fn() |
|
3
|
const mockEnd = jest.fn() |
|
4
|
|
|
5
|
jest.mock("@medusajs/framework/pg", () => ({ |
|
6
|
Client: jest.fn(() => ({ |
|
7
|
connect: mockConnect, |
|
8
|
query: mockQuery, |
|
9
|
end: mockEnd, |
|
10
|
on: jest.fn(), |
|
11
|
})), |
|
12
|
})) |
|
13
|
|
|
14
|
import { Client } from "@medusajs/framework/pg" |
|
15
|
import { |
|
16
|
createDatabase, |
|
17
|
dropDatabase, |
|
18
|
} from "../medusa-test-runner-utils/postgres-template" |
|
19
|
|
|
20
|
const pgError = (code: string, message = "pg error") => |
|
21
|
Object.assign(new Error(message), { code }) |
|
22
|
|
|
23
|
describe("test-utils database management (pg-god replacement)", () => { |
|
24
|
beforeEach(() => { |
|
25
|
jest.clearAllMocks() |
|
26
|
mockConnect.mockResolvedValue(undefined) |
|
27
|
mockEnd.mockResolvedValue(undefined) |
|
28
|
mockQuery.mockResolvedValue({ rows: [], rowCount: 0 }) |
|
29
|
}) |
|
30
|
|
|
31
|
describe("createDatabase", () => { |
|
32
|
it("connects and issues a quoted CREATE DATABASE, then closes the client", async () => { |
|
33
|
await createDatabase({ databaseName: "test_db" }) |
|
34
|
|
|
35
|
expect(mockConnect).toHaveBeenCalledTimes(1) |
|
36
|
expect(mockQuery).toHaveBeenCalledWith(`CREATE DATABASE "test_db";`) |
|
37
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
38
|
}) |
|
39
|
|
|
40
|
it("merges the provided credentials over the defaults", async () => { |
|
41
|
await createDatabase( |
|
42
|
{ databaseName: "test_db" }, |
|
43
|
{ user: "tester", host: "db.example.com", port: 5433 } |
|
44
|
) |
|
45
|
|
|
46
|
expect(Client).toHaveBeenCalledWith({ |
|
47
|
user: "tester", |
|
48
|
database: "postgres", |
|
49
|
password: "", |
|
50
|
port: 5433, |
|
51
|
host: "db.example.com", |
|
52
|
}) |
|
53
|
}) |
|
54
|
|
|
55
|
it("treats an already existing database as success when errorIfExist is false", async () => { |
|
56
|
mockQuery.mockRejectedValueOnce(pgError("42P04", "already exists")) |
|
57
|
|
|
58
|
await expect( |
|
59
|
createDatabase({ databaseName: "test_db" }) |
|
60
|
).resolves.toBeUndefined() |
|
61
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
62
|
}) |
|
63
|
|
|
64
|
it("rethrows the duplicate-database error when errorIfExist is true", async () => { |
|
65
|
mockQuery.mockRejectedValueOnce(pgError("42P04", "already exists")) |
|
66
|
|
|
67
|
await expect( |
|
68
|
createDatabase({ databaseName: "test_db", errorIfExist: true }) |
|
69
|
).rejects.toThrow("already exists") |
|
70
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
71
|
}) |
|
72
|
|
|
73
|
it("rethrows unexpected errors and still closes the client", async () => { |
|
74
|
mockQuery.mockRejectedValueOnce(pgError("28P01", "auth failed")) |
|
75
|
|
|
76
|
await expect( |
|
77
|
createDatabase({ databaseName: "test_db" }) |
|
78
|
).rejects.toThrow("auth failed") |
|
79
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
80
|
}) |
|
81
|
}) |
|
82
|
|
|
83
|
describe("dropDatabase", () => { |
|
84
|
it("terminates connections before issuing a quoted DROP DATABASE, then closes the client", async () => { |
|
85
|
await dropDatabase({ databaseName: "test_db" }) |
|
86
|
|
|
87
|
const queries = mockQuery.mock.calls.map((c) => String(c[0])) |
|
88
|
const terminateIdx = queries.findIndex((q) => |
|
89
|
q.includes("pg_terminate_backend") |
|
90
|
) |
|
91
|
const dropIdx = queries.findIndex((q) => |
|
92
|
q.includes(`DROP DATABASE "test_db";`) |
|
93
|
) |
|
94
|
|
|
95
|
expect(terminateIdx).toBeGreaterThanOrEqual(0) |
|
96
|
expect(dropIdx).toBeGreaterThan(terminateIdx) |
|
97
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
98
|
}) |
|
99
|
|
|
100
|
it("skips terminating connections when dropConnections is false", async () => { |
|
101
|
await dropDatabase({ databaseName: "test_db", dropConnections: false }) |
|
102
|
|
|
103
|
const terminated = mockQuery.mock.calls.some((c) => |
|
104
|
String(c[0]).includes("pg_terminate_backend") |
|
105
|
) |
|
106
|
|
|
107
|
expect(terminated).toBe(false) |
|
108
|
expect(mockQuery).toHaveBeenCalledWith(`DROP DATABASE "test_db";`) |
|
109
|
}) |
|
110
|
|
|
111
|
it("treats a missing database as success when errorIfNonExist is false", async () => { |
|
112
|
mockQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 }) // terminate |
|
113
|
mockQuery.mockRejectedValueOnce(pgError("3D000", "does not exist")) // drop |
|
114
|
|
|
115
|
await expect( |
|
116
|
dropDatabase({ databaseName: "test_db" }) |
|
117
|
).resolves.toBeUndefined() |
|
118
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
119
|
}) |
|
120
|
|
|
121
|
it("rethrows the missing-database error when errorIfNonExist is true", async () => { |
|
122
|
mockQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 }) // terminate |
|
123
|
mockQuery.mockRejectedValueOnce(pgError("3D000", "does not exist")) // drop |
|
124
|
|
|
125
|
await expect( |
|
126
|
dropDatabase({ databaseName: "test_db", errorIfNonExist: true }) |
|
127
|
).rejects.toThrow("does not exist") |
|
128
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
129
|
}) |
|
130
|
|
|
131
|
it("rethrows unexpected errors and still closes the client", async () => { |
|
132
|
mockQuery.mockResolvedValueOnce({ rows: [], rowCount: 0 }) // terminate |
|
133
|
mockQuery.mockRejectedValueOnce(pgError("55006", "in use")) // drop |
|
134
|
|
|
135
|
await expect( |
|
136
|
dropDatabase({ databaseName: "test_db" }) |
|
137
|
).rejects.toThrow("in use") |
|
138
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
139
|
}) |
|
140
|
}) |
|
141
|
}) |