Raise a specific class error instead of string literal (#156)

* Raise a specific class error instead of string literal when the type returned doesn't match the type expected. Allows for drivers to catch the specific error.

* Add ResultSet#next_column_index

* Add shared specs for next_column_index

* Add properties to ColumnTypeMismatchError

* Add shared specs for ColumnTypeMismatchError

* Fix specs

Co-authored-by: Brian J. Cardiff <bcardiff@gmail.com>
This commit is contained in:
Jeremy Woertink 2021-10-12 16:49:26 -07:00 committed by GitHub
parent a25f33611c
commit b1299fcada
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 92 additions and 3 deletions

View file

@ -20,6 +20,10 @@ module GenericResultSet
@index += 1
@row[@index - 1]
end
def next_column_index : Int32
@index
end
end
class FooValue
@ -197,7 +201,7 @@ describe DB do
FooDriver.fake_row = [1] of FooDriver::Any
db.query "query" do |rs|
rs.move_next
expect_raises(Exception, "FooResultSet#read returned a Int32. A BarValue was expected.") do
expect_raises(DB::ColumnTypeMismatchError, "In FooDriver::FooResultSet#read the column 0 returned a Int32 but a BarValue was expected.") do
w.check
rs.read(BarValue)
end
@ -210,7 +214,7 @@ describe DB do
BarDriver.fake_row = [1] of BarDriver::Any
db.query "query" do |rs|
rs.move_next
expect_raises(Exception, "BarResultSet#read returned a Int32. A FooValue was expected.") do
expect_raises(DB::ColumnTypeMismatchError, "In BarDriver::BarResultSet#read the column 0 returned a Int32 but a FooValue was expected.") do
w.check
rs.read(FooValue)
end

View file

@ -190,6 +190,10 @@ class DummyDriver < DB::Driver
return n
end
def next_column_index : Int32
@column_count - @values.not_nil!.size
end
def read(t : String.class)
read.to_s
end