|
1 | | -import { chance, expect } from '@tests/unit/common' |
| 1 | +import { chance, expect, isNitroSQLiteError } from '@tests/unit/common' |
2 | 2 | import { |
3 | 3 | NitroSQLiteError, |
4 | 4 | type BatchQueryCommand, |
@@ -362,5 +362,80 @@ export default function registerExecuteBatchUnitTests() { |
362 | 362 | { value: 1 }, |
363 | 363 | ]) |
364 | 364 | }) |
| 365 | + it('throws when executeBatch receives an extra parameter without exposing it', () => { |
| 366 | + const extraParameter = 'do-not-expose-batch-parameter' |
| 367 | + |
| 368 | + try { |
| 369 | + testDb.executeBatch([ |
| 370 | + { |
| 371 | + query: 'SELECT ?', |
| 372 | + params: [1, extraParameter], |
| 373 | + }, |
| 374 | + ]) |
| 375 | + throw new Error( |
| 376 | + 'Expected executeBatch to throw for the extra parameter', |
| 377 | + ) |
| 378 | + } catch (error: unknown) { |
| 379 | + if (!isNitroSQLiteError(error)) { |
| 380 | + throw new Error('Should have thrown a valid NitroSQLiteError') |
| 381 | + } |
| 382 | + |
| 383 | + expect(error.message).toContain('parameter 2') |
| 384 | + expect(error.message).toContain('25') |
| 385 | + expect(error.message).toContain('column index out of range') |
| 386 | + expect(error.message.includes(extraParameter)).toBe(false) |
| 387 | + } |
| 388 | + }) |
| 389 | + |
| 390 | + it('rejects when executeBatchAsync receives an extra parameter without exposing it', async () => { |
| 391 | + const extraParameter = 'do-not-expose-batch-async-parameter' |
| 392 | + |
| 393 | + try { |
| 394 | + await testDb.executeBatchAsync([ |
| 395 | + { |
| 396 | + query: 'SELECT ?', |
| 397 | + params: [1, extraParameter], |
| 398 | + }, |
| 399 | + ]) |
| 400 | + throw new Error( |
| 401 | + 'Expected executeBatchAsync to reject for the extra parameter', |
| 402 | + ) |
| 403 | + } catch (error: unknown) { |
| 404 | + if (!isNitroSQLiteError(error)) { |
| 405 | + throw new Error('Should have thrown a valid NitroSQLiteError') |
| 406 | + } |
| 407 | + |
| 408 | + expect(error.message).toContain('parameter 2') |
| 409 | + expect(error.message).toContain('25') |
| 410 | + expect(error.message).toContain('column index out of range') |
| 411 | + expect(error.message.includes(extraParameter)).toBe(false) |
| 412 | + } |
| 413 | + }) |
| 414 | + it('rolls back grouped writes after a later parameter set fails to bind', async () => { |
| 415 | + testDb.execute('CREATE TABLE BindFailureBatch (value TEXT)') |
| 416 | + const secret = 'do-not-expose-grouped-parameter' |
| 417 | + for (const asynchronous of [false, true]) { |
| 418 | + const commands = [ |
| 419 | + { |
| 420 | + query: 'INSERT INTO BindFailureBatch VALUES (?)', |
| 421 | + params: [['rolled back'], ['invalid', secret]], |
| 422 | + }, |
| 423 | + ] |
| 424 | + try { |
| 425 | + if (asynchronous) await testDb.executeBatchAsync(commands) |
| 426 | + else testDb.executeBatch(commands) |
| 427 | + throw new Error('Expected grouped binding to fail') |
| 428 | + } catch (error) { |
| 429 | + if (!isNitroSQLiteError(error)) throw error |
| 430 | + expect(error.message).toContain('parameter 2') |
| 431 | + expect(error.message).toContain('25') |
| 432 | + expect(error.message.includes(secret)).toBe(false) |
| 433 | + } |
| 434 | + expect( |
| 435 | + testDb.execute('SELECT * FROM BindFailureBatch').rows._array, |
| 436 | + ).toEqual([]) |
| 437 | + } |
| 438 | + testDb.execute('DROP TABLE BindFailureBatch') |
| 439 | + }) |
365 | 440 | }) |
366 | 441 | } |
0 commit comments