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
import { sqlite } from "https://esm.town/v/stevekrouse/sqlite_upgraded";
import { expect, prettify, Test } from "npm:tiny-jest";
const { it, run, title, before, after } = new Test(`basic`);
const TABLE_NAME = `__test_delete_me_${Date.now()}`;
before(async () => {
await sqlite.execute(`CREATE TABLE IF NOT EXISTS ${TABLE_NAME} (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)`);
await sqlite.execute({ sql: `INSERT INTO ${TABLE_NAME} (name) VALUES (?)`, args: [`Alice`] });
await sqlite.execute({ sql: `INSERT INTO ${TABLE_NAME} (name) VALUES (?)`, args: [`Bob`] });
});
after(async () => {
await sqlite.execute(`DROP TABLE IF EXISTS ${TABLE_NAME}`);
});
it(`Test execute method`, async () => {
const result = await sqlite.execute(`SELECT * FROM ${TABLE_NAME}`);
const row = result.rows[0];
expect(row.length).toBe(2);
expect(row.id).toBe(1);
expect(row[0]).toBe(1);
expect(row.name).toBe(`Alice`);
expect(row[1]).toBe(`Alice`);
const row2 = result.rows[1];
expect(row2.length).toBe(2);
expect(row2.id).toBe(2);
expect(row2[0]).toBe(2);
expect(row2.name).toBe(`Bob`);
expect(row2[1]).toBe(`Bob`);
});
it(`Test batch method`, async () => {
const result = await sqlite.batch([
{ sql: `INSERT INTO ${TABLE_NAME} (name) VALUES (?)`, args: [`Charlie`] },
{ sql: `SELECT * FROM ${TABLE_NAME} WHERE name = ?`, args: [`Charlie`] },
]);
const row = result[1].rows[0];
expect(row.length).toBe(2);
expect(row.id).toBe(3);
expect(row[0]).toBe(3);
expect(row.name).toBe(`Charlie`);
expect(row[1]).toBe(`Charlie`);
});
run().then(prettify);
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
June 30, 2024