Commit 6d8e36d1d72b017c34c117d3ff9377ed15300113
1 parent
79ef0598
Exists in
master
and in
1 other branch
Fixed relation tests
Showing
3 changed files
with
11 additions
and
7 deletions
Show diff stats
test/parameters_test.rb
@@ -43,13 +43,13 @@ class ParametersTest < Minitest::Test | @@ -43,13 +43,13 @@ class ParametersTest < Minitest::Test | ||
43 | def test_where_permitted_relation | 43 | def test_where_permitted_relation |
44 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] | 44 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] |
45 | params = ActionController::Parameters.new({store_id: 1}) | 45 | params = ActionController::Parameters.new({store_id: 1}) |
46 | - assert_equal ["Product A"], Product.search("product").where(params.permit(:store_id)).map(&:name) | 46 | + assert_search_relation ["Product A"], Product.search("product").where(params.permit(:store_id)) |
47 | end | 47 | end |
48 | 48 | ||
49 | def test_rewhere_permitted_relation | 49 | def test_rewhere_permitted_relation |
50 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] | 50 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] |
51 | params = ActionController::Parameters.new({store_id: 1}) | 51 | params = ActionController::Parameters.new({store_id: 1}) |
52 | - assert_equal ["Product A"], Product.search("product").rewhere(params.permit(:store_id)).map(&:name) | 52 | + assert_search_relation ["Product A"], Product.search("product").rewhere(params.permit(:store_id)) |
53 | end | 53 | end |
54 | 54 | ||
55 | def test_where_value | 55 | def test_where_value |
@@ -61,13 +61,13 @@ class ParametersTest < Minitest::Test | @@ -61,13 +61,13 @@ class ParametersTest < Minitest::Test | ||
61 | def test_where_value_relation | 61 | def test_where_value_relation |
62 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] | 62 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] |
63 | params = ActionController::Parameters.new({store_id: 1}) | 63 | params = ActionController::Parameters.new({store_id: 1}) |
64 | - assert_equal ["Product A"], Product.search("product").where(store_id: params[:store_id]).map(&:name) | 64 | + assert_search_relation ["Product A"], Product.search("product").where(store_id: params[:store_id]) |
65 | end | 65 | end |
66 | 66 | ||
67 | def test_rewhere_value_relation | 67 | def test_rewhere_value_relation |
68 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] | 68 | store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}] |
69 | params = ActionController::Parameters.new({store_id: 1}) | 69 | params = ActionController::Parameters.new({store_id: 1}) |
70 | - assert_equal ["Product A"], Product.search("product").where(store_id: params[:store_id]).map(&:name) | 70 | + assert_search_relation ["Product A"], Product.search("product").where(store_id: params[:store_id]) |
71 | end | 71 | end |
72 | 72 | ||
73 | def test_where_hash | 73 | def test_where_hash |
test/support/helpers.rb
@@ -53,6 +53,10 @@ class Minitest::Test | @@ -53,6 +53,10 @@ class Minitest::Test | ||
53 | assert_equal expected.sort, model.search(term, **options).map(&:name).sort | 53 | assert_equal expected.sort, model.search(term, **options).map(&:name).sort |
54 | end | 54 | end |
55 | 55 | ||
56 | + def assert_search_relation(expected, relation) | ||
57 | + assert_equal expected.sort, relation.map(&:name).sort | ||
58 | + end | ||
59 | + | ||
56 | def assert_order(term, expected, options = {}, model = default_model) | 60 | def assert_order(term, expected, options = {}, model = default_model) |
57 | assert_equal expected, model.search(term, **options).map(&:name) | 61 | assert_equal expected, model.search(term, **options).map(&:name) |
58 | end | 62 | end |
test/where_test.rb
@@ -90,13 +90,13 @@ class WhereTest < Minitest::Test | @@ -90,13 +90,13 @@ class WhereTest < Minitest::Test | ||
90 | {name: "Product C", store_id: 3, in_stock: false, backordered: true, created_at: now - 2, orders_count: 2, user_ids: [1, 3]}, | 90 | {name: "Product C", store_id: 3, in_stock: false, backordered: true, created_at: now - 2, orders_count: 2, user_ids: [1, 3]}, |
91 | {name: "Product D", store_id: 4, in_stock: false, backordered: false, created_at: now - 3, orders_count: 1} | 91 | {name: "Product D", store_id: 4, in_stock: false, backordered: false, created_at: now - 3, orders_count: 1} |
92 | ] | 92 | ] |
93 | - assert_equal ["Product A", "Product B"], Product.search("product").where(in_stock: true).map(&:name) | 93 | + assert_search_relation ["Product A", "Product B"], Product.search("product").where(in_stock: true) |
94 | 94 | ||
95 | # multiple where | 95 | # multiple where |
96 | - assert_equal ["Product A"], Product.search("product").where(in_stock: true).where(backordered: true).map(&:name) | 96 | + assert_search_relation ["Product A"], Product.search("product").where(in_stock: true).where(backordered: true) |
97 | 97 | ||
98 | # rewhere | 98 | # rewhere |
99 | - assert_equal ["Product A", "Product C"], Product.search("product").where(in_stock: true).rewhere(backordered: true).map(&:name) | 99 | + assert_search_relation ["Product A", "Product C"], Product.search("product").where(in_stock: true).rewhere(backordered: true) |
100 | end | 100 | end |
101 | 101 | ||
102 | def test_where_string_operators | 102 | def test_where_string_operators |