-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.test.ts
More file actions
125 lines (96 loc) · 3.17 KB
/
db.test.ts
File metadata and controls
125 lines (96 loc) · 3.17 KB
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
import assert from "node:assert";
import { after, afterEach, before, test } from "node:test";
import { OnlyOneError } from "./errors.ts";
import { testDB } from "./test-help.ts";
const db = testDB();
type DbTesting = {
id: number;
name: string;
};
before(async () => {
await db.query`CREATE TABLE IF NOT EXISTS db_testing (id SERIAL NOT NULL, name TEXT NOT NULL)`;
});
afterEach(async () => {
await db.query`DELETE FROM db_testing`;
});
after(() => {
db.end();
});
test("query", async () => {
const result =
await db.query<DbTesting>`INSERT INTO db_testing(name) VALUES (${"Jimmy"}) RETURNING *`;
const rows = Array.from(result);
assert.ok(rows[0]);
assert.deepStrictEqual(rows, [{ id: rows[0].id, name: "Jimmy" }]);
});
test("one", async () => {
const result =
await db.one<DbTesting>`INSERT INTO db_testing(name) VALUES (${"Sally"}), (${"Jimmy"}) RETURNING *`;
assert.ok(result);
assert.deepStrictEqual(result, { id: result.id, name: "Sally" });
});
test("onlyOne with results", async () => {
const result =
await db.onlyOne<DbTesting>`INSERT INTO db_testing(name) VALUES (${"Sally"}), (${"Jimmy"}) RETURNING *`;
assert.deepStrictEqual(result, { id: result.id, name: "Sally" });
});
test("onlyOne without results", async () => {
await assert.rejects(
async () => await db.onlyOne`SELECT * FROM db_testing WHERE id = -1`,
OnlyOneError,
);
});
test("tx with success", async () => {
const inserted: DbTesting | undefined = await db.tx(async (c) => {
return await c.one<DbTesting>`INSERT INTO db_testing(name) VALUES (${"Tx Sally"}) RETURNING *`;
});
assert.ok(inserted);
const result =
await db.one`SELECT * FROM db_testing WHERE id = ${inserted.id}`;
assert.deepStrictEqual(result, { id: inserted.id, name: "Tx Sally" });
});
test("tx with rollback", async () => {
const inserted = await db.tx(async (tx) => {
const txInserted =
await tx.one<DbTesting>`INSERT INTO db_testing(name) VALUES (${"Tx Sally"}) RETURNING *`;
await tx.rollback();
return txInserted;
});
assert.ok(inserted);
const result =
await db.one`SELECT * FROM db_testing WHERE id = ${inserted.id}`;
assert.strictEqual(result, undefined);
});
test("tx with exception", async () => {
let caught = false;
let inserted: DbTesting | undefined;
try {
await db.tx(async (tx) => {
inserted =
await tx.one<DbTesting>`INSERT INTO db_testing(name) VALUES (${"Tx Sally"}) RETURNING *`;
throw new Error("Something broke");
});
} catch {
caught = true;
}
// since ts doesn't infer the type across the function boundary
inserted ||= undefined;
assert.strictEqual(caught, true);
assert.ok(inserted);
const result =
await db.one`SELECT * FROM db_testing WHERE id = ${inserted.id}`;
assert.strictEqual(result, undefined);
});
test("cursor", async () => {
for (let i = 0; i < 150; i++) {
await db.one`INSERT INTO db_testing(name) VALUES (${`Human ${i}`})`;
}
const lowest = 0;
const cursor = db.cursor(100);
let count = 0;
for await (const row of cursor<DbTesting>`SELECT * FROM db_testing WHERE id > ${lowest}`) {
count += 1;
assert.ok(row.id > 1);
}
assert.equal(count, 150);
});