forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-sqlite-template-tag.js
More file actions
126 lines (103 loc) Β· 4.26 KB
/
test-sqlite-template-tag.js
File metadata and controls
126 lines (103 loc) Β· 4.26 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
126
'use strict';
const { skipIfSQLiteMissing } = require('../common');
skipIfSQLiteMissing();
const assert = require('assert');
const { DatabaseSync } = require('node:sqlite');
const { test, beforeEach } = require('node:test');
const db = new DatabaseSync(':memory:');
const sql = db.createTagStore(10);
beforeEach(() => {
db.exec('DROP TABLE IF EXISTS foo');
db.exec('CREATE TABLE foo (id INTEGER PRIMARY KEY, text TEXT)');
sql.clear();
});
test('sql.run inserts data', () => {
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'bob'})`.changes, 1);
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'mac'})`.changes, 1);
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'alice'})`.changes, 1);
const count = db.prepare('SELECT COUNT(*) as count FROM foo').get().count;
assert.strictEqual(count, 3);
});
test('sql.get retrieves a single row', () => {
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'bob'})`.changes, 1);
const first = sql.get`SELECT * FROM foo ORDER BY id ASC`;
assert.ok(first);
assert.strictEqual(first.text, 'bob');
assert.strictEqual(first.id, 1);
assert.strictEqual(Object.getPrototypeOf(first), null);
});
test('sql.all retrieves all rows', () => {
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'bob'})`.changes, 1);
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'mac'})`.changes, 1);
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'alice'})`.changes, 1);
const all = sql.all`SELECT * FROM foo ORDER BY id ASC`;
assert.strictEqual(Array.isArray(all), true);
assert.strictEqual(all.length, 3);
for (const row of all) {
assert.strictEqual(Object.getPrototypeOf(row), null);
}
assert.deepStrictEqual(all.map((r) => r.text), ['bob', 'mac', 'alice']);
});
test('sql.iterate retrieves rows via iterator', () => {
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'bob'})`.changes, 1);
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'mac'})`.changes, 1);
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'alice'})`.changes, 1);
const iter = sql.iterate`SELECT * FROM foo ORDER BY id ASC`;
const iterRows = [];
for (const row of iter) {
assert.ok(row);
assert.strictEqual(Object.getPrototypeOf(row), null);
iterRows.push(row.text);
}
assert.deepStrictEqual(iterRows, ['bob', 'mac', 'alice']);
});
test('queries with no results', () => {
const none = sql.get`SELECT * FROM foo WHERE text = ${'notfound'}`;
assert.strictEqual(none, undefined);
const empty = sql.all`SELECT * FROM foo WHERE text = ${'notfound'}`;
assert.deepStrictEqual(empty, []);
let count = 0;
// eslint-disable-next-line no-unused-vars
for (const _ of sql.iterate`SELECT * FROM foo WHERE text = ${'notfound'}`) {
count++;
}
assert.strictEqual(count, 0);
});
test('TagStore capacity, size, and clear', () => {
assert.strictEqual(sql.capacity, 10);
assert.strictEqual(sql.size, 0);
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'one'})`.changes, 1);
assert.strictEqual(sql.size, 1);
assert.ok(sql.get`SELECT * FROM foo WHERE text = ${'one'}`);
assert.strictEqual(sql.size, 2);
// Using the same template string shouldn't increase the size
assert.strictEqual(sql.get`SELECT * FROM foo WHERE text = ${'two'}`, undefined);
assert.strictEqual(sql.size, 2);
assert.strictEqual(sql.all`SELECT * FROM foo`.length, 1);
assert.strictEqual(sql.size, 3);
sql.clear();
assert.strictEqual(sql.size, 0);
assert.strictEqual(sql.capacity, 10);
});
test('sql.db returns the associated DatabaseSync instance', () => {
assert.strictEqual(sql.db, db);
});
test('sql error messages are descriptive', () => {
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'test'})`.changes, 1);
// Test with non-existent column
assert.throws(() => {
const result = sql.get`SELECT nonexistent_column FROM foo`;
assert.fail(`Expected error, got: ${JSON.stringify(result)}`);
}, {
code: 'ERR_SQLITE_ERROR',
message: /no such column/i,
});
// Test with non-existent table
assert.throws(() => {
const result = sql.get`SELECT * FROM nonexistent_table`;
assert.fail(`Expected error, got: ${JSON.stringify(result)}`);
}, {
code: 'ERR_SQLITE_ERROR',
message: /no such table/i,
});
});