-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
test-utils.ts
71 lines (65 loc) · 2.08 KB
/
test-utils.ts
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
/* istanbul ignore file */
import { IMemoryDb, ISubscription } from '../interfaces';
import { assert, expect } from 'chai';
import { BaseEntity, Connection } from 'typeorm';
import { _IDb } from '../interfaces-private';
import { newDb } from '../index';
import { Ctor } from '../utils';
export function preventSeqScan(db: IMemoryDb, table?: string): ISubscription {
if (table) {
return db.getTable(table).on('seq-scan', () => {
assert.fail('Should have used index');
});
} else {
return db.on('seq-scan', table => {
assert.fail('Should have used index when requesting table ' + table);
});
}
}
export function preventCataJoin(db: IMemoryDb) {
return db.on('catastrophic-join-optimization', () => {
assert.fail('Should have used index when performing join');
});
}
export function watchCataJoins(db: IMemoryDb) {
let got = 0;
db.on('catastrophic-join-optimization', () => {
got++;
});
return {
check() {
expect(got).to.equal(0, 'Should have used index when performing join');
}
}
}
interface TypeOrmTest {
db: Connection;
mem: _IDb;
many: (sql: string) => any[];
one: (sql: string) => any;
none: (sql: string) => void;
}
export async function typeOrm(title: string
, entities: () => Ctor<BaseEntity>[]
, setup: ((mem: Omit<TypeOrmTest, 'db'>) => any) | null
, fn: (data: TypeOrmTest) => Promise<any>) {
it(title, async () => {
const mem = newDb({
autoCreateForeignKeyIndices: true,
}) as _IDb;
const many = mem.public.many.bind(mem.public);
const none = mem.public.none.bind(mem.public);
const one = mem.public.one.bind(mem.public);
setup?.({ mem, many, none, one });
const db: Connection = await mem.adapters.createTypeormConnection({
type: 'postgres',
entities: entities(),
});
try {
await db.synchronize();
await fn({ db, mem, many, none, one });
} finally {
await db.close()
}
});
}