@zerotal/testing 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/LICENSE +21 -0
- package/README.md +121 -0
- package/package.json +56 -0
- package/src/TestApp.ts +573 -0
- package/src/TestExceptionHandler.ts +54 -0
- package/src/TestResponse.ts +953 -0
- package/src/assertions.ts +84 -0
- package/src/data.ts +4433 -0
- package/src/factory.ts +288 -0
- package/src/fake.ts +462 -0
- package/src/fakeFile.ts +229 -0
- package/src/global.d.ts +20 -0
- package/src/index.ts +30 -0
- package/src/migrateDatabase.ts +66 -0
- package/src/preload.ts +33 -0
- package/src/refreshDatabase.ts +115 -0
- package/src/resetTestState.ts +12 -0
- package/src/storageAssertions.ts +52 -0
- package/src/withDatabase.ts +52 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { _getModelConnection } from "@zerotal/orm";
|
|
2
|
+
|
|
3
|
+
async function countRows(table: string, where: Record<string, unknown>): Promise<number> {
|
|
4
|
+
const conn = _getModelConnection() as unknown as (
|
|
5
|
+
strings: TemplateStringsArray,
|
|
6
|
+
...values: unknown[]
|
|
7
|
+
) => Promise<Array<{ cnt: number }>>;
|
|
8
|
+
|
|
9
|
+
const entries = Object.entries(where);
|
|
10
|
+
const parts: string[] = [];
|
|
11
|
+
const vals: unknown[] = [];
|
|
12
|
+
|
|
13
|
+
let sql = `SELECT COUNT(*) as cnt FROM ${table}`;
|
|
14
|
+
entries.forEach(([col, val], i) => {
|
|
15
|
+
sql += i === 0 ? ` WHERE ${col} = ` : ` AND ${col} = `;
|
|
16
|
+
parts.push(sql);
|
|
17
|
+
vals.push(val);
|
|
18
|
+
sql = "";
|
|
19
|
+
});
|
|
20
|
+
parts.push(sql);
|
|
21
|
+
|
|
22
|
+
const tpl = Object.assign([...parts], { raw: [...parts] }) as unknown as TemplateStringsArray;
|
|
23
|
+
const rows = await conn(tpl, ...vals);
|
|
24
|
+
return Number(rows[0]?.cnt ?? 0);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Assert that at least one row in `table` matches all key/value pairs in `where`.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* await assertDatabaseHas('users', { email: 'alice@example.com' });
|
|
32
|
+
*/
|
|
33
|
+
export async function assertDatabaseHas(
|
|
34
|
+
table: string,
|
|
35
|
+
where: Record<string, unknown>,
|
|
36
|
+
): Promise<void> {
|
|
37
|
+
const count = await countRows(table, where);
|
|
38
|
+
if (count === 0) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`assertDatabaseHas: expected table "${table}" to contain a row matching ` +
|
|
41
|
+
`${JSON.stringify(where)}, but no such row was found.`,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Assert that `table` contains exactly `expected` rows (optionally filtered by `where`).
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* await assertDatabaseCount('users', 3);
|
|
51
|
+
* await assertDatabaseCount('posts', 1, { published: 1 });
|
|
52
|
+
*/
|
|
53
|
+
export async function assertDatabaseCount(
|
|
54
|
+
table: string,
|
|
55
|
+
expected: number,
|
|
56
|
+
where: Record<string, unknown> = {},
|
|
57
|
+
): Promise<void> {
|
|
58
|
+
const count = await countRows(table, where);
|
|
59
|
+
if (count !== expected) {
|
|
60
|
+
const filter = Object.keys(where).length ? ` matching ${JSON.stringify(where)}` : "";
|
|
61
|
+
throw new Error(
|
|
62
|
+
`assertDatabaseCount: expected table "${table}"${filter} to have ${expected} row(s), but found ${count}.`,
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Assert that no row in `table` matches all key/value pairs in `where`.
|
|
69
|
+
*
|
|
70
|
+
* @example
|
|
71
|
+
* await assertDatabaseMissing('users', { email: 'deleted@example.com' });
|
|
72
|
+
*/
|
|
73
|
+
export async function assertDatabaseMissing(
|
|
74
|
+
table: string,
|
|
75
|
+
where: Record<string, unknown>,
|
|
76
|
+
): Promise<void> {
|
|
77
|
+
const count = await countRows(table, where);
|
|
78
|
+
if (count > 0) {
|
|
79
|
+
throw new Error(
|
|
80
|
+
`assertDatabaseMissing: expected table "${table}" NOT to contain a row matching ` +
|
|
81
|
+
`${JSON.stringify(where)}, but ${count} row(s) were found.`,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|