multi_indices_test.rb
2.23 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
require_relative "test_helper"
class MultiIndicesTest < Minitest::Test
def setup
super
setup_speaker
end
def test_basic
store_names ["Product A"]
store_names ["Product B"], Speaker
assert_search_multi "product", ["Product A", "Product B"]
end
def test_index_name
store_names ["Product A"]
assert_equal ["Product A"], Product.search("product", index_name: Product.searchkick_index.name).map(&:name)
assert_equal ["Product A"], Product.search("product", index_name: Product).map(&:name)
Speaker.search_index.refresh
assert_equal [], Product.search("product", index_name: Speaker.searchkick_index.name, conversions: false).map(&:name)
end
def test_models_and_index_name
store_names ["Product A"]
store_names ["Product B"], Speaker
assert_equal ["Product A"], Searchkick.search("product", models: [Product, Store], index_name: Product.searchkick_index.name).map(&:name)
error = assert_raises(Searchkick::Error) do
Searchkick.search("product", models: [Product, Store], index_name: Speaker.searchkick_index.name).map(&:name)
end
assert_includes error.message, "Unknown model"
# legacy
assert_equal ["Product A"], Searchkick.search("product", index_name: [Product, Store]).map(&:name)
end
def test_model_with_another_model
error = assert_raises(ArgumentError) do
Product.search(models: [Store])
end
assert_includes error.message, "Use Searchkick.search"
end
def test_model_with_another_model_in_index_name
error = assert_raises(ArgumentError) do
# legacy protection
Product.search(index_name: [Store, "another"])
end
assert_includes error.message, "Use Searchkick.search"
end
def test_no_models_or_index_name
store_names ["Product A"]
error = assert_raises(Searchkick::Error) do
Searchkick.search("product").results
end
assert_includes error.message, "Unknown model"
end
def test_no_models_or_index_name_load_false
store_names ["Product A"]
Searchkick.search("product", load: false).results
end
private
def assert_search_multi(term, expected, options = {})
options[:models] = [Product, Speaker]
options[:fields] = [:name]
assert_search(term, expected, options, Searchkick)
end
end