22 * database.test.ts - test driver api
33 */
44
5- import { SQLiteCloudRowset , SQLiteCloudRow , SQLiteCloudError , sanitizeSQLiteIdentifier } from '../src/index'
5+ import { describe , expect , it } from '@jest/globals'
6+ import { RowCountCallback } from '../src/drivers/types'
7+ import { SQLiteCloudError , SQLiteCloudRow , SQLiteCloudRowset , sanitizeSQLiteIdentifier } from '../src/index'
68import {
9+ LONG_TIMEOUT ,
10+ getChinookDatabase ,
711 getTestingDatabase ,
812 getTestingDatabaseAsync ,
9- getChinookDatabase ,
1013 removeDatabase ,
11- removeDatabaseAsync ,
12- LONG_TIMEOUT ,
13- getChinookWebsocketConnection
14+ removeDatabaseAsync
1415} from './shared'
15- import { RowCountCallback } from '../src/drivers/types'
16- import { expect , describe , it } from '@jest/globals'
17- import { Database } from 'sqlite3'
1816
1917//
2018// utility methods to setup and destroy temporary test databases
@@ -55,6 +53,7 @@ describe('Database.run', () => {
5553 expect ( error ) . toBeNull ( )
5654 database . run ( updateSql , plainCallbackNotALambda )
5755 } )
56+ removeDatabase ( database )
5857 } ,
5958 LONG_TIMEOUT
6059 )
@@ -114,6 +113,7 @@ describe('Database.run', () => {
114113 expect ( error ) . toBeNull ( )
115114 database . run ( insertSql , plainCallbackNotALambdaOne )
116115 } )
116+ removeDatabase ( database )
117117 } ,
118118 LONG_TIMEOUT
119119 )
@@ -317,7 +317,7 @@ describe('Database.sql (async)', () => {
317317 const results = await database . sql ( 'SELECT * FROM people WHERE name = ?' , 'Emma Johnson' )
318318 expect ( results ) . toHaveLength ( 1 )
319319 } finally {
320- database ?. close ( )
320+ await removeDatabaseAsync ( database )
321321 }
322322 } )
323323
@@ -337,7 +337,7 @@ describe('Database.sql (async)', () => {
337337 hobby : 'Collecting clouds'
338338 } )
339339 } finally {
340- database ?. close ( )
340+ await removeDatabaseAsync ( database )
341341 }
342342 } )
343343
@@ -487,30 +487,45 @@ describe('Database.sql (async)', () => {
487487
488488 describe ( 'should sanitize identifiers' , ( ) => {
489489 it ( 'should sanitize database name and run the query' , async ( ) => {
490- const database = await getTestingDatabaseAsync ( )
490+ let database
491+ try {
492+ database = await getTestingDatabaseAsync ( )
491493
492- const databaseName = sanitizeSQLiteIdentifier ( database . getConfiguration ( ) . database || '' )
493- await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . resolves . toBe ( 'OK' )
494+ const databaseName = sanitizeSQLiteIdentifier ( database . getConfiguration ( ) . database || '' )
495+ await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . resolves . toBe ( 'OK' )
496+ } finally {
497+ await removeDatabaseAsync ( database )
498+ }
494499 } )
495500
496501 it ( 'should sanitize table name and run the query' , async ( ) => {
497- const database = await getTestingDatabaseAsync ( )
502+ let database
503+ try {
504+ database = await getTestingDatabaseAsync ( )
498505
499- const table = sanitizeSQLiteIdentifier ( 'people' )
500- await expect ( database . sql ( `SELECT id FROM ${ table } LIMIT 1` ) ) . resolves . toMatchObject ( [ { id : 1 } ] )
506+ const table = sanitizeSQLiteIdentifier ( 'people' )
507+ await expect ( database . sql ( `SELECT id FROM ${ table } LIMIT 1` ) ) . resolves . toMatchObject ( [ { id : 1 } ] )
508+ } finally {
509+ await removeDatabaseAsync ( database )
510+ }
501511 } )
502512
503513 it ( 'should sanitize SQL Injection as table name' , async ( ) => {
504- const database = await getTestingDatabaseAsync ( )
505- const databaseName = database . getConfiguration ( ) . database
514+ let database
515+ try {
516+ database = await getTestingDatabaseAsync ( )
517+ const databaseName = database . getConfiguration ( ) . database
506518
507- const sanitizedDBName = sanitizeSQLiteIdentifier ( `${ databaseName } ; SELECT * FROM people; -- ` )
508- await expect ( database . sql ( `USE DATABASE ${ sanitizedDBName } ` ) ) . rejects . toThrow (
509- `Database name contains invalid characters (${ databaseName } ; SELECT * FROM people; --).`
510- )
519+ const sanitizedDBName = sanitizeSQLiteIdentifier ( `${ databaseName } ; SELECT * FROM people; -- ` )
520+ await expect ( database . sql ( `USE DATABASE ${ sanitizedDBName } ` ) ) . rejects . toThrow (
521+ `Database name contains invalid characters (${ databaseName } ; SELECT * FROM people; --).`
522+ )
511523
512- const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
513- await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
524+ const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
525+ await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
526+ } finally {
527+ await removeDatabaseAsync ( database )
528+ }
514529 } )
515530 } )
516531
0 commit comments