forked from Vincit/objection.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestSession.js
451 lines (402 loc) · 12.6 KB
/
TestSession.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
const _ = require('lodash');
const path = require('path');
const Promise = require('bluebird');
const knexUtils = require('../lib/utils/knexUtils');
const { Model, transaction, snakeCaseMappers, ref } = require('../');
const chai = require('chai');
chai.use(require('chai-subset'));
class TestSession {
static init() {
if (this.staticInitCalled) {
return;
}
registerUnhandledRejectionHandler();
this.staticInitCalled = true;
}
constructor(opt) {
TestSession.init();
this.opt = opt;
this.knex = this.createKnex(opt);
this.unboundModels = this.createModels();
this.models = _.mapValues(this.unboundModels, (model) => model.bindKnex(this.knex));
}
createKnex() {
return require('knex')(this.opt.knexConfig);
}
createModels() {
class Model1 extends Model {
static get tableName() {
return 'Model1';
}
// Function instead of getter on purpose.
static idColumn() {
return 'id';
}
static get modifiers() {
return {
orderById: (builder) => builder.orderBy('Model1.id'),
'select:id': (builder) => builder.select(this.ref('id')),
'select:model1Prop1': (builder) => builder.select('model1Prop1'),
'select:model1Prop1Aliased': (builder) =>
builder.select('model1Prop1 as aliasedInFilter'),
'orderBy:model1Prop1': (builder) => builder.orderBy('model1Prop1'),
idGreaterThan: (builder) => builder.where('id', '>', builder.context().filterArgs[0]),
};
}
static get relationMappings() {
return {
model1Relation1: {
relation: Model.BelongsToOneRelation,
modelClass: Model1,
join: {
from: 'Model1.model1Id',
to: 'Model1.id',
},
},
model1Relation1Inverse: {
relation: Model.HasOneRelation,
modelClass: Model1,
join: {
from: 'Model1.id',
to: 'Model1.model1Id',
},
},
model1Relation2: {
relation: Model.HasManyRelation,
modelClass: Model2,
join: {
from: 'Model1.id',
to: 'model2.model1_id',
},
},
model1Relation3: {
relation: Model.ManyToManyRelation,
modelClass: Model2,
join: {
from: 'Model1.id',
through: {
from: 'Model1Model2.model1Id',
to: 'Model1Model2.model2Id',
extra: ['extra1', 'extra2'],
},
to: 'model2.id_col',
},
},
};
}
}
class Model2 extends Model {
// Function instead of getter on purpose.
static tableName() {
return 'model2';
}
static get idColumn() {
return 'id_col';
}
static get columnNameMappers() {
return snakeCaseMappers();
}
static get modifiers() {
return {
orderById: (builder) => builder.orderBy('model2.id_col'),
};
}
static get relationMappings() {
return {
model2Relation1: {
relation: Model.ManyToManyRelation,
modelClass: Model1,
join: {
from: 'model2.id_col',
through: {
from: 'Model1Model2.model2Id',
to: 'Model1Model2.model1Id',
extra: { aliasedExtra: 'extra3' },
},
to: 'Model1.id',
},
},
model2Relation2: {
relation: Model.HasOneThroughRelation,
modelClass: Model1,
join: {
from: 'model2.id_col',
through: {
from: 'Model1Model2One.model2Id',
to: 'Model1Model2One.model1Id',
},
to: 'Model1.id',
},
},
model2Relation3: {
relation: Model.ManyToManyRelation,
modelClass: Model3,
join: {
from: 'model2.id_col',
through: {
from: 'Model2Model3ManyToMany.model2Id',
to: 'Model2Model3ManyToMany.model3Id',
},
to: 'model3.id',
},
},
};
}
}
class Model3 extends Model {
// Function instead of getter on purpose.
static tableName() {
return 'model3';
}
static get idColumn() {
return 'id';
}
static get jsonAttributes() {
return ['model3JsonProp'];
}
static get modifiers() {
return {
orderById: (builder) => builder.orderBy('model3.id'),
};
}
}
[
['$beforeInsert', 1],
['$afterInsert', 0],
['$beforeDelete', 1],
['$afterDelete', 1],
['$beforeUpdate', 1, (self, args) => (self.$beforeUpdateOptions = _.cloneDeep(args[0]))],
['$afterUpdate', 1, (self, args) => (self.$afterUpdateOptions = _.cloneDeep(args[0]))],
['$afterFind', 1],
].forEach((hook) => {
Model1.prototype[hook[0]] = createHook(hook[0], hook[1], hook[2]);
Model2.prototype[hook[0]] = createHook(hook[0], hook[1], hook[2]);
Model3.prototype[hook[0]] = createHook(hook[0], hook[1], hook[2]);
});
return {
Model1: Model1,
Model2: Model2,
Model3: Model3,
};
}
createDb() {
const knex = this.knex;
const opt = this.opt;
return Promise.resolve()
.then(() => knex.schema.dropTableIfExists('Model1Model2'))
.then(() => knex.schema.dropTableIfExists('Model1Model2One'))
.then(() => knex.schema.dropTableIfExists('Model2Model3ManyToMany'))
.then(() => knex.schema.dropTableIfExists('model2'))
.then(() => knex.schema.dropTableIfExists('Model1'))
.then(() => knex.schema.dropTableIfExists('model3'))
.then(() => {
return knex.schema
.createTable('Model1', (table) => {
table.increments('id').primary();
table
.integer('model1Id')
.index()
.unsigned()
.references('Model1.id')
.onDelete('SET NULL');
table.string('model1Prop1');
table.integer('model1Prop2');
})
.createTable('model2', (table) => {
table.increments('id_col').primary();
table
.integer('model1_id')
.index()
.unsigned()
.references('Model1.id')
.onDelete('SET NULL');
table.string('model2_prop1');
table.integer('model2_prop2');
})
.createTable('model3', (table) => {
table.increments('id').primary();
table.string('model3Prop1');
table.text('model3JsonProp');
})
.createTable('Model1Model2', (table) => {
table.increments('id').primary();
table.string('extra1');
table.string('extra2');
table.string('extra3');
table
.integer('model1Id')
.unsigned()
.notNullable()
.references('id')
.inTable('Model1')
.onDelete('CASCADE')
.index();
table
.integer('model2Id')
.unsigned()
.notNullable()
.references('id_col')
.inTable('model2')
.onDelete('CASCADE')
.index();
})
.createTable('Model1Model2One', (table) => {
table
.integer('model1Id')
.unsigned()
.notNullable()
.references('id')
.inTable('Model1')
.onDelete('CASCADE')
.index();
table
.integer('model2Id')
.unsigned()
.notNullable()
.references('id_col')
.inTable('model2')
.onDelete('CASCADE')
.index();
})
.createTable('Model2Model3ManyToMany', (table) => {
table
.integer('model2Id')
.unsigned()
.notNullable()
.references('id_col')
.inTable('model2')
.onDelete('CASCADE')
.index();
table
.integer('model3Id')
.unsigned()
.notNullable()
.references('id')
.inTable('model3')
.onDelete('CASCADE')
.index();
});
})
.catch((cause) => {
const err = new Error(
'Could not connect to ' +
opt.knexConfig.client +
'. Make sure the server is running and the database ' +
opt.knexConfig.connection.database +
' is created. You can see the test database configurations from file ' +
path.join(__dirname, 'index.js')
);
const oldStack = err.stack;
Object.defineProperties(err, {
stack: {
get() {
return oldStack + `\n\nCaused by:\n${cause.stack}`;
},
},
});
throw err;
});
}
populate(data) {
return transaction(this.knex, (trx) => {
return trx('Model1Model2')
.delete()
.then(() => trx('Model1Model2One').delete())
.then(() => trx('Model2Model3ManyToMany').delete())
.then(() => trx('model2').delete())
.then(() => trx('Model1').delete())
.then(() => trx('model3').delete())
.then(() => this.models.Model1.query(trx).insertGraph(data))
.then(() => {
return Promise.resolve(['Model1', 'model2', 'model3', 'Model1Model2']).map((table) => {
const idCol = (
_.find(this.models, (it) => it.getTableName() === table) || {
getIdColumn: () => 'id',
}
).getIdColumn();
return trx(table)
.max(idCol)
.then((res) => {
const maxId = parseInt(res[0][_.keys(res[0])[0]], 10) || 0;
// Reset sequence.
if (knexUtils.isSqlite(trx)) {
return trx.raw(
'UPDATE sqlite_sequence SET seq = ' + maxId + ' WHERE name = "' + table + '"'
);
} else if (knexUtils.isPostgres(trx)) {
return trx.raw(
'ALTER SEQUENCE "' + table + '_' + idCol + '_seq" RESTART WITH ' + (maxId + 1)
);
} else if (knexUtils.isMySql(trx)) {
return trx.raw('ALTER TABLE ' + table + ' AUTO_INCREMENT = ' + (maxId + 1));
} else {
throw new Error('sequence truncate not implemented for the given database');
}
});
});
})
.then(() => data);
});
}
destroy() {
return this.knex.destroy();
}
addUnhandledRejectionHandler(handler) {
const handlers = TestSession.unhandledRejectionHandlers;
handlers.push(handler);
}
removeUnhandledRejectionHandler(handler) {
const handlers = TestSession.unhandledRejectionHandlers;
handlers.splice(handlers.indexOf(handler), 1);
}
isPostgres() {
return knexUtils.isPostgres(this.knex);
}
isMySql() {
return knexUtils.isMySql(this.knex);
}
isSqlite() {
return knexUtils.isSqlite(this.knex);
}
}
TestSession.staticInitCalled = false;
TestSession.unhandledRejectionHandlers = [];
TestSession.hookCounter = 0;
// Creates a hook that waits for `delay` milliseconds and then
// increments a `${name}Called` property. The hook is asynchonous
// every other time it is called so that the synchronous path is
// also tested.
function createHook(name, delay, extraAction) {
const hook = (model, args) => {
// Increment the property so that it can be checked in the tests.
inc(model, `${name}Called`);
// Optionally run the extraAction function.
(extraAction || _.noop)(model, args);
};
return function () {
const args = arguments;
if (TestSession.hookCounter++ % 2 === 0) {
return hook(this, args);
} else {
return Promise.delay(delay).then(() => hook(this, args));
}
};
}
function inc(obj, key) {
if (!_.has(obj, key)) {
obj[key] = 1;
} else {
obj[key]++;
}
}
function registerUnhandledRejectionHandler() {
Promise.onPossiblyUnhandledRejection((error) => {
if (_.isEmpty(TestSession.unhandledRejectionHandlers)) {
console.error(error.stack);
}
TestSession.unhandledRejectionHandlers.forEach((handler) => {
handler(error);
});
});
}
module.exports = TestSession;