Skip to content

Commit

Permalink
Merge pull request #387 from sparklemotion/flavorjones-test-valgrind-…
Browse files Browse the repository at this point in the history
…rake-task

Add a valgrind test task, and clean up resource management in the tests
  • Loading branch information
flavorjones committed Apr 28, 2023
2 parents 69c9fa7 + a9bddc7 commit 78151d4
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 26 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/sqlite3-ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,16 @@ jobs:
vcpkg: sqlcipher
- run: bundle exec rake compile -- --with-sqlcipher
- run: bundle exec rake test

valgrind:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ruby/setup-ruby-pkgs@v1
with:
ruby-version: "3.2"
bundler-cache: true
apt-get: libsqlite3-dev valgrind
- run: bundle install
- run: bundle exec rake compile
- run: bundle exec rake test:valgrind
8 changes: 8 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
source "https://rubygems.org"

gemspec

gem("minitest", "~> 5.15")
gem("rake-compiler", "~> 1.2.0")
gem("rake-compiler-dock", "1.3.0")
gem("rdoc", ">= 4.0", "< 7")
gem("psych", "~> 4.0") # psych 5 doesn't build on some CI platforms yet

gem("ruby_memcheck") if Gem::Platform.local.os == "linux"
17 changes: 15 additions & 2 deletions rakelib/test.rake
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
require "rake/testtask"

Rake::TestTask.new(:test) do |t|
test_config = lambda do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/test_*.rb"]
end

Rake::TestTask.new(:test, &test_config)

begin
require "ruby_memcheck"

RubyMemcheck.config(binary_name: "sqlite3_native")

namespace :test do
RubyMemcheck::TestTask.new(:valgrind, &test_config)
end
rescue LoadError => e
warn("NOTE: ruby_memcheck is not available in this environment: #{e}")
end
6 changes: 0 additions & 6 deletions sqlite3.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,5 @@ Gem::Specification.new do |s|

s.add_dependency("mini_portile2", "~> 2.8.0")

s.add_development_dependency("minitest", "~> 5.15")
s.add_development_dependency("rake-compiler", "~> 1.2.0")
s.add_development_dependency("rake-compiler-dock", "1.3.0")
s.add_development_dependency("rdoc", ">= 4.0", "< 7")
s.add_development_dependency("psych", "~> 4.0") # psych 5 doesn't build on some CI platforms yet

s.extensions << "ext/sqlite3/extconf.rb"
end
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 78151d4

Please sign in to comment.