search_test.rb
2.44 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
require_relative "test_helper"
class SearchTest < Minitest::Test
def test_search_relation
error = assert_raises(Searchkick::Error) do
Product.all.search("*")
end
assert_equal "search must be called on model, not relation", error.message
end
def test_body
store_names ["Dollar Tree"], Store
assert_equal ["Dollar Tree"], Store.search(body: {query: {match: {name: "dollar"}}}, load: false).map(&:name)
end
def test_body_incompatible_options
assert_raises(ArgumentError) do
Store.search(body: {query: {match: {name: "dollar"}}}, where: {id: 1})
end
end
def test_block
store_names ["Dollar Tree"]
products =
Product.search "boom" do |body|
body[:query] = {match_all: {}}
end
assert_equal ["Dollar Tree"], products.map(&:name)
end
def test_missing_records
store_names ["Product A", "Product B"]
product = Product.find_by(name: "Product A")
product.delete
assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do
result = Product.search("product")
assert_equal ["Product B"], result.map(&:name)
assert_equal [product.id.to_s], result.missing_records.map { |v| v[:id] }
assert_equal [Product], result.missing_records.map { |v| v[:model] }
end
assert_empty Product.search("product", load: false).missing_records
ensure
Product.reindex
end
def test_bad_mapping
Product.search_index.delete
store_names ["Product A"]
error = assert_raises(Searchkick::InvalidQueryError) { Product.search("test").to_a }
assert_equal "Bad mapping - run Product.reindex", error.message
ensure
Product.reindex
end
def test_missing_index
assert_raises(Searchkick::MissingIndexError) { Product.search("test", index_name: "not_found").to_a }
end
def test_unsupported_version
skip if Searchkick.opensearch?
raises_exception = lambda do |*|
if defined?(Elastic::Transport)
raise Elastic::Transport::Transport::Error, "[500] No query registered for [multi_match]"
else
raise Elasticsearch::Transport::Transport::Error, "[500] No query registered for [multi_match]"
end
end
Searchkick.client.stub :search, raises_exception do
assert_raises(Searchkick::UnsupportedVersionError) { Product.search("test").to_a }
end
end
def test_invalid_body
assert_raises(Searchkick::InvalidQueryError) { Product.search(body: {boom: true}).to_a }
end
end