-
Notifications
You must be signed in to change notification settings - Fork 2
/
postgres.spec.ts
81 lines (67 loc) · 1.93 KB
/
postgres.spec.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
72
73
74
75
76
77
78
79
80
81
import anytest, { TestFn } from "ava";
import { v4 } from "uuid";
import { suites } from "./driver.suite.js";
import { Context } from "./driver.types.js";
import { fromPg, PgDriver, QUERIES } from "../../src/driver/postgres.js";
import pg from "pg";
const ENABLED = typeof process.env.POSTGRES_URL === "string";
const test = anytest as TestFn<Context<PgDriver>>;
(ENABLED ? test.before : test.before.skip)(async (t) => {
const pool = new pg.Pool({
connectionString: process.env.POSTGRES_URL,
min: 1,
max: 20,
});
await pool.query(`DROP SCHEMA IF EXISTS "test" CASCADE`);
// for testing in mass-parallel, create the schema first to avoid
// deadlocking the pg_namespace_nspname_index table
const tmp = new PgDriver(pool, {
schema: "test",
table: v4(),
});
await tmp.ready();
t.context.createDriver = async (options) => {
const driver = new PgDriver(pool, {
schema: "test",
table: v4(),
...(options ?? {}),
});
driver.events.on("error", (e) => {
// log driver errors for sanity
t.log(e);
if (e.original) {
t.log(e.original);
}
t.fail();
});
return Promise.resolve(driver);
};
return Promise.resolve();
});
test.beforeEach(async (t) => {
t.context.setDriver = async (d) => {
t.context.insert = async (doc) => {
const p = d.getPool();
const tn = d.getQueryObjects();
await p.query(
QUERIES.insertOne.query(tn),
QUERIES.insertOne.variables(doc)
);
};
t.context.dump = async () => {
const p = d.getPool();
const tn = d.getQueryObjects();
const res = await p.query(`
SELECT * FROM ${tn.table}
`);
return res.rows.map(fromPg);
};
t.context.driver = d;
await d.ready();
};
const driver = await t.context.createDriver();
await t.context.setDriver(driver);
});
for (const s of suites<PgDriver>()) {
(ENABLED ? test : test.skip)(s.title, s.test);
}