-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathPostgresDBRunner.js
194 lines (181 loc) · 6.31 KB
/
PostgresDBRunner.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// eslint-disable-next-line import/no-extraneous-dependencies
import { PostgresQuery } from '../../../src';
const pgPromise = require('pg-promise');
// eslint-disable-next-line import/no-extraneous-dependencies
const { GenericContainer, Wait } = require('testcontainers');
const { BaseDbRunner } = require('../utils/BaseDbRunner');
process.env.TZ = 'GMT';
export class PostgresDBRunner extends BaseDbRunner {
async connectionLazyInit(port) {
const pgp = pgPromise();
const db = pgp({
host: 'localhost',
port,
password: this.password(),
database: 'model_test',
poolSize: 1,
user: process.env.TEST_PG_USER || 'root',
});
const defaultFixture = this.prepareFixture.bind(this);
return {
testQueries(queries, prepareDataSet) {
prepareDataSet = prepareDataSet || defaultFixture;
return db.tx(async tx => {
await tx.query('SET TIME ZONE \'UTC\'');
await prepareDataSet(tx);
let lastResult;
for (const [query, params] of queries) {
try {
lastResult = await tx.query(query, params);
} catch (e) {
throw new Error(`Execution failed for '${query}', params: ${params}: ${e.stack || e}`);
}
}
return JSON.parse(JSON.stringify(lastResult));
});
},
close() {
return pgp.end();
}
};
}
async prepareFixture(tx) {
return tx.batch([
tx.query('CREATE TEMPORARY TABLE visitors (id INT, amount INT, created_at TIMESTAMP, updated_at TIMESTAMP, status INT, source TEXT, latitude DECIMAL, longitude DECIMAL) ON COMMIT DROP'),
tx.query('CREATE TEMPORARY TABLE visitor_checkins (id INT, visitor_id INT, created_at TIMESTAMP, source TEXT) ON COMMIT DROP'),
tx.query('CREATE TEMPORARY TABLE cards (id INT, visitor_id INT, visitor_checkin_id INT) ON COMMIT DROP'),
tx.query('CREATE TEMPORARY TABLE left_table (id INT, total DOUBLE PRECISION, description character varying) ON COMMIT DROP'),
tx.query('CREATE TEMPORARY TABLE right_table (id INT, total DOUBLE PRECISION, description character varying) ON COMMIT DROP'),
tx.query('CREATE TEMPORARY TABLE mid_table (id INT, left_id INT, right_id INT) ON COMMIT DROP'),
tx.query('CREATE TEMPORARY TABLE compound_key_cards (id_a INT, id_b INT, visitor_id INT, visitor_checkin_id INT, visit_rank INT) ON COMMIT DROP'),
tx.query('CREATE TEMPORARY TABLE sales (id INT PRIMARY KEY, date DATE, category TEXT, region TEXT, amount DECIMAL) ON COMMIT DROP'),
tx.query(`
INSERT INTO
visitors
(id, amount, created_at, updated_at, status, source, latitude, longitude) VALUES
(1, 100, '2017-01-03', '2017-01-30', 1, 'some', 120.120, 40.60),
(2, 200, '2017-01-05', '2017-01-15', 1, 'some', 120.120, 58.60),
(3, 300, '2017-01-06', '2017-01-20', 2, 'google', 120.120, 70.60),
(4, 400, '2017-01-07', '2017-01-25', 2, NULL, 120.120, 10.60),
(5, 500, '2017-01-07', '2017-01-25', 2, NULL, 120.120, 58.10),
(6, 500, '2016-09-07', '2016-09-07', 2, NULL, 120.120, 58.10)
`),
tx.query(`
INSERT INTO
visitor_checkins
(id, visitor_id, created_at, source) VALUES
(1, 1, '2017-01-03', NULL),
(2, 1, '2017-01-04', NULL),
(3, 1, '2017-01-05', 'google'),
(4, 2, '2017-01-05', NULL),
(5, 2, '2017-01-05', NULL),
(6, 3, '2017-01-06', NULL)
`),
tx.query(`
INSERT INTO
cards
(id, visitor_id, visitor_checkin_id) VALUES
(1, 1, 1),
(2, 1, 2),
(3, 3, 6)
`),
tx.query(`
INSERT INTO
left_table
(id, total, description) VALUES
(1, 2.5, 'ABC'),
(2, 2.5, 'BAC'),
(3, 2.5, 'BCA'),
(4, 2.5, 'CBA'),
(5, 2.5, 'CAB'),
(6, 2.5, 'ACB')
`),
tx.query(`
INSERT INTO
right_table
(id, total, description) VALUES
(1, 2.5, 'ABC'),
(2, 2.5, 'BAC'),
(3, 2.5, 'BCA'),
(4, 2.5, 'CBA'),
(5, 2.5, 'CAB'),
(6, 2.5, 'ACB')
`),
tx.query(`
INSERT INTO
mid_table
(id, left_id, right_id) VALUES
(1, 1, 1),
(2, 2, 2),
(3, 3, 3),
(4, 4, 4),
(5, 5, 5),
(6, 6, 6)
`),
tx.query(`
INSERT INTO
compound_key_cards
(id_a, id_b, visitor_id, visitor_checkin_id, visit_rank) VALUES
(1, 1, 1, 1, 10),
(2, 1, 1, 2, 5),
(2, 2, 3, 6, 7),
(2, 2, 2, 4, 8),
(2, 3, 4, 5, 2);
`),
tx.query(`
INSERT INTO
sales
(id, date, category, region, amount) VALUES
(1, '2023-01-01', 'Electronics', 'North', 300),
(2, '2023-01-01', 'Electronics', 'South', 200),
(3, '2023-01-01', 'Clothing', 'North', 200),
(4, '2023-01-01', 'Clothing', 'South', 100),
(5, '2023-02-01', 'Electronics', 'North', 400),
(6, '2023-02-01', 'Electronics', 'South', 200),
(7, '2023-02-01', 'Clothing', 'North', 300),
(8, '2023-02-01', 'Clothing', 'South', 100),
(9, '2023-03-01', 'Electronics', 'North', 500),
(10, '2023-07-01', 'Electronics', 'South', 300),
(11, '2023-12-01', 'Clothing', 'North', 400),
(12, '2023-12-01', 'Clothing', 'South', 200)
`)
]);
}
async containerLazyInit() {
const version = process.env.TEST_PGSQL_VERSION || '12.22';
return new GenericContainer(`postgres:${version}`)
.withEnvironment({
POSTGRES_USER: 'root',
POSTGRES_DB: 'model_test',
POSTGRES_PASSWORD: this.password(),
})
.withExposedPorts(this.port())
.withHealthCheck({
test: [
'CMD-SHELL',
`pg_isready -h localhost -p ${this.port()} -U root -d model_test || exit 1`
],
interval: 1000,
timeout: 500,
retries: 20,
startPeriod: 5 * 1000,
})
.withWaitStrategy(Wait.forHealthCheck())
.withStartupTimeout(15 * 1000)
.start();
}
password() {
return 'passwd';
}
port() {
return 5432;
}
newTestQuery(compilers, query) {
return new PostgresQuery(compilers, query);
}
}
export const dbRunner = new PostgresDBRunner();
// eslint-disable-next-line no-undef
afterAll(async () => {
await dbRunner.tearDown();
});