123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555 |
- require 'helper'
- class TC_Database_Integration < SQLite3::TestCase
- def setup
- @db = SQLite3::Database.new(":memory:")
- @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
- end
- def test_table_info_with_type_translation_active
- 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
- # FIXME: do people really need UTF16 sql statements?
- #def test_complete_fail_utf16
- # assert !@db.complete?( "select * from foo".to_utf16(false), true )
- #end
- # FIXME: do people really need UTF16 sql statements?
- #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
- # FIXME: do people really need UTF16 error messages?
- #def test_errmsg_utf16
- # msg = Iconv.conv('UTF-16', 'UTF-8', 'not an error')
- # assert_equal msg, @db.errmsg(true)
- #end
- def test_errcode
- assert_equal 0, @db.errcode
- end
- def test_trace
- result = nil
- @db.trace { |sql| result = sql }
- @db.execute "select * from foo"
- assert_equal "select * from foo", result
- end
- def test_authorizer_okay
- @db.authorizer { |type,a,b,c,d| 0 }
- rows = @db.execute "select * from foo"
- assert_equal 3, rows.length
- end
- def test_authorizer_error
- @db.authorizer { |type,a,b,c,d| 1 }
- assert_raise( SQLite3::AuthorizationException ) do
- @db.execute "select * from foo"
- end
- end
- def test_authorizer_silent
- @db.authorizer { |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_equal [ "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_equal [ "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_equal [ "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_equal [ "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_equal [ 1, "foo" ], 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_equal [ "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 [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_transaction_implicit_rollback
- assert !@db.transaction_active?
- @db.transaction
- @db.execute('create table bar (x CHECK(1 = 0))')
- assert @db.transaction_active?
- assert_raises( SQLite3::ConstraintException ) do
- @db.execute("insert or rollback into bar (x) VALUES ('x')")
- end
- 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::InterruptException ) 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
|