Commit a5dfd1020299cb09c3f213ef516808c3a993e984
1 parent
b759b229
Exists in
master
and in
5 other branches
More test reorg [skip ci]
Showing
4 changed files
with
145 additions
and
143 deletions
Show diff stats
@@ -0,0 +1,44 @@ | @@ -0,0 +1,44 @@ | ||
1 | +require_relative "test_helper" | ||
2 | + | ||
3 | +class ExcludeTest < Minitest::Test | ||
4 | + def test_butter | ||
5 | + store_names ["Butter Tub", "Peanut Butter Tub"] | ||
6 | + assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"] | ||
7 | + end | ||
8 | + | ||
9 | + def test_butter_word_start | ||
10 | + store_names ["Butter Tub", "Peanut Butter Tub"] | ||
11 | + assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"], match: :word_start | ||
12 | + end | ||
13 | + | ||
14 | + def test_butter_exact | ||
15 | + store_names ["Butter Tub", "Peanut Butter Tub"] | ||
16 | + assert_search "butter", [], exclude: ["peanut butter"], fields: [{name: :exact}] | ||
17 | + end | ||
18 | + | ||
19 | + def test_same_exact | ||
20 | + store_names ["Butter Tub", "Peanut Butter Tub"] | ||
21 | + assert_search "Butter Tub", ["Butter Tub"], exclude: ["Peanut Butter Tub"], fields: [{name: :exact}] | ||
22 | + end | ||
23 | + | ||
24 | + def test_egg_word_start | ||
25 | + store_names ["eggs", "eggplant"] | ||
26 | + assert_search "egg", ["eggs"], exclude: ["eggplant"], match: :word_start | ||
27 | + end | ||
28 | + | ||
29 | + def test_string | ||
30 | + store_names ["Butter Tub", "Peanut Butter Tub"] | ||
31 | + assert_search "butter", ["Butter Tub"], exclude: "peanut butter" | ||
32 | + end | ||
33 | + | ||
34 | + def test_match_all | ||
35 | + store_names ["Butter"] | ||
36 | + assert_search "*", [], exclude: "butter" | ||
37 | + end | ||
38 | + | ||
39 | + def test_match_all_fields | ||
40 | + store_names ["Butter"] | ||
41 | + assert_search "*", [], fields: [:name], exclude: "butter" | ||
42 | + assert_search "*", ["Butter"], fields: [:color], exclude: "butter" | ||
43 | + end | ||
44 | +end |
test/match_test.rb
@@ -176,49 +176,6 @@ class MatchTest < Minitest::Test | @@ -176,49 +176,6 @@ class MatchTest < Minitest::Test | ||
176 | assert_search "almondmilks", ["Almond Milk"] | 176 | assert_search "almondmilks", ["Almond Milk"] |
177 | end | 177 | end |
178 | 178 | ||
179 | - # butter | ||
180 | - | ||
181 | - def test_exclude_butter | ||
182 | - store_names ["Butter Tub", "Peanut Butter Tub"] | ||
183 | - assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"] | ||
184 | - end | ||
185 | - | ||
186 | - def test_exclude_butter_word_start | ||
187 | - store_names ["Butter Tub", "Peanut Butter Tub"] | ||
188 | - assert_search "butter", ["Butter Tub"], exclude: ["peanut butter"], match: :word_start | ||
189 | - end | ||
190 | - | ||
191 | - def test_exclude_butter_exact | ||
192 | - store_names ["Butter Tub", "Peanut Butter Tub"] | ||
193 | - assert_search "butter", [], exclude: ["peanut butter"], fields: [{name: :exact}] | ||
194 | - end | ||
195 | - | ||
196 | - def test_exclude_same_exact | ||
197 | - store_names ["Butter Tub", "Peanut Butter Tub"] | ||
198 | - assert_search "Butter Tub", ["Butter Tub"], exclude: ["Peanut Butter Tub"], fields: [{name: :exact}] | ||
199 | - end | ||
200 | - | ||
201 | - def test_exclude_egg_word_start | ||
202 | - store_names ["eggs", "eggplant"] | ||
203 | - assert_search "egg", ["eggs"], exclude: ["eggplant"], match: :word_start | ||
204 | - end | ||
205 | - | ||
206 | - def test_exclude_string | ||
207 | - store_names ["Butter Tub", "Peanut Butter Tub"] | ||
208 | - assert_search "butter", ["Butter Tub"], exclude: "peanut butter" | ||
209 | - end | ||
210 | - | ||
211 | - def test_exclude_match_all | ||
212 | - store_names ["Butter"] | ||
213 | - assert_search "*", [], exclude: "butter" | ||
214 | - end | ||
215 | - | ||
216 | - def test_exclude_match_all_fields | ||
217 | - store_names ["Butter"] | ||
218 | - assert_search "*", [], fields: [:name], exclude: "butter" | ||
219 | - assert_search "*", ["Butter"], fields: [:color], exclude: "butter" | ||
220 | - end | ||
221 | - | ||
222 | # other | 179 | # other |
223 | 180 | ||
224 | def test_all | 181 | def test_all |
@@ -306,6 +263,54 @@ class MatchTest < Minitest::Test | @@ -306,6 +263,54 @@ class MatchTest < Minitest::Test | ||
306 | assert_search "🍨🍰", ["Ice Cream Cake"], emoji: true | 263 | assert_search "🍨🍰", ["Ice Cream Cake"], emoji: true |
307 | end | 264 | end |
308 | 265 | ||
266 | + # operator | ||
267 | + | ||
268 | + def test_operator | ||
269 | + store_names ["Honey"] | ||
270 | + assert_search "fresh honey", [] | ||
271 | + assert_search "fresh honey", ["Honey"], operator: "or" | ||
272 | + end | ||
273 | + | ||
274 | + def test_operator_scoring | ||
275 | + store_names ["Big Red Circle", "Big Green Circle", "Small Orange Circle"] | ||
276 | + assert_order "big red circle", ["Big Red Circle", "Big Green Circle", "Small Orange Circle"], operator: "or" | ||
277 | + end | ||
278 | + | ||
279 | + # fields | ||
280 | + | ||
281 | + def test_fields_operator | ||
282 | + store [ | ||
283 | + {name: "red", color: "red"}, | ||
284 | + {name: "blue", color: "blue"}, | ||
285 | + {name: "cyan", color: "blue green"}, | ||
286 | + {name: "magenta", color: "red blue"}, | ||
287 | + {name: "green", color: "green"} | ||
288 | + ] | ||
289 | + assert_search "red blue", ["red", "blue", "cyan", "magenta"], operator: "or", fields: ["color"] | ||
290 | + end | ||
291 | + | ||
292 | + def test_fields | ||
293 | + store [ | ||
294 | + {name: "red", color: "light blue"}, | ||
295 | + {name: "blue", color: "red fish"} | ||
296 | + ] | ||
297 | + assert_search "blue", ["red"], fields: ["color"] | ||
298 | + end | ||
299 | + | ||
300 | + def test_non_existent_field | ||
301 | + store_names ["Milk"] | ||
302 | + assert_search "milk", [], fields: ["not_here"] | ||
303 | + end | ||
304 | + | ||
305 | + def test_fields_both_match | ||
306 | + # have same score due to dismax | ||
307 | + store [ | ||
308 | + {name: "Blue A", color: "red"}, | ||
309 | + {name: "Blue B", color: "light blue"} | ||
310 | + ] | ||
311 | + assert_first "blue", "Blue B", fields: [:name, :color] | ||
312 | + end | ||
313 | + | ||
309 | # TODO find better place | 314 | # TODO find better place |
310 | 315 | ||
311 | def test_search_relation | 316 | def test_search_relation |
test/query_test.rb
@@ -33,4 +33,57 @@ class QueryTest < Minitest::Test | @@ -33,4 +33,57 @@ class QueryTest < Minitest::Test | ||
33 | end | 33 | end |
34 | refute_includes out, "Error" | 34 | refute_includes out, "Error" |
35 | end | 35 | end |
36 | + | ||
37 | + def test_big_decimal | ||
38 | + store [ | ||
39 | + {name: "Product", latitude: 80.0} | ||
40 | + ] | ||
41 | + assert_search "product", ["Product"], where: {latitude: {gt: 79}} | ||
42 | + end | ||
43 | + | ||
44 | + # body_options | ||
45 | + | ||
46 | + def test_body_options_should_merge_into_body | ||
47 | + query = Product.search("*", body_options: {min_score: 1.0}, execute: false) | ||
48 | + assert_equal 1.0, query.body[:min_score] | ||
49 | + end | ||
50 | + | ||
51 | + # nested | ||
52 | + | ||
53 | + def test_nested_search | ||
54 | + store [{name: "Product A", aisle: {"id" => 1, "name" => "Frozen"}}], Speaker | ||
55 | + assert_search "frozen", ["Product A"], {fields: ["aisle.name"]}, Speaker | ||
56 | + end | ||
57 | + | ||
58 | + # other tests | ||
59 | + | ||
60 | + def test_includes | ||
61 | + skip unless activerecord? | ||
62 | + | ||
63 | + store_names ["Product A"] | ||
64 | + assert Product.search("product", includes: [:store]).first.association(:store).loaded? | ||
65 | + end | ||
66 | + | ||
67 | + def test_model_includes | ||
68 | + skip unless activerecord? | ||
69 | + | ||
70 | + store_names ["Product A"] | ||
71 | + store_names ["Store A"], Store | ||
72 | + | ||
73 | + associations = {Product => [:store], Store => [:products]} | ||
74 | + result = Searchkick.search("*", models: [Product, Store], model_includes: associations) | ||
75 | + | ||
76 | + assert_equal 2, result.length | ||
77 | + | ||
78 | + result.group_by(&:class).each_pair do |klass, records| | ||
79 | + assert records.first.association(associations[klass].first).loaded? | ||
80 | + end | ||
81 | + end | ||
82 | + | ||
83 | + def test_scope_results | ||
84 | + skip unless activerecord? | ||
85 | + | ||
86 | + store_names ["Product A", "Product B"] | ||
87 | + assert_search "product", ["Product A"], scope_results: ->(r) { r.where(name: "Product A") } | ||
88 | + end | ||
36 | end | 89 | end |
test/search_options_test.rb
@@ -1,100 +0,0 @@ | @@ -1,100 +0,0 @@ | ||
1 | -require_relative "test_helper" | ||
2 | - | ||
3 | -class SearchOptionsTest < Minitest::Test | ||
4 | - def test_operator | ||
5 | - store_names ["Honey"] | ||
6 | - assert_search "fresh honey", [] | ||
7 | - assert_search "fresh honey", ["Honey"], operator: "or" | ||
8 | - end | ||
9 | - | ||
10 | - def test_operator_scoring | ||
11 | - store_names ["Big Red Circle", "Big Green Circle", "Small Orange Circle"] | ||
12 | - assert_order "big red circle", ["Big Red Circle", "Big Green Circle", "Small Orange Circle"], operator: "or" | ||
13 | - end | ||
14 | - | ||
15 | - def test_fields_operator | ||
16 | - store [ | ||
17 | - {name: "red", color: "red"}, | ||
18 | - {name: "blue", color: "blue"}, | ||
19 | - {name: "cyan", color: "blue green"}, | ||
20 | - {name: "magenta", color: "red blue"}, | ||
21 | - {name: "green", color: "green"} | ||
22 | - ] | ||
23 | - assert_search "red blue", ["red", "blue", "cyan", "magenta"], operator: "or", fields: ["color"] | ||
24 | - end | ||
25 | - | ||
26 | - def test_fields | ||
27 | - store [ | ||
28 | - {name: "red", color: "light blue"}, | ||
29 | - {name: "blue", color: "red fish"} | ||
30 | - ] | ||
31 | - assert_search "blue", ["red"], fields: ["color"] | ||
32 | - end | ||
33 | - | ||
34 | - def test_non_existent_field | ||
35 | - store_names ["Milk"] | ||
36 | - assert_search "milk", [], fields: ["not_here"] | ||
37 | - end | ||
38 | - | ||
39 | - def test_fields_both_match | ||
40 | - # have same score due to dismax | ||
41 | - store [ | ||
42 | - {name: "Blue A", color: "red"}, | ||
43 | - {name: "Blue B", color: "light blue"} | ||
44 | - ] | ||
45 | - assert_first "blue", "Blue B", fields: [:name, :color] | ||
46 | - end | ||
47 | - | ||
48 | - def test_big_decimal | ||
49 | - store [ | ||
50 | - {name: "Product", latitude: 80.0} | ||
51 | - ] | ||
52 | - assert_search "product", ["Product"], where: {latitude: {gt: 79}} | ||
53 | - end | ||
54 | - | ||
55 | - # body_options | ||
56 | - | ||
57 | - def test_body_options_should_merge_into_body | ||
58 | - query = Product.search("*", body_options: {min_score: 1.0}, execute: false) | ||
59 | - assert_equal 1.0, query.body[:min_score] | ||
60 | - end | ||
61 | - | ||
62 | - # nested | ||
63 | - | ||
64 | - def test_nested_search | ||
65 | - store [{name: "Product A", aisle: {"id" => 1, "name" => "Frozen"}}], Speaker | ||
66 | - assert_search "frozen", ["Product A"], {fields: ["aisle.name"]}, Speaker | ||
67 | - end | ||
68 | - | ||
69 | - # other tests | ||
70 | - | ||
71 | - def test_includes | ||
72 | - skip unless activerecord? | ||
73 | - | ||
74 | - store_names ["Product A"] | ||
75 | - assert Product.search("product", includes: [:store]).first.association(:store).loaded? | ||
76 | - end | ||
77 | - | ||
78 | - def test_model_includes | ||
79 | - skip unless activerecord? | ||
80 | - | ||
81 | - store_names ["Product A"] | ||
82 | - store_names ["Store A"], Store | ||
83 | - | ||
84 | - associations = {Product => [:store], Store => [:products]} | ||
85 | - result = Searchkick.search("*", models: [Product, Store], model_includes: associations) | ||
86 | - | ||
87 | - assert_equal 2, result.length | ||
88 | - | ||
89 | - result.group_by(&:class).each_pair do |klass, records| | ||
90 | - assert records.first.association(associations[klass].first).loaded? | ||
91 | - end | ||
92 | - end | ||
93 | - | ||
94 | - def test_scope_results | ||
95 | - skip unless activerecord? | ||
96 | - | ||
97 | - store_names ["Product A", "Product B"] | ||
98 | - assert_search "product", ["Product A"], scope_results: ->(r) { r.where(name: "Product A") } | ||
99 | - end | ||
100 | -end |