test_helper.rb
6.02 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
require "bundler/setup"
Bundler.require(:default)
require "minitest/autorun"
require "minitest/pride"
require "logger"
ENV["RACK_ENV"] = "test"
Minitest::Test = Minitest::Unit::TestCase unless defined?(Minitest::Test)
File.delete("elasticsearch.log") if File.exist?("elasticsearch.log")
Searchkick.client.transport.logger = Logger.new("elasticsearch.log")
puts "Running against Elasticsearch #{Searchkick.server_version}"
I18n.config.enforce_available_locales = true
ActiveJob::Base.logger = nil if defined?(ActiveJob)
if defined?(Mongoid)
def mongoid2?
Mongoid::VERSION.starts_with?("2.")
end
if mongoid2?
# enable comparison of BSON::ObjectIds
module BSON
class ObjectId
def <=>(other)
data <=> other.data
end
end
end
end
Mongoid.configure do |config|
if mongoid2?
config.master = Mongo::Connection.new.db("searchkick_test")
else
config.connect_to "searchkick_test"
end
end
class Product
include Mongoid::Document
include Mongoid::Timestamps
field :name
field :store_id, type: Integer
field :in_stock, type: Boolean
field :backordered, type: Boolean
field :orders_count, type: Integer
field :found_rate, type: BigDecimal
field :price, type: Integer
field :color
field :latitude, type: BigDecimal
field :longitude, type: BigDecimal
field :description
end
class Store
include Mongoid::Document
field :name
end
class Animal
include Mongoid::Document
field :name
end
class Dog < Animal
end
class Cat < Animal
end
elsif defined?(NoBrainer)
NoBrainer.configure do |config|
config.app_name = :searchkick
config.environment = :test
end
class Product
include NoBrainer::Document
include NoBrainer::Document::Timestamps
field :id, type: Object
field :name, type: String
field :in_stock, type: Boolean
field :backordered, type: Boolean
field :orders_count, type: Integer
field :found_rate
field :price, type: Integer
field :color, type: String
field :latitude
field :longitude
field :description, type: String
belongs_to :store, validates: false
end
class Store
include NoBrainer::Document
field :id, type: Object
field :name, type: String
end
class Animal
include NoBrainer::Document
field :id, type: Object
field :name, type: String
end
class Dog < Animal
end
class Cat < Animal
end
else
require "active_record"
# for debugging
# ActiveRecord::Base.logger = Logger.new(STDOUT)
# rails does this in activerecord/lib/active_record/railtie.rb
ActiveRecord::Base.default_timezone = :utc
ActiveRecord::Base.time_zone_aware_attributes = true
# migrations
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: ":memory:"
ActiveRecord::Base.raise_in_transactional_callbacks = true if ActiveRecord::Base.respond_to?(:raise_in_transactional_callbacks=)
ActiveRecord::Migration.create_table :products do |t|
t.string :name
t.integer :store_id
t.boolean :in_stock
t.boolean :backordered
t.integer :orders_count
t.decimal :found_rate
t.integer :price
t.string :color
t.decimal :latitude, precision: 10, scale: 7
t.decimal :longitude, precision: 10, scale: 7
t.text :description
t.timestamps null: true
end
ActiveRecord::Migration.create_table :stores do |t|
t.string :name
end
ActiveRecord::Migration.create_table :animals do |t|
t.string :name
t.string :type
end
class Product < ActiveRecord::Base
end
class Store < ActiveRecord::Base
end
class Animal < ActiveRecord::Base
end
class Dog < Animal
end
class Cat < Animal
end
end
class Product
belongs_to :store
searchkick \
synonyms: [
["clorox", "bleach"],
["scallion", "greenonion"],
["saranwrap", "plasticwrap"],
["qtip", "cottonswab"],
["burger", "hamburger"],
["bandaid", "bandag"]
],
autocomplete: [:name],
suggest: [:name, :color],
conversions: "conversions",
personalize: "user_ids",
locations: ["location", "multiple_locations"],
text_start: [:name],
text_middle: [:name],
text_end: [:name],
word_start: [:name],
word_middle: [:name],
word_end: [:name],
highlight: [:name],
unsearchable: [:description]
attr_accessor :conversions, :user_ids, :aisle
def search_data
serializable_hash.except("id").merge(
conversions: conversions,
user_ids: user_ids,
location: [latitude, longitude],
multiple_locations: [[latitude, longitude], [0, 0]],
aisle: aisle
)
end
def should_index?
name != "DO NOT INDEX"
end
end
class Store
searchkick \
routing: :name,
merge_mappings: true,
mappings: {
store: {
properties: {
name: {type: "string", analyzer: "keyword"},
}
}
}
end
class Animal
searchkick \
autocomplete: [:name],
suggest: [:name],
index_name: -> { "#{name.tableize}-#{Date.today.year}" }
# wordnet: true
end
Product.searchkick_index.delete if Product.searchkick_index.exists?
Product.reindex
Product.reindex # run twice for both index paths
Store.reindex
Animal.reindex
class Minitest::Test
def setup
Product.destroy_all
Store.destroy_all
Animal.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_order(term, expected, options = {}, klass = Product)
assert_equal expected, klass.search(term, options).map(&:name)
end
def assert_first(term, expected, options = {}, klass = Product)
assert_equal expected, klass.search(term, options).map(&:name).first
end
end