test/test_rhubarb.rb in rhubarb-0.3.0 vs test/test_rhubarb.rb in rhubarb-0.4.0
- old
+ new
@@ -13,12 +13,21 @@
require 'helper'
require 'fileutils'
Customer = Struct.new(:name, :address)
+SQLITE_13 = ::Rhubarb::Persistence::sqlite_13
+CONSTRAINT_EXCEPTION = SQLITE_13 ? SQLite3::ConstraintException : SQLite3::SQLException
+BFB_MAX = 512
+class ParityTest
+ include Rhubarb::Persisting
+ declare_column :number, :integer
+ declare_column :parity, :boolean
+end
+
class TestClass
include Rhubarb::Persisting
declare_column :foo, :integer
declare_column :bar, :string
end
@@ -136,17 +145,17 @@
class Order
include Rhubarb::Persisting
declare_column :group, :int
end
-class PreparedStmtBackendTests < Test::Unit::TestCase
+class NoPreparedStmtBackendTests < Test::Unit::TestCase
def dbfile
ENV['RHUBARB_TEST_DB'] || ":memory:"
end
def use_prepared
- true
+ false
end
def setup
unless dbfile == ":memory:"
FileUtils::safe_unlink(dbfile)
@@ -166,20 +175,48 @@
klasses << BlobTestTable
klasses << ZBlobTestTable
klasses << ObjectTestTable
klasses << RhubarbNamespace::NMTC
klasses << RhubarbNamespace::NMTC2
-
+ klasses << ParityTest
klasses.each { |klass| klass.create_table }
@flist = []
end
def teardown
Rhubarb::Persistence::close()
end
+ def test_boolean_find_by
+ # believe me, I chuckled while writing this
+ bit_parity = Proc.new do |k|
+ k.to_s(2).split("0").join.size.even?
+ end
+
+ expected_numbers = Set.new
+
+ BFB_MAX.times do |i|
+ ParityTest.create(:number=>i, :parity=>bit_parity.call(i))
+ expected_numbers << i
+ end
+
+ even = ParityTest.find_by(:parity=>true)
+ odd = ParityTest.find_by(:parity=>false)
+
+ assert_equal(BFB_MAX, even.size+odd.size)
+ {even=>true, odd=>false}.each do |collection, par|
+ collection.each do |pt|
+ assert_equal(bit_parity.call(pt.number), pt.parity)
+ assert_equal(par, pt.parity)
+ expected_numbers.delete(pt.number)
+ end
+ end
+
+ assert_equal(0, expected_numbers.size)
+ end
+
def test_reserved_word_table
assert_nothing_raised do
Create.create_table
Create.create(:name=>"bar")
c = Create.find_first_by_name("bar")
@@ -512,13 +549,13 @@
vals = {:foo => 2, :bar => "argh"}
TestClass.create(vals)
result = TestClass.find_by_foo(2)
tc = result[0]
- assert(result.size == 1, "TestClass.find_by_foo(2) should return exactly one result")
- assert(tc.foo == 2, "tc.foo (found by foo) should have the value 2")
- assert(tc.bar == "argh", "tc.bar (found by foo) should have the value \"argh\"")
+ assert_equal(1, result.size, "TestClass.find_by_foo(2) should return exactly one result")
+ assert_equal(2, tc.foo, "tc.foo (found by foo) should have the value 2")
+ assert_equal("argh", tc.bar, "tc.bar (found by foo) should have the value \"argh\"")
end
def test_create_and_find_first_by_foo
vals = {:foo => 2, :bar => "argh"}
TestClass.create(vals)
@@ -651,11 +688,11 @@
sr.one = sr
assert(sr == sr.one, "self-referential rows should work; instead #{sr} isn't the same as #{sr.one}")
end
def test_referential_integrity
- assert_raise SQLite3::SQLException do
+ assert_raise CONSTRAINT_EXCEPTION do
FromRef.create(:t => 42)
end
assert_nothing_thrown do
1.upto(20) do |x|
@@ -979,10 +1016,12 @@
end
end
end
end
-class NoPreparedStmtBackendTests < PreparedStmtBackendTests
- def use_prepared
- false
+unless SQLITE_13
+ class PreparedStmtBackendTests < NoPreparedStmtBackendTests
+ def use_prepared
+ true
+ end
end
end