Update Vendored sqlite3-ruby

This commit is contained in:
Jacques Distler 2009-12-22 20:48:32 -06:00
parent 9874650e4b
commit a71e64a172
26 changed files with 6501 additions and 4335 deletions

View file

@ -1,6 +1,12 @@
require 'rubygems'
gem 'mocha'
# add lib folder to the path
$:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'sqlite3'
require 'rubygems'
require 'test/unit'
# define mocks to be used
require 'mocha'
class Driver < Mocha::Mock
@ -43,3 +49,19 @@ class Statement < Mocha::Mock
stubs( :execute ).returns(MockResultSet.new)
end
end
# UTF conversion extensions
class String
def to_utf16(terminate=false)
self.split(//).map { |c| c[0] }.pack("v*") +
(terminate ? "\0\0" : "")
end
def from_utf16
result = ""
length.times do |i|
result << self[i,1] if i % 2 == 0 && self[i] != 0
end
result
end
end

File diff suppressed because it is too large Load diff

View file

@ -1,9 +1,4 @@
$:.unshift "../lib"
require 'sqlite3/database'
require 'test/unit'
require 'mocks'
require File.join(File.dirname(__FILE__), 'helper')
class TC_Database_Init < Test::Unit::TestCase
def test_new
@ -16,6 +11,18 @@ class TC_Database_Init < Test::Unit::TestCase
assert !db.results_as_hash
assert !db.type_translation
end
def test_new_with_block
driver = Driver.new
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
Driver.stubs(:new).returns(driver)
returned_db = SQLite3::Database.new( "foo.db", :driver => Driver ) do |db|
assert !db.closed?
assert !db.results_as_hash
assert !db.type_translation
end
assert returned_db.closed?
end
def test_open
driver = Driver.new
@ -27,6 +34,18 @@ class TC_Database_Init < Test::Unit::TestCase
assert !db.type_translation
end
def test_open_with_block
driver = Driver.new
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
Driver.stubs(:new).returns(driver)
returned_db = SQLite3::Database.open( "foo.db", :driver => Driver ) do |db|
assert !db.closed?
assert !db.results_as_hash
assert !db.type_translation
end
assert returned_db.closed?
end
def test_with_type_translation
db = SQLite3::Database.open( "foo.db", :driver => Driver,
:type_translation => true )

View file

@ -1,8 +1,4 @@
$:.unshift "../lib"
require 'sqlite3/errors'
require 'test/unit'
require 'mocha'
require File.join(File.dirname(__FILE__), 'helper')
class TC_Errors < Test::Unit::TestCase
(1..26).each do |code|

View file

@ -0,0 +1,542 @@
require File.join(File.dirname(__FILE__), 'helper')
class TC_Database_Integration < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@db.transaction do
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo ( b ) values ( 'foo' )"
@db.execute "insert into foo ( b ) values ( 'bar' )"
@db.execute "insert into foo ( b ) values ( 'baz' )"
end
end
def teardown
@db.close
File.delete( "test.db" )
end
def test_table_info_with_type_translation_active
@db.type_translation = true
assert_nothing_raised { @db.table_info("foo") }
end
def test_table_info_with_defaults_for_version_3_3_8_and_higher
@db.transaction do
@db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
data = @db.table_info( "defaults_test" )
assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => "0", "cid" => "0", "pk" => "0"},
data[0])
assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => "0", "cid" => "1", "pk" => "0"},
data[1])
end
end
def test_table_info_without_defaults_for_version_3_3_8_and_higher
@db.transaction do
@db.execute "create table no_defaults_test ( a integer default 1, b integer )"
data = @db.table_info( "no_defaults_test" )
assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => "0", "cid" => "0", "pk" => "0"},
data[0])
assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => "0", "cid" => "1", "pk" => "0"},
data[1])
end
end
def test_complete_fail
assert !@db.complete?( "select * from foo" )
end
def test_complete_success
assert @db.complete?( "select * from foo;" )
end
def test_complete_fail_utf16
assert !@db.complete?( "select * from foo".to_utf16(false), true )
end
def test_complete_success_utf16
assert @db.complete?( "select * from foo;".to_utf16(true), true )
end
def test_errmsg
assert_equal "not an error", @db.errmsg
end
def test_errmsg_utf16
assert_equal "not an error".to_utf16, @db.errmsg(true)
end
def test_errcode
assert_equal 0, @db.errcode
end
def test_trace
result = nil
@db.trace( "data" ) { |data,sql| result = [ data, sql ]; 0 }
@db.execute "select * from foo"
assert_equal ["data","select * from foo"], result
end
def test_authorizer_okay
@db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
rows = @db.execute "select * from foo"
assert_equal 3, rows.length
end
def test_authorizer_error
@db.authorizer( "data" ) { |data,type,a,b,c,d| 1 }
assert_raise( SQLite3::AuthorizationException ) do
@db.execute "select * from foo"
end
end
def test_authorizer_silent
@db.authorizer( "data" ) { |data,type,a,b,c,d| 2 }
rows = @db.execute "select * from foo"
assert rows.empty?
end
def test_prepare_invalid_syntax
assert_raise( SQLite3::SQLException ) do
@db.prepare "select from foo"
end
end
def test_prepare_invalid_column
assert_raise( SQLite3::SQLException ) do
@db.prepare "select k from foo"
end
end
def test_prepare_invalid_table
assert_raise( SQLite3::SQLException ) do
@db.prepare "select * from barf"
end
end
def test_prepare_no_block
stmt = @db.prepare "select * from foo"
assert stmt.respond_to?(:execute)
stmt.close
end
def test_prepare_with_block
called = false
@db.prepare "select * from foo" do |stmt|
called = true
assert stmt.respond_to?(:execute)
end
assert called
end
def test_execute_no_block_no_bind_no_match
rows = @db.execute( "select * from foo where a > 100" )
assert rows.empty?
end
def test_execute_with_block_no_bind_no_match
called = false
@db.execute( "select * from foo where a > 100" ) do |row|
called = true
end
assert !called
end
def test_execute_no_block_with_bind_no_match
rows = @db.execute( "select * from foo where a > ?", 100 )
assert rows.empty?
end
def test_execute_with_block_with_bind_no_match
called = false
@db.execute( "select * from foo where a > ?", 100 ) do |row|
called = true
end
assert !called
end
def test_execute_no_block_no_bind_with_match
rows = @db.execute( "select * from foo where a = 1" )
assert_equal 1, rows.length
end
def test_execute_with_block_no_bind_with_match
called = 0
@db.execute( "select * from foo where a = 1" ) do |row|
called += 1
end
assert_equal 1, called
end
def test_execute_no_block_with_bind_with_match
rows = @db.execute( "select * from foo where a = ?", 1 )
assert_equal 1, rows.length
end
def test_execute_with_block_with_bind_with_match
called = 0
@db.execute( "select * from foo where a = ?", 1 ) do |row|
called += 1
end
assert_equal 1, called
end
def test_execute2_no_block_no_bind_no_match
columns, *rows = @db.execute2( "select * from foo where a > 100" )
assert rows.empty?
assert [ "a", "b" ], columns
end
def test_execute2_with_block_no_bind_no_match
called = 0
@db.execute2( "select * from foo where a > 100" ) do |row|
assert [ "a", "b" ], row unless called == 0
called += 1
end
assert_equal 1, called
end
def test_execute2_no_block_with_bind_no_match
columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
assert rows.empty?
assert [ "a", "b" ], columns
end
def test_execute2_with_block_with_bind_no_match
called = 0
@db.execute2( "select * from foo where a > ?", 100 ) do |row|
assert [ "a", "b" ], row unless called == 0
called += 1
end
assert_equal 1, called
end
def test_execute2_no_block_no_bind_with_match
columns, *rows = @db.execute2( "select * from foo where a = 1" )
assert_equal 1, rows.length
assert [ "a", "b" ], columns
end
def test_execute2_with_block_no_bind_with_match
called = 0
@db.execute2( "select * from foo where a = 1" ) do |row|
assert [ "a", "b" ], row unless called == 0
called += 1
end
assert_equal 2, called
end
def test_execute2_no_block_with_bind_with_match
columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
assert_equal 1, rows.length
assert [ "a", "b" ], columns
end
def test_execute2_with_block_with_bind_with_match
called = 0
@db.execute2( "select * from foo where a = ?", 1 ) do |row|
called += 1
end
assert_equal 2, called
end
def test_execute_batch_empty
assert_nothing_raised { @db.execute_batch "" }
end
def test_execute_batch_no_bind
@db.transaction do
@db.execute_batch <<-SQL
create table bar ( a, b, c );
insert into bar values ( 'one', 2, 'three' );
insert into bar values ( 'four', 5, 'six' );
insert into bar values ( 'seven', 8, 'nine' );
SQL
end
rows = @db.execute( "select * from bar" )
assert_equal 3, rows.length
end
def test_execute_batch_with_bind
@db.execute_batch( <<-SQL, 1 )
create table bar ( a, b, c );
insert into bar values ( 'one', 2, ? );
insert into bar values ( 'four', 5, ? );
insert into bar values ( 'seven', 8, ? );
SQL
rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
assert_equal %w{1 1 1}, rows
end
def test_query_no_block_no_bind_no_match
result = @db.query( "select * from foo where a > 100" )
assert_nil result.next
result.close
end
def test_query_with_block_no_bind_no_match
r = nil
@db.query( "select * from foo where a > 100" ) do |result|
assert_nil result.next
r = result
end
assert r.closed?
end
def test_query_no_block_with_bind_no_match
result = @db.query( "select * from foo where a > ?", 100 )
assert_nil result.next
result.close
end
def test_query_with_block_with_bind_no_match
r = nil
@db.query( "select * from foo where a > ?", 100 ) do |result|
assert_nil result.next
r = result
end
assert r.closed?
end
def test_query_no_block_no_bind_with_match
result = @db.query( "select * from foo where a = 1" )
assert_not_nil result.next
assert_nil result.next
result.close
end
def test_query_with_block_no_bind_with_match
r = nil
@db.query( "select * from foo where a = 1" ) do |result|
assert_not_nil result.next
assert_nil result.next
r = result
end
assert r.closed?
end
def test_query_no_block_with_bind_with_match
result = @db.query( "select * from foo where a = ?", 1 )
assert_not_nil result.next
assert_nil result.next
result.close
end
def test_query_with_block_with_bind_with_match
r = nil
@db.query( "select * from foo where a = ?", 1 ) do |result|
assert_not_nil result.next
assert_nil result.next
r = result
end
assert r.closed?
end
def test_get_first_row_no_bind_no_match
result = @db.get_first_row( "select * from foo where a=100" )
assert_nil result
end
def test_get_first_row_no_bind_with_match
result = @db.get_first_row( "select * from foo where a=1" )
assert_equal [ "1", "foo" ], result
end
def test_get_first_row_with_bind_no_match
result = @db.get_first_row( "select * from foo where a=?", 100 )
assert_nil result
end
def test_get_first_row_with_bind_with_match
result = @db.get_first_row( "select * from foo where a=?", 1 )
assert_equal [ "1", "foo" ], result
end
def test_get_first_value_no_bind_no_match
result = @db.get_first_value( "select b, a from foo where a=100" )
assert_nil result
end
def test_get_first_value_no_bind_with_match
result = @db.get_first_value( "select b, a from foo where a=1" )
assert_equal "foo", result
end
def test_get_first_value_with_bind_no_match
result = @db.get_first_value( "select b, a from foo where a=?", 100 )
assert_nil result
end
def test_get_first_value_with_bind_with_match
result = @db.get_first_value( "select b, a from foo where a=?", 1 )
assert_equal "foo", result
end
def test_last_insert_row_id
@db.execute "insert into foo ( b ) values ( 'test' )"
assert_equal 4, @db.last_insert_row_id
@db.execute "insert into foo ( b ) values ( 'again' )"
assert_equal 5, @db.last_insert_row_id
end
def test_changes
@db.execute "insert into foo ( b ) values ( 'test' )"
assert_equal 1, @db.changes
@db.execute "delete from foo where 1=1"
assert_equal 4, @db.changes
end
def test_total_changes
assert_equal 3, @db.total_changes
@db.execute "insert into foo ( b ) values ( 'test' )"
@db.execute "delete from foo where 1=1"
assert_equal 8, @db.total_changes
end
def test_transaction_nest
assert_raise( SQLite3::SQLException ) do
@db.transaction do
@db.transaction do
end
end
end
end
def test_transaction_rollback
@db.transaction
@db.execute_batch <<-SQL
insert into foo (b) values ( 'test1' );
insert into foo (b) values ( 'test2' );
insert into foo (b) values ( 'test3' );
insert into foo (b) values ( 'test4' );
SQL
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
@db.rollback
assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
end
def test_transaction_commit
@db.transaction
@db.execute_batch <<-SQL
insert into foo (b) values ( 'test1' );
insert into foo (b) values ( 'test2' );
insert into foo (b) values ( 'test3' );
insert into foo (b) values ( 'test4' );
SQL
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
@db.commit
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
end
def test_transaction_rollback_in_block
assert_raise( SQLite3::SQLException ) do
@db.transaction do
@db.rollback
end
end
end
def test_transaction_commit_in_block
assert_raise( SQLite3::SQLException ) do
@db.transaction do
@db.commit
end
end
end
def test_transaction_active
assert !@db.transaction_active?
@db.transaction
assert @db.transaction_active?
@db.commit
assert !@db.transaction_active?
end
def test_interrupt
@db.create_function( "abort", 1 ) do |func,x|
@db.interrupt
func.result = x
end
assert_raise( SQLite3::SQLException ) do
@db.execute "select abort(a) from foo"
end
end
def test_create_function
@db.create_function( "munge", 1 ) do |func,x|
func.result = ">>>#{x}<<<"
end
value = @db.get_first_value( "select munge(b) from foo where a=1" )
assert_match( />>>.*<<</, value )
end
def test_create_aggregate_without_block
step = proc do |ctx,a|
ctx[:sum] ||= 0
ctx[:sum] += a.to_i
end
final = proc { |ctx| ctx.result = ctx[:sum] }
@db.create_aggregate( "accumulate", 1, step, final )
value = @db.get_first_value( "select accumulate(a) from foo" )
assert_equal "6", value
end
def test_create_aggregate_with_block
@db.create_aggregate( "accumulate", 1 ) do
step do |ctx,a|
ctx[:sum] ||= 0
ctx[:sum] += a.to_i
end
finalize { |ctx| ctx.result = ctx[:sum] }
end
value = @db.get_first_value( "select accumulate(a) from foo" )
assert_equal "6", value
end
def test_create_aggregate_with_no_data
@db.create_aggregate( "accumulate", 1 ) do
step do |ctx,a|
ctx[:sum] ||= 0
ctx[:sum] += a.to_i
end
finalize { |ctx| ctx.result = ctx[:sum] || 0 }
end
value = @db.get_first_value(
"select accumulate(a) from foo where a = 100" )
assert_equal "0", value
end
def test_create_aggregate_handler
handler = Class.new do
class << self
def arity; 1; end
def text_rep; SQLite3::Constants::TextRep::ANY; end
def name; "multiply"; end
end
def step(ctx, a)
ctx[:buffer] ||= 1
ctx[:buffer] *= a.to_i
end
def finalize(ctx); ctx.result = ctx[:buffer]; end
end
@db.create_aggregate_handler( handler )
value = @db.get_first_value( "select multiply(a) from foo" )
assert_equal "6", value
end
def test_bind_array_parameter
result = @db.get_first_value( "select b from foo where a=? and b=?",
[ 1, "foo" ] )
assert_equal "foo", result
end
end

View file

@ -0,0 +1,30 @@
require File.join(File.dirname(__FILE__), 'helper')
class TC_OpenClose < Test::Unit::TestCase
def test_create_close
begin
db = SQLite3::Database.new( "test-create.db" )
assert File.exist?( "test-create.db" )
assert_nothing_raised { db.close }
ensure
File.delete( "test-create.db" ) rescue nil
end
end
def test_open_close
begin
File.open( "test-open.db", "w" ) { |f| }
assert File.exist?( "test-open.db" )
db = SQLite3::Database.new( "test-open.db" )
assert_nothing_raised { db.close }
ensure
File.delete( "test-open.db" ) rescue nil
end
end
def test_bad_open
assert_raise( SQLite3::CantOpenException ) do
SQLite3::Database.new( "." )
end
end
end

View file

@ -0,0 +1,111 @@
require File.join(File.dirname(__FILE__), 'helper')
require 'thread'
require 'benchmark'
class TC_Integration_Pending < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@db.transaction do
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo ( b ) values ( 'foo' )"
@db.execute "insert into foo ( b ) values ( 'bar' )"
@db.execute "insert into foo ( b ) values ( 'baz' )"
end
end
def teardown
@db.close
File.delete( "test.db" )
end
def test_busy_handler_outwait
busy = Mutex.new
busy.lock
handler_call_count = 0
t = Thread.new(busy) do |locker|
begin
db2 = SQLite3::Database.open( "test.db" )
db2.transaction( :exclusive ) do
locker.lock
end
ensure
db2.close if db2
end
end
@db.busy_handler do |data,count|
handler_call_count += 1
busy.unlock
true
end
assert_nothing_raised do
@db.execute "insert into foo (b) values ( 'from 2' )"
end
t.join
assert_equal 1, handler_call_count
end
def test_busy_handler_impatient
busy = Mutex.new
busy.lock
handler_call_count = 0
t = Thread.new do
begin
db2 = SQLite3::Database.open( "test.db" )
db2.transaction( :exclusive ) do
busy.lock
end
ensure
db2.close if db2
end
end
@db.busy_handler do |data, count|
handler_call_count += 1
false
end
assert_raise( SQLite3::BusyException ) do
@db.execute "insert into foo (b) values ( 'from 2' )"
end
busy.unlock
t.join
assert_equal 1, handler_call_count
end
def test_busy_timeout
@db.busy_timeout 1000
busy = Mutex.new
busy.lock
t = Thread.new do
begin
db2 = SQLite3::Database.open( "test.db" )
db2.transaction( :exclusive ) do
busy.lock
end
ensure
db2.close if db2
end
end
time = Benchmark.measure do
assert_raise( SQLite3::BusyException ) do
@db.execute "insert into foo (b) values ( 'from 2' )"
end
end
busy.unlock
t.join
assert time.real*1000 >= 1000
end
end

View file

@ -0,0 +1,159 @@
require File.join(File.dirname(__FILE__), 'helper')
class TC_ResultSet < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@db.transaction do
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo ( b ) values ( 'foo' )"
@db.execute "insert into foo ( b ) values ( 'bar' )"
@db.execute "insert into foo ( b ) values ( 'baz' )"
end
@stmt = @db.prepare( "select * from foo where a in ( ?, ? )" )
@result = @stmt.execute
end
def teardown
@stmt.close
@db.close
File.delete( "test.db" )
end
def test_reset_unused
assert_nothing_raised { @result.reset }
assert @result.to_a.empty?
end
def test_reset_used
@result.to_a
assert_nothing_raised { @result.reset }
assert @result.to_a.empty?
end
def test_reset_with_bind
@result.to_a
assert_nothing_raised { @result.reset( 1, 2 ) }
assert_equal 2, @result.to_a.length
end
def test_eof_inner
@result.reset( 1 )
assert !@result.eof?
end
def test_eof_edge
@result.reset( 1 )
@result.next # to first row
@result.next # to end of result set
assert @result.eof?
end
def test_next_eof
@result.reset( 1 )
assert_not_nil @result.next
assert_nil @result.next
end
def test_next_no_type_translation_no_hash
@result.reset( 1 )
assert_equal [ "1", "foo" ], @result.next
end
def test_next_type_translation
@db.type_translation = true
@result.reset( 1 )
assert_equal [ 1, "foo" ], @result.next
end
def test_next_type_translation_with_untyped_column
@db.type_translation = true
@db.query( "select count(*) from foo" ) do |result|
assert_equal ["3"], result.next
end
end
def test_type_translation_with_null_column
@db.type_translation = true
@db.execute "create table bar ( a integer, b time, c string )"
@db.execute "insert into bar (a, b, c) values (NULL, '1974-07-25 14:39:00', 'hello')"
@db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')"
@db.execute "insert into bar (a, b, c) values (2, '1974-07-25 14:39:00', NULL)"
@db.query( "select * from bar" ) do |result|
assert_equal [nil, Time.local(1974, 7, 25, 14, 39, 0), 'hello'], result.next
assert_equal [1, nil, 'hello'], result.next
assert_equal [2, Time.local(1974, 7, 25, 14, 39, 0), nil], result.next
end
end
def test_date_and_time_translation
@db.type_translation = true
@db.execute "create table bar ( a date, b datetime, c time, d timestamp )"
@db.execute "insert into bar (a, b, c, d) values ('1999-01-08', '1997-12-17 07:37:16', '07:37:16', '2004-10-19 10:23:54')"
@db.query( "select * from bar" ) do |result|
result = result.next
assert result[0].is_a?(Date)
assert result[1].is_a?(DateTime)
assert result[2].is_a?(Time)
assert result[3].is_a?(Time)
end
end
def test_next_results_as_hash
@db.results_as_hash = true
@result.reset( 1 )
assert_equal( { "a" => "1", "b" => "foo", 0 => "1", 1 => "foo" },
@result.next )
end
def test_tainted_results_as_hash
@db.results_as_hash = true
@result.reset( 1 )
row = @result.next
row.each do |_, v|
assert_equal true, v.tainted?
end
end
def test_tainted_row_values
@result.reset( 1 )
row = @result.next
row.each do |v|
assert_equal true, v.tainted?
end
end
def test_each
called = 0
@result.reset( 1, 2 )
@result.each { |row| called += 1 }
assert_equal 2, called
end
def test_enumerable
@result.reset( 1, 2 )
assert_equal 2, @result.to_a.length
end
def test_types
assert_equal [ "integer", "text" ], @result.types
end
def test_columns
assert_equal [ "a", "b" ], @result.columns
end
def test_close
stmt = @db.prepare( "select * from foo" )
result = stmt.execute
assert !result.closed?
result.close
assert result.closed?
assert stmt.closed?
assert_raise( SQLite3::Exception ) { result.reset }
assert_raise( SQLite3::Exception ) { result.next }
assert_raise( SQLite3::Exception ) { result.each }
assert_raise( SQLite3::Exception ) { result.close }
assert_raise( SQLite3::Exception ) { result.types }
assert_raise( SQLite3::Exception ) { result.columns }
end
end

View file

@ -0,0 +1,195 @@
require File.join(File.dirname(__FILE__), 'helper')
class TC_Statement < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@db.transaction do
@db.execute "create table foo ( a integer primary key, b text )"
@db.execute "insert into foo ( b ) values ( 'foo' )"
@db.execute "insert into foo ( b ) values ( 'bar' )"
@db.execute "insert into foo ( b ) values ( 'baz' )"
end
@stmt = @db.prepare( "select * from foo where a in ( ?, :named )" )
end
def teardown
@stmt.close
@db.close
File.delete( "test.db" )
end
def test_remainder_empty
assert_equal "", @stmt.remainder
end
def test_remainder_nonempty
called = false
@db.prepare( "select * from foo;\n blah" ) do |stmt|
called = true
assert_equal "\n blah", stmt.remainder
end
assert called
end
def test_bind_params_empty
assert_nothing_raised { @stmt.bind_params }
assert @stmt.execute!.empty?
end
def test_bind_params_array
@stmt.bind_params 1, 2
assert_equal 2, @stmt.execute!.length
end
def test_bind_params_hash
@stmt.bind_params ":named" => 2
assert_equal 1, @stmt.execute!.length
end
def test_bind_params_hash_without_colon
@stmt.bind_params "named" => 2
assert_equal 1, @stmt.execute!.length
end
def test_bind_params_hash_as_symbol
@stmt.bind_params :named => 2
assert_equal 1, @stmt.execute!.length
end
def test_bind_params_mixed
@stmt.bind_params( 1, ":named" => 2 )
assert_equal 2, @stmt.execute!.length
end
def test_bind_param_by_index
@stmt.bind_params( 1, 2 )
assert_equal 2, @stmt.execute!.length
end
def test_bind_param_by_name_bad
assert_raise( SQLite3::Exception ) { @stmt.bind_param( "@named", 2 ) }
end
def test_bind_param_by_name_good
@stmt.bind_param( ":named", 2 )
assert_equal 1, @stmt.execute!.length
end
def test_bind_param_with_various_types
@db.transaction do
@db.execute "create table all_types ( a integer primary key, b float, c string, d integer )"
@db.execute "insert into all_types ( b, c, d ) values ( 1.4, 'hello', 68719476735 )"
end
assert_equal 1, @db.execute( "select * from all_types where b = ?", 1.4 ).length
assert_equal 1, @db.execute( "select * from all_types where c = ?", 'hello').length
assert_equal 1, @db.execute( "select * from all_types where d = ?", 68719476735).length
end
def test_execute_no_bind_no_block
assert_instance_of SQLite3::ResultSet, @stmt.execute
end
def test_execute_with_bind_no_block
assert_instance_of SQLite3::ResultSet, @stmt.execute( 1, 2 )
end
def test_execute_no_bind_with_block
called = false
@stmt.execute { |row| called = true }
assert called
end
def test_execute_with_bind_with_block
called = 0
@stmt.execute( 1, 2 ) { |row| called += 1 }
assert_equal 1, called
end
def test_reexecute
r = @stmt.execute( 1, 2 )
assert_equal 2, r.to_a.length
assert_nothing_raised { r = @stmt.execute( 1, 2 ) }
assert_equal 2, r.to_a.length
end
def test_execute_bang_no_bind_no_block
assert @stmt.execute!.empty?
end
def test_execute_bang_with_bind_no_block
assert_equal 2, @stmt.execute!( 1, 2 ).length
end
def test_execute_bang_no_bind_with_block
called = 0
@stmt.execute! { |row| called += 1 }
assert_equal 0, called
end
def test_execute_bang_with_bind_with_block
called = 0
@stmt.execute!( 1, 2 ) { |row| called += 1 }
assert_equal 2, called
end
def test_columns
c1 = @stmt.columns
c2 = @stmt.columns
assert_same c1, c2
assert_equal 2, c1.length
end
def test_columns_computed
called = false
@db.prepare( "select count(*) from foo" ) do |stmt|
called = true
assert_equal [ "count(*)" ], stmt.columns
end
assert called
end
def test_types
t1 = @stmt.types
t2 = @stmt.types
assert_same t1, t2
assert_equal 2, t1.length
end
def test_types_computed
called = false
@db.prepare( "select count(*) from foo" ) do |stmt|
called = true
assert_equal [ nil ], stmt.types
end
assert called
end
def test_close
stmt = @db.prepare( "select * from foo" )
assert !stmt.closed?
stmt.close
assert stmt.closed?
assert_raise( SQLite3::Exception ) { stmt.execute }
assert_raise( SQLite3::Exception ) { stmt.execute! }
assert_raise( SQLite3::Exception ) { stmt.close }
assert_raise( SQLite3::Exception ) { stmt.bind_params 5 }
assert_raise( SQLite3::Exception ) { stmt.bind_param 1, 5 }
assert_raise( SQLite3::Exception ) { stmt.columns }
assert_raise( SQLite3::Exception ) { stmt.types }
end
def test_committing_tx_with_statement_active
called = false
@db.prepare( "select count(*) from foo" ) do |stmt|
called = true
count = stmt.execute!.first.first.to_i
@db.transaction do
@db.execute "insert into foo ( b ) values ( 'hello' )"
end
new_count = stmt.execute!.first.first.to_i
assert_equal new_count, count+1
end
assert called
end
end

View file

@ -1,6 +0,0 @@
Dir.chdir File.dirname( __FILE__ )
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../ext/sqlite3_api"
p $LOAD_PATH
Dir["**/tc_*.rb"].each { |file| load file }