11var expect = require ( 'chai' ) . expect ;
22var fs = require ( 'fs' ) ;
33var Database = require ( '../.' ) ;
4- var db ;
5-
6- before ( function ( done ) {
7- db = new Database ( 'temp/' + require ( 'path' ) . basename ( __filename ) . split ( '.' ) [ 0 ] + '.db' ) ;
8- db . on ( 'open' , done ) ;
9- } ) ;
4+ var util = ( function ( ) {
5+ var path = require ( 'path' ) ;
6+ var dbId = 0 ;
7+ var obj ;
8+ return obj = {
9+ current : function ( ) {
10+ return 'temp/' + path . basename ( __filename ) . split ( '.' ) [ 0 ] + '.' + dbId + '.db' ;
11+ } ,
12+ next : function ( ) { ++ dbId ; return obj . current ( ) ; }
13+ } ;
14+ } ( ) ) ;
1015
1116describe ( 'Database#close()' , function ( ) {
1217 it ( 'should prevent statements and transactions from operating' , function ( done ) {
13- db . prepare ( 'CREATE TABLE people (name TEXT)' ) . run ( ) ;
14- var stmt1 = db . prepare ( 'SELECT * FROM people' ) ;
15- var stmt2 = db . prepare ( "INSERT INTO people VALUES ('foobar')" ) ;
16- var trans = db . transaction ( [ "INSERT INTO people VALUES ('foobar')" ] ) ;
17-
18- db . prepare ( 'SELECT * FROM people' ) . bind ( ) ;
19- db . prepare ( "INSERT INTO people VALUES ('foobar')" ) . bind ( ) ;
20- db . transaction ( [ "INSERT INTO people VALUES ('foobar')" ] ) . bind ( ) ;
21- db . prepare ( 'SELECT * FROM people' ) . get ( ) ;
22- db . prepare ( 'SELECT * FROM people' ) . all ( ) ;
23- db . prepare ( 'SELECT * FROM people' ) . each ( function ( ) { } ) ;
24- db . prepare ( "INSERT INTO people VALUES ('foobar')" ) . run ( ) ;
25- db . transaction ( [ "INSERT INTO people VALUES ('foobar')" ] ) . run ( ) ;
26-
27- db . close ( ) ;
28-
29- expect ( function ( ) { stmt1 . bind ( ) ; } ) . to . throw ( Error ) ;
30- expect ( function ( ) { stmt2 . bind ( ) ; } ) . to . throw ( Error ) ;
31- expect ( function ( ) { trans . bind ( ) ; } ) . to . throw ( Error ) ;
32- expect ( function ( ) { stmt1 . get ( ) ; } ) . to . throw ( Error ) ;
33- expect ( function ( ) { stmt1 . all ( ) ; } ) . to . throw ( Error ) ;
34- expect ( function ( ) { stmt1 . each ( function ( ) { } ) ; } ) . to . throw ( Error ) ;
35- expect ( function ( ) { stmt2 . run ( ) ; } ) . to . throw ( Error ) ;
36- expect ( function ( ) { trans . run ( ) ; } ) . to . throw ( Error ) ;
37-
38- db . on ( 'close' , function ( ) {
18+ var db = new Database ( util . next ( ) ) ;
19+ db . on ( 'open' , function ( ) {
20+ db . prepare ( 'CREATE TABLE people (name TEXT)' ) . run ( ) ;
21+ var stmt1 = db . prepare ( 'SELECT * FROM people' ) ;
22+ var stmt2 = db . prepare ( "INSERT INTO people VALUES ('foobar')" ) ;
23+ var trans = db . transaction ( [ "INSERT INTO people VALUES ('foobar')" ] ) ;
24+
25+ db . prepare ( 'SELECT * FROM people' ) . bind ( ) ;
26+ db . prepare ( "INSERT INTO people VALUES ('foobar')" ) . bind ( ) ;
27+ db . transaction ( [ "INSERT INTO people VALUES ('foobar')" ] ) . bind ( ) ;
28+ db . prepare ( 'SELECT * FROM people' ) . get ( ) ;
29+ db . prepare ( 'SELECT * FROM people' ) . all ( ) ;
30+ db . prepare ( 'SELECT * FROM people' ) . each ( function ( ) { } ) ;
31+ db . prepare ( "INSERT INTO people VALUES ('foobar')" ) . run ( ) ;
32+ db . transaction ( [ "INSERT INTO people VALUES ('foobar')" ] ) . run ( ) ;
33+
34+ db . close ( ) ;
35+
3936 expect ( function ( ) { stmt1 . bind ( ) ; } ) . to . throw ( Error ) ;
4037 expect ( function ( ) { stmt2 . bind ( ) ; } ) . to . throw ( Error ) ;
4138 expect ( function ( ) { trans . bind ( ) ; } ) . to . throw ( Error ) ;
@@ -44,7 +41,36 @@ describe('Database#close()', function () {
4441 expect ( function ( ) { stmt1 . each ( function ( ) { } ) ; } ) . to . throw ( Error ) ;
4542 expect ( function ( ) { stmt2 . run ( ) ; } ) . to . throw ( Error ) ;
4643 expect ( function ( ) { trans . run ( ) ; } ) . to . throw ( Error ) ;
47- done ( ) ;
44+
45+ db . on ( 'close' , function ( ) {
46+ expect ( function ( ) { stmt1 . bind ( ) ; } ) . to . throw ( Error ) ;
47+ expect ( function ( ) { stmt2 . bind ( ) ; } ) . to . throw ( Error ) ;
48+ expect ( function ( ) { trans . bind ( ) ; } ) . to . throw ( Error ) ;
49+ expect ( function ( ) { stmt1 . get ( ) ; } ) . to . throw ( Error ) ;
50+ expect ( function ( ) { stmt1 . all ( ) ; } ) . to . throw ( Error ) ;
51+ expect ( function ( ) { stmt1 . each ( function ( ) { } ) ; } ) . to . throw ( Error ) ;
52+ expect ( function ( ) { stmt2 . run ( ) ; } ) . to . throw ( Error ) ;
53+ expect ( function ( ) { trans . run ( ) ; } ) . to . throw ( Error ) ;
54+ done ( ) ;
55+ } ) ;
56+ } ) ;
57+ } ) ;
58+ it ( 'should delete the database\'s associated temporary files' , function ( done ) {
59+ var db = new Database ( util . next ( ) ) ;
60+ db . on ( 'open' , function ( ) {
61+ fs . accessSync ( util . current ( ) ) ;
62+ db . pragma ( 'journal_mode = WAL' ) ;
63+ db . prepare ( 'CREATE TABLE people (name TEXT)' ) . run ( ) ;
64+ db . prepare ( 'INSERT INTO people VALUES (?)' ) . run ( 'foobar' ) ;
65+ fs . accessSync ( util . current ( ) + '-wal' ) ;
66+ db . close ( ) ;
67+ db . on ( 'close' , function ( err ) {
68+ fs . accessSync ( util . current ( ) ) ;
69+ expect ( function ( ) {
70+ fs . accessSync ( util . current ( ) + '-wal' ) ;
71+ } ) . to . throw ( ) ;
72+ done ( ) ;
73+ } ) ;
4874 } ) ;
4975 } ) ;
5076} ) ;
0 commit comments