diff --git a/test/test_database.rb b/test/test_database.rb index 2ac409ff..790710d3 100644 --- a/test/test_database.rb +++ b/test/test_database.rb @@ -11,6 +11,10 @@ def setup super end + def teardown + @db.close unless @db.closed? + end + def test_segv assert_raises { SQLite3::Database.new 1 } end @@ -54,6 +58,7 @@ def test_filename_to_path assert_equal pn.realdirpath.to_s, File.realdirpath(db.filename) ensure tf.close! if tf + db.close if db end @@ -189,6 +194,8 @@ def test_execute_batch2 def test_new db = SQLite3::Database.new(':memory:') assert db + ensure + db.close if db end def test_new_yields_self @@ -210,6 +217,8 @@ def test_new_with_options :utf16 => true) end assert db + ensure + db.close if db end def test_close @@ -243,6 +252,8 @@ def test_prepare db = SQLite3::Database.new(':memory:') stmt = db.prepare('select "hello world"') assert_instance_of(SQLite3::Statement, stmt) + ensure + stmt.close if stmt end def test_block_prepare_does_not_double_close @@ -459,15 +470,19 @@ def step a end def test_authorizer_ok + statements = [] + @db.authorizer = Class.new { def call action, a, b, c, d; true end }.new - @db.prepare("select 'fooooo'") + statements << @db.prepare("select 'fooooo'") @db.authorizer = Class.new { def call action, a, b, c, d; 0 end }.new - @db.prepare("select 'fooooo'") + statements << @db.prepare("select 'fooooo'") + ensure + statements.each(&:close) end def test_authorizer_ignore @@ -476,6 +491,8 @@ def call action, a, b, c, d; nil end }.new stmt = @db.prepare("select 'fooooo'") assert_nil stmt.step + ensure + stmt.close if stmt end def test_authorizer_fail @@ -496,14 +513,18 @@ def call action, a, b, c, d; false end end @db.authorizer = nil - @db.prepare("select 'fooooo'") + s = @db.prepare("select 'fooooo'") + ensure + s.close if s end def test_close_with_open_statements - @db.prepare("select 'foo'") + s = @db.prepare("select 'foo'") assert_raises(SQLite3::BusyException) do @db.close end + ensure + s.close if s end def test_execute_with_empty_bind_params @@ -511,7 +532,10 @@ def test_execute_with_empty_bind_params end def test_query_with_named_bind_params - assert_equal [['foo']], @db.query("select :n", {'n' => 'foo'}).to_a + resultset = @db.query("select :n", {'n' => 'foo'}) + assert_equal [['foo']], resultset.to_a + ensure + resultset.close if resultset end def test_execute_with_named_bind_params diff --git a/test/test_deprecated.rb b/test/test_deprecated.rb index 4fa1dc40..1248cb9a 100644 --- a/test/test_deprecated.rb +++ b/test/test_deprecated.rb @@ -2,8 +2,6 @@ module SQLite3 class TestDeprecated < SQLite3::TestCase - attr_reader :db - def setup super @warn_before = $-w @@ -15,10 +13,13 @@ def setup def teardown super $-w = @warn_before + @db.close end def test_query_with_many_bind_params_not_nil - assert_equal [[1, 2]], db.query('select ?, ?', 1, 2).to_a + rs = @db.query('select ?, ?', 1, 2) + assert_equal [[1, 2]], rs.to_a + rs.close end def test_execute_with_many_bind_params_not_nil @@ -26,11 +27,15 @@ def test_execute_with_many_bind_params_not_nil end def test_query_with_many_bind_params - assert_equal [[nil, 1]], @db.query("select ?, ?", nil, 1).to_a + rs = @db.query("select ?, ?", nil, 1) + assert_equal [[nil, 1]], rs.to_a + rs.close end def test_query_with_nil_bind_params - assert_equal [['foo']], @db.query("select 'foo'", nil).to_a + rs = @db.query("select 'foo'", nil) + assert_equal [['foo']], rs.to_a + rs.close end def test_execute_with_many_bind_params diff --git a/test/test_encoding.rb b/test/test_encoding.rb index 32bf6169..ea1bfb47 100644 --- a/test/test_encoding.rb +++ b/test/test_encoding.rb @@ -11,6 +11,10 @@ def setup @db.execute(@create); end + def teardown + @db.close + end + def test_select_encoding_on_utf_16 str = "foo" utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE" @@ -24,6 +28,7 @@ def test_select_encoding_on_utf_16 assert_equal 1, stmt.to_a.length stmt.reset! end + stmt.close end def test_insert_encoding @@ -39,6 +44,7 @@ def test_insert_encoding stmt.to_a stmt.reset! end + stmt.close db.execute('select data from ex').flatten.each do |s| assert_equal str, s @@ -55,6 +61,7 @@ def test_default_internal_is_honored stmt = @db.prepare('insert into ex(data) values (?)') stmt.bind_param 1, str stmt.step + stmt.close Encoding.default_internal = 'EUC-JP' string = @db.execute('select data from ex').first.first @@ -73,6 +80,7 @@ def test_blob_is_binary stmt = @db.prepare('insert into foo(data) values (?)') stmt.bind_param(1, SQLite3::Blob.new(str)) stmt.step + stmt.close string = @db.execute('select data from foo').first.first assert_equal Encoding.find('ASCII-8BIT'), string.encoding @@ -85,6 +93,7 @@ def test_blob_is_ascii8bit stmt = @db.prepare('insert into foo(data) values (?)') stmt.bind_param(1, str.dup.force_encoding("ASCII-8BIT")) stmt.step + stmt.close string = @db.execute('select data from foo').first.first assert_equal Encoding.find('ASCII-8BIT'), string.encoding @@ -97,6 +106,7 @@ def test_blob_with_eucjp stmt = @db.prepare('insert into foo(data) values (?)') stmt.bind_param(1, SQLite3::Blob.new(str)) stmt.step + stmt.close string = @db.execute('select data from foo').first.first assert_equal Encoding.find('ASCII-8BIT'), string.encoding diff --git a/test/test_result_set.rb b/test/test_result_set.rb index fa3df516..f16ffdf6 100644 --- a/test/test_result_set.rb +++ b/test/test_result_set.rb @@ -2,29 +2,38 @@ module SQLite3 class TestResultSet < SQLite3::TestCase + def setup + @db = SQLite3::Database.new ':memory:' + super + end + + def teardown + super + @db.close + end + def test_each_hash - db = SQLite3::Database.new ':memory:' - db.execute "create table foo ( a integer primary key, b text )" + @db.execute "create table foo ( a integer primary key, b text )" list = ('a'..'z').to_a list.each do |t| - db.execute "insert into foo (b) values (\"#{t}\")" + @db.execute "insert into foo (b) values (\"#{t}\")" end - rs = db.prepare('select * from foo').execute + rs = @db.prepare('select * from foo').execute rs.each_hash do |hash| assert_equal list[hash['a'] - 1], hash['b'] end + rs.close end def test_next_hash - db = SQLite3::Database.new ':memory:' - db.execute "create table foo ( a integer primary key, b text )" + @db.execute "create table foo ( a integer primary key, b text )" list = ('a'..'z').to_a list.each do |t| - db.execute "insert into foo (b) values (\"#{t}\")" + @db.execute "insert into foo (b) values (\"#{t}\")" end - rs = db.prepare('select * from foo').execute + rs = @db.prepare('select * from foo').execute rows = [] while row = rs.next_hash rows << row @@ -32,6 +41,7 @@ def test_next_hash rows.each do |hash| assert_equal list[hash['a'] - 1], hash['b'] end + rs.close end end end diff --git a/test/test_statement.rb b/test/test_statement.rb index 734d1bdd..4881f180 100644 --- a/test/test_statement.rb +++ b/test/test_statement.rb @@ -7,6 +7,11 @@ def setup @stmt = SQLite3::Statement.new(@db, "select 'foo'") end + def teardown + @stmt.close if !@stmt.closed? + @db.close + end + def test_double_close_does_not_segv @db.execute 'CREATE TABLE "things" ("number" float NOT NULL)' @@ -35,6 +40,8 @@ def test_insert_duplicate_records # Older versions of SQLite return: # column *column_name* is not unique assert_match(/(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)/, exception.message) + + stmt.close end ### @@ -46,6 +53,7 @@ def test_database_name if stmt.respond_to?(:database_name) assert_equal 'main', stmt.database_name(0) end + stmt.close end def test_prepare_blob @@ -77,6 +85,7 @@ def test_new_closed_handle def test_new_with_remainder stmt = SQLite3::Statement.new(@db, "select 'foo';bar") assert_equal 'bar', stmt.remainder + stmt.close end def test_empty_remainder @@ -101,6 +110,7 @@ def test_bind_param_string result = nil stmt.each { |x| result = x } assert_equal ['hello'], result + stmt.close end def test_bind_param_int @@ -109,6 +119,7 @@ def test_bind_param_int result = nil stmt.each { |x| result = x } assert_equal [10], result + stmt.close end def test_bind_nil @@ -117,6 +128,7 @@ def test_bind_nil result = nil stmt.each { |x| result = x } assert_equal [nil], result + stmt.close end def test_bind_blob @@ -125,6 +137,7 @@ def test_bind_blob stmt.bind_param(1, SQLite3::Blob.new('hello')) stmt.execute row = @db.execute('select * from foo') + stmt.close assert_equal ['hello'], row.first assert_equal ['blob'], row.first.types @@ -136,6 +149,7 @@ def test_bind_64 result = nil stmt.each { |x| result = x } assert_equal [2 ** 31], result + stmt.close end def test_bind_double @@ -144,6 +158,7 @@ def test_bind_double result = nil stmt.each { |x| result = x } assert_equal [2.2], result + stmt.close end def test_named_bind @@ -152,6 +167,7 @@ def test_named_bind result = nil stmt.each { |x| result = x } assert_equal ['hello'], result + stmt.close end def test_named_bind_no_colon @@ -160,6 +176,7 @@ def test_named_bind_no_colon result = nil stmt.each { |x| result = x } assert_equal ['hello'], result + stmt.close end def test_named_bind_symbol @@ -168,6 +185,7 @@ def test_named_bind_symbol result = nil stmt.each { |x| result = x } assert_equal ['hello'], result + stmt.close end def test_named_bind_not_found @@ -175,6 +193,7 @@ def test_named_bind_not_found assert_raises(SQLite3::Exception) do stmt.bind_param('bar', 'hello') end + stmt.close end def test_each @@ -225,16 +244,19 @@ def test_column_name def test_bind_parameter_count stmt = SQLite3::Statement.new(@db, "select ?, ?, ?") assert_equal 3, stmt.bind_parameter_count + stmt.close end def test_execute_with_varargs stmt = @db.prepare('select ?, ?') assert_equal [[nil, nil]], stmt.execute(nil, nil).to_a + stmt.close end def test_execute_with_hash stmt = @db.prepare('select :n, :h') assert_equal [[10, nil]], stmt.execute('n' => 10, 'h' => nil).to_a + stmt.close end def test_with_error @@ -244,6 +266,7 @@ def test_with_error stmt.execute('employee-1') rescue SQLite3::ConstraintException stmt.reset! assert stmt.execute('employee-2') + stmt.close end def test_clear_bindings! @@ -258,6 +281,8 @@ def test_clear_bindings! while x = stmt.step assert_equal [nil, nil], x end + + stmt.close end end end diff --git a/test/test_statement_execute.rb b/test/test_statement_execute.rb index 63e022b2..6ff0a527 100644 --- a/test/test_statement_execute.rb +++ b/test/test_statement_execute.rb @@ -8,6 +8,10 @@ def setup "CREATE TABLE items (id integer PRIMARY KEY, number integer)") end + def teardown + @db.close + end + def test_execute_insert ps = @db.prepare("INSERT INTO items (number) VALUES (:n)") ps.execute('n'=>10)