thinking-sphinx 1.5.0 → 2.0.0.rc1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. data/README.textile +15 -48
  2. data/VERSION +1 -0
  3. data/features/attribute_transformation.feature +7 -7
  4. data/features/attribute_updates.feature +16 -18
  5. data/features/deleting_instances.feature +13 -16
  6. data/features/excerpts.feature +0 -8
  7. data/features/facets.feature +19 -25
  8. data/features/handling_edits.feature +20 -25
  9. data/features/searching_across_models.feature +1 -1
  10. data/features/searching_by_index.feature +5 -6
  11. data/features/searching_by_model.feature +29 -29
  12. data/features/sphinx_scopes.feature +0 -26
  13. data/features/step_definitions/common_steps.rb +6 -18
  14. data/features/step_definitions/scope_steps.rb +0 -4
  15. data/features/step_definitions/search_steps.rb +4 -9
  16. data/features/support/env.rb +10 -3
  17. data/features/thinking_sphinx/db/fixtures/alphas.rb +10 -8
  18. data/features/thinking_sphinx/db/fixtures/cats.rb +1 -1
  19. data/features/thinking_sphinx/db/fixtures/dogs.rb +1 -1
  20. data/features/thinking_sphinx/db/fixtures/foxes.rb +1 -1
  21. data/features/thinking_sphinx/db/fixtures/people.rb +1 -1
  22. data/features/thinking_sphinx/db/fixtures/posts.rb +1 -5
  23. data/features/thinking_sphinx/db/migrations/create_posts.rb +0 -1
  24. data/features/thinking_sphinx/models/alpha.rb +0 -1
  25. data/features/thinking_sphinx/models/beta.rb +0 -5
  26. data/features/thinking_sphinx/models/developer.rb +1 -6
  27. data/features/thinking_sphinx/models/music.rb +1 -3
  28. data/features/thinking_sphinx/models/person.rb +1 -2
  29. data/features/thinking_sphinx/models/post.rb +0 -1
  30. data/lib/cucumber/thinking_sphinx/external_world.rb +4 -8
  31. data/lib/cucumber/thinking_sphinx/internal_world.rb +27 -36
  32. data/lib/thinking_sphinx.rb +60 -132
  33. data/lib/thinking_sphinx/active_record.rb +98 -124
  34. data/lib/thinking_sphinx/active_record/attribute_updates.rb +13 -17
  35. data/lib/thinking_sphinx/active_record/delta.rb +15 -21
  36. data/lib/thinking_sphinx/active_record/has_many_association.rb +23 -16
  37. data/lib/thinking_sphinx/active_record/scopes.rb +0 -18
  38. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +15 -63
  39. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +0 -4
  40. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +24 -65
  41. data/lib/thinking_sphinx/association.rb +11 -36
  42. data/lib/thinking_sphinx/attribute.rb +85 -92
  43. data/lib/thinking_sphinx/auto_version.rb +3 -21
  44. data/lib/thinking_sphinx/class_facet.rb +3 -8
  45. data/lib/thinking_sphinx/configuration.rb +58 -114
  46. data/lib/thinking_sphinx/context.rb +20 -22
  47. data/lib/thinking_sphinx/core/array.rb +13 -0
  48. data/lib/thinking_sphinx/deltas.rb +0 -2
  49. data/lib/thinking_sphinx/deltas/default_delta.rb +22 -18
  50. data/lib/thinking_sphinx/deploy/capistrano.rb +31 -30
  51. data/lib/thinking_sphinx/excerpter.rb +1 -2
  52. data/lib/thinking_sphinx/facet.rb +35 -45
  53. data/lib/thinking_sphinx/facet_search.rb +24 -58
  54. data/lib/thinking_sphinx/field.rb +0 -18
  55. data/lib/thinking_sphinx/index.rb +36 -38
  56. data/lib/thinking_sphinx/index/builder.rb +59 -74
  57. data/lib/thinking_sphinx/property.rb +45 -66
  58. data/lib/thinking_sphinx/railtie.rb +35 -0
  59. data/lib/thinking_sphinx/search.rb +250 -506
  60. data/lib/thinking_sphinx/source.rb +31 -50
  61. data/lib/thinking_sphinx/source/internal_properties.rb +3 -8
  62. data/lib/thinking_sphinx/source/sql.rb +31 -71
  63. data/lib/thinking_sphinx/tasks.rb +27 -48
  64. data/spec/thinking_sphinx/active_record/delta_spec.rb +41 -36
  65. data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +0 -96
  66. data/spec/thinking_sphinx/active_record/scopes_spec.rb +29 -29
  67. data/spec/thinking_sphinx/active_record_spec.rb +169 -140
  68. data/spec/thinking_sphinx/association_spec.rb +2 -20
  69. data/spec/thinking_sphinx/attribute_spec.rb +97 -101
  70. data/spec/thinking_sphinx/auto_version_spec.rb +11 -75
  71. data/spec/thinking_sphinx/configuration_spec.rb +62 -63
  72. data/spec/thinking_sphinx/context_spec.rb +66 -66
  73. data/spec/thinking_sphinx/facet_search_spec.rb +99 -99
  74. data/spec/thinking_sphinx/facet_spec.rb +4 -30
  75. data/spec/thinking_sphinx/field_spec.rb +3 -17
  76. data/spec/thinking_sphinx/index/builder_spec.rb +132 -169
  77. data/spec/thinking_sphinx/index_spec.rb +39 -45
  78. data/spec/thinking_sphinx/search_methods_spec.rb +33 -37
  79. data/spec/thinking_sphinx/search_spec.rb +269 -491
  80. data/spec/thinking_sphinx/source_spec.rb +48 -62
  81. data/spec/thinking_sphinx_spec.rb +49 -49
  82. data/tasks/distribution.rb +46 -0
  83. data/tasks/testing.rb +74 -0
  84. metadata +123 -199
  85. data/features/field_sorting.feature +0 -18
  86. data/features/thinking_sphinx/db/.gitignore +0 -1
  87. data/features/thinking_sphinx/db/fixtures/post_keywords.txt +0 -1
  88. data/features/thinking_sphinx/models/andrew.rb +0 -17
  89. data/lib/thinking-sphinx.rb +0 -1
  90. data/lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb +0 -21
  91. data/lib/thinking_sphinx/bundled_search.rb +0 -40
  92. data/lib/thinking_sphinx/connection.rb +0 -71
  93. data/lib/thinking_sphinx/deltas/delete_job.rb +0 -16
  94. data/lib/thinking_sphinx/deltas/index_job.rb +0 -17
  95. data/lib/thinking_sphinx/rails_additions.rb +0 -181
  96. data/spec/fixtures/data.sql +0 -32
  97. data/spec/fixtures/database.yml.default +0 -3
  98. data/spec/fixtures/models.rb +0 -161
  99. data/spec/fixtures/structure.sql +0 -146
  100. data/spec/spec_helper.rb +0 -54
  101. data/spec/sphinx_helper.rb +0 -67
  102. data/spec/thinking_sphinx/adapters/abstract_adapter_spec.rb +0 -163
  103. data/spec/thinking_sphinx/connection_spec.rb +0 -77
  104. data/spec/thinking_sphinx/rails_additions_spec.rb +0 -203
@@ -4,81 +4,80 @@ describe ThinkingSphinx::ActiveRecord do
4
4
  before :each do
5
5
  @existing_alpha_indexes = Alpha.sphinx_indexes.clone
6
6
  @existing_beta_indexes = Beta.sphinx_indexes.clone
7
-
7
+
8
8
  Alpha.send :defined_indexes=, false
9
9
  Beta.send :defined_indexes=, false
10
-
10
+
11
11
  Alpha.sphinx_indexes.clear
12
12
  Beta.sphinx_indexes.clear
13
13
  end
14
-
14
+
15
15
  after :each do
16
16
  Alpha.sphinx_indexes.replace @existing_alpha_indexes
17
17
  Beta.sphinx_indexes.replace @existing_beta_indexes
18
-
18
+
19
19
  Alpha.send :defined_indexes=, true
20
20
  Beta.send :defined_indexes=, true
21
-
21
+
22
22
  Alpha.sphinx_index_blocks.clear
23
23
  Beta.sphinx_index_blocks.clear
24
24
  end
25
-
25
+
26
26
  describe '.define_index' do
27
27
  it "should do nothing if indexes are disabled" do
28
28
  ThinkingSphinx.define_indexes = false
29
29
  ThinkingSphinx::Index.should_not_receive(:new)
30
-
30
+
31
31
  Alpha.define_index { }
32
32
  Alpha.define_indexes
33
-
33
+
34
34
  ThinkingSphinx.define_indexes = true
35
35
  end
36
-
36
+
37
37
  it "should not evaluate the index block automatically" do
38
38
  lambda {
39
39
  Alpha.define_index { raise StandardError }
40
40
  }.should_not raise_error
41
41
  end
42
-
42
+
43
43
  it "should add the model to the context collection" do
44
44
  Alpha.define_index { indexes :name }
45
-
45
+
46
46
  ThinkingSphinx.context.indexed_models.should include("Alpha")
47
47
  end
48
-
48
+
49
49
  it "should die quietly if there is a database error" do
50
- ThinkingSphinx::Index::Builder.stub(:generate).
51
- and_raise(Mysql2::Error.new(''))
50
+ ThinkingSphinx::Index::Builder.stub(:generate) { raise Mysql::Error }
52
51
  Alpha.define_index { indexes :name }
53
-
52
+
54
53
  lambda {
55
54
  Alpha.define_indexes
56
55
  }.should_not raise_error
57
- end unless RUBY_PLATFORM == 'java'
58
-
56
+ end
57
+
59
58
  it "should die noisily if there is a non-database error" do
60
59
  ThinkingSphinx::Index::Builder.stub(:generate) { raise StandardError }
61
60
  Alpha.define_index { indexes :name }
62
-
61
+
63
62
  lambda {
64
63
  Alpha.define_indexes
65
64
  }.should raise_error
66
65
  end
67
-
66
+
68
67
  it "should set the index's name using the parameter if provided" do
69
68
  Alpha.define_index('custom') { indexes :name }
70
69
  Alpha.define_indexes
71
-
70
+
72
71
  Alpha.sphinx_indexes.first.name.should == 'custom'
73
72
  end
74
-
73
+
75
74
  context 'callbacks' do
76
75
  it "should add a before_validation callback to define_indexes" do
77
76
  Alpha.should_receive(:before_validation).with(:define_indexes)
78
77
 
79
78
  Alpha.define_index { }
80
79
  end
81
-
80
+
82
81
  it "should not add a before_validation callback twice" do
83
82
  Alpha.should_receive(:before_validation).with(:define_indexes).once
84
83
 
@@ -91,44 +90,44 @@ describe ThinkingSphinx::ActiveRecord do
91
90
 
92
91
  Alpha.define_index { }
93
92
  end
94
-
93
+
95
94
  it "should not add a before_destroy callback twice" do
96
95
  Alpha.should_receive(:before_destroy).with(:define_indexes).once
97
96
 
98
97
  Alpha.define_index { }
99
98
  Alpha.define_index { }
100
99
  end
101
-
100
+
102
101
  it "should add a toggle_deleted callback when defined" do
103
102
  Alpha.should_receive(:after_destroy).with(:toggle_deleted)
104
-
103
+
105
104
  Alpha.define_index { indexes :name }
106
105
  Alpha.define_indexes
107
106
  end
108
-
107
+
109
108
  it "should not add toggle_deleted callback more than once" do
110
109
  Alpha.should_receive(:after_destroy).with(:toggle_deleted).once
111
-
110
+
112
111
  Alpha.define_index { indexes :name }
113
112
  Alpha.define_index { indexes :name }
114
113
  Alpha.define_indexes
115
114
  end
116
-
115
+
117
116
  it "should add a update_attribute_values callback when defined" do
118
- Alpha.should_receive(:after_commit).with(:update_attribute_values)
119
-
117
+ Alpha.should_receive(:after_save).with(:update_attribute_values)
118
+
120
119
  Alpha.define_index { indexes :name }
121
120
  Alpha.define_indexes
122
121
  end
123
-
122
+
124
123
  it "should not add update_attribute_values callback more than once" do
125
- Alpha.should_receive(:after_commit).with(:update_attribute_values).once
126
-
124
+ Alpha.should_receive(:after_save).with(:update_attribute_values).once
125
+
127
126
  Alpha.define_index { indexes :name }
128
127
  Alpha.define_index { indexes :name }
129
128
  Alpha.define_indexes
130
129
  end
131
-
130
+
132
131
  it "should add a toggle_delta callback if deltas are enabled" do
133
132
  Beta.should_receive(:before_save).with(:toggle_delta)
134
133
 
@@ -138,14 +137,14 @@ describe ThinkingSphinx::ActiveRecord do
138
137
  }
139
138
  Beta.define_indexes
140
139
  end
141
-
140
+
142
141
  it "should not add a toggle_delta callback if deltas are disabled" do
143
142
  Alpha.should_not_receive(:before_save).with(:toggle_delta)
144
143
 
145
144
  Alpha.define_index { indexes :name }
146
145
  Alpha.define_indexes
147
146
  end
148
-
147
+
149
148
  it "should add the toggle_delta callback if deltas are disabled in other indexes" do
150
149
  Beta.should_receive(:before_save).with(:toggle_delta).once
151
150
 
@@ -156,7 +155,7 @@ describe ThinkingSphinx::ActiveRecord do
156
155
  }
157
156
  Beta.define_indexes
158
157
  end
159
-
158
+
160
159
  it "should only add the toggle_delta callback once" do
161
160
  Beta.should_receive(:before_save).with(:toggle_delta).once
162
161
 
@@ -170,9 +169,9 @@ describe ThinkingSphinx::ActiveRecord do
170
169
  }
171
170
  Beta.define_indexes
172
171
  end
173
-
172
+
174
173
  it "should add an index_delta callback if deltas are enabled" do
175
- Beta.stub!(:after_commit => true)
174
+ Beta.stub!(:after_save => true)
176
175
  Beta.should_receive(:after_commit).with(:index_delta)
177
176
 
178
177
  Beta.define_index {
@@ -181,14 +180,14 @@ describe ThinkingSphinx::ActiveRecord do
181
180
  }
182
181
  Beta.define_indexes
183
182
  end
184
-
183
+
185
184
  it "should not add an index_delta callback if deltas are disabled" do
186
185
  Alpha.should_not_receive(:after_commit).with(:index_delta)
187
186
 
188
187
  Alpha.define_index { indexes :name }
189
188
  Alpha.define_indexes
190
189
  end
191
-
190
+
192
191
  it "should add the index_delta callback if deltas are disabled in other indexes" do
193
192
  Beta.stub!(:after_commit => true)
194
193
  Beta.should_receive(:after_commit).with(:index_delta).once
@@ -200,7 +199,7 @@ describe ThinkingSphinx::ActiveRecord do
200
199
  }
201
200
  Beta.define_indexes
202
201
  end
203
-
202
+
204
203
  it "should only add the index_delta callback once" do
205
204
  Beta.stub!(:after_commit => true)
206
205
  Beta.should_receive(:after_commit).with(:index_delta).once
@@ -215,26 +214,70 @@ describe ThinkingSphinx::ActiveRecord do
215
214
  }
216
215
  Beta.define_indexes
217
216
  end
218
- end
217
+ end
219
218
  end
220
-
219
+
221
220
  describe '.define_indexes' do
222
221
  it "should process define_index blocks" do
223
222
  Beta.define_index { indexes :name }
224
223
  Beta.sphinx_indexes.length.should == 0
225
-
224
+
226
225
  Beta.define_indexes
227
226
  Beta.sphinx_indexes.length.should == 1
228
227
  end
229
-
228
+
230
229
  it "should not re-add indexes" do
231
230
  Beta.define_index { indexes :name }
232
231
  Beta.define_indexes
233
232
  Beta.define_indexes
234
-
233
+
235
234
  Beta.sphinx_indexes.length.should == 1
236
235
  end
237
236
  end
237
+
238
+ describe "index methods" do
239
+ before(:all) do
240
+ @person = Person.find(:first)
241
+ end
242
+
243
+ describe "in_both_indexes?" do
244
+ it "should return true if in core and delta indexes" do
245
+ @person.should_receive(:in_core_index?).and_return(true)
246
+ @person.should_receive(:in_delta_index?).and_return(true)
247
+ @person.in_both_indexes?.should be_true
248
+ end
249
+
250
+ it "should return false if in one index and not the other" do
251
+ @person.should_receive(:in_core_index?).and_return(true)
252
+ @person.should_receive(:in_delta_index?).and_return(false)
253
+ @person.in_both_indexes?.should be_false
254
+ end
255
+ end
256
+
257
+ describe "in_core_index?" do
258
+ it "should call in_index? with core" do
259
+ @person.should_receive(:in_index?).with('core')
260
+ @person.in_core_index?
261
+ end
262
+ end
263
+
264
+ describe "in_delta_index?" do
265
+ it "should call in_index? with delta" do
266
+ @person.should_receive(:in_index?).with('delta')
267
+ @person.in_delta_index?
268
+ end
269
+ end
270
+
271
+ describe "in_index?" do
272
+ it "should return true if in the specified index" do
273
+ @person.should_receive(:sphinx_document_id).and_return(1)
274
+ @person.should_receive(:sphinx_index_name).and_return('person_core')
275
+ Person.should_receive(:search_for_id).with(1, 'person_core').and_return(true)
276
+
277
+ @person.in_index?('core').should be_true
278
+ end
279
+ end
280
+ end
238
281
 
239
282
  describe '.source_of_sphinx_index' do
240
283
  it "should return self if model defines an index" do
@@ -245,23 +288,23 @@ describe ThinkingSphinx::ActiveRecord do
245
288
  Admin::Person.source_of_sphinx_index.should == Person
246
289
  end
247
290
  end
248
-
291
+
249
292
  describe '.to_crc32' do
250
293
  it "should return an integer" do
251
294
  Person.to_crc32.should be_a_kind_of(Integer)
252
295
  end
253
296
  end
254
-
297
+
255
298
  describe '.to_crc32s' do
256
299
  it "should return an array" do
257
300
  Person.to_crc32s.should be_a_kind_of(Array)
258
301
  end
259
302
  end
260
-
303
+
261
304
  describe "toggle_deleted method" do
262
305
  before :each do
263
306
  ThinkingSphinx.stub!(:sphinx_running? => true)
264
-
307
+
265
308
  @configuration = ThinkingSphinx::Configuration.instance
266
309
  @configuration.stub!(
267
310
  :address => "an address",
@@ -270,26 +313,37 @@ describe ThinkingSphinx::ActiveRecord do
270
313
  @client = Riddle::Client.new
271
314
  @client.stub!(:update => true)
272
315
  @person = Person.find(:first)
273
-
274
- ThinkingSphinx::Connection.stub(:take).and_yield(@client)
316
+
317
+ @configuration.stub!(:client => @client)
275
318
  Person.sphinx_indexes.each { |index| index.stub!(:delta? => false) }
319
+ Person.stub!(:search_for_id => true)
276
320
  end
277
-
321
+
278
322
  it "should update the core index's deleted flag if in core index" do
279
323
  @client.should_receive(:update).with(
280
324
  "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
281
325
  )
282
-
326
+
283
327
  @person.toggle_deleted
284
328
  end
285
-
329
+
330
+ it "shouldn't update the core index's deleted flag if the record isn't in it" do
331
+ Person.stub!(:search_for_id => false)
332
+ @client.should_not_receive(:update).with(
333
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
334
+ )
335
+
336
+ @person.toggle_deleted
337
+ end
338
+
286
339
  it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
287
340
  ThinkingSphinx.stub!(:sphinx_running? => false)
288
341
  @client.should_not_receive(:update)
289
-
342
+ Person.should_not_receive(:search_for_id)
343
+
290
344
  @person.toggle_deleted
291
345
  end
292
-
346
+
293
347
  it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
294
348
  ThinkingSphinx.deltas_enabled = true
295
349
  Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
@@ -297,10 +351,10 @@ describe ThinkingSphinx::ActiveRecord do
297
351
  @client.should_receive(:update).with(
298
352
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
299
353
  )
300
-
354
+
301
355
  @person.toggle_deleted
302
356
  end
303
-
357
+
304
358
  it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
305
359
  ThinkingSphinx.deltas_enabled = true
306
360
  Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
@@ -308,10 +362,10 @@ describe ThinkingSphinx::ActiveRecord do
308
362
  @client.should_not_receive(:update).with(
309
363
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
310
364
  )
311
-
365
+
312
366
  @person.toggle_deleted
313
367
  end
314
-
368
+
315
369
  it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is equivalent to false" do
316
370
  ThinkingSphinx.deltas_enabled = true
317
371
  Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
@@ -328,17 +382,17 @@ describe ThinkingSphinx::ActiveRecord do
328
382
  @client.should_not_receive(:update).with(
329
383
  "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
330
384
  )
331
-
385
+
332
386
  @person.toggle_deleted
333
387
  end
334
-
388
+
335
389
  it "should not update either index if updates are disabled" do
336
390
  ThinkingSphinx.updates_enabled = false
337
391
  ThinkingSphinx.deltas_enabled = true
338
392
  Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
339
393
  @person.delta = true
340
394
  @client.should_not_receive(:update)
341
-
395
+
342
396
  @person.toggle_deleted
343
397
  end
344
398
  end
@@ -357,107 +411,82 @@ describe ThinkingSphinx::ActiveRecord do
357
411
  }.should_not raise_error(ActiveRecord::StatementInvalid)
358
412
  end
359
413
  end
360
-
414
+
361
415
  describe '#sphinx_document_id' do
362
416
  before :each do
363
417
  Alpha.define_index { indexes :name }
364
418
  Beta.define_index { indexes :name }
365
419
  end
366
-
420
+
367
421
  it "should return values with the expected offset" do
368
422
  person = Person.find(:first)
369
423
  model_count = ThinkingSphinx.context.indexed_models.length
370
424
  Person.stub!(:sphinx_offset => 3)
371
-
425
+
372
426
  (person.id * model_count + 3).should == person.sphinx_document_id
373
427
  end
374
428
  end
375
-
429
+
376
430
  describe '#primary_key_for_sphinx' do
377
431
  before :each do
378
432
  @person = Person.find(:first)
379
433
  end
380
-
381
- after :each do
382
- Person.clear_primary_key_for_sphinx
383
- end
384
-
434
+
385
435
  after :each do
386
436
  Person.set_sphinx_primary_key nil
387
437
  end
388
-
438
+
389
439
  it "should return the id by default" do
390
440
  @person.primary_key_for_sphinx.should == @person.id
391
441
  end
392
-
442
+
393
443
  it "should use the sphinx primary key to determine the value" do
394
444
  Person.set_sphinx_primary_key :first_name
395
445
  @person.primary_key_for_sphinx.should == @person.first_name
396
446
  end
397
-
447
+
398
448
  it "should not use accessor methods but the attributes hash" do
399
449
  id = @person.id
400
450
  @person.stub!(:id => 'unique_hash')
401
451
  @person.primary_key_for_sphinx.should == id
402
452
  end
403
-
404
- it "should be inherited by subclasses" do
405
- Person.set_sphinx_primary_key :first_name
406
- Parent.superclass.custom_primary_key_for_sphinx?
407
- Parent.primary_key_for_sphinx.should == Person.primary_key_for_sphinx
408
- end
409
- end
410
-
411
- describe '#types_for_sphinx' do
412
- after :each do
413
- Person.set_sphinx_types nil
414
- end
415
-
416
- it "should return nil by default" do
417
- Person.sphinx_types.should == nil
418
- end
419
-
420
- it "should return the specified value" do
421
- Person.set_sphinx_types %w(Person Parent)
422
- Person.sphinx_types.should == %w(Person Parent)
423
- end
424
453
  end
425
-
454
+
426
455
  describe '.sphinx_index_names' do
427
456
  it "should return the core index" do
428
457
  Alpha.define_index { indexes :name }
429
458
  Alpha.define_indexes
430
459
  Alpha.sphinx_index_names.should == ['alpha_core']
431
460
  end
432
-
461
+
433
462
  it "should return the delta index if enabled" do
434
463
  Beta.define_index {
435
464
  indexes :name
436
465
  set_property :delta => true
437
466
  }
438
467
  Beta.define_indexes
439
-
468
+
440
469
  Beta.sphinx_index_names.should == ['beta_core', 'beta_delta']
441
470
  end
442
-
471
+
443
472
  it "should return the superclass with an index definition" do
444
473
  Parent.sphinx_index_names.should == ['person_core', 'person_delta']
445
474
  end
446
475
  end
447
-
476
+
448
477
  describe '.indexed_by_sphinx?' do
449
478
  it "should return true if there is at least one index on the model" do
450
479
  Alpha.define_index { indexes :name }
451
480
  Alpha.define_indexes
452
-
481
+
453
482
  Alpha.should be_indexed_by_sphinx
454
483
  end
455
-
484
+
456
485
  it "should return false if there are no indexes on the model" do
457
486
  Gamma.should_not be_indexed_by_sphinx
458
487
  end
459
488
  end
460
-
489
+
461
490
  describe '.delta_indexed_by_sphinx?' do
462
491
  it "should return true if there is at least one delta index on the model" do
463
492
  Beta.define_index {
@@ -465,117 +494,117 @@ describe ThinkingSphinx::ActiveRecord do
465
494
  set_property :delta => true
466
495
  }
467
496
  Beta.define_indexes
468
-
497
+
469
498
  Beta.should be_delta_indexed_by_sphinx
470
499
  end
471
-
500
+
472
501
  it "should return false if there are no delta indexes on the model" do
473
502
  Alpha.define_index { indexes :name }
474
503
  Alpha.define_indexes
475
-
504
+
476
505
  Alpha.should_not be_delta_indexed_by_sphinx
477
506
  end
478
507
  end
479
-
508
+
480
509
  describe '.delete_in_index' do
481
510
  before :each do
482
511
  @client = stub('client')
483
512
  ThinkingSphinx.stub!(:sphinx_running? => true)
484
- ThinkingSphinx::Connection.stub(:take).and_yield(@client)
513
+ ThinkingSphinx::Configuration.instance.stub!(:client => @client)
514
+ Alpha.stub!(:search_for_id => true)
485
515
  end
486
-
516
+
517
+ it "should not update if the document isn't in the given index" do
518
+ Alpha.stub!(:search_for_id => false)
519
+ @client.should_not_receive(:update)
520
+
521
+ Alpha.delete_in_index('alpha_core', 42)
522
+ end
523
+
487
524
  it "should direct the update to the supplied index" do
488
525
  @client.should_receive(:update) do |index, attributes, values|
489
526
  index.should == 'custom_index_core'
490
527
  end
491
-
528
+
492
529
  Alpha.delete_in_index('custom_index_core', 42)
493
530
  end
494
-
531
+
495
532
  it "should set the sphinx_deleted flag to true" do
496
533
  @client.should_receive(:update) do |index, attributes, values|
497
534
  attributes.should == ['sphinx_deleted']
498
535
  values.should == {42 => [1]}
499
536
  end
500
-
537
+
501
538
  Alpha.delete_in_index('alpha_core', 42)
502
539
  end
503
540
  end
504
-
541
+
505
542
  describe '.core_index_names' do
506
543
  it "should return each index's core name" do
507
544
  Alpha.define_index('foo') { indexes :name }
508
545
  Alpha.define_index('bar') { indexes :name }
509
546
  Alpha.define_indexes
510
-
547
+
511
548
  Alpha.core_index_names.should == ['foo_core', 'bar_core']
512
549
  end
513
550
  end
514
-
551
+
515
552
  describe '.delta_index_names' do
516
553
  it "should return index delta names, for indexes with deltas enabled" do
517
554
  Alpha.define_index('foo') { indexes :name }
518
555
  Alpha.define_index('bar') { indexes :name }
519
556
  Alpha.define_indexes
520
557
  Alpha.sphinx_indexes.first.delta_object = stub('delta')
521
-
558
+
522
559
  Alpha.delta_index_names.should == ['foo_delta']
523
560
  end
524
561
  end
525
-
562
+
526
563
  describe '.sphinx_offset' do
527
564
  before :each do
528
565
  @context = ThinkingSphinx.context
529
566
  end
530
-
567
+
531
568
  it "should return the index of the model's name in all known indexed models" do
532
569
  @context.stub!(:indexed_models => ['Alpha', 'Beta'])
533
-
570
+
534
571
  Alpha.sphinx_offset.should == 0
535
572
  Beta.sphinx_offset.should == 1
536
573
  end
537
-
574
+
538
575
  it "should ignore classes that have indexed superclasses" do
539
576
  @context.stub!(:indexed_models => ['Alpha', 'Parent', 'Person'])
540
-
577
+
541
578
  Person.sphinx_offset.should == 1
542
579
  end
543
-
580
+
544
581
  it "should respect first known indexed parents" do
545
582
  @context.stub!(:indexed_models => ['Alpha', 'Parent', 'Person'])
546
-
583
+
547
584
  Parent.sphinx_offset.should == 1
548
585
  end
549
586
  end
550
-
587
+
551
588
  describe '.has_sphinx_indexes?' do
552
589
  it "should return true if there are sphinx indexes defined" do
553
590
  Alpha.sphinx_indexes.replace [stub('index')]
554
591
  Alpha.sphinx_index_blocks.replace []
555
-
592
+
556
593
  Alpha.should have_sphinx_indexes
557
594
  end
558
-
595
+
559
596
  it "should return true if there are sphinx index blocks defined" do
560
597
  Alpha.sphinx_indexes.replace []
561
598
  Alpha.sphinx_index_blocks.replace [stub('lambda')]
562
-
599
+
563
600
  Alpha.should have_sphinx_indexes
564
601
  end
565
-
602
+
566
603
  it "should return false if there are no sphinx indexes or blocks" do
567
604
  Alpha.sphinx_indexes.clear
568
605
  Alpha.sphinx_index_blocks.clear
569
-
606
+
570
607
  Alpha.should_not have_sphinx_indexes
571
608
  end
572
609
  end
573
-
574
- describe '.reset_subclasses' do
575
- it "should reset the stored context" do
576
- ThinkingSphinx.should_receive(:reset_context!)
577
-
578
- ActiveRecord::Base.reset_subclasses
579
- end
580
- end
581
610
  end