Skip to content

Commit

Permalink
test: update tests to clean up resources
Browse files Browse the repository at this point in the history
lots of Database, Statement, and ResultSet objects needed a `close`
  • Loading branch information
flavorjones committed Apr 27, 2023
1 parent b6bc75c commit a9bddc7
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 18 deletions.
34 changes: 29 additions & 5 deletions test/test_database.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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
Expand All @@ -210,6 +217,8 @@ def test_new_with_options
:utf16 => true)
end
assert db
ensure
db.close if db
end

def test_close
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -496,22 +513,29 @@ 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
assert_equal [['foo']], @db.execute("select 'foo'", [])
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
Expand Down
15 changes: 10 additions & 5 deletions test/test_deprecated.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

module SQLite3
class TestDeprecated < SQLite3::TestCase
attr_reader :db

def setup
super
@warn_before = $-w
Expand All @@ -15,22 +13,29 @@ 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
assert_equal [[1, 2]], @db.execute("select ?, ?", 1, 2).to_a
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
Expand Down
10 changes: 10 additions & 0 deletions test/test_encoding.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
26 changes: 18 additions & 8 deletions test/test_result_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,46 @@

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
end
rows.each do |hash|
assert_equal list[hash['a'] - 1], hash['b']
end
rs.close
end
end
end
Loading

0 comments on commit a9bddc7

Please sign in to comment.