Commit b2e5050cfa3ebc68e6051e7058cd65469a7532af
1 parent
cc6cde78
Exists in
relation
and in
1 other branch
Test operations
Showing
3 changed files
with
20 additions
and
2 deletions
Show diff stats
lib/searchkick/relation.rb
... | ... | @@ -163,6 +163,15 @@ module Searchkick |
163 | 163 | self |
164 | 164 | end |
165 | 165 | |
166 | + def operator(value) | |
167 | + spawn.operator!(value) | |
168 | + end | |
169 | + | |
170 | + def operator!(value) | |
171 | + options[:operator] = value | |
172 | + self | |
173 | + end | |
174 | + | |
166 | 175 | # same as Active Record |
167 | 176 | def inspect |
168 | 177 | entries = results.first(11).map!(&:inspect) | ... | ... |
test/sql_test.rb
... | ... | @@ -5,11 +5,14 @@ class SqlTest < Minitest::Test |
5 | 5 | store_names ["Honey"] |
6 | 6 | assert_search "fresh honey", [] |
7 | 7 | assert_search "fresh honey", ["Honey"], operator: "or" |
8 | + assert_search_relation ["Honey"], Product.search("fresh honey", relation: true).operator(:or) | |
8 | 9 | end |
9 | 10 | |
10 | 11 | def test_operator_scoring |
11 | 12 | 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 | + expected = ["Big Red Circle", "Big Green Circle", "Small Orange Circle"] | |
14 | + assert_order "big red circle", expected, operator: "or" | |
15 | + assert_search_relation expected, Product.search("big red circle", relation: true).operator(:or) | |
13 | 16 | end |
14 | 17 | |
15 | 18 | def test_fields_operator |
... | ... | @@ -20,7 +23,9 @@ class SqlTest < Minitest::Test |
20 | 23 | {name: "magenta", color: "red blue"}, |
21 | 24 | {name: "green", color: "green"} |
22 | 25 | ] |
23 | - assert_search "red blue", ["red", "blue", "cyan", "magenta"], operator: "or", fields: ["color"] | |
26 | + expected = ["red", "blue", "cyan", "magenta"] | |
27 | + assert_search "red blue", expected, operator: "or", fields: ["color"] | |
28 | + assert_search_relation expected, Product.search("red blue", relation: true).operator(:or).fields(:color) | |
24 | 29 | end |
25 | 30 | |
26 | 31 | def test_fields | ... | ... |
test/test_helper.rb
... | ... | @@ -87,6 +87,10 @@ class Minitest::Test |
87 | 87 | assert_equal expected.sort, klass.search(term, **options).map(&:name).sort |
88 | 88 | end |
89 | 89 | |
90 | + def assert_search_relation(expected, relation) | |
91 | + assert_equal expected.sort, relation.map(&:name).sort | |
92 | + end | |
93 | + | |
90 | 94 | def assert_order(term, expected, options = {}, klass = Product) |
91 | 95 | assert_equal expected, klass.search(term, **options).map(&:name) |
92 | 96 | end | ... | ... |