|
| 1 | +jest.mock('../nitro') |
| 2 | + |
| 3 | +import { NitroSQLite } from '../index' |
| 4 | +import { HybridNitroSQLite } from '../nitro' |
| 5 | +import { |
| 6 | + closeDatabaseQueue, |
| 7 | + isDatabaseOpen, |
| 8 | + openDatabaseQueue, |
| 9 | +} from '../DatabaseQueue' |
| 10 | +import NitroSQLiteError from '../NitroSQLiteError' |
| 11 | +import { deferred, nativeRawResult, nativeResult } from './testUtils' |
| 12 | + |
| 13 | +const dbName = 'execute-raw-test' |
| 14 | +const query = 'SELECT ? AS value' |
| 15 | +const raw = nativeRawResult(['value'], [[7]]) |
| 16 | + |
| 17 | +beforeEach(() => { |
| 18 | + jest.clearAllMocks() |
| 19 | + jest.mocked(HybridNitroSQLite.executeRaw).mockReturnValue(raw) |
| 20 | + jest.mocked(HybridNitroSQLite.executeRawAsync).mockResolvedValue(raw) |
| 21 | + jest.mocked(HybridNitroSQLite.execute).mockReturnValue(nativeResult()) |
| 22 | + jest.mocked(HybridNitroSQLite.executeAsync).mockResolvedValue(nativeResult()) |
| 23 | +}) |
| 24 | + |
| 25 | +afterEach(() => { |
| 26 | + if (isDatabaseOpen(dbName)) closeDatabaseQueue(dbName) |
| 27 | +}) |
| 28 | + |
| 29 | +it('forwards unmanaged raw queries and parameters without adapting their rows', async () => { |
| 30 | + expect(NitroSQLite.executeRaw(dbName, query, [7])).toBe(raw) |
| 31 | + await expect(NitroSQLite.executeRawAsync(dbName, query, [7])).resolves.toBe( |
| 32 | + raw, |
| 33 | + ) |
| 34 | + expect(HybridNitroSQLite.executeRaw).toHaveBeenCalledWith(dbName, query, [7]) |
| 35 | + expect(HybridNitroSQLite.executeRawAsync).toHaveBeenCalledWith( |
| 36 | + dbName, |
| 37 | + query, |
| 38 | + [7], |
| 39 | + ) |
| 40 | +}) |
| 41 | + |
| 42 | +it('rejects global synchronous raw queries while managed work is pending', async () => { |
| 43 | + openDatabaseQueue(dbName) |
| 44 | + expect(NitroSQLite.executeRaw(dbName, query)).toBe(raw) |
| 45 | + |
| 46 | + const gate = deferred<typeof raw>() |
| 47 | + jest |
| 48 | + .mocked(HybridNitroSQLite.executeRawAsync) |
| 49 | + .mockReturnValueOnce(gate.promise) |
| 50 | + const pending = NitroSQLite.executeRawAsync(dbName, query) |
| 51 | + try { |
| 52 | + expect(() => NitroSQLite.executeRaw(dbName, 'SELECT blocked')).toThrow( |
| 53 | + 'busy', |
| 54 | + ) |
| 55 | + expect(HybridNitroSQLite.executeRaw).toHaveBeenCalledTimes(1) |
| 56 | + } finally { |
| 57 | + gate.resolve(raw) |
| 58 | + await pending |
| 59 | + } |
| 60 | +}) |
| 61 | + |
| 62 | +it('keeps global raw queries outside an active managed transaction', async () => { |
| 63 | + openDatabaseQueue(dbName) |
| 64 | + const entered = deferred<void>() |
| 65 | + const finish = deferred<void>() |
| 66 | + const transaction = NitroSQLite.transaction(dbName, async () => { |
| 67 | + entered.resolve() |
| 68 | + await finish.promise |
| 69 | + }) |
| 70 | + await entered.promise |
| 71 | + |
| 72 | + const pending = NitroSQLite.executeRawAsync(dbName, query) |
| 73 | + try { |
| 74 | + expect(() => NitroSQLite.executeRaw(dbName, query)).toThrow('busy') |
| 75 | + expect(HybridNitroSQLite.executeRaw).not.toHaveBeenCalled() |
| 76 | + expect(HybridNitroSQLite.executeRawAsync).not.toHaveBeenCalled() |
| 77 | + } finally { |
| 78 | + finish.resolve() |
| 79 | + await transaction |
| 80 | + await pending |
| 81 | + } |
| 82 | + expect(HybridNitroSQLite.executeRawAsync).toHaveBeenCalledWith( |
| 83 | + dbName, |
| 84 | + query, |
| 85 | + undefined, |
| 86 | + ) |
| 87 | + expect( |
| 88 | + jest.mocked(HybridNitroSQLite.execute).mock.invocationCallOrder[0], |
| 89 | + ).toBeLessThan( |
| 90 | + jest.mocked(HybridNitroSQLite.executeRawAsync).mock.invocationCallOrder[0]!, |
| 91 | + ) |
| 92 | +}) |
| 93 | + |
| 94 | +it('normalizes native errors and releases the managed queue after failure', async () => { |
| 95 | + openDatabaseQueue(dbName) |
| 96 | + jest.mocked(HybridNitroSQLite.executeRaw).mockImplementationOnce(() => { |
| 97 | + throw new Error('raw failed') |
| 98 | + }) |
| 99 | + expect(() => NitroSQLite.executeRaw(dbName, query)).toThrow(NitroSQLiteError) |
| 100 | + expect(NitroSQLite.executeRaw(dbName, query)).toBe(raw) |
| 101 | + |
| 102 | + jest |
| 103 | + .mocked(HybridNitroSQLite.executeRawAsync) |
| 104 | + .mockRejectedValueOnce('raw async failed') |
| 105 | + await expect( |
| 106 | + NitroSQLite.executeRawAsync(dbName, query), |
| 107 | + ).rejects.toMatchObject({ |
| 108 | + name: 'NitroSQLiteError', |
| 109 | + message: 'raw async failed', |
| 110 | + }) |
| 111 | + await expect(NitroSQLite.executeRawAsync(dbName, query)).resolves.toBe(raw) |
| 112 | +}) |
0 commit comments