Update vendored Sqlite3-ruby to 1.3.0

Also, some tweaks to Maruku.
This commit is contained in:
Jacques Distler 2010-06-10 22:42:33 -05:00
parent 9a80cacc34
commit 4f8759cdf3
55 changed files with 3071 additions and 8028 deletions

View file

@ -1,140 +0,0 @@
require 'benchmark'
N = 1000
$VERBOSE=nil
puts "file require"
Benchmark.bm( 7 ) do |x|
x.report('sqlite') do
N.times do
$".delete_if { |i| i =~ /sqlite/ }
require 'sqlite'
end
end
x.report('sqlite3') do
N.times do
$".delete_if { |i| i =~ /sqlite3/ }
require 'sqlite3'
end
end
end
puts
puts "database creation..."
Benchmark.bm( 7 ) do |x|
x.report('sqlite') do
N.times do
File.delete "test.db" rescue nil
SQLite::Database.open( "test.db" ).close
end
end
x.report('sqlite3') do
N.times do
File.delete "test.db" rescue nil
SQLite3::Database.open( "test.db" ).close
end
end
end
File.delete "test.db" rescue nil
SQLite::Database.open( "test.db" ).close
SQLite3::Database.open( "test3.db" ).close
puts
puts "database open..."
Benchmark.bm( 7 ) do |x|
x.report('sqlite') do
N.times do
SQLite::Database.open( "test.db" ).close
end
end
x.report('sqlite3') do
N.times do
SQLite3::Database.open( "test3.db" ).close
end
end
end
File.delete "test.db" rescue nil
File.delete "test3.db" rescue nil
db = SQLite::Database.open( "test.db" )
db3 = SQLite3::Database.open( "test3.db" )
db.execute "create table foo (a,b)"
db3.execute "create table foo (a,b)"
puts
puts "insertions"
Benchmark.bm( 7 ) do |x|
x.report('sqlite') do
db.transaction do
N.times do |i|
db.execute "insert into foo values (#{i}, #{i+1})"
end
end
end
x.report('sqlite3') do
db3.transaction do
N.times do |i|
db3.execute "insert into foo values (#{i}, #{i+1})"
end
end
end
end
puts
puts "insertions using prepared statement"
Benchmark.bm( 7 ) do |x|
x.report('sqlite') do
db.transaction do
stmt = db.prepare "insert into foo values (?,?)"
N.times { |i| stmt.execute i, i+1 }
end
end
x.report('sqlite3') do
db3.transaction do
db3.prepare( "insert into foo values (?,?)" ) do |stmt|
N.times { |i| stmt.execute i, i+1 }
end
end
end
end
db.close
db3.close
File.delete "test.db" rescue nil
File.delete "test3.db" rescue nil
db = SQLite::Database.open( "test.db" )
db3 = SQLite3::Database.open( "test3.db" )
db.execute "create table foo (a,b)"
db.execute "insert into foo values (1,2)"
db.execute "insert into foo values (3,4)"
db.execute "insert into foo values (5,6)"
db3.execute "create table foo (a,b)"
db3.execute "insert into foo values (1,2)"
db3.execute "insert into foo values (3,4)"
db3.execute "insert into foo values (5,6)"
puts
puts "queries"
Benchmark.bm( 7 ) do |x|
x.report('sqlite') do
N.times do
db.execute "select * from foo"
end
end
x.report('sqlite3') do
N.times do
db3.execute "select * from foo"
end
end
end
db.close
db3.close
File.delete "test.db" rescue nil
File.delete "test3.db" rescue nil

View file

@ -1,292 +0,0 @@
if (ENV["SQLITE3_DRIVERS"] || "Native").split(/,/).include?("DL")
$:.unshift "../../../lib"
require 'sqlite3/constants'
require 'sqlite3/driver/dl/driver'
require 'test/unit'
class TC_DL_Driver < Test::Unit::TestCase
def utf16ify( str )
chars = str.split(//)
chars.zip(["\0"] * chars.length).flatten.join
end
def setup
@driver = SQLite3::Driver::DL::Driver.new
@dbname = "test.db"
@db = nil
end
def teardown
@driver.close( @db ) rescue nil
File.delete @dbname rescue nil
end
def test_open
result, @db = @driver.open( @dbname )
assert_equal SQLite3::Constants::ErrorCode::OK, result
assert File.exist?( @dbname )
end
def test_open_utf16
name = utf16ify( @dbname )
result, @db = @driver.open( name, true )
assert_equal SQLite3::Constants::ErrorCode::OK, result
assert File.exist?( @dbname )
end
def test_errmsg
result, @db = @driver.open( @dbname )
msg = @driver.errmsg( @db )
assert_equal msg, "not an error"
end
def test_errmsg16
result, @db = @driver.open( @dbname )
msg = @driver.errmsg( @db, true )
assert_equal msg, utf16ify( "not an error" )
end
def test_prepare
result, @db = @driver.open( @dbname )
sql = "create table foo ( a, b )"
result, handle, remainder = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
assert_equal "", remainder
@driver.finalize( handle )
end
def test_prepare_error
result, @db = @driver.open( @dbname )
sql = "create tble foo ( a, b )"
result, handle, remainder = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::ERROR, result
end
def test_prepare_remainder
result, @db = @driver.open( @dbname )
sql = "create table foo ( a, b ); select * from foo"
result, handle, remainder = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
assert_equal " select * from foo", remainder
@driver.finalize( handle )
end
def test_prepare16
result, @db = @driver.open( @dbname )
sql = utf16ify( "create table foo ( a, b )" )
result, handle, remainder = @driver.prepare( @db, sql, true )
assert_equal SQLite3::Constants::ErrorCode::OK, result
assert_equal "", remainder
@driver.finalize( handle )
end
def test_prepare16_remainder
result, @db = @driver.open( @dbname )
sql = utf16ify( "create table foo ( a, b ); select * from foo" )
result, handle, remainder = @driver.prepare( @db, sql, true )
assert_equal SQLite3::Constants::ErrorCode::OK, result
assert_equal utf16ify( " select * from foo" ), remainder
@driver.finalize( handle )
end
def test_complete
assert @driver.complete?( "select * from foo;" )
end
def test_complete_fail
assert !@driver.complete?( "select * from foo" )
end
def test_complete16
assert @driver.complete?( utf16ify("select * from foo;"), true )
end
def create_foo
result, @db = @driver.open( @dbname )
sql = "create table foo ( a, b )"
result, handle, = @driver.prepare( @db, sql )
@driver.step( handle )
@driver.finalize( handle )
end
def populate_foo
create_foo
sql = "insert into foo values ( 100, 200 )"
result, handle, = @driver.prepare( @db, sql )
@driver.step( handle )
@driver.finalize( handle )
end
def test_step
populate_foo
sql = "select * from foo"
result, handle, = @driver.prepare( @db, sql )
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::ROW, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::DONE, result
@driver.finalize( handle )
end
def test_step_fail
populate_foo
sql = "select * from"
result, handle, = @driver.prepare( @db, sql )
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::MISUSE, result
@driver.finalize( handle )
end
def test_bind_blob
create_foo
sql = "insert into foo (b) values (?)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.bind_blob( handle, 1, "a\0b\1c\2d\0e" )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::DONE, result
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
sql = "select b from foo"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::ROW, result
assert_equal "a\0b\1c\2d\0e", @driver.column_blob( handle, 0 )
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
end
def test_bind_double
create_foo
sql = "insert into foo (b) values (?)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.bind_double( handle, 1, 3.14 )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::DONE, result
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
sql = "select b from foo"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::ROW, result
assert_equal 3.14, @driver.column_double( handle, 0 )
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
end
def test_bind_int
create_foo
sql = "insert into foo (b) values (?)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.bind_int( handle, 1, 14 )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::DONE, result
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
sql = "select b from foo"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::ROW, result
assert_equal 14, @driver.column_int( handle, 0 )
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
end
def test_bind_null
create_foo
sql = "insert into foo (b) values (?)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.bind_null( handle, 1 )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::DONE, result
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
sql = "select b from foo"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::ROW, result
assert_equal SQLite3::Constants::ColumnType::NULL,
@driver.column_type( handle, 0 )
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
end
def test_bind_text
create_foo
sql = "insert into foo (b) values (?)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.bind_text( handle, 1, "hello, world" )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::DONE, result
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
sql = "select b from foo"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::ROW, result
assert_equal "hello, world", @driver.column_text( handle, 0 )
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
end
def test_bind_text16
create_foo
sql = "insert into foo (b) values (?)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.bind_text( handle, 1, utf16ify("hello, world"), true )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::DONE, result
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
sql = "select b from foo"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.step( handle )
assert_equal SQLite3::Constants::ErrorCode::ROW, result
assert_equal "hello, world", @driver.column_text( handle, 0 )
result = @driver.finalize( handle )
assert_equal SQLite3::Constants::ErrorCode::OK, result
end
def test_bind_parameter_index
create_foo
sql = "insert into foo (b) values (:hello)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
result = @driver.bind_parameter_index( handle, ":hello" )
assert_equal 1, result
result = @driver.bind_parameter_index( handle, ":foo" )
assert_equal 0, result
@driver.finalize( handle )
end
def test_bind_parameter_name
create_foo
sql = "insert into foo (a,b) values (?,:foo)"
result, handle, = @driver.prepare( @db, sql )
assert_equal SQLite3::Constants::ErrorCode::OK, result
assert_nil nil, @driver.bind_parameter_name(handle,1)
assert_equal ":foo", @driver.bind_parameter_name(handle,2)
@driver.finalize( handle )
end
end
end

View file

@ -1,67 +1,3 @@
# 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
def initialize
super
stubs( :open ).returns([0, 'cookie'])
stubs( :close ).returns(0)
stubs( :complete? ).returns(0)
stubs( :errmsg ).returns('')
stubs( :errcode ).returns(0)
stubs( :trace ).returns(nil)
stubs( :set_authorizer ).returns(0)
stubs( :prepare ).returns([0, 'stmt', 'remainder'])
stubs( :finalize ).returns(0)
stubs( :changes ).returns(14)
stubs( :total_changes ).returns(28)
stubs( :interrupt ).returns(0)
end
end
class MockResultSet < Mocha::Mock
def initialize
super
stubs( :each ).yields(['foo'])
stubs( :columns ).returns(['name'])
end
end
class Statement < Mocha::Mock
attr_reader :handle
attr_reader :sql
attr_reader :last_result
def initialize( handle, sql )
super()
@handle = handle
@sql = sql
stubs( :close ).returns(0)
stubs( :remainder ).returns('')
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
require 'iconv'

View file

@ -1,126 +0,0 @@
$:.unshift "../lib", "../ext/sqlite3_api"
require 'sqlite3'
require 'benchmark'
N = 1000
$VERBOSE=nil
puts "database creation..."
Benchmark.bm( 7 ) do |x|
x.report('dl') do
N.times do
File.delete "test.db" rescue nil
SQLite3::Database.open( "test.db", :driver => "DL" ).close
end
end
x.report('native') do
N.times do
File.delete "test.db" rescue nil
SQLite3::Database.open( "test.db", :driver => "Native" ).close
end
end
end
File.delete "test.db" rescue nil
SQLite3::Database.open( "test.db" ).close
puts
puts "database open..."
Benchmark.bm( 7 ) do |x|
x.report('dl') do
N.times do
SQLite3::Database.open( "test.db", :driver => "DL" ).close
end
end
x.report('native') do
N.times do
SQLite3::Database.open( "test.db", :driver => "Native" ).close
end
end
end
File.delete "test.db" rescue nil
dl = SQLite3::Database.open( "test-dl.db", :driver => "DL" )
native = SQLite3::Database.open( "test-native.db", :driver => "Native" )
dl.execute "create table foo (a,b)"
native.execute "create table foo (a,b)"
puts
puts "insertions"
Benchmark.bm( 7 ) do |x|
x.report('dl') do
dl.transaction do
N.times do |i|
dl.execute "insert into foo values (#{i}, #{i+1})"
end
end
end
x.report('native') do
native.transaction do
N.times do |i|
native.execute "insert into foo values (#{i}, #{i+1})"
end
end
end
end
puts
puts "insertions using prepared statement"
Benchmark.bm( 7 ) do |x|
x.report('dl') do
dl.transaction do
dl.prepare "insert into foo values (?,?)" do |stmt|
N.times { |i| stmt.execute i, i+1 }
end
end
end
x.report('native') do
native.transaction do
native.prepare( "insert into foo values (?,?)" ) do |stmt|
N.times { |i| stmt.execute i, i+1 }
end
end
end
end
dl.close
native.close
File.delete "test-dl.db" rescue nil
File.delete "test-native.db" rescue nil
dl = SQLite3::Database.open( "test-dl.db", :driver => "DL" )
native = SQLite3::Database.open( "test-native.db", :driver => "Native" )
dl.execute "create table foo (a,b)"
dl.execute "insert into foo values (1,2)"
dl.execute "insert into foo values (3,4)"
dl.execute "insert into foo values (5,6)"
native.execute "create table foo (a,b)"
native.execute "insert into foo values (1,2)"
native.execute "insert into foo values (3,4)"
native.execute "insert into foo values (5,6)"
puts
puts "queries"
Benchmark.bm( 7 ) do |x|
x.report('dl') do
N.times do
dl.execute "select * from foo"
end
end
x.report('native') do
N.times do
native.execute "select * from foo"
end
end
end
dl.close
native.close
File.delete "test-dl.db" rescue nil
File.delete "test-native.db" rescue nil

View file

@ -1,217 +1,291 @@
require File.join(File.dirname(__FILE__), 'helper')
require 'helper'
require 'iconv'
class TC_Database_Init < Test::Unit::TestCase
def test_new
# any_instance fails here...
driver = Driver.new
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
Driver.stubs(:new).returns(driver)
db = SQLite3::Database.new( 'foo.db', :driver => Driver )
assert !db.closed?
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
module SQLite3
class TestDatabase < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new(':memory:')
end
assert returned_db.closed?
end
def test_open
driver = Driver.new
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
Driver.stubs(:new).returns(driver)
db = SQLite3::Database.open( "foo.db", :driver => Driver )
assert !db.closed?
assert !db.results_as_hash
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
def test_changes
@db.execute("CREATE TABLE items (id integer PRIMARY KEY AUTOINCREMENT, number integer)")
assert_equal 0, @db.changes
@db.execute("INSERT INTO items (number) VALUES (10)")
assert_equal 1, @db.changes
@db.execute_batch(
"UPDATE items SET number = (number + :nn) WHERE (number = :n)",
{"nn" => 20, "n" => 10})
assert_equal 1, @db.changes
assert_equal [[30]], @db.execute("select number from items")
end
def test_new
db = SQLite3::Database.new(':memory:')
assert db
end
def test_new_yields_self
thing = nil
SQLite3::Database.new(':memory:') do |db|
thing = db
end
assert_instance_of(SQLite3::Database, thing)
end
def test_new_with_options
db = SQLite3::Database.new(Iconv.conv('UTF-16LE', 'UTF-8', ':memory:'),
:utf16 => true)
assert db
end
def test_close
db = SQLite3::Database.new(':memory:')
db.close
assert db.closed?
end
def test_block_closes_self
thing = nil
SQLite3::Database.new(':memory:') do |db|
thing = db
assert !thing.closed?
end
assert thing.closed?
end
def test_prepare
db = SQLite3::Database.new(':memory:')
stmt = db.prepare('select "hello world"')
assert_instance_of(SQLite3::Statement, stmt)
end
def test_total_changes
db = SQLite3::Database.new(':memory:')
db.execute("create table foo ( a integer primary key, b text )")
db.execute("insert into foo (b) values ('hello')")
assert_equal 1, db.total_changes
end
def test_execute_returns_list_of_hash
db = SQLite3::Database.new(':memory:', :results_as_hash => true)
db.execute("create table foo ( a integer primary key, b text )")
db.execute("insert into foo (b) values ('hello')")
rows = db.execute("select * from foo")
assert_equal [{0=>1, "a"=>1, "b"=>"hello", 1=>"hello"}], rows
end
def test_execute_yields_hash
db = SQLite3::Database.new(':memory:', :results_as_hash => true)
db.execute("create table foo ( a integer primary key, b text )")
db.execute("insert into foo (b) values ('hello')")
db.execute("select * from foo") do |row|
assert_equal({0=>1, "a"=>1, "b"=>"hello", 1=>"hello"}, row)
end
end
def test_table_info
db = SQLite3::Database.new(':memory:', :results_as_hash => true)
db.execute("create table foo ( a integer primary key, b text )")
info = [{
"name" => "a",
"pk" => 1,
"notnull" => 0,
"type" => "integer",
"dflt_value" => nil,
"cid" => 0
},
{
"name" => "b",
"pk" => 0,
"notnull" => 0,
"type" => "text",
"dflt_value" => nil,
"cid" => 1
}]
assert_equal info, db.table_info('foo')
end
def test_total_changes_closed
db = SQLite3::Database.new(':memory:')
db.close
assert_raise(SQLite3::Exception) do
db.total_changes
end
end
def test_trace_requires_opendb
@db.close
assert_raise(SQLite3::Exception) do
@db.trace { |x| }
end
end
def test_trace_with_block
result = nil
@db.trace { |sql| result = sql }
@db.execute "select 'foo'"
assert_equal "select 'foo'", result
end
def test_trace_with_object
obj = Class.new {
attr_accessor :result
def call sql; @result = sql end
}.new
@db.trace(obj)
@db.execute "select 'foo'"
assert_equal "select 'foo'", obj.result
end
def test_trace_takes_nil
@db.trace(nil)
@db.execute "select 'foo'"
end
def test_last_insert_row_id_closed
@db.close
assert_raise(SQLite3::Exception) do
@db.last_insert_row_id
end
end
def test_define_function
called_with = nil
@db.define_function("hello") do |value|
called_with = value
end
@db.execute("select hello(10)")
assert_equal 10, called_with
end
def test_call_func_arg_type
called_with = nil
@db.define_function("hello") do |b, c, d|
called_with = [b, c, d]
nil
end
@db.execute("select hello(2.2, 'foo', NULL)")
assert_equal [2.2, 'foo', nil], called_with
end
def test_define_varargs
called_with = nil
@db.define_function("hello") do |*args|
called_with = args
nil
end
@db.execute("select hello(2.2, 'foo', NULL)")
assert_equal [2.2, 'foo', nil], called_with
end
def test_function_return
@db.define_function("hello") { |a| 10 }
assert_equal [10], @db.execute("select hello('world')").first
end
def test_function_return_types
[10, 2.2, nil, "foo"].each do |thing|
@db.define_function("hello") { |a| thing }
assert_equal [thing], @db.execute("select hello('world')").first
end
end
def test_define_function_closed
@db.close
assert_raise(SQLite3::Exception) do
@db.define_function('foo') { }
end
end
def test_inerrupt_closed
@db.close
assert_raise(SQLite3::Exception) do
@db.interrupt
end
end
def test_define_aggregate
@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' )"
acc = Class.new {
attr_reader :sum
alias :finalize :sum
def initialize
@sum = 0
end
def step a
@sum += a
end
}.new
@db.define_aggregator("accumulate", acc)
value = @db.get_first_value( "select accumulate(a) from foo" )
assert_equal 6, value
end
def test_authorizer_ok
@db.authorizer = Class.new {
def call action, a, b, c, d; true end
}.new
@db.prepare("select 'fooooo'")
@db.authorizer = Class.new {
def call action, a, b, c, d; 0 end
}.new
@db.prepare("select 'fooooo'")
end
def test_authorizer_ignore
@db.authorizer = Class.new {
def call action, a, b, c, d; nil end
}.new
stmt = @db.prepare("select 'fooooo'")
assert_equal nil, stmt.step
end
def test_authorizer_fail
@db.authorizer = Class.new {
def call action, a, b, c, d; false end
}.new
assert_raises(SQLite3::AuthorizationException) do
@db.prepare("select 'fooooo'")
end
end
def test_remove_auth
@db.authorizer = Class.new {
def call action, a, b, c, d; false end
}.new
assert_raises(SQLite3::AuthorizationException) do
@db.prepare("select 'fooooo'")
end
@db.authorizer = nil
@db.prepare("select 'fooooo'")
end
def test_close_with_open_statements
stmt = @db.prepare("select 'foo'")
assert_raises(SQLite3::BusyException) do
@db.close
end
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
end
def test_execute_with_named_bind_params
assert_equal [['foo']], @db.execute("select :n", {'n' => 'foo'})
end
assert returned_db.closed?
end
def test_with_type_translation
db = SQLite3::Database.open( "foo.db", :driver => Driver,
:type_translation => true )
assert db.type_translation
end
def test_with_results_as_hash
db = SQLite3::Database.open( "foo.db", :driver => Driver,
:results_as_hash => true )
assert db.results_as_hash
end
def test_with_type_translation_and_results_as_hash
db = SQLite3::Database.open( "foo.db", :driver => Driver,
:results_as_hash => true,
:type_translation => true )
assert db.results_as_hash
assert db.type_translation
end
end
class TC_Database < Test::Unit::TestCase
def setup
@db = SQLite3::Database.open( "foo.db",
:driver => Driver, :statement_factory => Statement )
end
def test_quote
assert_equal "''one''two''three''", SQLite3::Database.quote(
"'one'two'three'" )
end
def test_complete
Driver.any_instance.expects(:complete?)
@db.complete? "foo"
end
def test_errmsg
Driver.any_instance.expects(:errmsg)
@db.errmsg
end
def test_errcode
Driver.any_instance.expects(:errcode)
@db.errcode
end
def test_translator
translator = @db.translator
assert_instance_of SQLite3::Translator, translator
end
def test_close
Driver.any_instance.expects(:close).returns(0)
@db.close
assert @db.closed?
Driver.any_instance.expects(:close).never
@db.close
end
def test_trace
Driver.any_instance.expects(:trace).with('cookie', 15)
@db.trace( 15 ) { "foo" }
# assert_equal 1, driver.mock_blocks[:trace].length
end
def test_authorizer
Driver.any_instance.expects(:set_authorizer).with('cookie', 15).returns(0)
@db.authorizer( 15 ) { "foo" }
# assert_equal 1, driver.mock_blocks[:set_authorizer].length
end
def test_prepare_no_block
Statement.any_instance.expects(:close).never
assert_nothing_raised { @db.prepare( "foo" ) }
end
def test_prepare_with_block
called = false
# any_instance fails here...
statement = Statement.new('cookie', 'foo')
statement.expects(:close).once
Statement.stubs(:new).returns(statement)
@db.prepare( "foo" ) { |stmt| called = true }
assert called
end
def test_execute_no_block
# any_instance fails here...
statement = Statement.new('cookie', 'foo')
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
Statement.stubs(:new).returns(statement)
MockResultSet.any_instance.stubs(:inject).returns([['foo']])
result = @db.execute( "foo", "bar", "baz" )
assert_equal [["foo"]], result
end
def test_execute_with_block
called = false
# any_instance fails here...
statement = Statement.new('cookie', 'foo')
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
Statement.stubs(:new).returns(statement)
@db.execute( "foo", "bar", "baz" ) do |row|
called = true
assert_equal ["foo"], row
end
assert called
end
def test_execute2_no_block
# any_instance fails here...
statement = Statement.new('cookie', 'foo')
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
Statement.stubs(:new).returns(statement)
MockResultSet.any_instance.stubs(:inject).returns([['name'], ['foo']])
result = @db.execute2( "foo", "bar", "baz" )
assert_equal [["name"],["foo"]], result
end
def test_execute2_with_block
called = false
parts = [ ["name"],["foo"] ]
# any_instance fails here...
statement = Statement.new('cookie', 'foo')
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
Statement.stubs(:new).returns(statement)
@db.execute2( "foo", "bar", "baz" ) do |row|
called = true
assert_equal parts.shift, row
end
assert called
end
def test_execute_batch
# any_instance fails here...
statement = Statement.new('cookie', 'foo')
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
Statement.stubs(:new).returns(statement)
@db.execute_batch( "foo", "bar", "baz" )
end
def test_get_first_row
result = @db.get_first_row( "foo", "bar", "baz" )
assert_equal ["foo"], result
end
def test_get_first_value
result = @db.get_first_value( "foo", "bar", "baz" )
assert_equal "foo", result
end
def test_changes
Driver.any_instance.expects(:changes).returns(14)
assert_equal 14, @db.changes
end
def test_total_changes
Driver.any_instance.expects(:total_changes).returns(28)
assert_equal 28, @db.total_changes
end
def test_interrupt
Driver.any_instance.expects(:interrupt)
@db.interrupt
end
end

View file

@ -0,0 +1,25 @@
require 'helper'
module SQLite3
class TestDeprecated < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new(':memory:')
end
def test_query_with_many_bind_params
assert_equal [[nil, 1]], @db.query("select ?, ?", nil, 1).to_a
end
def test_query_with_nil_bind_params
assert_equal [['foo']], @db.query("select 'foo'", nil).to_a
end
def test_execute_with_many_bind_params
assert_equal [[nil, 1]], @db.execute("select ?, ?", nil, 1)
end
def test_execute_with_nil_bind_params
assert_equal [['foo']], @db.execute("select 'foo'", nil)
end
end
end

View file

@ -0,0 +1,115 @@
# -*- coding: utf-8 -*-
require 'helper'
module SQLite3
class TestEncoding < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new(':memory:')
@create = "create table ex(id int, data string)"
@insert = "insert into ex(id, data) values (?, ?)"
@db.execute(@create);
end
def test_default_internal_is_honored
before_enc = Encoding.default_internal
str = "壁に耳あり、障子に目あり"
stmt = @db.prepare('insert into ex(data) values (?)')
stmt.bind_param 1, str
stmt.step
Encoding.default_internal = 'EUC-JP'
string = @db.execute('select data from ex').first.first
assert_equal Encoding.default_internal, string.encoding
assert_equal str.encode('EUC-JP'), string
assert_equal str, string.encode(str.encoding)
ensure
Encoding.default_internal = before_enc
end
def test_blob_is_binary
str = "猫舌"
@db.execute('create table foo(data text)')
stmt = @db.prepare('insert into foo(data) values (?)')
stmt.bind_param(1, SQLite3::Blob.new(str))
stmt.step
string = @db.execute('select data from foo').first.first
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
assert_equal str, string.force_encoding('UTF-8')
end
def test_blob_is_ascii8bit
str = "猫舌"
@db.execute('create table foo(data text)')
stmt = @db.prepare('insert into foo(data) values (?)')
stmt.bind_param(1, str.dup.force_encoding("ASCII-8BIT"))
stmt.step
string = @db.execute('select data from foo').first.first
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
assert_equal str, string.force_encoding('UTF-8')
end
def test_blob_with_eucjp
str = "猫舌".encode("EUC-JP")
@db.execute('create table foo(data text)')
stmt = @db.prepare('insert into foo(data) values (?)')
stmt.bind_param(1, SQLite3::Blob.new(str))
stmt.step
string = @db.execute('select data from foo').first.first
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
assert_equal str, string.force_encoding('EUC-JP')
end
def test_db_with_eucjp
db = SQLite3::Database.new(':memory:'.encode('EUC-JP'))
assert_equal(Encoding.find('UTF-8'), db.encoding)
end
def test_db_with_utf16
db = SQLite3::Database.new(':memory:'.encode('UTF-16LE'))
assert_equal(Encoding.find('UTF-16LE'), db.encoding)
end
def test_statement_eucjp
str = "猫舌"
@db.execute("insert into ex(data) values ('#{str}')".encode('EUC-JP'))
row = @db.execute("select data from ex")
assert_equal @db.encoding, row.first.first.encoding
assert_equal str, row.first.first
end
def test_statement_utf8
str = "猫舌"
@db.execute("insert into ex(data) values ('#{str}')")
row = @db.execute("select data from ex")
assert_equal @db.encoding, row.first.first.encoding
assert_equal str, row.first.first
end
def test_encoding
assert_equal Encoding.find("UTF-8"), @db.encoding
end
def test_utf_8
str = "猫舌"
@db.execute(@insert, 10, str)
row = @db.execute("select data from ex")
assert_equal @db.encoding, row.first.first.encoding
assert_equal str, row.first.first
end
def test_euc_jp
str = "猫舌".encode('EUC-JP')
@db.execute(@insert, 10, str)
row = @db.execute("select data from ex")
assert_equal @db.encoding, row.first.first.encoding
assert_equal str.encode('UTF-8'), row.first.first
end
end if RUBY_VERSION >= '1.9.1'
end

View file

@ -1,17 +0,0 @@
require File.join(File.dirname(__FILE__), 'helper')
class TC_Errors < Test::Unit::TestCase
(1..26).each do |code|
define_method( "test_error_code_%02d" % code ) do
db = stub('database', :errmsg => 'message')
begin
SQLite3::Error.check( code, db )
rescue SQLite3::Exception => e
assert_instance_of SQLite3::EXCEPTIONS[code], e
assert_equal code, e.code
assert_equal code, e.class.code
assert_equal "message", e.message
end
end
end
end

View file

@ -1,8 +1,8 @@
require File.join(File.dirname(__FILE__), 'helper')
require 'helper'
class TC_Database_Integration < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@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' )"
@ -13,7 +13,6 @@ class TC_Database_Integration < Test::Unit::TestCase
def teardown
@db.close
File.delete( "test.db" )
end
def test_table_info_with_type_translation_active
@ -25,9 +24,9 @@ class TC_Database_Integration < Test::Unit::TestCase
@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"},
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"},
assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => 0, "cid" => 1, "pk" => 0},
data[1])
end
end
@ -36,9 +35,9 @@ class TC_Database_Integration < Test::Unit::TestCase
@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"},
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"},
assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => 0, "cid" => 1, "pk" => 0},
data[1])
end
end
@ -50,21 +49,25 @@ class TC_Database_Integration < Test::Unit::TestCase
assert @db.complete?( "select * from foo;" )
end
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_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
# 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
def test_errmsg_utf16
assert_equal "not an error".to_utf16, @db.errmsg(true)
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
@ -72,26 +75,26 @@ class TC_Database_Integration < Test::Unit::TestCase
def test_trace
result = nil
@db.trace( "data" ) { |data,sql| result = [ data, sql ]; 0 }
@db.trace { |sql| result = sql }
@db.execute "select * from foo"
assert_equal ["data","select * from foo"], result
assert_equal "select * from foo", result
end
def test_authorizer_okay
@db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
@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( "data" ) { |data,type,a,b,c,d| 1 }
@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( "data" ) { |data,type,a,b,c,d| 2 }
@db.authorizer { |type,a,b,c,d| 2 }
rows = @db.execute "select * from foo"
assert rows.empty?
end
@ -184,7 +187,7 @@ class TC_Database_Integration < Test::Unit::TestCase
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
assert_equal [ "a", "b" ], columns
end
def test_execute2_with_block_no_bind_no_match
@ -199,13 +202,13 @@ class TC_Database_Integration < Test::Unit::TestCase
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
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 [ "a", "b" ], row unless called == 0
assert_equal [ "a", "b" ], row unless called == 0
called += 1
end
assert_equal 1, called
@ -214,13 +217,13 @@ class TC_Database_Integration < Test::Unit::TestCase
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
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 [ "a", "b" ], row unless called == 0
assert_equal [ 1, "foo" ], row unless called == 0
called += 1
end
assert_equal 2, called
@ -229,7 +232,7 @@ class TC_Database_Integration < Test::Unit::TestCase
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
assert_equal [ "a", "b" ], columns
end
def test_execute2_with_block_with_bind_with_match
@ -265,7 +268,7 @@ class TC_Database_Integration < Test::Unit::TestCase
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
assert_equal [1, 1, 1], rows
end
def test_query_no_block_no_bind_no_match
@ -339,7 +342,7 @@ class TC_Database_Integration < Test::Unit::TestCase
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
assert_equal [ 1, "foo" ], result
end
def test_get_first_row_with_bind_no_match
@ -349,7 +352,7 @@ class TC_Database_Integration < Test::Unit::TestCase
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
assert_equal [ 1, "foo" ], result
end
def test_get_first_value_no_bind_no_match
@ -458,7 +461,7 @@ class TC_Database_Integration < Test::Unit::TestCase
func.result = x
end
assert_raise( SQLite3::SQLException ) do
assert_raise( SQLite3::InterruptException ) do
@db.execute "select abort(a) from foo"
end
end
@ -483,7 +486,7 @@ class TC_Database_Integration < Test::Unit::TestCase
@db.create_aggregate( "accumulate", 1, step, final )
value = @db.get_first_value( "select accumulate(a) from foo" )
assert_equal "6", value
assert_equal 6, value
end
def test_create_aggregate_with_block
@ -497,7 +500,7 @@ class TC_Database_Integration < Test::Unit::TestCase
end
value = @db.get_first_value( "select accumulate(a) from foo" )
assert_equal "6", value
assert_equal 6, value
end
def test_create_aggregate_with_no_data
@ -512,7 +515,7 @@ class TC_Database_Integration < Test::Unit::TestCase
value = @db.get_first_value(
"select accumulate(a) from foo where a = 100" )
assert_equal "0", value
assert_equal 0, value
end
def test_create_aggregate_handler
@ -531,7 +534,7 @@ class TC_Database_Integration < Test::Unit::TestCase
@db.create_aggregate_handler( handler )
value = @db.get_first_value( "select multiply(a) from foo" )
assert_equal "6", value
assert_equal 6, value
end
def test_bind_array_parameter

View file

@ -1,4 +1,4 @@
require File.join(File.dirname(__FILE__), 'helper')
require 'helper'
class TC_OpenClose < Test::Unit::TestCase
def test_create_close

View file

@ -1,11 +1,11 @@
require File.join(File.dirname(__FILE__), 'helper')
require 'helper'
require 'thread'
require 'benchmark'
class TC_Integration_Pending < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@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' )"
@ -65,8 +65,9 @@ class TC_Integration_Pending < Test::Unit::TestCase
db2.close if db2
end
end
sleep 1
@db.busy_handler do |data, count|
@db.busy_handler do |count|
handler_call_count += 1
false
end
@ -97,6 +98,7 @@ class TC_Integration_Pending < Test::Unit::TestCase
end
end
sleep 1
time = Benchmark.measure do
assert_raise( SQLite3::BusyException ) do
@db.execute "insert into foo (b) values ( 'from 2' )"
@ -108,4 +110,4 @@ class TC_Integration_Pending < Test::Unit::TestCase
assert time.real*1000 >= 1000
end
end
end

View file

@ -1,8 +1,8 @@
require File.join(File.dirname(__FILE__), 'helper')
require 'helper'
class TC_ResultSet < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@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' )"
@ -16,7 +16,6 @@ class TC_ResultSet < Test::Unit::TestCase
def teardown
@stmt.close
@db.close
File.delete( "test.db" )
end
def test_reset_unused
@ -56,7 +55,7 @@ class TC_ResultSet < Test::Unit::TestCase
def test_next_no_type_translation_no_hash
@result.reset( 1 )
assert_equal [ "1", "foo" ], @result.next
assert_equal [ 1, "foo" ], @result.next
end
def test_next_type_translation
@ -68,10 +67,22 @@ class TC_ResultSet < Test::Unit::TestCase
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
assert_equal [3], result.next
end
end
def test_type_translation_execute
@db.type_translation = true
@db.execute "create table bar ( a integer, b america )"
@db.execute "insert into bar (a, b) values (NULL, '1974-07-25 14:39:00')"
@db.translator.add_translator('america') do |type, thing|
'america'
end
assert_equal [[nil, 'america']], @db.execute("select * from bar")
end
def test_type_translation_with_null_column
@db.type_translation = true
@db.execute "create table bar ( a integer, b time, c string )"
@ -98,10 +109,23 @@ class TC_ResultSet < Test::Unit::TestCase
end
end
def test_real_translation
@db.type_translation = true
@db.execute('create table foo_real(a real)')
@db.execute('insert into foo_real values (42)' )
@db.query('select a, sum(a), typeof(a), typeof(sum(a)) from foo_real') do |result|
result = result.next
assert result[0].is_a?(Float)
assert result[1].is_a?(Float)
assert result[2].is_a?(String)
assert result[3].is_a?(String)
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" },
assert_equal( { "a" => 1, "b" => "foo", 0 => 1, 1 => "foo" },
@result.next )
end
@ -110,7 +134,7 @@ class TC_ResultSet < Test::Unit::TestCase
@result.reset( 1 )
row = @result.next
row.each do |_, v|
assert_equal true, v.tainted?
assert(v.tainted?) if String === v
end
end
@ -118,7 +142,7 @@ class TC_ResultSet < Test::Unit::TestCase
@result.reset( 1 )
row = @result.next
row.each do |v|
assert_equal true, v.tainted?
assert(v.tainted?) if String === v
end
end

View file

@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'helper')
class TC_Statement < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new( "test.db" )
@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' )"
@ -15,7 +15,6 @@ class TC_Statement < Test::Unit::TestCase
def teardown
@stmt.close
@db.close
File.delete( "test.db" )
end
def test_remainder_empty

View file

@ -0,0 +1,9 @@
require 'helper'
module SQLite3
class TestSQLite3 < Test::Unit::TestCase
def test_libversion
assert_not_nil SQLite3.libversion
end
end
end

View file

@ -0,0 +1,207 @@
require 'helper'
module SQLite3
class TestStatement < Test::Unit::TestCase
def setup
@db = SQLite3::Database.new(':memory:')
@stmt = SQLite3::Statement.new(@db, "select 'foo'")
end
###
# This method may not exist depending on how sqlite3 was compiled
def test_database_name
@db.execute('create table foo(text BLOB)')
@db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
stmt = @db.prepare('select text from foo')
if stmt.respond_to?(:database_name)
assert_equal 'main', stmt.database_name(0)
end
end
def test_prepare_blob
@db.execute('create table foo(text BLOB)')
stmt = @db.prepare('insert into foo(text) values (?)')
stmt.bind_param(1, SQLite3::Blob.new('hello'))
stmt.step
stmt.close
end
def test_select_blob
@db.execute('create table foo(text BLOB)')
@db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
assert_equal 'hello', @db.execute('select * from foo').first.first
end
def test_new
assert @stmt
end
def test_new_closed_handle
@db = SQLite3::Database.new(':memory:')
@db.close
assert_raises(ArgumentError) do
SQLite3::Statement.new(@db, 'select "foo"')
end
end
def test_new_with_remainder
stmt = SQLite3::Statement.new(@db, "select 'foo';bar")
assert_equal 'bar', stmt.remainder
end
def test_empty_remainder
assert_equal '', @stmt.remainder
end
def test_close
@stmt.close
assert @stmt.closed?
end
def test_double_close
@stmt.close
assert_raises(SQLite3::Exception) do
@stmt.close
end
end
def test_bind_param_string
stmt = SQLite3::Statement.new(@db, "select ?")
stmt.bind_param(1, "hello")
result = nil
stmt.each { |x| result = x }
assert_equal ['hello'], result
end
def test_bind_param_int
stmt = SQLite3::Statement.new(@db, "select ?")
stmt.bind_param(1, 10)
result = nil
stmt.each { |x| result = x }
assert_equal [10], result
end
def test_bind_nil
stmt = SQLite3::Statement.new(@db, "select ?")
stmt.bind_param(1, nil)
result = nil
stmt.each { |x| result = x }
assert_equal [nil], result
end
def test_bind_blobs
end
def test_bind_64
stmt = SQLite3::Statement.new(@db, "select ?")
stmt.bind_param(1, 2 ** 31)
result = nil
stmt.each { |x| result = x }
assert_equal [2 ** 31], result
end
def test_bind_double
stmt = SQLite3::Statement.new(@db, "select ?")
stmt.bind_param(1, 2.2)
result = nil
stmt.each { |x| result = x }
assert_equal [2.2], result
end
def test_named_bind
stmt = SQLite3::Statement.new(@db, "select :foo")
stmt.bind_param(':foo', 'hello')
result = nil
stmt.each { |x| result = x }
assert_equal ['hello'], result
end
def test_named_bind_no_colon
stmt = SQLite3::Statement.new(@db, "select :foo")
stmt.bind_param('foo', 'hello')
result = nil
stmt.each { |x| result = x }
assert_equal ['hello'], result
end
def test_named_bind_symbol
stmt = SQLite3::Statement.new(@db, "select :foo")
stmt.bind_param(:foo, 'hello')
result = nil
stmt.each { |x| result = x }
assert_equal ['hello'], result
end
def test_named_bind_not_found
stmt = SQLite3::Statement.new(@db, "select :foo")
assert_raises(SQLite3::Exception) do
stmt.bind_param('bar', 'hello')
end
end
def test_each
r = nil
@stmt.each do |row|
r = row
end
assert_equal(['foo'], r)
end
def test_reset!
r = []
@stmt.each { |row| r << row }
@stmt.reset!
@stmt.each { |row| r << row }
assert_equal [['foo'], ['foo']], r
end
def test_step
r = @stmt.step
assert_equal ['foo'], r
end
def test_tainted
r = @stmt.step
assert r.first.tainted?
end
def test_step_twice
assert_not_nil @stmt.step
assert !@stmt.done?
assert_nil @stmt.step
assert @stmt.done?
@stmt.reset!
assert !@stmt.done?
end
def test_step_never_moves_past_done
10.times { @stmt.step }
@stmt.done?
end
def test_column_count
assert_equal 1, @stmt.column_count
end
def test_column_name
assert_equal "'foo'", @stmt.column_name(0)
assert_equal nil, @stmt.column_name(10)
end
def test_bind_parameter_count
stmt = SQLite3::Statement.new(@db, "select ?, ?, ?")
assert_equal 3, stmt.bind_parameter_count
end
def test_execute_with_varargs
stmt = @db.prepare('select ?, ?')
assert_equal [[nil, nil]], stmt.execute(nil, nil).to_a
end
def test_execute_with_hash
stmt = @db.prepare('select :n, :h')
assert_equal [[10, nil]], stmt.execute('n' => 10, 'h' => nil).to_a
end
end
end