forked from sunspot/sunspot
/
model_spec.rb
342 lines (281 loc) · 9.07 KB
/
model_spec.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
require File.dirname(__FILE__) + '/spec_helper'
describe 'ActiveRecord mixin' do
describe 'index()' do
before :each do
@post = Post.create!
@post.index
end
it 'should not commit the model' do
Post.search.results.should be_empty
end
it 'should index the model' do
Sunspot.commit
Post.search.results.should == [@post]
end
end
describe 'single table inheritence' do
before :each do
@post = PhotoPost.create!
end
it 'should not break auto-indexing' do
@post.title = 'Title'
lambda { @post.save! }.should_not raise_error
end
end
describe 'index!()' do
before :each do
@post = Post.create!
@post.index!
end
it 'should immediately index and commit' do
Post.search.results.should == [@post]
end
end
describe 'remove_from_index()' do
before :each do
@post = Post.create!
@post.index!
@post.remove_from_index
end
it 'should not commit immediately' do
Post.search.results.should == [@post]
end
it 'should remove the model from the index' do
Sunspot.commit
Post.search.results.should be_empty
end
end
describe 'remove_from_index!()' do
before :each do
@post = Post.create!
@post.index!
@post.remove_from_index!
end
it 'should immediately remove the model and commit' do
Post.search.results.should be_empty
end
end
describe 'remove_all_from_index' do
before :each do
@posts = Array.new(10) { Post.create! }.each { |post| Sunspot.index(post) }
Sunspot.commit
Post.remove_all_from_index
end
it 'should not commit immediately' do
Post.search.results.to_set.should == @posts.to_set
end
it 'should remove all instances from the index' do
Sunspot.commit
Post.search.results.should be_empty
end
end
describe 'remove_all_from_index!' do
before :each do
Array.new(10) { Post.create! }.each { |post| Sunspot.index(post) }
Sunspot.commit
Post.remove_all_from_index!
end
it 'should remove all instances from the index and commit immediately' do
Post.search.results.should be_empty
end
end
describe 'search()' do
before :each do
@post = Post.create!(:title => 'Test Post')
@post.index!
end
it 'should return results specified by search' do
Post.search do
with :title, 'Test Post'
end.results.should == [@post]
end
it 'should not return results excluded by search' do
Post.search do
with :title, 'Bogus Post'
end.results.should be_empty
end
it 'should find ActiveRecord objects with an integer, not a string' do
Post.should_receive(:all).with(hash_including(:conditions => { "id" => [@post.id.to_i] })).and_return([@post])
Post.search do
with :title, 'Test Post'
end.results.should == [@post]
end
it 'should use the include option on the data accessor when specified' do
Post.should_receive(:find).with(anything(), hash_including(:include => [:blog])).and_return([@post])
Post.search do
with :title, 'Test Post'
data_accessor_for(Post).include = [:blog]
end.results.should == [@post]
end
it 'should use the select option on the data accessor when specified' do
Post.should_receive(:find).with(anything(), hash_including(:select => 'title, published_at')).and_return([@post])
Post.search do
with :title, 'Test Post'
data_accessor_for(Post).select = [:title, :published_at]
end.results.should == [@post]
end
it 'should not use the select option on the data accessor when not specified' do
Post.should_receive(:find).with(anything(), hash_not_including(:select)).and_return([@post])
Post.search do
with :title, 'Test Post'
end.results.should == [@post]
end
it 'should gracefully handle nonexistent records' do
post2 = Post.create!(:title => 'Test Post')
post2.index!
post2.destroy
Post.search do
with :title, 'Test Post'
end.results.should == [@post]
end
it 'should use an ActiveRecord object for coordinates' do
post = Post.new(:title => 'Test Post')
post.location = Location.create!(:lat => 40.0, :lng => -70.0)
post.save
post.index!
Post.search { near([40.0, -70.0], :distance => 1) }.results.should == [post]
end
end
describe 'search_ids()' do
before :each do
@posts = Array.new(2) { Post.create! }.each { |post| post.index }
Sunspot.commit
end
it 'should return IDs' do
Post.search_ids.to_set.should == @posts.map { |post| post.id }.to_set
end
end
describe 'searchable?()' do
it 'should not be true for models that have not been configured for search' do
Blog.should_not be_searchable
end
it 'should be true for models that have been configured for search' do
Post.should be_searchable
end
end
describe 'index_orphans()' do
before :each do
@posts = Array.new(2) { Post.create }.each { |post| post.index }
Sunspot.commit
@posts.first.destroy
end
it 'should return IDs of objects that are in the index but not the database' do
Post.index_orphans.should == [@posts.first.id]
end
end
describe 'clean_index_orphans()' do
before :each do
@posts = Array.new(2) { Post.create }.each { |post| post.index }
Sunspot.commit
@posts.first.destroy
end
it 'should remove orphans from the index' do
Post.clean_index_orphans
Sunspot.commit
Post.search.results.should == [@posts.last]
end
end
describe 'reindex()' do
before :each do
@posts = Array.new(2) { Post.create }
end
it 'should index all instances' do
Post.reindex(:batch_size => nil)
Sunspot.commit
Post.search.results.to_set.should == @posts.to_set
end
it 'should remove all currently indexed instances' do
old_post = Post.create!
old_post.index!
old_post.destroy
Post.reindex
Sunspot.commit
Post.search.results.to_set.should == @posts.to_set
end
end
describe 'reindex() with real data' do
before :each do
@posts = Array.new(2) { Post.create }
end
it 'should index all instances' do
Post.reindex(:batch_size => nil)
Sunspot.commit
Post.search.results.to_set.should == @posts.to_set
end
it 'should remove all currently indexed instances' do
old_post = Post.create!
old_post.index!
old_post.destroy
Post.reindex
Sunspot.commit
Post.search.results.to_set.should == @posts.to_set
end
describe "using batch sizes" do
it 'should index with a specified batch size' do
Post.reindex(:batch_size => 1)
Sunspot.commit
Post.search.results.to_set.should == @posts.to_set
end
end
end
describe "reindex()" do
before(:each) do
@posts = Array.new(10) { Post.create }
end
describe "when not using batches" do
it "should select all if the batch_size is nil" do
Post.should_receive(:all).with(:include => []).and_return([])
Post.reindex(:batch_size => nil)
end
it "should search for models with includes" do
Post.should_receive(:all).with(:include => :author).and_return([])
Post.reindex(:batch_size => nil, :include => :author)
end
end
describe "when using batches" do
it "should use the default options" do
Post.should_receive(:all).with do |params|
params[:limit].should == 500
params[:include].should == []
params[:conditions].should == ['posts.id > ?', 0]
params[:order].should == 'id'
end.and_return(@posts)
Post.reindex
end
it "should set the conditions using the overridden table attributes" do
@posts = Array.new(10) { Author.create }
Author.should_receive(:all).with do |params|
params[:conditions].should == ['writers.writer_id > ?', 0]
params[:order].should == 'writer_id'
end.and_return(@posts)
Author.reindex
end
it "should count the number of records to index" do
Post.should_receive(:count).and_return(10)
Post.reindex
end
it "should override the batch_size" do
Post.should_receive(:all).with do |params|
params[:limit].should == 20
@posts
end.and_return(@posts)
Post.reindex(:batch_size => 20)
end
it "should set the include option" do
Post.should_receive(:all).with do |params|
params[:include].should == [{:author => :address}]
@posts
end.and_return(@posts)
Post.reindex(:include => [{:author => :address}])
end
it "should commit after indexing each batch" do
Sunspot.should_receive(:commit).twice
Post.reindex(:batch_size => 5)
end
it "should commit after indexing everything" do
Sunspot.should_receive(:commit).once
Post.reindex(:batch_commit => false)
end
end
end
end