2016-01-30 22:46:43 +00:00
|
|
|
require "spec"
|
|
|
|
|
2016-01-28 23:31:35 +00:00
|
|
|
class DummyDriver < DB::Driver
|
2016-01-30 22:46:43 +00:00
|
|
|
def build_connection
|
2016-01-31 20:30:21 +00:00
|
|
|
DummyConnection.new(connection_string)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class DummyConnection < DB::Connection
|
2016-02-02 02:02:04 +00:00
|
|
|
getter connection_string
|
|
|
|
|
|
|
|
def initialize(@connection_string)
|
2016-02-03 03:36:50 +00:00
|
|
|
@@connections ||= [] of DummyConnection
|
|
|
|
@@connections.not_nil! << self
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.connections
|
|
|
|
@@connections.not_nil!
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.clear_connections
|
|
|
|
@@connections.try &.clear
|
2016-02-02 02:02:04 +00:00
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
def prepare(query)
|
2016-02-02 00:55:30 +00:00
|
|
|
DummyStatement.new(self, query)
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-01-31 17:01:52 +00:00
|
|
|
def last_insert_id : Int64
|
|
|
|
0
|
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
def perform_close
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class DummyStatement < DB::Statement
|
2016-02-02 00:55:30 +00:00
|
|
|
property params
|
2016-01-29 22:21:48 +00:00
|
|
|
|
2016-02-03 03:36:50 +00:00
|
|
|
def initialize(connection, @query)
|
2016-02-02 00:55:30 +00:00
|
|
|
@params = Hash(Int32 | String, DB::Any).new
|
2016-02-03 03:36:50 +00:00
|
|
|
super(connection)
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
|
2016-02-02 02:02:04 +00:00
|
|
|
protected def perform_query(args : Slice(DB::Any))
|
|
|
|
set_params args
|
|
|
|
DummyResultSet.new self, @query
|
|
|
|
end
|
|
|
|
|
|
|
|
protected def perform_exec(args : Slice(DB::Any))
|
|
|
|
set_params args
|
|
|
|
DB::ExecResult.new 0, 0
|
|
|
|
end
|
|
|
|
|
|
|
|
private def set_params(args)
|
2016-02-02 00:55:30 +00:00
|
|
|
@params.clear
|
|
|
|
args.each_with_index do |arg, index|
|
|
|
|
@params[index] = arg
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DummyResultSet < DB::ResultSet
|
2016-02-02 01:33:58 +00:00
|
|
|
@@next_column_type = String
|
|
|
|
|
2016-02-02 00:55:30 +00:00
|
|
|
def initialize(statement, query)
|
2016-01-28 23:31:35 +00:00
|
|
|
super(statement)
|
2016-02-02 00:55:30 +00:00
|
|
|
@iterator = query.split.map { |r| r.split(',') }.to_a.each
|
|
|
|
|
2016-01-31 00:03:02 +00:00
|
|
|
@@last_result_set = self
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.last_result_set
|
|
|
|
@@last_result_set.not_nil!
|
|
|
|
end
|
|
|
|
|
2016-01-29 19:13:01 +00:00
|
|
|
def move_next
|
2016-01-28 23:31:35 +00:00
|
|
|
@iterator.next.tap do |n|
|
|
|
|
return false if n.is_a?(Iterator::Stop)
|
|
|
|
@values = n.each
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-01-30 00:57:00 +00:00
|
|
|
def column_count
|
|
|
|
2
|
|
|
|
end
|
|
|
|
|
|
|
|
def column_name(index)
|
|
|
|
"c#{index}"
|
|
|
|
end
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
def column_type(index : Int32)
|
2016-02-02 01:33:58 +00:00
|
|
|
@@next_column_type
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.next_column_type=(value)
|
|
|
|
@@next_column_type = value
|
2016-01-30 22:46:43 +00:00
|
|
|
end
|
|
|
|
|
2016-01-29 22:21:48 +00:00
|
|
|
private def read? : DB::Any?
|
2016-01-29 19:13:01 +00:00
|
|
|
n = @values.not_nil!.next
|
|
|
|
raise "end of row" if n.is_a?(Iterator::Stop)
|
|
|
|
return nil if n == "NULL"
|
2016-01-29 22:21:48 +00:00
|
|
|
|
|
|
|
if n == "?"
|
2016-01-31 22:40:02 +00:00
|
|
|
return @statement.params[0]
|
2016-01-29 22:21:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : String.class)
|
|
|
|
read?.try &.to_s
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
|
2016-01-29 19:13:01 +00:00
|
|
|
def read?(t : Int32.class)
|
|
|
|
read?(String).try &.to_i32
|
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : Int64.class)
|
|
|
|
read?(String).try &.to_i64
|
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : Float32.class)
|
2016-01-29 22:21:48 +00:00
|
|
|
read?(String).try &.to_f32
|
2016-01-29 19:13:01 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def read?(t : Float64.class)
|
|
|
|
read?(String).try &.to_f64
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
2016-01-29 22:21:48 +00:00
|
|
|
|
|
|
|
def read?(t : Slice(UInt8).class)
|
|
|
|
value = read?
|
|
|
|
if value.is_a?(Nil)
|
|
|
|
value
|
|
|
|
elsif value.is_a?(String)
|
|
|
|
ary = value.bytes
|
|
|
|
Slice.new(ary.to_unsafe, ary.size)
|
2016-01-30 22:46:43 +00:00
|
|
|
elsif value.is_a?(Slice(UInt8))
|
|
|
|
value
|
2016-01-29 22:21:48 +00:00
|
|
|
else
|
2016-01-30 22:46:43 +00:00
|
|
|
raise "#{value} is not convertible to Slice(UInt8)"
|
2016-01-29 22:21:48 +00:00
|
|
|
end
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
DB.register_driver "dummy", DummyDriver
|
|
|
|
|
2016-01-30 22:46:43 +00:00
|
|
|
class Witness
|
|
|
|
getter count
|
|
|
|
|
|
|
|
def initialize(@count)
|
|
|
|
end
|
|
|
|
|
|
|
|
def check
|
|
|
|
@count -= 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_witness(count = 1)
|
|
|
|
w = Witness.new(count)
|
|
|
|
yield w
|
|
|
|
w.count.should eq(0), "The expected coverage was unmet"
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_dummy
|
2016-02-03 03:36:50 +00:00
|
|
|
DummyDriver::DummyConnection.clear_connections
|
|
|
|
|
2016-01-31 20:30:21 +00:00
|
|
|
DB.open "dummy", "" do |db|
|
2016-01-30 22:46:43 +00:00
|
|
|
yield db
|
|
|
|
end
|
2016-01-28 23:31:35 +00:00
|
|
|
end
|