mirror of
https://gitea.invidious.io/iv-org/shard-crystal-db.git
synced 2024-08-15 00:53:32 +00:00
assert connection is closed after db is used.
tidy up close methods
This commit is contained in:
parent
3598dddb65
commit
6a5b2a8758
5 changed files with 58 additions and 28 deletions
|
@ -2,6 +2,10 @@ require "spec"
|
|||
require "db"
|
||||
require "./dummy_driver"
|
||||
|
||||
private def connections
|
||||
DummyDriver::DummyConnection.connections
|
||||
end
|
||||
|
||||
describe DB do
|
||||
it "should get driver class by name" do
|
||||
DB.driver_class("dummy").should eq(DummyDriver)
|
||||
|
@ -42,4 +46,17 @@ describe DB do
|
|||
DummyDriver::DummyResultSet.last_result_set.closed?.should be_true
|
||||
end
|
||||
end
|
||||
|
||||
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
|
||||
end
|
||||
|
|
|
@ -9,6 +9,16 @@ class DummyDriver < DB::Driver
|
|||
getter connection_string
|
||||
|
||||
def initialize(@connection_string)
|
||||
@@connections ||= [] of DummyConnection
|
||||
@@connections.not_nil! << self
|
||||
end
|
||||
|
||||
def self.connections
|
||||
@@connections.not_nil!
|
||||
end
|
||||
|
||||
def self.clear_connections
|
||||
@@connections.try &.clear
|
||||
end
|
||||
|
||||
def prepare(query)
|
||||
|
@ -26,9 +36,9 @@ class DummyDriver < DB::Driver
|
|||
class DummyStatement < DB::Statement
|
||||
property params
|
||||
|
||||
def initialize(driver, @query)
|
||||
def initialize(connection, @query)
|
||||
@params = Hash(Int32 | String, DB::Any).new
|
||||
super(driver)
|
||||
super(connection)
|
||||
end
|
||||
|
||||
protected def perform_query(args : Slice(DB::Any))
|
||||
|
@ -155,6 +165,8 @@ def with_witness(count = 1)
|
|||
end
|
||||
|
||||
def with_dummy
|
||||
DummyDriver::DummyConnection.clear_connections
|
||||
|
||||
DB.open "dummy", "" do |db|
|
||||
yield db
|
||||
end
|
||||
|
|
|
@ -39,25 +39,25 @@ describe DummyDriver do
|
|||
end
|
||||
end
|
||||
|
||||
it "should query with block should executes always" do
|
||||
with_witness do |w|
|
||||
with_dummy do |db|
|
||||
db.query "" 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
|
||||
# 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
|
||||
|
||||
it "should enumerate string fields" do
|
||||
with_dummy do |db|
|
||||
|
|
|
@ -21,20 +21,20 @@ module DB
|
|||
|
||||
# Closes this connection.
|
||||
def close
|
||||
raise "Connection already closed" if @closed # TODO make it no fail if closed
|
||||
return if @closed
|
||||
@closed = true
|
||||
perform_close
|
||||
end
|
||||
|
||||
# Returns `true` if this statement is closed. See `#close`.
|
||||
# Returns `true` if this connection is closed. See `#close`.
|
||||
def closed?
|
||||
@closed
|
||||
end
|
||||
|
||||
# # :nodoc:
|
||||
# def finalize
|
||||
# close unless closed?
|
||||
# end
|
||||
# :nodoc:
|
||||
def finalize
|
||||
close unless closed?
|
||||
end
|
||||
|
||||
# Returns an `Statement` with the prepared `query`
|
||||
abstract def prepare(query) : Statement
|
||||
|
|
|
@ -16,6 +16,7 @@ module DB
|
|||
@closed = false
|
||||
end
|
||||
|
||||
|
||||
# See `QueryMethods#exec`
|
||||
def exec
|
||||
perform_exec(Slice(Any).new(0)) # no overload matches ... with types Slice(NoReturn)
|
||||
|
@ -83,7 +84,7 @@ module DB
|
|||
|
||||
# Closes this statement.
|
||||
def close
|
||||
return if @closed # make it work if closed
|
||||
return if @closed
|
||||
@closed = true
|
||||
do_close
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue