test_helper.rb
3.46 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
require "bundler/setup"
Bundler.require(:default)
require "minitest/autorun"
require "minitest/pride"
require "logger"
require "active_support/core_ext" if defined?(NoBrainer)
require "active_support/notifications"
ENV["RACK_ENV"] = "test"
$logger = ActiveSupport::Logger.new(ENV["VERBOSE"] ? STDOUT : nil)
Searchkick.client.transport.logger = $logger
Searchkick.search_timeout = 5
Searchkick.index_suffix = ENV["TEST_ENV_NUMBER"] # for parallel tests
# add to elasticsearch-7.0.0/config/
Searchkick.wordnet_path = "wn_s.pl" if ENV["WORDNET"]
puts "Running against Elasticsearch #{Searchkick.server_version}"
if defined?(Redis)
if defined?(ConnectionPool)
Searchkick.redis = ConnectionPool.new { Redis.new(logger: $logger) }
else
Searchkick.redis = Redis.new(logger: $logger)
end
end
I18n.config.enforce_available_locales = true
if defined?(ActiveJob)
ActiveJob::Base.logger = $logger
ActiveJob::Base.queue_adapter = :inline
end
ActiveSupport::LogSubscriber.logger = ActiveSupport::Logger.new(STDOUT) if ENV["NOTIFICATIONS"]
if defined?(Mongoid)
require_relative "support/mongoid"
elsif defined?(NoBrainer)
require_relative "support/nobrainer"
elsif defined?(Cequel)
require_relative "support/cequel"
else
require_relative "support/activerecord"
end
# models
Dir["#{__dir__}/models/*"].each do |file|
require file
end
Product.searchkick_index.delete if Product.searchkick_index.exists?
Product.reindex
Product.reindex # run twice for both index paths
Product.create!(name: "Set mapping")
Store.reindex
Animal.reindex
Speaker.reindex
Region.reindex
class Minitest::Test
def setup
Product.destroy_all
Store.destroy_all
Animal.destroy_all
Speaker.destroy_all
end
protected
def store(documents, klass = Product)
documents.shuffle.each do |document|
klass.create!(document)
end
klass.searchkick_index.refresh
end
def store_names(names, klass = Product)
store names.map { |name| {name: name} }, klass
end
# no order
def assert_search(term, expected, options = {}, klass = Product)
assert_equal expected.sort, klass.search(term, **options).map(&:name).sort
end
def assert_search_relation(expected, relation)
assert_equal expected.sort, relation.map(&:name).sort
end
def assert_order(term, expected, options = {}, klass = Product)
assert_equal expected, klass.search(term, **options).map(&:name)
end
def assert_order_relation(expected, relation)
assert_equal expected, relation.map(&:name)
end
def assert_equal_scores(term, options = {}, klass = Product)
assert_equal 1, klass.search(term, **options).hits.map { |a| a["_score"] }.uniq.size
end
def assert_first(term, expected, options = {}, klass = Product)
assert_equal expected, klass.search(term, **options).map(&:name).first
end
def assert_first_relation(expected, relation)
assert_equal expected, relation.map(&:name).first
end
def assert_misspellings(term, expected, misspellings = {}, klass = Product)
options = {
fields: [:name, :color],
misspellings: misspellings
}
assert_search(term, expected, options, klass)
end
def with_options(klass, options)
previous_options = klass.searchkick_options.dup
begin
klass.searchkick_options.merge!(options)
klass.reindex
yield
ensure
klass.searchkick_options.clear
klass.searchkick_options.merge!(previous_options)
end
end
def nobrainer?
defined?(NoBrainer)
end
def cequel?
defined?(Cequel)
end
end