1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
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"],
["row 2 col 1", "row 2 col 2"],
["row 3 col 1", "row 3 col 2"],
])
end
test "length" do
assert_equal 3, result.length
end
test "to_hash returns row_hashes" do
assert_equal [
{ "col_1" => "row 1 col 1", "col_2" => "row 1 col 2" },
{ "col_1" => "row 2 col 1", "col_2" => "row 2 col 2" },
{ "col_1" => "row 3 col 1", "col_2" => "row 3 col 2" },
], result.to_hash
end
test "first returns first row as a hash" do
assert_equal(
{ "col_1" => "row 1 col 1", "col_2" => "row 1 col 2" }, result.first)
end
test "last returns last row as a hash" do
assert_equal(
{ "col_1" => "row 3 col 1", "col_2" => "row 3 col 2" }, result.last)
end
test "each with block returns row hashes" do
result.each do |row|
assert_equal ["col_1", "col_2"], row.keys
end
end
test "each without block returns an enumerator" do
result.each.with_index do |row, index|
assert_equal ["col_1", "col_2"], row.keys
assert_kind_of Integer, index
end
end
if Enumerator.method_defined? :size
test "each without block returns a sized enumerator" do
assert_equal 3, result.each.size
end
end
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
end
end
|