This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 534
/
active_record_searchable_test.rb
414 lines (324 loc) · 13.9 KB
/
active_record_searchable_test.rb
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
require 'test_helper'
module Tire
class ActiveRecordSearchableIntegrationTest < Test::Unit::TestCase
include Test::Integration
def setup
super
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
ActiveRecord::Migration.verbose = false
ActiveRecord::Schema.define(:version => 1) do
create_table :active_record_articles do |t|
t.string :title
t.datetime :created_at, :default => 'NOW()'
end
create_table :active_record_comments do |t|
t.string :author
t.text :body
t.references :article
t.timestamps
end
create_table :active_record_stats do |t|
t.integer :pageviews
t.string :period
t.references :article
end
create_table :active_record_class_with_tire_methods do |t|
t.string :title
end
create_table :active_record_class_with_dynamic_index_names do |t|
t.string :title
end
end
end
context "ActiveRecord integration" do
setup do
ActiveRecordArticle.destroy_all
Tire.index('active_record_articles').delete
load File.expand_path('../../models/active_record_models.rb', __FILE__)
end
teardown do
ActiveRecordArticle.destroy_all
Tire.index('active_record_articles').delete
end
should "configure mapping" do
assert_equal 'snowball', ActiveRecordArticle.mapping[:title][:analyzer]
assert_equal 10, ActiveRecordArticle.mapping[:title][:boost]
assert_equal 'snowball', ActiveRecordArticle.index.mapping['active_record_article']['properties']['title']['analyzer']
end
should "save document into index on save and find it" do
a = ActiveRecordArticle.new :title => 'Test'
a.save!
id = a.id
a.index.refresh
results = ActiveRecordArticle.search 'test'
assert results.any?
assert_equal 1, results.count
assert_instance_of Results::Item, results.first
assert_not_nil results.first.id
assert_equal id.to_s, results.first.id.to_s
assert results.first.persisted?, "Record should be persisted"
assert_not_nil results.first._score
assert_equal 'Test', results.first.title
end
should "raise exception on invalid query" do
ActiveRecordArticle.create! :title => 'Test'
assert_raise Search::SearchRequestFailed do
ActiveRecordArticle.search '[x'
end
end
context "with eager loading" do
setup do
ActiveRecordArticle.destroy_all
5.times { |n| ActiveRecordArticle.create! :title => "Test #{n+1}" }
ActiveRecordArticle.index.refresh
end
should "load records on query search" do
results = ActiveRecordArticle.search '"Test 1"', :load => true
assert results.any?
assert_equal ActiveRecordArticle.find(1), results.first
end
should "load records on block search" do
results = ActiveRecordArticle.search :load => true do
query { string '"Test 1"' }
end
assert_equal ActiveRecordArticle.find(1), results.first
end
should "load records with options on query search" do
assert_equal ActiveRecordArticle.find(['1'], :include => 'comments').first,
ActiveRecordArticle.search('"Test 1"',
:load => { :include => 'comments' }).results.first
end
should "return empty collection for nonmatching query" do
assert_nothing_raised do
results = ActiveRecordArticle.search :load => true do
query { string '"Hic Sunt Leones"' }
end
assert_equal 0, results.size
assert ! results.any?
end
end
end
should "remove document from index on destroy" do
a = ActiveRecordArticle.new :title => 'Test remove...'
a.save!
assert_equal 1, ActiveRecordArticle.count
a.destroy
assert_equal 0, ActiveRecordArticle.all.size
a.index.refresh
results = ActiveRecordArticle.search 'test'
assert_equal 0, results.count
end
should "return documents with scores" do
ActiveRecordArticle.create! :title => 'foo'
ActiveRecordArticle.create! :title => 'bar'
ActiveRecordArticle.index.refresh
results = ActiveRecordArticle.search 'foo OR bar^100'
assert_equal 2, results.count
assert_equal 'bar', results.first.title
end
context "with pagination" do
setup do
1.upto(9) { |number| ActiveRecordArticle.create :title => "Test#{number}" }
ActiveRecordArticle.index.refresh
end
context "and parameter searches" do
should "find first page with five results" do
results = ActiveRecordArticle.search 'test*', :sort => 'title', :per_page => 5, :page => 1
assert_equal 5, results.size
# WillPaginate
#
assert_equal 2, results.total_pages
assert_equal 1, results.current_page
assert_equal nil, results.previous_page
assert_equal 2, results.next_page
# Kaminari
#
assert_equal 5, results.limit_value
assert_equal 9, results.total_count
assert_equal 2, results.num_pages
assert_equal 0, results.offset_value
assert_equal 'Test1', results.first.title
end
should "find next page with five results" do
results = ActiveRecordArticle.search 'test*', :sort => 'title', :per_page => 5, :page => 2
assert_equal 4, results.size
assert_equal 2, results.total_pages
assert_equal 2, results.current_page
assert_equal 1, results.previous_page
assert_equal nil, results.next_page
#kaminari
assert_equal 5, results.limit_value
assert_equal 9, results.total_count
assert_equal 2, results.num_pages
assert_equal 5, results.offset_value
assert_equal 'Test6', results.first.title
end
should "find not find missing page" do
results = ActiveRecordArticle.search 'test*', :sort => 'title', :per_page => 5, :page => 3
assert_equal 0, results.size
assert_equal 2, results.total_pages
assert_equal 3, results.current_page
assert_equal 2, results.previous_page
assert_equal nil, results.next_page
#kaminari
assert_equal 5, results.limit_value
assert_equal 9, results.total_count
assert_equal 2, results.num_pages
assert_equal 10, results.offset_value
assert_nil results.first
end
end
context "and block searches" do
setup { @q = 'test*' }
should "find first page with five results" do
results = ActiveRecordArticle.search do |search|
search.query { |query| query.string @q }
search.sort { by :title }
search.from 0
search.size 5
end
assert_equal 5, results.size
assert_equal 2, results.total_pages
assert_equal 1, results.current_page
assert_equal nil, results.previous_page
assert_equal 2, results.next_page
assert_equal 'Test1', results.first.title
end
should "find next page with five results" do
results = ActiveRecordArticle.search do |search|
search.query { |query| query.string @q }
search.sort { by :title }
search.from 5
search.size 5
end
assert_equal 4, results.size
assert_equal 2, results.total_pages
assert_equal 2, results.current_page
assert_equal 1, results.previous_page
assert_equal nil, results.next_page
assert_equal 'Test6', results.first.title
end
should "not find a missing page" do
results = ActiveRecordArticle.search do |search|
search.query { |query| query.string @q }
search.sort { by :title }
search.from 10
search.size 5
end
assert_equal 0, results.size
assert_equal 2, results.total_pages
assert_equal 3, results.current_page
assert_equal 2, results.previous_page
assert_equal nil, results.next_page
assert_nil results.first
end
end
end
context "with proxy" do
should "allow access to Tire instance methods" do
a = ActiveRecordClassWithTireMethods.create :title => 'One'
assert_equal "THIS IS MY INDEX!", a.index
assert_instance_of Tire::Index, a.tire.index
assert a.tire.index.exists?, "Index should exist"
end
should "allow access to Tire class methods" do
class ::ActiveRecordClassWithTireMethods < ActiveRecord::Base
def self.search(*)
"THIS IS MY SEARCH!"
end
end
ActiveRecordClassWithTireMethods.create :title => 'One'
ActiveRecordClassWithTireMethods.tire.index.refresh
assert_equal "THIS IS MY SEARCH!", ActiveRecordClassWithTireMethods.search
results = ActiveRecordClassWithTireMethods.tire.search 'one'
assert_equal 'One', results.first.title
end
end
context "with dynamic index name" do
setup do
@a = ActiveRecordClassWithDynamicIndexName.create! :title => 'Test'
@a.index.refresh
end
should "search in proper index" do
assert_equal 'dynamic_index', ActiveRecordClassWithDynamicIndexName.index.name
assert_equal 'dynamic_index', @a.index.name
results = ActiveRecordClassWithDynamicIndexName.search 'test'
assert_equal 'dynamic_index', results.first._index
end
end
context "within Rails" do
setup do
module ::Rails; end
a = ActiveRecordArticle.new :title => 'Test'
a.comments.build :author => 'fool', :body => 'Works!'
a.stats.build :pageviews => 12, :period => '2011-08'
a.save!
@id = a.id.to_s
a.index.refresh
@item = ActiveRecordArticle.search('test').first
end
should "have access to indexed properties" do
assert_equal 'Test', @item.title
assert_equal 'fool', @item.comments.first.author
assert_equal 12, @item.stats.first.pageviews
end
should "load the underlying models" do
assert_instance_of Results::Item, @item
assert_instance_of ActiveRecordArticle, @item.load
assert_equal 'Test', @item.load.title
assert_instance_of Results::Item, @item.comments.first
assert_instance_of ActiveRecordComment, @item.comments.first.load
assert_equal 'fool', @item.comments.first.load.author
end
should "load the underlying model with options" do
ActiveRecordArticle.expects(:find).with(@id, :include => 'comments')
@item.load(:include => 'comments')
end
end
context "with multiple class instances in one index" do
setup do
ActiveRecord::Schema.define do
create_table(:active_record_assets) { |t| t.string :title, :timestamp }
create_table(:active_record_model_one) { |t| t.string :title, :timestamp }
create_table(:active_record_model_two) { |t| t.string :title, :timestamp }
end
ActiveRecordModelOne.create :title => 'Title One', timestamp: Time.now.to_i
ActiveRecordModelTwo.create :title => 'Title Two', timestamp: Time.now.to_i
ActiveRecordModelOne.tire.index.refresh
ActiveRecordModelTwo.tire.index.refresh
ActiveRecordVideo.create! :title => 'Title One', timestamp: Time.now.to_i
ActiveRecordPhoto.create! :title => 'Title Two', timestamp: Time.now.to_i
ActiveRecordAsset.tire.index.refresh
end
teardown do
ActiveRecordModelOne.destroy_all
ActiveRecordModelTwo.destroy_all
ActiveRecordModelOne.tire.index.delete
ActiveRecordModelTwo.tire.index.delete
ActiveRecordAsset.destroy_all
ActiveRecordAsset.tire.index.delete
ActiveRecordModelOne.destroy_all
end
should "eagerly load instances of multiple classes, from multiple indices" do
s = Tire.search ['active_record_model_one', 'active_record_model_two'], :load => true do
query { string 'title' }
sort { by :timestamp }
end
# puts s.results[0].inspect
assert_equal 2, s.results.length
assert_instance_of ActiveRecordModelOne, s.results[0]
assert_instance_of ActiveRecordModelTwo, s.results[1]
end
should "eagerly load all STI descendant records" do
s = Tire.search('active_record_assets', :load => true) do
query { string 'title' }
sort { by :timestamp }
end
assert_equal 2, s.results.length
assert_instance_of ActiveRecordVideo, s.results[0]
assert_instance_of ActiveRecordPhoto, s.results[1]
end
end
end
end
end