@@ -11,6 +11,10 @@ def setup
1111 super
1212 end
1313
14+ def teardown
15+ @db . close unless @db . closed?
16+ end
17+
1418 def test_segv
1519 assert_raises { SQLite3 ::Database . new 1 }
1620 end
@@ -54,6 +58,7 @@ def test_filename_to_path
5458 assert_equal pn . realdirpath . to_s , File . realdirpath ( db . filename )
5559 ensure
5660 tf . close! if tf
61+ db . close if db
5762 end
5863
5964
@@ -189,6 +194,8 @@ def test_execute_batch2
189194 def test_new
190195 db = SQLite3 ::Database . new ( ':memory:' )
191196 assert db
197+ ensure
198+ db . close if db
192199 end
193200
194201 def test_new_yields_self
@@ -210,6 +217,8 @@ def test_new_with_options
210217 :utf16 => true )
211218 end
212219 assert db
220+ ensure
221+ db . close if db
213222 end
214223
215224 def test_close
@@ -243,6 +252,8 @@ def test_prepare
243252 db = SQLite3 ::Database . new ( ':memory:' )
244253 stmt = db . prepare ( 'select "hello world"' )
245254 assert_instance_of ( SQLite3 ::Statement , stmt )
255+ ensure
256+ stmt . close if stmt
246257 end
247258
248259 def test_block_prepare_does_not_double_close
@@ -459,15 +470,19 @@ def step a
459470 end
460471
461472 def test_authorizer_ok
473+ statements = [ ]
474+
462475 @db . authorizer = Class . new {
463476 def call action , a , b , c , d ; true end
464477 } . new
465- @db . prepare ( "select 'fooooo'" )
478+ statements << @db . prepare ( "select 'fooooo'" )
466479
467480 @db . authorizer = Class . new {
468481 def call action , a , b , c , d ; 0 end
469482 } . new
470- @db . prepare ( "select 'fooooo'" )
483+ statements << @db . prepare ( "select 'fooooo'" )
484+ ensure
485+ statements . each ( &:close )
471486 end
472487
473488 def test_authorizer_ignore
@@ -476,6 +491,8 @@ def call action, a, b, c, d; nil end
476491 } . new
477492 stmt = @db . prepare ( "select 'fooooo'" )
478493 assert_nil stmt . step
494+ ensure
495+ stmt . close if stmt
479496 end
480497
481498 def test_authorizer_fail
@@ -496,22 +513,29 @@ def call action, a, b, c, d; false end
496513 end
497514
498515 @db . authorizer = nil
499- @db . prepare ( "select 'fooooo'" )
516+ s = @db . prepare ( "select 'fooooo'" )
517+ ensure
518+ s . close if s
500519 end
501520
502521 def test_close_with_open_statements
503- @db . prepare ( "select 'foo'" )
522+ s = @db . prepare ( "select 'foo'" )
504523 assert_raises ( SQLite3 ::BusyException ) do
505524 @db . close
506525 end
526+ ensure
527+ s . close if s
507528 end
508529
509530 def test_execute_with_empty_bind_params
510531 assert_equal [ [ 'foo' ] ] , @db . execute ( "select 'foo'" , [ ] )
511532 end
512533
513534 def test_query_with_named_bind_params
514- assert_equal [ [ 'foo' ] ] , @db . query ( "select :n" , { 'n' => 'foo' } ) . to_a
535+ resultset = @db . query ( "select :n" , { 'n' => 'foo' } )
536+ assert_equal [ [ 'foo' ] ] , resultset . to_a
537+ ensure
538+ resultset . close if resultset
515539 end
516540
517541 def test_execute_with_named_bind_params
0 commit comments