|
| 1 | +import { |
| 2 | + expect, |
| 3 | + isNitroSQLiteError, |
| 4 | + TEST_ERROR_CODES, |
| 5 | +} from '@tests/unit/common' |
| 6 | +import { describe, it } from '@tests/TestApi' |
| 7 | +import { testDb } from '@tests/db' |
| 8 | + |
| 9 | +const valuesQuery = |
| 10 | + 'SELECT 1 AS duplicate, 2 AS duplicate, 3.5 AS "café", NULL AS nullable, zeroblob(2) AS payload' |
| 11 | + |
| 12 | +const joinQuery = ` |
| 13 | + SELECT owners.id, owners.name, pets.id, pets.name |
| 14 | + FROM owners LEFT JOIN pets ON pets.owner_id = owners.id |
| 15 | + ORDER BY owners.id |
| 16 | +` |
| 17 | + |
| 18 | +function createJoinTables() { |
| 19 | + testDb.execute('CREATE TABLE owners (id INTEGER PRIMARY KEY, name TEXT)') |
| 20 | + testDb.execute( |
| 21 | + 'CREATE TABLE pets (id INTEGER PRIMARY KEY, name TEXT, owner_id INTEGER)', |
| 22 | + ) |
| 23 | + testDb.execute("INSERT INTO owners VALUES (1, 'Dave'), (2, 'Ritesh')") |
| 24 | + testDb.execute("INSERT INTO pets VALUES (7, 'Rex', 1)") |
| 25 | +} |
| 26 | + |
| 27 | +function expectValuesRow(row: unknown[] | undefined) { |
| 28 | + expect(row?.slice(0, 4)).toEqual([1, 2, 3.5, null]) |
| 29 | + expect(Array.from(new Uint8Array(row?.[4] as ArrayBuffer))).toEqual([0, 0]) |
| 30 | +} |
| 31 | + |
| 32 | +export default function registerExecuteRawUnitTests() { |
| 33 | + describe('executeRaw', () => { |
| 34 | + it('returns every value type and keeps repeated column names', async () => { |
| 35 | + const result = testDb.executeRaw(valuesQuery) |
| 36 | + expect(result.columnNames).toEqual([ |
| 37 | + 'duplicate', |
| 38 | + 'duplicate', |
| 39 | + 'café', |
| 40 | + 'nullable', |
| 41 | + 'payload', |
| 42 | + ]) |
| 43 | + expect(result.rows).toHaveLength(1) |
| 44 | + expectValuesRow(result.rows[0]) |
| 45 | + |
| 46 | + const asyncResult = await testDb.executeRawAsync(valuesQuery) |
| 47 | + expect(asyncResult.columnNames).toEqual(result.columnNames) |
| 48 | + expectValuesRow(asyncResult.rows[0]) |
| 49 | + }) |
| 50 | + |
| 51 | + it('keeps both sides of a join with matching column names', async () => { |
| 52 | + createJoinTables() |
| 53 | + const expected = [ |
| 54 | + [1, 'Dave', 7, 'Rex'], |
| 55 | + [2, 'Ritesh', null, null], |
| 56 | + ] |
| 57 | + |
| 58 | + const result = testDb.executeRaw(joinQuery) |
| 59 | + expect(result.columnNames).toEqual(['id', 'name', 'id', 'name']) |
| 60 | + expect(result.rows).toEqual(expected) |
| 61 | + expect((await testDb.executeRawAsync(joinQuery)).rows).toEqual(expected) |
| 62 | + }) |
| 63 | + |
| 64 | + it('binds parameters and reports changes for writes', async () => { |
| 65 | + const inserted = testDb.executeRaw( |
| 66 | + 'INSERT INTO User (id, name, age, networth) VALUES (?, ?, ?, ?)', |
| 67 | + [1, 'Ada', 36, 1.5], |
| 68 | + ) |
| 69 | + expect(inserted.rowsAffected).toBe(1) |
| 70 | + expect(inserted.insertId).toBe(1) |
| 71 | + expect(inserted.columnNames).toEqual([]) |
| 72 | + expect(inserted.rows).toEqual([]) |
| 73 | + |
| 74 | + const selected = await testDb.executeRawAsync( |
| 75 | + 'SELECT name, age FROM User WHERE id = ?', |
| 76 | + [1], |
| 77 | + ) |
| 78 | + expect(selected.rows).toEqual([['Ada', 36]]) |
| 79 | + }) |
| 80 | + |
| 81 | + it('returns column names for an empty result', () => { |
| 82 | + const result = testDb.executeRaw('SELECT id, name FROM User WHERE 0') |
| 83 | + expect(result.columnNames).toEqual(['id', 'name']) |
| 84 | + expect(result.rows).toEqual([]) |
| 85 | + }) |
| 86 | + |
| 87 | + it('returns large results in order', async () => { |
| 88 | + const query = ` |
| 89 | + WITH RECURSIVE counter(value) AS ( |
| 90 | + SELECT 1 UNION ALL SELECT value + 1 FROM counter WHERE value < 10000 |
| 91 | + ) |
| 92 | + SELECT value, 'row-' || value FROM counter |
| 93 | + ` |
| 94 | + for (const result of [ |
| 95 | + testDb.executeRaw(query), |
| 96 | + await testDb.executeRawAsync(query), |
| 97 | + ]) { |
| 98 | + expect(result.rows).toHaveLength(10_000) |
| 99 | + expect(result.rows[0]).toEqual([1, 'row-1']) |
| 100 | + expect(result.rows[9_999]).toEqual([10_000, 'row-10000']) |
| 101 | + } |
| 102 | + }) |
| 103 | + |
| 104 | + it('runs inside a transaction', async () => { |
| 105 | + createJoinTables() |
| 106 | + await testDb.transaction(async (tx) => { |
| 107 | + tx.executeRaw("INSERT INTO pets VALUES (8, 'Tom', 2)") |
| 108 | + expect((await tx.executeRawAsync(joinQuery)).rows).toEqual([ |
| 109 | + [1, 'Dave', 7, 'Rex'], |
| 110 | + [2, 'Ritesh', 8, 'Tom'], |
| 111 | + ]) |
| 112 | + }) |
| 113 | + }) |
| 114 | + |
| 115 | + it('runs prepared statements and switches between raw and object rows', async () => { |
| 116 | + createJoinTables() |
| 117 | + const statement = testDb.prepare(`${joinQuery} LIMIT ?`) |
| 118 | + try { |
| 119 | + expect(statement.executeRaw([1]).rows).toEqual([[1, 'Dave', 7, 'Rex']]) |
| 120 | + expect(statement.execute([1]).results).toEqual([{ id: 7, name: 'Rex' }]) |
| 121 | + const asyncResult = await statement.executeRawAsync([2]) |
| 122 | + expect(asyncResult.columnNames).toEqual(['id', 'name', 'id', 'name']) |
| 123 | + expect(asyncResult.rows).toHaveLength(2) |
| 124 | + } finally { |
| 125 | + statement.finalize() |
| 126 | + } |
| 127 | + }) |
| 128 | + |
| 129 | + it('throws NitroSQLiteError for invalid SQL', async () => { |
| 130 | + try { |
| 131 | + testDb.executeRaw('SELECT * FROM missing_table') |
| 132 | + throw new Error(TEST_ERROR_CODES.EXPECT_NITRO_SQLITE_ERROR) |
| 133 | + } catch (error) { |
| 134 | + expect(isNitroSQLiteError(error)).toBe(true) |
| 135 | + } |
| 136 | + |
| 137 | + try { |
| 138 | + await testDb.executeRawAsync('SELECT * FROM missing_table') |
| 139 | + throw new Error(TEST_ERROR_CODES.EXPECT_PROMISE_REJECTION) |
| 140 | + } catch (error) { |
| 141 | + expect(isNitroSQLiteError(error)).toBe(true) |
| 142 | + } |
| 143 | + }) |
| 144 | + }) |
| 145 | +} |
0 commit comments