-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_tests.js
363 lines (322 loc) · 8.6 KB
/
run_tests.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/* Run the test suite.
Depending on environment variables, it will cycle the tests repeatedly
against all available test databases and data sets.
*/
// eslint-disable-next-line import/no-extraneous-dependencies
const Mocha = require("mocha");
const { lookupFiles } = require("mocha/lib/cli");
// eslint-disable-next-line import/no-extraneous-dependencies
const chai = require("chai");
// eslint-disable-next-line import/no-extraneous-dependencies
const chalk = require("chalk");
const fs = require("fs");
const knex = require("knex");
// eslint-disable-next-line import/no-extraneous-dependencies
const sqlite3 = require("sqlite3");
const path = require("path");
const test_fixture_data = JSON.parse(
fs.readFileSync("test/fixtures/table_data.json")
);
const sqlite_test_db_configuration = {
// debug: true,
client: "sqlite3",
connection: {},
useNullAsDefault: true
};
const mysql_test_db_configuration = {
// debug: true,
client: "mysql",
connection: {
host: "127.0.0.1",
user: "jaorm_test",
password: "jaorm_test",
database: "jaorm"
}
};
const mysql2_test_db_configuration = {
// debug: true,
client: "mysql2",
connection: {
host: "127.0.0.1",
user: "jaorm_test",
password: "jaorm_test",
database: "jaorm"
}
};
const pg_test_db_configuration = {
// debug: true,
client: "pg",
connection: {
host: "127.0.0.1",
user: "jaorm_test",
password: "jaorm_test",
database: "jaorm"
}
};
let db_type = "sqlite";
async function run_tests(options) {
Object.defineProperty(Mocha.Suite.prototype, "assert", {
get() {
return chai.assert;
},
set() {}
});
Object.defineProperty(Mocha.Suite.prototype, "fixtures", {
get() {
return test_fixture_data;
},
set() {}
});
Object.defineProperty(Mocha.Suite.prototype, "schema_options", {
get() {
return {
cache_results: true,
logging_level: "error",
result_dir: "test/lib/result",
resultset_dir: "test/lib/resultset"
};
},
set() {}
});
Object.defineProperty(Mocha.Suite.prototype, "test_db_config", {
get() {
switch (db_type) {
case "mysql":
case "mysql2":
return mysql_test_db_configuration;
case "pg":
return pg_test_db_configuration;
case "sqlite":
default:
return sqlite_test_db_configuration;
}
},
set() {}
});
const databases_to_test = ["sqlite", "mysql", "mysql2", "pg", "dummy"];
// We need a separate mocha instance for each round of this, because it
// doesn't clear listeners properly.
const mochas = {};
let num_failures = 0;
for (const dbt of databases_to_test) {
process.stdout.write(`Starting type ${dbt}\n`);
db_type = dbt;
const test_timeout = options.timeout || 120000;
mochas[db_type] = new Mocha({ reporter: "spec", timeout: test_timeout });
const files = [];
let test_sections = [
"test/schema",
"test/resultset",
"test/result",
"test/drivers"
];
for (const ts of test_sections) {
let section_files = lookupFiles(ts, ["js"], true).map(file => path.resolve(file));
Array.prototype.push.apply(files, section_files);
}
// Nuke the require cache in case this isn't the first run
mochas[db_type].files = files;
const enabled = await _setup_tests(db_type);
if (enabled === true) {
process.stdout.write(`Running test suite against DB type '${db_type}'\n`);
// eslint-disable-next-line no-loop-func
const failures = await new Promise((resolve, reject) =>
mochas[db_type].run(resolve)
);
// Clear Node's require cache so the next round doesn't think the tests
// have been run already
mochas[db_type].unloadFiles();
num_failures += failures;
// Do any cleanup that we need to, but only if we passed
if (num_failures === 0) {
await _cleanup(db_type);
}
} else {
process.stdout.write(`Skipping DB type '${db_type}'\n`);
}
}
return num_failures;
}
if (require.main === module) {
const start_time = new Date();
run_tests({})
.then(num_failures => {
const end_time = new Date();
const elapsed = end_time - start_time;
if (num_failures === 0) {
process.stdout.write(
chalk.cyan("Test results: ") +
chalk.green("PASS") +
"\n" +
chalk.cyan("Duration: ") +
chalk.green(elapsed) +
"ms\n"
);
process.exit(0);
} else {
process.stdout.write(
chalk.cyan("Test results: ") +
chalk.red("FAIL") +
"\n" +
chalk.cyan("Duration: ") +
chalk.red(elapsed) +
"ms\n"
);
process.exit(1);
}
})
.catch(err => {
const end_time = new Date();
const elapsed = end_time - start_time;
process.stderr.write(err.stack + "\n\n");
process.stderr.write(
chalk.cyan("Test results: ") +
chalk.magenta("ERROR") +
"\n" +
chalk.cyan("Duration: ") +
chalk.magenta(elapsed) +
"ms\n"
);
process.exit(2);
});
}
// Generic helpers
// Dispatch
async function _setup_tests(database_type) {
switch (database_type) {
case "sqlite":
return await _setup_sqlite();
case "mysql":
return await _setup_mysql();
case "mysql2":
return await _setup_mysql2();
case "pg":
return await _setup_pg();
case "dummy":
return false;
default:
// Not real sure how we'd get here, but just in case
throw new Error("Got an invalid db_type to test!");
}
}
// Clean up afterwards
function _cleanup(database_type) {
switch (database_type) {
case "sqlite":
return _cleanup_sqlite();
case "mysql":
return _cleanup_mysql();
case "mysql2":
return _cleanup_mysql2();
case "pg":
return _cleanup_pg();
default:
return true;
}
}
// Populate the DBs
async function _populate_db(test_db) {
for (const table_name of test_fixture_data.table_order) {
const table_db = test_db(table_name);
for (const row of test_fixture_data.table_data[table_name]) {
await table_db.insert(row);
}
}
}
async function _build_table_creation(options) {
const { database_type, test_db, drop, quote } = options;
const quoted = quote || "";
// Clear out the tables in case this isn't sqlite
if (drop === true) {
for (const table_name of test_fixture_data.table_order.slice().reverse()) {
await test_db.raw(
`DROP TABLE IF EXISTS ${quoted}${table_name}${quoted};`
);
}
}
const statements = fs
.readFileSync(`test/databases/table_creation_${database_type}.sql`)
.toString()
.split("\n");
for (const stmt of statements) {
if (stmt.length > 0) {
await test_db.raw(stmt);
}
}
return true;
}
// SQLite helpers
async function _setup_sqlite() {
const sqlite_db_filename = `jaorm-test-${Date.now()}.db`;
sqlite_test_db_configuration.connection.filename = sqlite_db_filename;
// We don't actually use the sqlite db directly, so it's fine that we don't
// assign it to anything
// eslint-disable-next-line no-new
new sqlite3.Database(sqlite_db_filename);
const test_db = knex(sqlite_test_db_configuration);
await _build_table_creation({
database_type: "sqlite",
test_db
});
await _populate_db(test_db);
return true;
}
function _cleanup_sqlite() {
const sqlite_filename = sqlite_test_db_configuration.connection.filename;
if (sqlite_filename) {
fs.unlinkSync(sqlite_filename);
}
return true;
}
// MySQL helpers
async function _setup_mysql() {
if (!process.env.JAORM_MYSQL_TEST_ENABLED) {
return false;
}
const test_db = knex(mysql_test_db_configuration);
await _build_table_creation({
database_type: "mysql",
drop: true,
test_db
});
await _populate_db(test_db);
return true;
}
function _cleanup_mysql() {
return true;
}
// MySQL2 helpers
async function _setup_mysql2() {
if (!process.env.JAORM_MYSQL2_TEST_ENABLED) {
return false;
}
const test_db = knex(mysql2_test_db_configuration);
await _build_table_creation({
database_type: "mysql",
drop: true,
test_db
});
await _populate_db(test_db);
return true;
}
function _cleanup_mysql2() {
return true;
}
// PG helpers
async function _setup_pg() {
if (!process.env.JAORM_PG_TEST_ENABLED) {
return false;
}
const test_db = knex(pg_test_db_configuration);
await _build_table_creation({
database_type: "pg",
drop: true,
test_db,
quote: '"'
});
await _populate_db(test_db);
return true;
}
function _cleanup_pg() {
return true;
}