2013-06-18 06:32:01 -04:00
|
|
|
require "cases/helper"
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class ResultTest < ActiveRecord::TestCase
|
|
|
|
def result
|
|
|
|
Result.new(['col_1', 'col_2'], [
|
|
|
|
['row 1 col 1', 'row 1 col 2'],
|
2014-02-04 01:47:40 -05:00
|
|
|
['row 2 col 1', 'row 2 col 2'],
|
|
|
|
['row 3 col 1', 'row 3 col 2'],
|
2013-06-18 06:32:01 -04:00
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2014-10-13 15:01:41 -04:00
|
|
|
test "length" do
|
|
|
|
assert_equal 3, result.length
|
|
|
|
end
|
|
|
|
|
2014-06-21 13:31:57 -04:00
|
|
|
test "to_hash returns row_hashes" do
|
2013-06-18 06:32:01 -04:00
|
|
|
assert_equal [
|
|
|
|
{'col_1' => 'row 1 col 1', 'col_2' => 'row 1 col 2'},
|
2014-02-04 01:47:40 -05:00
|
|
|
{'col_1' => 'row 2 col 1', 'col_2' => 'row 2 col 2'},
|
|
|
|
{'col_1' => 'row 3 col 1', 'col_2' => 'row 3 col 2'},
|
2013-06-18 06:32:01 -04:00
|
|
|
], result.to_hash
|
|
|
|
end
|
|
|
|
|
2014-06-21 13:31:57 -04:00
|
|
|
test "each with block returns row hashes" do
|
2013-06-18 06:32:01 -04:00
|
|
|
result.each do |row|
|
|
|
|
assert_equal ['col_1', 'col_2'], row.keys
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-21 13:31:57 -04:00
|
|
|
test "each without block returns an enumerator" do
|
2013-06-18 06:32:01 -04:00
|
|
|
result.each.with_index do |row, index|
|
|
|
|
assert_equal ['col_1', 'col_2'], row.keys
|
|
|
|
assert_kind_of Integer, index
|
|
|
|
end
|
|
|
|
end
|
2014-01-29 15:42:07 -05:00
|
|
|
|
|
|
|
if Enumerator.method_defined? :size
|
2014-06-21 13:31:57 -04:00
|
|
|
test "each without block returns a sized enumerator" do
|
2014-01-29 15:42:07 -05:00
|
|
|
assert_equal 3, result.each.size
|
|
|
|
end
|
|
|
|
end
|
2014-06-21 13:31:57 -04:00
|
|
|
|
|
|
|
test "cast_values returns rows after type casting" do
|
|
|
|
values = [["1.1", "2.2"], ["3.3", "4.4"]]
|
|
|
|
columns = ["col1", "col2"]
|
|
|
|
types = { "col1" => Type::Integer.new, "col2" => Type::Float.new }
|
|
|
|
result = Result.new(columns, values, types)
|
|
|
|
|
|
|
|
assert_equal [[1, 2.2], [3, 4.4]], result.cast_values
|
|
|
|
end
|
|
|
|
|
|
|
|
test "cast_values uses identity type for unknown types" do
|
|
|
|
values = [["1.1", "2.2"], ["3.3", "4.4"]]
|
|
|
|
columns = ["col1", "col2"]
|
|
|
|
types = { "col1" => Type::Integer.new }
|
|
|
|
result = Result.new(columns, values, types)
|
|
|
|
|
|
|
|
assert_equal [[1, "2.2"], [3, "4.4"]], result.cast_values
|
|
|
|
end
|
|
|
|
|
|
|
|
test "cast_values returns single dimensional array if single column" do
|
|
|
|
values = [["1.1"], ["3.3"]]
|
|
|
|
columns = ["col1"]
|
|
|
|
types = { "col1" => Type::Integer.new }
|
|
|
|
result = Result.new(columns, values, types)
|
|
|
|
|
|
|
|
assert_equal [1, 3], result.cast_values
|
|
|
|
end
|
|
|
|
|
|
|
|
test "cast_values can receive types to use instead" do
|
|
|
|
values = [["1.1", "2.2"], ["3.3", "4.4"]]
|
|
|
|
columns = ["col1", "col2"]
|
|
|
|
types = { "col1" => Type::Integer.new, "col2" => Type::Float.new }
|
|
|
|
result = Result.new(columns, values, types)
|
|
|
|
|
|
|
|
assert_equal [[1.1, 2.2], [3.3, 4.4]], result.cast_values("col1" => Type::Float.new)
|
|
|
|
end
|
2013-06-18 06:32:01 -04:00
|
|
|
end
|
|
|
|
end
|