2016-02-26 01:37:10 +00:00
|
|
|
require "./spec_helper"
|
2016-01-30 22:46:43 +00:00
|
|
|
|
2016-02-03 03:36:50 +00:00
|
|
|
private def connections
|
|
|
|
DummyDriver::DummyConnection.connections
|
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
describe DB do
|
|
|
|
it "should get driver class by name" do
|
|
|
|
DB.driver_class("dummy").should eq(DummyDriver)
|
|
|
|
end
|
|
|
|
|
2016-02-03 21:29:09 +00:00
|
|
|
it "should instantiate driver with connection uri" do
|
|
|
|
db = DB.open "dummy://localhost:1027"
|
2016-02-03 22:30:51 +00:00
|
|
|
db.driver.should be_a(DummyDriver)
|
2016-02-03 21:29:09 +00:00
|
|
|
db.uri.scheme.should eq("dummy")
|
|
|
|
db.uri.host.should eq("localhost")
|
|
|
|
db.uri.port.should eq(1027)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should create a connection and close it" do
|
2016-02-03 22:30:51 +00:00
|
|
|
DummyDriver::DummyConnection.clear_connections
|
2016-02-03 21:29:09 +00:00
|
|
|
DB.open "dummy://localhost" do |db|
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
2016-02-03 22:30:51 +00:00
|
|
|
connections.size.should eq(1)
|
|
|
|
connections.first.closed?.should be_true
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
it "query should close result_set" do
|
2016-01-30 22:46:43 +00:00
|
|
|
with_witness do |w|
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query "1,2" do
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
w.check
|
2016-02-02 00:55:30 +00:00
|
|
|
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "scalar should close statement" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.scalar "1"
|
2016-02-02 00:55:30 +00:00
|
|
|
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
end
|
2016-02-03 03:36:50 +00:00
|
|
|
|
|
|
|
it "initially a single connection should be created" do
|
|
|
|
with_dummy do |db|
|
|
|
|
connections.size.should eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "the connection should be closed after db usage" do
|
|
|
|
with_dummy do |db|
|
|
|
|
connections.first.closed?.should be_false
|
|
|
|
end
|
|
|
|
connections.first.closed?.should be_true
|
|
|
|
end
|
2016-02-03 22:30:51 +00:00
|
|
|
|
|
|
|
it "should raise if the sole connection is been used" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.query "1" do |rs|
|
2016-07-07 18:50:09 +00:00
|
|
|
expect_raises DB::PoolTimeout do
|
2016-02-03 22:30:51 +00:00
|
|
|
db.scalar "2"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-04 00:28:53 +00:00
|
|
|
|
|
|
|
it "exec should return to pool" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.exec "foo"
|
|
|
|
db.exec "bar"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "scalar should return to pool" do
|
|
|
|
with_dummy do |db|
|
|
|
|
db.scalar "foo"
|
|
|
|
db.scalar "bar"
|
|
|
|
end
|
|
|
|
end
|
2016-10-21 14:24:32 +00:00
|
|
|
|
|
|
|
it "gives nice error message when no driver is registered for schema (#21)" do
|
|
|
|
expect_raises(ArgumentError, %(no driver was registered for the schema "foobar", did you maybe forget to require the database driver?)) do
|
|
|
|
DB.open "foobar://baz"
|
|
|
|
end
|
|
|
|
end
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|