2017-07-09 13:41:28 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-18 06:32:01 -04:00
|
|
|
require "cases/helper"
|
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class ResultTest < ActiveRecord::TestCase
|
|
|
|
def result
|
2016-08-06 12:26:20 -04:00
|
|
|
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"],
|
2013-06-18 06:32:01 -04:00
|
|
|
])
|
|
|
|
end
|
|
|
|
|
2020-06-10 03:46:58 -04:00
|
|
|
test "map! is deprecated" do
|
|
|
|
assert_deprecated do
|
|
|
|
result.map! { nil }
|
|
|
|
end
|
|
|
|
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_a
|
|
|
|
end
|
|
|
|
|
|
|
|
test "collect! is deprecated" do
|
|
|
|
assert_deprecated do
|
|
|
|
result.collect! { nil }
|
|
|
|
end
|
|
|
|
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_a
|
|
|
|
end
|
|
|
|
|
2018-06-25 18:52:59 -04:00
|
|
|
test "includes_column?" do
|
|
|
|
assert result.includes_column?("col_1")
|
|
|
|
assert_not result.includes_column?("foo")
|
|
|
|
end
|
|
|
|
|
2014-10-13 15:01:41 -04:00
|
|
|
test "length" do
|
|
|
|
assert_equal 3, result.length
|
|
|
|
end
|
|
|
|
|
2016-01-29 17:44:04 -05:00
|
|
|
test "to_a returns row_hashes" do
|
2013-06-18 06:32:01 -04:00
|
|
|
assert_equal [
|
2016-08-16 03:30:11 -04:00
|
|
|
{ "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" },
|
2016-01-29 17:44:04 -05:00
|
|
|
], result.to_a
|
|
|
|
end
|
|
|
|
|
2016-06-24 03:50:41 -04:00
|
|
|
test "first returns first row as a hash" do
|
|
|
|
assert_equal(
|
2016-08-16 03:30:11 -04:00
|
|
|
{ "col_1" => "row 1 col 1", "col_2" => "row 1 col 2" }, result.first)
|
2020-06-07 23:58:36 -04:00
|
|
|
assert_equal [
|
|
|
|
{ "col_1" => "row 1 col 1", "col_2" => "row 1 col 2" },
|
|
|
|
], result.first(1)
|
|
|
|
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" },
|
|
|
|
], result.first(2)
|
|
|
|
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.first(3)
|
2016-06-24 03:50:41 -04:00
|
|
|
end
|
|
|
|
|
2016-06-24 03:49:42 -04:00
|
|
|
test "last returns last row as a hash" do
|
|
|
|
assert_equal(
|
2016-08-16 03:30:11 -04:00
|
|
|
{ "col_1" => "row 3 col 1", "col_2" => "row 3 col 2" }, result.last)
|
2020-06-07 23:58:36 -04:00
|
|
|
assert_equal [
|
|
|
|
{ "col_1" => "row 3 col 1", "col_2" => "row 3 col 2" },
|
|
|
|
], result.last(1)
|
|
|
|
assert_equal [
|
|
|
|
{ "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.last(2)
|
|
|
|
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.last(3)
|
2016-06-24 03:49:42 -04:00
|
|
|
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|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ["col_1", "col_2"], row.keys
|
2013-06-18 06:32:01 -04:00
|
|
|
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|
|
2016-08-06 12:26:20 -04:00
|
|
|
assert_equal ["col_1", "col_2"], row.keys
|
2013-06-18 06:32:01 -04:00
|
|
|
assert_kind_of Integer, index
|
|
|
|
end
|
|
|
|
end
|
2014-01-29 15:42:07 -05:00
|
|
|
|
2017-04-25 10:40:51 -04:00
|
|
|
test "each without block returns a sized enumerator" do
|
|
|
|
assert_equal 3, result.each.size
|
2014-01-29 15:42:07 -05:00
|
|
|
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
|