helpers.rb
2.58 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class Minitest::Test
include ActiveJob::TestHelper
def setup
[Product, Store].each do |model|
setup_model(model)
end
end
protected
def setup_animal
setup_model(Animal)
end
def setup_region
setup_model(Region)
end
def setup_speaker
setup_model(Speaker)
end
def setup_model(model)
# reindex once
($setup_model ||= {})[model] ||= (model.reindex || true)
# clear every time
model.destroy_all
end
def store(documents, model = default_model, reindex: true)
if reindex
documents.shuffle.each do |document|
model.create!(document)
end
model.searchkick_index.refresh
else
Searchkick.callbacks(false) do
documents.shuffle.each do |document|
model.create!(document)
end
end
end
end
def store_names(names, model = default_model, reindex: true)
store names.map { |name| {name: name} }, model, reindex: reindex
end
# no order
def assert_search(term, expected, options = {}, model = default_model)
assert_equal expected.sort, model.search(term, **options).map(&:name).sort
end
def assert_order(term, expected, options = {}, model = default_model)
assert_equal expected, model.search(term, **options).map(&:name)
end
def assert_equal_scores(term, options = {}, model = default_model)
assert_equal 1, model.search(term, **options).hits.map { |a| a["_score"] }.uniq.size
end
def assert_first(term, expected, options = {}, model = default_model)
assert_equal expected, model.search(term, **options).map(&:name).first
end
def assert_misspellings(term, expected, misspellings = {}, model = default_model)
options = {
fields: [:name, :color],
misspellings: misspellings
}
assert_search(term, expected, options, model)
end
def assert_warns(message)
_, stderr = capture_io do
yield
end
assert_match "[searchkick] WARNING: #{message}", stderr
end
def with_options(options, model = default_model)
previous_options = model.searchkick_options.dup
begin
model.instance_variable_set(:@searchkick_index_name, nil)
model.searchkick_options.merge!(options)
model.reindex
yield
ensure
model.instance_variable_set(:@searchkick_index_name, nil)
model.searchkick_options.clear
model.searchkick_options.merge!(previous_options)
end
end
def activerecord?
defined?(ActiveRecord)
end
def mongoid?
defined?(Mongoid)
end
def default_model
Product
end
def ci?
ENV["CI"]
end
# for Active Job helpers
def tagged_logger
end
end