select_test.rb
3.72 KB
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
91
92
93
94
95
96
97
98
99
100
require_relative "test_helper"
class SelectTest < Minitest::Test
def test_basic
store [{name: "Product A", store_id: 1}]
result = Product.search("product", load: false, select: [:name, :store_id]).first
assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
assert_equal "Product A", result.name
assert_equal 1, result.store_id
end
def test_relation
store [{name: "Product A", store_id: 1}]
result = Product.search("product", load: false).select(:name, :store_id).first
assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
assert_equal "Product A", result.name
assert_equal 1, result.store_id
end
def test_block
store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}]
assert_equal ["Product B"], Product.search("product", load: false).select { |v| v.store_id == 2 }.map(&:name)
end
def test_block_arguments
store [{name: "Product A", store_id: 1}, {name: "Product B", store_id: 2}]
error = assert_raises(ArgumentError) do
Product.search("product", load: false).select(:name) { |v| v.store_id == 2 }
end
assert_equal "wrong number of arguments (given 1, expected 0)", error.message
end
def test_multiple
store [{name: "Product A", store_id: 1}]
result = Product.search("product", load: false).select(:name).select(:store_id).first
assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort
assert_equal "Product A", result.name
assert_equal 1, result.store_id
end
def test_reselect
store [{name: "Product A", store_id: 1}]
result = Product.search("product", load: false).select(:name).reselect(:store_id).first
assert_equal %w(id store_id), result.keys.reject { |k| k.start_with?("_") }.sort
assert_equal 1, result.store_id
end
def test_array
store [{name: "Product A", user_ids: [1, 2]}]
result = Product.search("product", load: false, select: [:user_ids]).first
assert_equal [1, 2], result.user_ids
end
def test_single_field
store [{name: "Product A", store_id: 1}]
result = Product.search("product", load: false, select: :name).first
assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
assert_equal "Product A", result.name
assert_nil result.store_id
end
def test_all
store [{name: "Product A", user_ids: [1, 2]}]
hit = Product.search("product", select: true).hits.first
assert_equal hit["_source"]["name"], "Product A"
assert_equal hit["_source"]["user_ids"], [1, 2]
end
def test_none
store [{name: "Product A", user_ids: [1, 2]}]
hit = Product.search("product", select: []).hits.first
assert_nil hit["_source"]
hit = Product.search("product", select: false).hits.first
assert_nil hit["_source"]
end
def test_includes
store [{name: "Product A", user_ids: [1, 2]}]
result = Product.search("product", load: false, select: {includes: [:name]}).first
assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort
assert_equal "Product A", result.name
assert_nil result.store_id
end
def test_excludes
store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
result = Product.search("product", load: false, select: {excludes: [:name]}).first
assert_nil result.name
assert_equal [1, 2], result.user_ids
assert_equal 1, result.store_id
end
def test_include_and_excludes
# let's take this to the next level
store [{name: "Product A", user_ids: [1, 2], store_id: 1}]
result = Product.search("product", load: false, select: {includes: [:store_id], excludes: [:name]}).first
assert_equal 1, result.store_id
assert_nil result.name
assert_nil result.user_ids
end
end