2016-02-26 01:37:10 +00:00
|
|
|
require "./spec_helper"
|
2016-01-28 23:31:35 +00:00
|
|
|
|
|
|
|
describe DummyDriver do
|
2016-01-30 22:46:43 +00:00
|
|
|
it "with_dummy executes the block with a database" do
|
|
|
|
with_witness do |w|
|
|
|
|
with_dummy do |db|
|
|
|
|
w.check
|
|
|
|
db.should be_a(DB::Database)
|
|
|
|
end
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe DummyDriver::DummyStatement do
|
2016-01-30 22:46:43 +00:00
|
|
|
it "should enumerate split rows by spaces" do
|
|
|
|
with_dummy do |db|
|
|
|
|
rs = db.query("")
|
|
|
|
rs.move_next.should be_false
|
|
|
|
rs.close
|
|
|
|
|
|
|
|
rs = db.query("a,b")
|
|
|
|
rs.move_next.should be_true
|
|
|
|
rs.move_next.should be_false
|
|
|
|
rs.close
|
|
|
|
|
|
|
|
rs = db.query("a,b 1,2")
|
|
|
|
rs.move_next.should be_true
|
|
|
|
rs.move_next.should be_true
|
|
|
|
rs.move_next.should be_false
|
|
|
|
rs.close
|
|
|
|
|
|
|
|
rs = db.query("a,b 1,2 c,d")
|
|
|
|
rs.move_next.should be_true
|
|
|
|
rs.move_next.should be_true
|
|
|
|
rs.move_next.should be_true
|
|
|
|
rs.move_next.should be_false
|
|
|
|
rs.close
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
|
2016-02-03 03:36:50 +00:00
|
|
|
# it "should query with block should executes always" do
|
|
|
|
# with_witness do |w|
|
|
|
|
# with_dummy do |db|
|
|
|
|
# db.query "a" do |rs|
|
|
|
|
# w.check
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
# it "should query with block should executes always" do
|
|
|
|
# with_witness do |w|
|
|
|
|
# with_dummy do |db|
|
|
|
|
# db.query "lorem ipsum" do |rs|
|
|
|
|
# w.check
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2016-01-28 23:31:35 +00:00
|
|
|
|
|
|
|
it "should enumerate string fields" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
db.query "a,b 1,2" do |rs|
|
|
|
|
rs.move_next
|
|
|
|
rs.read(String).should eq("a")
|
|
|
|
rs.read(String).should eq("b")
|
|
|
|
rs.move_next
|
|
|
|
rs.read(String).should eq("1")
|
|
|
|
rs.read(String).should eq("2")
|
|
|
|
end
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
|
2016-01-29 19:13:01 +00:00
|
|
|
it "should enumerate nil fields" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
db.query "a,NULL 1,NULL" do |rs|
|
|
|
|
rs.move_next
|
2016-06-28 17:02:08 +00:00
|
|
|
rs.read(String).should eq("a")
|
|
|
|
rs.read(String | Nil).should be_nil
|
2016-01-30 22:46:43 +00:00
|
|
|
rs.move_next
|
2016-06-28 17:02:08 +00:00
|
|
|
rs.read(Int64).should eq(1)
|
|
|
|
rs.read(Int64 | Nil).should be_nil
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should enumerate int64 fields" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
db.query "3,4 1,2" do |rs|
|
|
|
|
rs.move_next
|
|
|
|
rs.read(Int64).should eq(3i64)
|
|
|
|
rs.read(Int64).should eq(4i64)
|
|
|
|
rs.move_next
|
|
|
|
rs.read(Int64).should eq(1i64)
|
|
|
|
rs.read(Int64).should eq(2i64)
|
|
|
|
end
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
2016-01-28 23:51:03 +00:00
|
|
|
|
2016-07-04 15:13:39 +00:00
|
|
|
it "should enumerate nillable int64 fields" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query "3,4 1,NULL" do |rs|
|
|
|
|
rs.move_next
|
|
|
|
rs.read(Int64 | Nil).should eq(3i64)
|
|
|
|
rs.read(Int64 | Nil).should eq(4i64)
|
|
|
|
rs.move_next
|
|
|
|
rs.read(Int64 | Nil).should eq(1i64)
|
|
|
|
rs.read(Int64 | Nil).should be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-28 17:02:08 +00:00
|
|
|
describe "query one" do
|
|
|
|
it "queries" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query_one("3,4", &.read(Int64, Int64)).should eq({3i64, 4i64})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises if more than one row" do
|
|
|
|
with_dummy do |db|
|
|
|
|
expect_raises(DB::Error, "more than one row") do
|
|
|
|
db.query_one("3,4 5,6") { }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises if no rows" do
|
|
|
|
with_dummy do |db|
|
|
|
|
expect_raises(DB::Error, "no rows") do
|
|
|
|
db.query_one("") { }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "with as" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query_one("3,4", as: {Int64, Int64}).should eq({3i64, 4i64})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "with as, just one" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query_one("3", as: Int64).should eq(3i64)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "query one?" do
|
|
|
|
it "queries" do
|
|
|
|
with_dummy do |db|
|
|
|
|
value = db.query_one?("3,4", &.read(Int64, Int64))
|
|
|
|
value.should eq({3i64, 4i64})
|
|
|
|
value.should be_a(Tuple(Int64, Int64)?)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "raises if more than one row" do
|
|
|
|
with_dummy do |db|
|
|
|
|
expect_raises(DB::Error, "more than one row") do
|
|
|
|
db.query_one?("3,4 5,6") { }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns nil if no rows" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query_one?("") { fail("block shouldn't be invoked") }.should be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "with as" do
|
|
|
|
with_dummy do |db|
|
|
|
|
value = db.query_one?("3,4", as: {Int64, Int64})
|
|
|
|
value.should be_a(Tuple(Int64, Int64)?)
|
|
|
|
value.should eq({3i64, 4i64})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "with as, just one" do
|
|
|
|
with_dummy do |db|
|
|
|
|
value = db.query_one?("3", as: Int64)
|
|
|
|
value.should be_a(Int64?)
|
|
|
|
value.should eq(3i64)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "query all" do
|
|
|
|
it "queries" do
|
|
|
|
with_dummy do |db|
|
|
|
|
ary = db.query_all "3,4 1,2", &.read(Int64, Int64)
|
|
|
|
ary.should eq([{3, 4}, {1, 2}])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "queries with as" do
|
|
|
|
with_dummy do |db|
|
|
|
|
ary = db.query_all "3,4 1,2", as: {Int64, Int64}
|
|
|
|
ary.should eq([{3, 4}, {1, 2}])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "queries with as, just one" do
|
|
|
|
with_dummy do |db|
|
|
|
|
ary = db.query_all "3 1", as: Int64
|
|
|
|
ary.should eq([3, 1])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reads multiple values" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query "3,4 1,2" do |rs|
|
|
|
|
rs.move_next
|
|
|
|
rs.read(Int64, Int64).should eq({3i64, 4i64})
|
|
|
|
rs.move_next
|
|
|
|
rs.read(Int64, Int64).should eq({1i64, 2i64})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-29 22:21:48 +00:00
|
|
|
it "should enumerate blob fields" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
db.query("az,AZ") do |rs|
|
|
|
|
rs.move_next
|
|
|
|
ary = [97u8, 122u8]
|
2016-06-22 17:10:09 +00:00
|
|
|
rs.read(Bytes).should eq(Bytes.new(ary.to_unsafe, ary.size))
|
2016-01-30 22:46:43 +00:00
|
|
|
ary = [65u8, 90u8]
|
2016-06-22 17:10:09 +00:00
|
|
|
rs.read(Bytes).should eq(Bytes.new(ary.to_unsafe, ary.size))
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-29 22:21:48 +00:00
|
|
|
end
|
|
|
|
|
2016-02-02 01:33:58 +00:00
|
|
|
it "should get Nil scalars" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
2016-02-02 01:33:58 +00:00
|
|
|
db.scalar("NULL").should be_nil
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
2016-01-28 23:51:03 +00:00
|
|
|
end
|
2016-01-29 22:21:48 +00:00
|
|
|
|
|
|
|
{% for value in [1, 1_i64, "hello", 1.5, 1.5_f32] %}
|
2016-01-30 22:46:43 +00:00
|
|
|
it "should set positional arguments for {{value.id}}" do
|
|
|
|
with_dummy do |db|
|
2016-02-02 01:33:58 +00:00
|
|
|
db.scalar("?", {{value}}).should eq({{value}})
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
2016-01-29 22:21:48 +00:00
|
|
|
end
|
|
|
|
{% end %}
|
|
|
|
|
|
|
|
it "executes and selects blob" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_dummy do |db|
|
|
|
|
ary = UInt8[0x53, 0x51, 0x4C]
|
2016-06-22 17:10:09 +00:00
|
|
|
slice = Bytes.new(ary.to_unsafe, ary.size)
|
|
|
|
(db.scalar("?", slice).as(Bytes)).to_a.should eq(ary)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
2016-01-29 22:21:48 +00:00
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
end
|