Skip to content

Commit d327257

Browse files
Add finally in test code to dispose connections in case of errors
1 parent 21b42f1 commit d327257

File tree

3 files changed

+231
-176
lines changed

3 files changed

+231
-176
lines changed

test/database.test.ts

Lines changed: 128 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import { SQLiteCloudRowset, SQLiteCloudRow, SQLiteCloudError } from '../src/index'
66
import { getTestingDatabase, getTestingDatabaseAsync, getChinookDatabase, removeDatabase, removeDatabaseAsync, LONG_TIMEOUT } from './shared'
77
import { RowCountCallback } from '../src/types'
8+
import { finished } from 'stream'
89

910
//
1011
// utility methods to setup and destroy temporary test databases
@@ -240,125 +241,155 @@ describe('Database.exec', () => {
240241

241242
describe('Database.sql (async)', () => {
242243
it('should select from chinook', async () => {
243-
const chinook = getChinookDatabase()
244-
let name = 'Breaking The Rules'
245-
const results = await chinook.sql`SELECT * FROM tracks WHERE name = ${name}`
246-
expect(results).toBeDefined()
247-
248-
const row = results[0]
249-
expect(row).toBeDefined()
250-
expect(row).toMatchObject({
251-
AlbumId: 1,
252-
Bytes: 8596840,
253-
Composer: 'Angus Young, Malcolm Young, Brian Johnson',
254-
GenreId: 1,
255-
MediaTypeId: 1,
256-
Milliseconds: 263288,
257-
Name: 'Breaking The Rules',
258-
TrackId: 12,
259-
UnitPrice: 0.99
260-
})
261-
262-
chinook.close()
244+
let chinook
245+
try {
246+
chinook = getChinookDatabase()
247+
let name = 'Breaking The Rules'
248+
const results = await chinook.sql`SELECT * FROM tracks WHERE name = ${name}`
249+
expect(results).toBeDefined()
250+
251+
const row = results[0]
252+
expect(row).toBeDefined()
253+
expect(row).toMatchObject({
254+
AlbumId: 1,
255+
Bytes: 8596840,
256+
Composer: 'Angus Young, Malcolm Young, Brian Johnson',
257+
GenreId: 1,
258+
MediaTypeId: 1,
259+
Milliseconds: 263288,
260+
Name: 'Breaking The Rules',
261+
TrackId: 12,
262+
UnitPrice: 0.99
263+
})
264+
} finally {
265+
chinook?.close()
266+
}
263267
})
264268

265269
it('should work with regular function parameters', async () => {
266-
const database = await getTestingDatabase()
267-
const results = await database.sql('SELECT * FROM people WHERE name = ?', 'Emma Johnson')
268-
expect(results).toHaveLength(1)
269-
database.close()
270+
let database
271+
try {
272+
database = await getTestingDatabase()
273+
const results = await database.sql('SELECT * FROM people WHERE name = ?', 'Emma Johnson')
274+
expect(results).toHaveLength(1)
275+
} finally {
276+
database?.close()
277+
}
270278
})
271279

272280
it('should select and return multiple rows', async () => {
273-
const database = await getTestingDatabase()
274-
const results = await database.sql('SELECT * FROM people ORDER BY id')
275-
expect(results).toBeDefined()
276-
277-
const row = results[0]
278-
expect(row).toBeDefined()
279-
expect(row).toMatchObject({
280-
id: 1,
281-
name: 'Emma Johnson',
282-
age: 28,
283-
hobby: 'Collecting clouds'
284-
})
281+
let database
282+
try {
283+
database = await getTestingDatabase()
284+
const results = await database.sql('SELECT * FROM people ORDER BY id')
285+
expect(results).toBeDefined()
285286

286-
database.close()
287+
const row = results[0]
288+
expect(row).toBeDefined()
289+
expect(row).toMatchObject({
290+
id: 1,
291+
name: 'Emma Johnson',
292+
age: 28,
293+
hobby: 'Collecting clouds'
294+
})
295+
} finally {
296+
database?.close()
297+
}
287298
})
288299

289300
it('should select and return a single row', async () => {
290-
const database = await getTestingDatabaseAsync()
291-
const results = await database.sql('SELECT * FROM people ORDER BY id LIMIT 1')
292-
expect(results).toBeDefined()
293-
const row = results[0]
294-
expect(row).toBeDefined()
295-
expect(row).toMatchObject({
296-
id: 1,
297-
name: 'Emma Johnson',
298-
age: 28,
299-
hobby: 'Collecting clouds'
300-
})
301-
await removeDatabaseAsync(database)
301+
let database
302+
try {
303+
database = await getTestingDatabaseAsync()
304+
const results = await database.sql('SELECT * FROM people ORDER BY id LIMIT 1')
305+
expect(results).toBeDefined()
306+
const row = results[0]
307+
expect(row).toBeDefined()
308+
expect(row).toMatchObject({
309+
id: 1,
310+
name: 'Emma Johnson',
311+
age: 28,
312+
hobby: 'Collecting clouds'
313+
})
314+
} finally {
315+
await removeDatabaseAsync(database)
316+
}
302317
})
303318

304319
it('should select with template string parameters', async () => {
305-
// trivial example here but let's suppose we have this in a variable...
306-
let name = 'Ava Jones'
307-
308-
// prepared statement using familiar print syntax
309-
const database = await getTestingDatabaseAsync()
310-
let results = await database.sql`SELECT * FROM people WHERE name = ${name}`
311-
// => returns { id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' }
312-
313-
expect(results[0]).toMatchObject({
314-
id: 5,
315-
name: 'Ava Jones',
316-
age: 22,
317-
hobby: 'Time traveling'
318-
})
320+
let database
321+
try {
322+
// trivial example here but let's suppose we have this in a variable...
323+
let name = 'Ava Jones'
324+
325+
// prepared statement using familiar print syntax
326+
database = await getTestingDatabaseAsync()
327+
let results = await database.sql`SELECT * FROM people WHERE name = ${name}`
328+
// => returns { id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' }
329+
330+
expect(results[0]).toMatchObject({
331+
id: 5,
332+
name: 'Ava Jones',
333+
age: 22,
334+
hobby: 'Time traveling'
335+
})
319336

320-
results = await database.sql`SELECT * FROM people WHERE age < 30`
321-
expect(results).toHaveLength(11)
322-
await removeDatabaseAsync(database)
337+
results = await database.sql`SELECT * FROM people WHERE age < 30`
338+
expect(results).toHaveLength(11)
339+
} finally {
340+
await removeDatabaseAsync(database)
341+
}
323342
})
324343

325344
it('should take regular concatenated string as parameters', async () => {
326-
// trivial example here but let's suppose we have this in a variable...
327-
let name = 'Ava Jones'
328-
329-
// prepared statement with contacatenated string (shouldn't do this, weak against sql injection)
330-
const database = await getTestingDatabaseAsync()
331-
let results = await database.sql("SELECT * FROM people WHERE name = '" + name + "'")
332-
expect(results[0]).toMatchObject({ id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' })
333-
334-
results = await database.sql('SELECT * FROM people WHERE age < 30')
335-
expect(results).toHaveLength(11)
336-
await removeDatabaseAsync(database)
345+
let database
346+
try {
347+
// trivial example here but let's suppose we have this in a variable...
348+
let name = 'Ava Jones'
349+
350+
// prepared statement with contacatenated string (shouldn't do this, weak against sql injection)
351+
database = await getTestingDatabaseAsync()
352+
let results = await database.sql("SELECT * FROM people WHERE name = '" + name + "'")
353+
expect(results[0]).toMatchObject({ id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' })
354+
355+
results = await database.sql('SELECT * FROM people WHERE age < 30')
356+
expect(results).toHaveLength(11)
357+
} finally {
358+
await removeDatabaseAsync(database)
359+
}
337360
})
338361

339362
it('should update and respond with metadata', async () => {
340-
const database = await getTestingDatabaseAsync()
341-
const updateSql = "UPDATE people SET name = 'Charlie Brown' WHERE id = 3; UPDATE people SET name = 'David Bowie' WHERE id = 4; "
342-
let results = await database.sql(updateSql)
343-
expect(results).toMatchObject({
344-
lastID: 20,
345-
changes: 1,
346-
totalChanges: 22,
347-
finalized: 1
348-
})
349-
await removeDatabaseAsync(database)
363+
let database
364+
try {
365+
database = await getTestingDatabaseAsync()
366+
const updateSql = "UPDATE people SET name = 'Charlie Brown' WHERE id = 3; UPDATE people SET name = 'David Bowie' WHERE id = 4; "
367+
let results = await database.sql(updateSql)
368+
expect(results).toMatchObject({
369+
lastID: 20,
370+
changes: 1,
371+
totalChanges: 22,
372+
finalized: 1
373+
})
374+
} finally {
375+
await removeDatabaseAsync(database)
376+
}
350377
})
351378

352379
it('should insert and respond with metadata', async () => {
353-
const database = await getTestingDatabaseAsync()
354-
const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); "
355-
let results = await database.sql(insertSql)
356-
expect(results).toMatchObject({
357-
lastID: 21,
358-
changes: 1,
359-
totalChanges: 21,
360-
finalized: 1
361-
})
362-
await removeDatabaseAsync(database)
380+
let database
381+
try {
382+
database = await getTestingDatabaseAsync()
383+
const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); "
384+
let results = await database.sql(insertSql)
385+
expect(results).toMatchObject({
386+
lastID: 21,
387+
changes: 1,
388+
totalChanges: 21,
389+
finalized: 1
390+
})
391+
} finally {
392+
await removeDatabaseAsync(database)
393+
}
363394
})
364395
})

test/shared.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,15 @@ export function removeDatabase(database: Database, callback?: ResultsCallback) {
195195
})
196196
}
197197

198-
export async function removeDatabaseAsync(database: Database) {
199-
const databaseName = database.getConfiguration().database
200-
if (databaseName) {
201-
const result1 = await database.sql`UNUSE DATABASE;`
202-
console.assert(result1)
203-
const result2 = await database.sql`REMOVE DATABASE ${databaseName};`
204-
console.assert(result2)
198+
export async function removeDatabaseAsync(database?: Database) {
199+
if (database) {
200+
const databaseName = database.getConfiguration().database
201+
if (databaseName) {
202+
const result1 = await database.sql`UNUSE DATABASE;`
203+
console.assert(result1)
204+
const result2 = await database.sql`REMOVE DATABASE ${databaseName};`
205+
console.assert(result2)
206+
}
207+
database.close()
205208
}
206-
database.close()
207209
}

0 commit comments

Comments
 (0)