Commit cc6cde78ca4a58672ec573ed04cf077a3bc70402
1 parent
333e6fff
Exists in
relation
and in
1 other branch
Test select relation
Showing
2 changed files
with
65 additions
and
4 deletions
Show diff stats
lib/searchkick/relation.rb
@@ -77,8 +77,9 @@ module Searchkick | @@ -77,8 +77,9 @@ module Searchkick | ||
77 | end | 77 | end |
78 | 78 | ||
79 | # TODO decide how to handle block form | 79 | # TODO decide how to handle block form |
80 | + # TODO see how Active Record merges multiple calls | ||
80 | def select!(*fields) | 81 | def select!(*fields) |
81 | - options[:select] = Array(options[:select]) + fields | 82 | + options[:select] = fields.size == 1 ? fields.first : fields |
82 | self | 83 | self |
83 | end | 84 | end |
84 | 85 |
test/sql_test.rb
@@ -97,9 +97,7 @@ class SqlTest < Minitest::Test | @@ -97,9 +97,7 @@ class SqlTest < Minitest::Test | ||
97 | def test_select | 97 | def test_select |
98 | store [{name: "Product A", store_id: 1}] | 98 | store [{name: "Product A", store_id: 1}] |
99 | result = Product.search("product", load: false, select: [:name, :store_id]).first | 99 | result = Product.search("product", load: false, select: [:name, :store_id]).first |
100 | - expected = %w(id name store_id) | ||
101 | - assert_equal expected, result.keys.reject { |k| k.start_with?("_") }.sort | ||
102 | - assert_equal expected, Product.search("product", relation: true, load: false).select(:name, :store_id).first.keys.reject { |k| k.start_with?("_") }.sort | 100 | + assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort |
103 | assert_equal "Product A", result.name | 101 | assert_equal "Product A", result.name |
104 | assert_equal 1, result.store_id | 102 | assert_equal 1, result.store_id |
105 | end | 103 | end |
@@ -158,6 +156,68 @@ class SqlTest < Minitest::Test | @@ -158,6 +156,68 @@ class SqlTest < Minitest::Test | ||
158 | assert_nil result.user_ids | 156 | assert_nil result.user_ids |
159 | end | 157 | end |
160 | 158 | ||
159 | + # select relation | ||
160 | + | ||
161 | + def test_select_relation | ||
162 | + store [{name: "Product A", store_id: 1}] | ||
163 | + result = Product.search("product", relation: true).load(false).select(:name, :store_id).first | ||
164 | + assert_equal %w(id name store_id), result.keys.reject { |k| k.start_with?("_") }.sort | ||
165 | + assert_equal "Product A", result.name | ||
166 | + assert_equal 1, result.store_id | ||
167 | + end | ||
168 | + | ||
169 | + def test_select_array_relation | ||
170 | + store [{name: "Product A", user_ids: [1, 2]}] | ||
171 | + result = Product.search("product", relation: true).load(false).select(:user_ids).first | ||
172 | + assert_equal [1, 2], result.user_ids | ||
173 | + end | ||
174 | + | ||
175 | + def test_select_single_field_relation | ||
176 | + store [{name: "Product A", store_id: 1}] | ||
177 | + result = Product.search("product", relation: true).load(false).select(:name).first | ||
178 | + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort | ||
179 | + assert_equal "Product A", result.name | ||
180 | + assert_nil result.store_id | ||
181 | + end | ||
182 | + | ||
183 | + def test_select_all_relation | ||
184 | + store [{name: "Product A", user_ids: [1, 2]}] | ||
185 | + hit = Product.search("product", relation: true).select(true).hits.first | ||
186 | + assert_equal hit["_source"]["name"], "Product A" | ||
187 | + assert_equal hit["_source"]["user_ids"], [1, 2] | ||
188 | + end | ||
189 | + | ||
190 | + def test_select_none_relation | ||
191 | + store [{name: "Product A", user_ids: [1, 2]}] | ||
192 | + hit = Product.search("product", relation: true).select(false).hits.first | ||
193 | + assert_nil hit["_source"] | ||
194 | + end | ||
195 | + | ||
196 | + def test_select_includes_relation | ||
197 | + store [{name: "Product A", user_ids: [1, 2]}] | ||
198 | + result = Product.search("product", relation: true).load(false).select(includes: [:name]).first | ||
199 | + assert_equal %w(id name), result.keys.reject { |k| k.start_with?("_") }.sort | ||
200 | + assert_equal "Product A", result.name | ||
201 | + assert_nil result.store_id | ||
202 | + end | ||
203 | + | ||
204 | + def test_select_excludes_relation | ||
205 | + store [{name: "Product A", user_ids: [1, 2], store_id: 1}] | ||
206 | + result = Product.search("product", relation: true).load(false).select(excludes: [:name]).first | ||
207 | + assert_nil result.name | ||
208 | + assert_equal [1, 2], result.user_ids | ||
209 | + assert_equal 1, result.store_id | ||
210 | + end | ||
211 | + | ||
212 | + def test_select_include_and_excludes_relation | ||
213 | + # let's take this to the next level | ||
214 | + store [{name: "Product A", user_ids: [1, 2], store_id: 1}] | ||
215 | + result = Product.search("product", relation: true).load(false).select(includes: [:store_id], excludes: [:name]).first | ||
216 | + assert_equal 1, result.store_id | ||
217 | + assert_nil result.name | ||
218 | + assert_nil result.user_ids | ||
219 | + end | ||
220 | + | ||
161 | # nested | 221 | # nested |
162 | 222 | ||
163 | def test_nested_search | 223 | def test_nested_search |