|
5 | 5 | import { SQLiteCloudRowset, SQLiteCloudRow, SQLiteCloudError } from '../src/index' |
6 | 6 | import { getTestingDatabase, getTestingDatabaseAsync, getChinookDatabase, removeDatabase, removeDatabaseAsync, LONG_TIMEOUT } from './shared' |
7 | 7 | import { RowCountCallback } from '../src/types' |
| 8 | +import { finished } from 'stream' |
8 | 9 |
|
9 | 10 | // |
10 | 11 | // utility methods to setup and destroy temporary test databases |
@@ -240,125 +241,155 @@ describe('Database.exec', () => { |
240 | 241 |
|
241 | 242 | describe('Database.sql (async)', () => { |
242 | 243 | 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 | + } |
263 | 267 | }) |
264 | 268 |
|
265 | 269 | 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 | + } |
270 | 278 | }) |
271 | 279 |
|
272 | 280 | 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() |
285 | 286 |
|
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 | + } |
287 | 298 | }) |
288 | 299 |
|
289 | 300 | 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 | + } |
302 | 317 | }) |
303 | 318 |
|
304 | 319 | 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 | + }) |
319 | 336 |
|
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 | + } |
323 | 342 | }) |
324 | 343 |
|
325 | 344 | 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 | + } |
337 | 360 | }) |
338 | 361 |
|
339 | 362 | 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 | + } |
350 | 377 | }) |
351 | 378 |
|
352 | 379 | 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 | + } |
363 | 394 | }) |
364 | 395 | }) |
0 commit comments