22 * database.test.ts - test driver api
33 */
44
5- import { SQLiteCloudRowset , SQLiteCloudRow , SQLiteCloudError , sanitizeSQLiteIdentifier } from '../src/index'
6- import {
7- getTestingDatabase ,
8- getTestingDatabaseAsync ,
9- getChinookDatabase ,
10- removeDatabase ,
11- removeDatabaseAsync ,
12- LONG_TIMEOUT ,
13- getChinookWebsocketConnection
14- } from './shared'
5+ import { describe , expect , it } from '@jest/globals'
156import { RowCountCallback } from '../src/drivers/types'
16- import { expect , describe , it } from '@jest/globals '
17- import { Database } from 'sqlite3 '
7+ import { SQLiteCloudError , SQLiteCloudRow , SQLiteCloudRowset , sanitizeSQLiteIdentifier } from '../src/index '
8+ import { LONG_TIMEOUT , getChinookDatabase , getTestingDatabase , getTestingDatabaseAsync , removeDatabase , removeDatabaseAsync } from './shared '
189
1910//
2011// utility methods to setup and destroy temporary test databases
@@ -44,7 +35,6 @@ describe('Database.run', () => {
4435 expect ( context . totalChanges ) . toBe ( 22 )
4536 expect ( context . finalized ) . toBe ( 1 )
4637
47- done ( )
4838 removeDatabase ( database , error => {
4939 expect ( error ) . toBeNull ( )
5040 done ( )
@@ -103,7 +93,6 @@ describe('Database.run', () => {
10393 expect ( context . totalChanges ) . toBe ( 22 )
10494 expect ( context . finalized ) . toBe ( 1 )
10595
106- done ( )
10796 removeDatabase ( database , error => {
10897 expect ( error ) . toBeNull ( )
10998 done ( )
@@ -317,7 +306,7 @@ describe('Database.sql (async)', () => {
317306 const results = await database . sql ( 'SELECT * FROM people WHERE name = ?' , 'Emma Johnson' )
318307 expect ( results ) . toHaveLength ( 1 )
319308 } finally {
320- database ?. close ( )
309+ await removeDatabaseAsync ( database )
321310 }
322311 } )
323312
@@ -337,7 +326,7 @@ describe('Database.sql (async)', () => {
337326 hobby : 'Collecting clouds'
338327 } )
339328 } finally {
340- database ?. close ( )
329+ await removeDatabaseAsync ( database )
341330 }
342331 } )
343332
@@ -487,56 +476,98 @@ describe('Database.sql (async)', () => {
487476
488477 describe ( 'should sanitize identifiers' , ( ) => {
489478 it ( 'should sanitize database name and run the query' , async ( ) => {
490- const database = await getTestingDatabaseAsync ( )
479+ let database
480+ try {
481+ database = await getTestingDatabaseAsync ( )
491482
492- const databaseName = sanitizeSQLiteIdentifier ( database . getConfiguration ( ) . database || '' )
493- await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . resolves . toBe ( 'OK' )
483+ const databaseName = sanitizeSQLiteIdentifier ( database . getConfiguration ( ) . database || '' )
484+ await expect ( database . sql ( `USE DATABASE ${ databaseName } ` ) ) . resolves . toBe ( 'OK' )
485+ } finally {
486+ await removeDatabaseAsync ( database )
487+ }
494488 } )
495489
496490 it ( 'should sanitize table name and run the query' , async ( ) => {
497- const database = await getTestingDatabaseAsync ( )
491+ let database
492+ try {
493+ database = await getTestingDatabaseAsync ( )
498494
499- const table = sanitizeSQLiteIdentifier ( 'people' )
500- await expect ( database . sql ( `SELECT id FROM ${ table } LIMIT 1` ) ) . resolves . toMatchObject ( [ { id : 1 } ] )
495+ const table = sanitizeSQLiteIdentifier ( 'people' )
496+ await expect ( database . sql ( `SELECT id FROM ${ table } LIMIT 1` ) ) . resolves . toMatchObject ( [ { id : 1 } ] )
497+ } finally {
498+ await removeDatabaseAsync ( database )
499+ }
501500 } )
502501
503502 it ( 'should sanitize SQL Injection as table name' , async ( ) => {
504- const database = await getTestingDatabaseAsync ( )
505- const databaseName = database . getConfiguration ( ) . database
503+ let database
504+ try {
505+ database = await getTestingDatabaseAsync ( )
506+ const databaseName = database . getConfiguration ( ) . database
506507
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- )
508+ const sanitizedDBName = sanitizeSQLiteIdentifier ( `${ databaseName } ; SELECT * FROM people; -- ` )
509+ await expect ( database . sql ( `USE DATABASE ${ sanitizedDBName } ` ) ) . rejects . toThrow (
510+ `Database name contains invalid characters (${ databaseName } ; SELECT * FROM people; --).`
511+ )
511512
512- const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
513- await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
513+ const table = sanitizeSQLiteIdentifier ( 'people; -- ' )
514+ await expect ( database . sql ( `SELECT * FROM ${ table } WHERE people = 1` ) ) . rejects . toThrow ( 'no such table: people; --' )
515+ } finally {
516+ await removeDatabaseAsync ( database )
517+ }
514518 } )
515519 } )
516520
517521 it ( 'should throw exception when using table name as binding' , async ( ) => {
518- const database = await getTestingDatabaseAsync ( )
519- const table = 'people'
520- await expect ( database . sql `SELECT * FROM ${ table } ` ) . rejects . toThrow ( 'near "?": syntax error' )
522+ let database
523+ try {
524+ database = await getTestingDatabaseAsync ( )
525+ const table = 'people'
526+ await expect ( database . sql `SELECT * FROM ${ table } ` ) . rejects . toThrow ( 'near "?": syntax error' )
527+ } finally {
528+ await removeDatabaseAsync ( database )
529+ }
521530 } )
522531
523- it ( 'should built in commands accept bindings' , async ( ) => {
524- const database = await getTestingDatabaseAsync ( )
532+ it ( 'should commands accept bindings' , async ( ) => {
533+ let database
534+ try {
535+ database = await getTestingDatabaseAsync ( )
536+
537+ const databaseName = database . getConfiguration ( ) . database || ''
538+ await expect ( database . sql `USE DATABASE ${ databaseName } ` ) . resolves . toBe ( 'OK' )
539+
540+ const databaseNameInjectSQL = `${ databaseName } ; SELECT * FROM people`
541+ await expect ( database . sql `USE DATABASE ${ databaseNameInjectSQL } ` ) . rejects . toThrow ( `Database name contains invalid characters (${ databaseNameInjectSQL } ).` )
542+
543+ let key = 'logo_level'
544+ let value = 'debug'
545+ await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
525546
526- const databaseName = database . getConfiguration ( ) . database || ''
527- await expect ( database . sql `USE DATABASE ${ databaseName } ` ) . resolves . toBe ( 'OK' )
547+ key = 'logo_level'
548+ value = 'debug; DROP TABLE people'
549+ await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
550+ const result = await database . sql `SELECT * FROM people`
551+ expect ( result . length ) . toBeGreaterThan ( 0 )
552+ } finally {
553+ await removeDatabaseAsync ( database )
554+ }
555+ } )
528556
529- const databaseNameInjectSQL = `${ databaseName } ; SELECT * FROM people`
530- await expect ( database . sql `USE DATABASE ${ databaseNameInjectSQL } ` ) . rejects . toThrow ( `Database name contains invalid characters (${ databaseNameInjectSQL } ).` )
557+ it ( 'binding should work with unicode character' , async ( ) => {
558+ let database
559+ try {
560+ database = await getTestingDatabaseAsync ( )
561+ const name = 'unicorn-🦄'
531562
532- let key = 'logo_level'
533- let value = 'debug'
534- await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
563+ let results = await database . sql ( 'INSERT INTO people (name, age, hobby) VALUES (?, 11, "");' , name )
564+ expect ( results . changes ) . toEqual ( 1 )
535565
536- key = 'logo_level'
537- value = 'debug; DROP TABLE people'
538- await expect ( database . sql `SET KEY ${ key } TO ${ value } ` ) . resolves . toBe ( 'OK' )
539- const result = await database . sql `SELECT * FROM people`
540- expect ( result . length ) . toBeGreaterThan ( 0 )
566+ results = await database . sql ( 'SELECT * FROM people WHERE name = ?;' , name )
567+ expect ( results ) . toHaveLength ( 1 )
568+ expect ( results [ 0 ] . name ) . toEqual ( name )
569+ } finally {
570+ await removeDatabaseAsync ( database )
571+ }
541572 } )
542573} )
0 commit comments