(context)
| 2 | import * as SQLite from '../src/sqlite-api.js'; |
| 3 | |
| 4 | export function api_exec(context) { |
| 5 | describe('exec', function() { |
| 6 | let proxy, sqlite3, db; |
| 7 | beforeEach(async function() { |
| 8 | proxy = await context.create(); |
| 9 | sqlite3 = proxy.sqlite3; |
| 10 | db = await sqlite3.open_v2('demo'); |
| 11 | }); |
| 12 | |
| 13 | afterEach(async function() { |
| 14 | await sqlite3.close(db); |
| 15 | await context.destroy(proxy); |
| 16 | }); |
| 17 | |
| 18 | it('should execute a query', async function() { |
| 19 | let rc; |
| 20 | rc = await sqlite3.exec(db, 'CREATE TABLE t(x)'); |
| 21 | expect(rc).toEqual(SQLite.SQLITE_OK); |
| 22 | |
| 23 | rc = await sqlite3.exec(db, 'INSERT INTO t VALUES (1), (2), (3)'); |
| 24 | expect(rc).toEqual(SQLite.SQLITE_OK); |
| 25 | |
| 26 | const nChanges = await sqlite3.changes(db); |
| 27 | expect(nChanges).toEqual(3); |
| 28 | }); |
| 29 | |
| 30 | it('should execute multiple queries', async function() { |
| 31 | let rc; |
| 32 | rc = await sqlite3.exec(db, ` |
| 33 | CREATE TABLE t(x); |
| 34 | INSERT INTO t VALUES (1), (2), (3); |
| 35 | `); |
| 36 | expect(rc).toEqual(SQLite.SQLITE_OK); |
| 37 | await expectAsync(sqlite3.changes(db)).toBeResolvedTo(3); |
| 38 | }); |
| 39 | |
| 40 | it('should return query results via callback', async function() { |
| 41 | const results = { rows: [], columns: [] }; |
| 42 | const rc = await sqlite3.exec(db, ` |
| 43 | CREATE TABLE t(x); |
| 44 | INSERT INTO t VALUES (1), (2), (3); |
| 45 | SELECT * FROM t ORDER BY x; |
| 46 | `, Comlink.proxy((row, columns) => { |
| 47 | if (columns.length) { |
| 48 | results.columns = columns; |
| 49 | results.rows.push(row); |
| 50 | } |
| 51 | })); |
| 52 | expect(rc).toEqual(SQLite.SQLITE_OK); |
| 53 | expect(results).toEqual({ columns: ['x'], rows: [[1], [2], [3]] }); |
| 54 | }); |
| 55 | |
| 56 | it('should allow a transaction to span multiple calls', async function() { |
| 57 | let rc; |
| 58 | rc = await sqlite3.get_autocommit(db); |
| 59 | expect(rc).not.toEqual(0); |
| 60 | |
| 61 | rc = await sqlite3.exec(db, 'BEGIN TRANSACTION'); |
searching dependent graphs…