|
1
|
const mockQuery = jest.fn() |
|
2
|
const mockEnd = jest.fn() |
|
3
|
|
|
4
|
jest.mock("@medusajs/deps/pg", () => ({ |
|
5
|
Pool: jest.fn(() => ({ query: mockQuery, end: mockEnd })), |
|
6
|
})) |
|
7
|
|
|
8
|
jest.mock("@medusajs/telemetry", () => ({ track: jest.fn() })) |
|
9
|
|
|
10
|
jest.mock("@medusajs/utils", () => ({ |
|
11
|
isNodeVersionSupported: jest.fn(() => true), |
|
12
|
MIN_SUPPORTED_NODE_VERSION: "22.22.0", |
|
13
|
})) |
|
14
|
|
|
15
|
jest.mock("../../reporter", () => ({ |
|
16
|
__esModule: true, |
|
17
|
default: { |
|
18
|
activity: jest.fn(() => "db-activity"), |
|
19
|
success: jest.fn(), |
|
20
|
failure: jest.fn(), |
|
21
|
warn: jest.fn(), |
|
22
|
info: jest.fn(), |
|
23
|
error: jest.fn(), |
|
24
|
panic: jest.fn(), |
|
25
|
}, |
|
26
|
})) |
|
27
|
|
|
28
|
import { Pool } from "@medusajs/deps/pg" |
|
29
|
import reporter from "../../reporter" |
|
30
|
import { createDatabase, setupDB } from "../new" |
|
31
|
|
|
32
|
const credentials = { |
|
33
|
user: "postgres", |
|
34
|
database: "postgres", |
|
35
|
password: "", |
|
36
|
port: 5432, |
|
37
|
host: "localhost", |
|
38
|
} |
|
39
|
|
|
40
|
const pgError = (code: string, message = "pg error") => |
|
41
|
Object.assign(new Error(message), { code }) |
|
42
|
|
|
43
|
describe("medusa new - database setup", () => { |
|
44
|
beforeEach(() => { |
|
45
|
jest.clearAllMocks() |
|
46
|
}) |
|
47
|
|
|
48
|
describe("createDatabase", () => { |
|
49
|
it("connects with the given credentials and issues CREATE DATABASE", async () => { |
|
50
|
mockQuery.mockResolvedValueOnce({}) |
|
51
|
|
|
52
|
await createDatabase("medusa-db-abc12", credentials) |
|
53
|
|
|
54
|
expect(Pool).toHaveBeenCalledWith(credentials) |
|
55
|
expect(mockQuery).toHaveBeenCalledWith(`CREATE DATABASE "medusa-db-abc12"`) |
|
56
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
57
|
}) |
|
58
|
|
|
59
|
it("closes the pool even when the query fails", async () => { |
|
60
|
mockQuery.mockRejectedValueOnce(pgError("28P01", "auth failed")) |
|
61
|
|
|
62
|
await expect( |
|
63
|
createDatabase("medusa-db-abc12", credentials) |
|
64
|
).rejects.toThrow("auth failed") |
|
65
|
expect(mockEnd).toHaveBeenCalledTimes(1) |
|
66
|
}) |
|
67
|
}) |
|
68
|
|
|
69
|
describe("setupDB", () => { |
|
70
|
it("reports success when the database is created", async () => { |
|
71
|
mockQuery.mockResolvedValueOnce({}) |
|
72
|
|
|
73
|
await setupDB("medusa-db-abc12", credentials) |
|
74
|
|
|
75
|
expect(reporter.success).toHaveBeenCalledWith( |
|
76
|
"db-activity", |
|
77
|
`Created database "medusa-db-abc12"` |
|
78
|
) |
|
79
|
expect(reporter.failure).not.toHaveBeenCalled() |
|
80
|
}) |
|
81
|
|
|
82
|
it("treats Postgres error 42P04 as 'already exists' and still succeeds", async () => { |
|
83
|
mockQuery.mockRejectedValueOnce(pgError("42P04", "database already exists")) |
|
84
|
|
|
85
|
await setupDB("medusa-db-abc12", credentials) |
|
86
|
|
|
87
|
expect(reporter.success).toHaveBeenCalledWith( |
|
88
|
"db-activity", |
|
89
|
`Database medusa-db-abc12 already exists; skipping setup` |
|
90
|
) |
|
91
|
expect(reporter.failure).not.toHaveBeenCalled() |
|
92
|
}) |
|
93
|
|
|
94
|
it("reports a failure for any other error", async () => { |
|
95
|
const errorSpy = jest.spyOn(console, "error").mockImplementation(() => {}) |
|
96
|
mockQuery.mockRejectedValueOnce(pgError("ECONNREFUSED", "connection refused")) |
|
97
|
|
|
98
|
await setupDB("medusa-db-abc12", credentials) |
|
99
|
|
|
100
|
expect(reporter.failure).toHaveBeenCalledWith( |
|
101
|
"db-activity", |
|
102
|
`Skipping database setup.` |
|
103
|
) |
|
104
|
expect(reporter.warn).toHaveBeenCalledTimes(1) |
|
105
|
expect(reporter.success).not.toHaveBeenCalled() |
|
106
|
expect(errorSpy).toHaveBeenCalledTimes(1) |
|
107
|
|
|
108
|
errorSpy.mockRestore() |
|
109
|
}) |
|
110
|
}) |
|
111
|
}) |