dpickett-thinking-sphinx 1.1.12 → 1.1.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/README.textile +19 -0
  2. data/lib/thinking_sphinx.rb +36 -2
  3. data/lib/thinking_sphinx/active_record.rb +18 -3
  4. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +9 -3
  5. data/lib/thinking_sphinx/association.rb +4 -1
  6. data/lib/thinking_sphinx/attribute.rb +85 -43
  7. data/lib/thinking_sphinx/configuration.rb +33 -12
  8. data/lib/thinking_sphinx/deltas.rb +9 -6
  9. data/lib/thinking_sphinx/deltas/datetime_delta.rb +3 -3
  10. data/lib/thinking_sphinx/deltas/default_delta.rb +4 -4
  11. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +1 -1
  12. data/lib/thinking_sphinx/deploy/capistrano.rb +82 -64
  13. data/lib/thinking_sphinx/facet.rb +58 -21
  14. data/lib/thinking_sphinx/facet_collection.rb +12 -13
  15. data/lib/thinking_sphinx/field.rb +3 -1
  16. data/lib/thinking_sphinx/index.rb +28 -353
  17. data/lib/thinking_sphinx/index/builder.rb +255 -232
  18. data/lib/thinking_sphinx/property.rb +29 -2
  19. data/lib/thinking_sphinx/search.rb +32 -96
  20. data/lib/thinking_sphinx/search/facets.rb +104 -0
  21. data/lib/thinking_sphinx/source.rb +150 -0
  22. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  23. data/lib/thinking_sphinx/source/sql.rb +128 -0
  24. data/lib/thinking_sphinx/tasks.rb +42 -8
  25. data/rails/init.rb +14 -0
  26. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +5 -5
  27. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +4 -4
  28. data/spec/unit/thinking_sphinx/active_record_spec.rb +52 -39
  29. data/spec/unit/thinking_sphinx/association_spec.rb +4 -5
  30. data/spec/unit/thinking_sphinx/attribute_spec.rb +209 -19
  31. data/spec/unit/thinking_sphinx/collection_spec.rb +7 -6
  32. data/spec/unit/thinking_sphinx/configuration_spec.rb +93 -7
  33. data/spec/unit/thinking_sphinx/facet_spec.rb +256 -0
  34. data/spec/unit/thinking_sphinx/field_spec.rb +26 -17
  35. data/spec/unit/thinking_sphinx/index/builder_spec.rb +351 -1
  36. data/spec/unit/thinking_sphinx/index_spec.rb +3 -102
  37. data/spec/unit/thinking_sphinx/rails_additions_spec.rb +13 -5
  38. data/spec/unit/thinking_sphinx/search_spec.rb +154 -29
  39. data/spec/unit/thinking_sphinx/source_spec.rb +217 -0
  40. data/spec/unit/thinking_sphinx_spec.rb +22 -4
  41. data/tasks/distribution.rb +19 -0
  42. data/vendor/riddle/lib/riddle.rb +1 -1
  43. data/vendor/riddle/lib/riddle/configuration/section.rb +7 -1
  44. metadata +26 -3
@@ -79,7 +79,8 @@ describe ThinkingSphinx::Association do
79
79
  before :each do
80
80
  @parent_join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
81
81
  @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
82
- @parent = ThinkingSphinx::Association.stub_instance(:join_to => true, :join => nil)
82
+ @parent = ThinkingSphinx::Association.new(nil, nil)
83
+ @parent.stub!(:join_to => true, :join => nil)
83
84
  @base_join = Object.stub_instance(:joins => [:a, :b, :c])
84
85
 
85
86
  ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_method(:new => @join)
@@ -87,19 +88,17 @@ describe ThinkingSphinx::Association do
87
88
 
88
89
  it "should call the parent's join_to if parent has no join" do
89
90
  @assoc = ThinkingSphinx::Association.new(@parent, :ref)
91
+ @parent.should_receive(:join_to).with(@base_join)
90
92
 
91
93
  @assoc.join_to(@base_join)
92
-
93
- @parent.should have_received(:join_to).with(@base_join)
94
94
  end
95
95
 
96
96
  it "should not call the parent's join_to if it already has a join" do
97
97
  @assoc = ThinkingSphinx::Association.new(@parent, :ref)
98
98
  @parent.stub_method(:join => @parent_join)
99
+ @parent.should_not_receive(:join_to)
99
100
 
100
101
  @assoc.join_to(@base_join)
101
-
102
- @parent.should_not have_received(:join_to)
103
102
  end
104
103
 
105
104
  it "should define the join association with a JoinAssociation instance" do
@@ -1,23 +1,30 @@
1
1
  require 'spec/spec_helper'
2
2
 
3
3
  describe ThinkingSphinx::Attribute do
4
+ before :each do
5
+ @index = ThinkingSphinx::Index.new(Person)
6
+ @source = ThinkingSphinx::Source.new(@index)
7
+
8
+ @index.delta_object = ThinkingSphinx::Deltas::DefaultDelta.new @index, @index.local_options
9
+ end
10
+
4
11
  describe '#initialize' do
5
12
  it 'raises if no columns are provided so that configuration errors are easier to track down' do
6
13
  lambda {
7
- ThinkingSphinx::Attribute.new([])
14
+ ThinkingSphinx::Attribute.new(@source, [])
8
15
  }.should raise_error(RuntimeError)
9
16
  end
10
17
 
11
18
  it 'raises if an element of the columns param is an integer - as happens when you use id instead of :id - so that configuration errors are easier to track down' do
12
19
  lambda {
13
- ThinkingSphinx::Attribute.new([1234])
20
+ ThinkingSphinx::Attribute.new(@source, [1234])
14
21
  }.should raise_error(RuntimeError)
15
22
  end
16
23
  end
17
24
 
18
25
  describe "unique_name method" do
19
26
  before :each do
20
- @attribute = ThinkingSphinx::Attribute.new [
27
+ @attribute = ThinkingSphinx::Attribute.new @source, [
21
28
  Object.stub_instance(:__stack => [], :__name => "col_name")
22
29
  ]
23
30
  end
@@ -42,21 +49,21 @@ describe ThinkingSphinx::Attribute do
42
49
 
43
50
  describe "column_with_prefix method" do
44
51
  before :each do
45
- @attribute = ThinkingSphinx::Attribute.new [
52
+ @attribute = ThinkingSphinx::Attribute.new @source, [
46
53
  ThinkingSphinx::Index::FauxColumn.new(:col_name)
47
54
  ]
48
55
  @attribute.columns.each { |col| @attribute.associations[col] = [] }
49
56
  @attribute.model = Person
50
57
 
51
- @first_join = Object.stub_instance(:aliased_table_name => "tabular")
52
- @second_join = Object.stub_instance(:aliased_table_name => "data")
58
+ @first_join = Object.new
59
+ @first_join.stub!(:aliased_table_name => "tabular")
60
+ @second_join = Object.new
61
+ @second_join.stub!(:aliased_table_name => "data")
53
62
 
54
- @first_assoc = ThinkingSphinx::Association.stub_instance(
55
- :join => @first_join, :has_column? => true
56
- )
57
- @second_assoc = ThinkingSphinx::Association.stub_instance(
58
- :join => @second_join, :has_column? => true
59
- )
63
+ @first_assoc = ThinkingSphinx::Association.new nil, nil
64
+ @first_assoc.stub!(:join => @first_join, :has_column? => true)
65
+ @second_assoc = ThinkingSphinx::Association.new nil, nil
66
+ @second_assoc.stub!(:join => @second_join, :has_column? => true)
60
67
  end
61
68
 
62
69
  it "should return the column name if the column is a string" do
@@ -88,7 +95,7 @@ describe ThinkingSphinx::Attribute do
88
95
  @assoc_c = Object.stub_instance(:is_many? => true)
89
96
 
90
97
  @attribute = ThinkingSphinx::Attribute.new(
91
- [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
98
+ @source, [ThinkingSphinx::Index::FauxColumn.new(:col_name)]
92
99
  )
93
100
  @attribute.associations = {
94
101
  :a => @assoc_a, :b => @assoc_b, :c => @assoc_c
@@ -122,7 +129,7 @@ describe ThinkingSphinx::Attribute do
122
129
  @col_c = ThinkingSphinx::Index::FauxColumn.new("c")
123
130
 
124
131
  @attribute = ThinkingSphinx::Attribute.new(
125
- [@col_a, @col_b, @col_c]
132
+ @source, [@col_a, @col_b, @col_c]
126
133
  )
127
134
  end
128
135
 
@@ -146,7 +153,7 @@ describe ThinkingSphinx::Attribute do
146
153
  describe "type method" do
147
154
  before :each do
148
155
  @column = ThinkingSphinx::Index::FauxColumn.new(:col_name)
149
- @attribute = ThinkingSphinx::Attribute.new([@column])
156
+ @attribute = ThinkingSphinx::Attribute.new(@source, [@column])
150
157
  @attribute.model = Person
151
158
  @attribute.stub_method(:is_many? => false)
152
159
  end
@@ -177,7 +184,7 @@ describe ThinkingSphinx::Attribute do
177
184
 
178
185
  describe "all_ints? method" do
179
186
  it "should return true if all columns are integers" do
180
- attribute = ThinkingSphinx::Attribute.new(
187
+ attribute = ThinkingSphinx::Attribute.new(@source,
181
188
  [ ThinkingSphinx::Index::FauxColumn.new(:id),
182
189
  ThinkingSphinx::Index::FauxColumn.new(:team_id) ]
183
190
  )
@@ -188,7 +195,7 @@ describe ThinkingSphinx::Attribute do
188
195
  end
189
196
 
190
197
  it "should return false if only some columns are integers" do
191
- attribute = ThinkingSphinx::Attribute.new(
198
+ attribute = ThinkingSphinx::Attribute.new(@source,
192
199
  [ ThinkingSphinx::Index::FauxColumn.new(:id),
193
200
  ThinkingSphinx::Index::FauxColumn.new(:first_name) ]
194
201
  )
@@ -199,7 +206,7 @@ describe ThinkingSphinx::Attribute do
199
206
  end
200
207
 
201
208
  it "should return false if no columns are integers" do
202
- attribute = ThinkingSphinx::Attribute.new(
209
+ attribute = ThinkingSphinx::Attribute.new(@source,
203
210
  [ ThinkingSphinx::Index::FauxColumn.new(:first_name),
204
211
  ThinkingSphinx::Index::FauxColumn.new(:last_name) ]
205
212
  )
@@ -210,10 +217,193 @@ describe ThinkingSphinx::Attribute do
210
217
  end
211
218
  end
212
219
 
220
+ describe "MVA with source query" do
221
+ before :each do
222
+ @attribute = ThinkingSphinx::Attribute.new(@source,
223
+ [ThinkingSphinx::Index::FauxColumn.new(:tags, :id)],
224
+ :as => :tag_ids, :source => :query
225
+ )
226
+ end
227
+
228
+ it "should use a query" do
229
+ @attribute.type_to_config.should == :sql_attr_multi
230
+
231
+ declaration, query = @attribute.config_value.split('; ')
232
+ declaration.should == "uint tag_ids from query"
233
+ query.should == "SELECT `tags`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `tags`.`id` AS `tag_ids` FROM `tags`"
234
+ end
235
+ end
236
+
237
+ describe "MVA with source query for a delta source" do
238
+ before :each do
239
+ @attribute = ThinkingSphinx::Attribute.new(@source,
240
+ [ThinkingSphinx::Index::FauxColumn.new(:tags, :id)],
241
+ :as => :tag_ids, :source => :query
242
+ )
243
+ end
244
+
245
+ it "should use a query" do
246
+ @attribute.type_to_config.should == :sql_attr_multi
247
+
248
+ declaration, query = @attribute.config_value(nil, true).split('; ')
249
+ declaration.should == "uint tag_ids from query"
250
+ query.should == "SELECT `tags`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `tags`.`id` AS `tag_ids` FROM `tags` WHERE `tags`.`person_id` IN (SELECT `id` FROM `people` WHERE `people`.`delta` = 1)"
251
+ end
252
+ end
253
+
254
+ describe "MVA via a HABTM association with a source query" do
255
+ before :each do
256
+ @attribute = ThinkingSphinx::Attribute.new(@source,
257
+ [ThinkingSphinx::Index::FauxColumn.new(:links, :id)],
258
+ :as => :link_ids, :source => :query
259
+ )
260
+ end
261
+
262
+ it "should use a ranged query" do
263
+ @attribute.type_to_config.should == :sql_attr_multi
264
+
265
+ declaration, query = @attribute.config_value.split('; ')
266
+ declaration.should == "uint link_ids from query"
267
+ query.should == "SELECT `links_people`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `links_people`.`link_id` AS `link_ids` FROM `links_people`"
268
+ end
269
+ end
270
+
271
+ describe "MVA with ranged source query" do
272
+ before :each do
273
+ @attribute = ThinkingSphinx::Attribute.new(@source,
274
+ [ThinkingSphinx::Index::FauxColumn.new(:tags, :id)],
275
+ :as => :tag_ids, :source => :ranged_query
276
+ )
277
+ end
278
+
279
+ it "should use a ranged query" do
280
+ @attribute.type_to_config.should == :sql_attr_multi
281
+
282
+ declaration, query, range_query = @attribute.config_value.split('; ')
283
+ declaration.should == "uint tag_ids from ranged-query"
284
+ query.should == "SELECT `tags`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `tags`.`id` AS `tag_ids` FROM `tags` WHERE `tags`.`person_id` >= $start AND `tags`.`person_id` <= $end"
285
+ range_query.should == "SELECT MIN(`tags`.`person_id`), MAX(`tags`.`person_id`) FROM `tags`"
286
+ end
287
+ end
288
+
289
+ describe "MVA with ranged source query for a delta source" do
290
+ before :each do
291
+ @attribute = ThinkingSphinx::Attribute.new(@source,
292
+ [ThinkingSphinx::Index::FauxColumn.new(:tags, :id)],
293
+ :as => :tag_ids, :source => :ranged_query
294
+ )
295
+ end
296
+
297
+ it "should use a ranged query" do
298
+ @attribute.type_to_config.should == :sql_attr_multi
299
+
300
+ declaration, query, range_query = @attribute.config_value(nil, true).split('; ')
301
+ declaration.should == "uint tag_ids from ranged-query"
302
+ query.should == "SELECT `tags`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `tags`.`id` AS `tag_ids` FROM `tags` WHERE `tags`.`person_id` >= $start AND `tags`.`person_id` <= $end AND `tags`.`person_id` IN (SELECT `id` FROM `people` WHERE `people`.`delta` = 1)"
303
+ range_query.should == "SELECT MIN(`tags`.`person_id`), MAX(`tags`.`person_id`) FROM `tags`"
304
+ end
305
+ end
306
+
307
+ describe "MVA via a has-many :through with a ranged source query" do
308
+ before :each do
309
+ @attribute = ThinkingSphinx::Attribute.new(@source,
310
+ [ThinkingSphinx::Index::FauxColumn.new(:football_teams, :id)],
311
+ :as => :football_team_ids, :source => :ranged_query
312
+ )
313
+ end
314
+
315
+ it "should use a ranged query" do
316
+ @attribute.type_to_config.should == :sql_attr_multi
317
+
318
+ declaration, query, range_query = @attribute.config_value.split('; ')
319
+ declaration.should == "uint football_team_ids from ranged-query"
320
+ query.should == "SELECT `tags`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `tags`.`football_team_id` AS `football_team_ids` FROM `tags` WHERE `tags`.`person_id` >= $start AND `tags`.`person_id` <= $end"
321
+ range_query.should == "SELECT MIN(`tags`.`person_id`), MAX(`tags`.`person_id`) FROM `tags`"
322
+ end
323
+ end
324
+
325
+ describe "MVA via a has-many :through using a foreign key with a ranged source query" do
326
+ before :each do
327
+ @attribute = ThinkingSphinx::Attribute.new(@source,
328
+ [ThinkingSphinx::Index::FauxColumn.new(:friends, :id)],
329
+ :as => :friend_ids, :source => :ranged_query
330
+ )
331
+ end
332
+
333
+ it "should use a ranged query" do
334
+ @attribute.type_to_config.should == :sql_attr_multi
335
+
336
+ declaration, query, range_query = @attribute.config_value.split('; ')
337
+ declaration.should == "uint friend_ids from ranged-query"
338
+ query.should == "SELECT `friendships`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `friendships`.`friend_id` AS `friend_ids` FROM `friendships` WHERE `friendships`.`person_id` >= $start AND `friendships`.`person_id` <= $end"
339
+ range_query.should == "SELECT MIN(`friendships`.`person_id`), MAX(`friendships`.`person_id`) FROM `friendships`"
340
+ end
341
+ end
342
+
343
+ describe "MVA via a HABTM with a ranged source query" do
344
+ before :each do
345
+ @attribute = ThinkingSphinx::Attribute.new(@source,
346
+ [ThinkingSphinx::Index::FauxColumn.new(:links, :id)],
347
+ :as => :link_ids, :source => :ranged_query
348
+ )
349
+ end
350
+
351
+ it "should use a ranged query" do
352
+ @attribute.type_to_config.should == :sql_attr_multi
353
+
354
+ declaration, query, range_query = @attribute.config_value.split('; ')
355
+ declaration.should == "uint link_ids from ranged-query"
356
+ query.should == "SELECT `links_people`.`person_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `links_people`.`link_id` AS `link_ids` FROM `links_people` WHERE `links_people`.`person_id` >= $start AND `links_people`.`person_id` <= $end"
357
+ range_query.should == "SELECT MIN(`links_people`.`person_id`), MAX(`links_people`.`person_id`) FROM `links_people`"
358
+ end
359
+ end
360
+
361
+ describe "MVA via two has-many associations with a ranged source query" do
362
+ before :each do
363
+ @index = ThinkingSphinx::Index.new(Alpha)
364
+ @source = ThinkingSphinx::Source.new(@index)
365
+ @attribute = ThinkingSphinx::Attribute.new(@source,
366
+ [ThinkingSphinx::Index::FauxColumn.new(:betas, :gammas, :value)],
367
+ :as => :gamma_values, :source => :ranged_query
368
+ )
369
+ end
370
+
371
+ it "should use a ranged query" do
372
+ @attribute.type_to_config.should == :sql_attr_multi
373
+
374
+ declaration, query, range_query = @attribute.config_value.split('; ')
375
+ declaration.should == "uint gamma_values from ranged-query"
376
+ query.should == "SELECT `betas`.`alpha_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `gammas`.`value` AS `gamma_values` FROM `betas` LEFT OUTER JOIN `gammas` ON gammas.beta_id = betas.id WHERE `betas`.`alpha_id` >= $start AND `betas`.`alpha_id` <= $end"
377
+ range_query.should == "SELECT MIN(`betas`.`alpha_id`), MAX(`betas`.`alpha_id`) FROM `betas`"
378
+ end
379
+ end
380
+
381
+ describe "MVA via two has-many associations with a ranged source query for a delta source" do
382
+ before :each do
383
+ @index = ThinkingSphinx::Index.new(Alpha)
384
+ @source = ThinkingSphinx::Source.new(@index)
385
+ @attribute = ThinkingSphinx::Attribute.new(@source,
386
+ [ThinkingSphinx::Index::FauxColumn.new(:betas, :gammas, :value)],
387
+ :as => :gamma_values, :source => :ranged_query
388
+ )
389
+
390
+ @index.delta_object = ThinkingSphinx::Deltas::DefaultDelta.new @index, @index.local_options
391
+ end
392
+
393
+ it "should use a ranged query" do
394
+ @attribute.type_to_config.should == :sql_attr_multi
395
+
396
+ declaration, query, range_query = @attribute.config_value(nil, true).split('; ')
397
+ declaration.should == "uint gamma_values from ranged-query"
398
+ query.should == "SELECT `betas`.`alpha_id` #{ThinkingSphinx.unique_id_expression} AS `id`, `gammas`.`value` AS `gamma_values` FROM `betas` LEFT OUTER JOIN `gammas` ON gammas.beta_id = betas.id WHERE `betas`.`alpha_id` >= $start AND `betas`.`alpha_id` <= $end AND `betas`.`alpha_id` IN (SELECT `id` FROM `alphas` WHERE `alphas`.`delta` = 1)"
399
+ range_query.should == "SELECT MIN(`betas`.`alpha_id`), MAX(`betas`.`alpha_id`) FROM `betas`"
400
+ end
401
+ end
402
+
213
403
  describe "with custom queries" do
214
404
  before :each do
215
405
  index = CricketTeam.sphinx_indexes.first
216
- @statement = index.to_riddle_for_core(0, 0).sql_attr_multi.first
406
+ @statement = index.sources.first.to_riddle_for_core(0, 0).sql_attr_multi.last
217
407
  end
218
408
 
219
409
  it "should track the query type accordingly" do
@@ -2,12 +2,13 @@ require 'spec/spec_helper'
2
2
 
3
3
  describe ThinkingSphinx::Collection do
4
4
  it "should behave like WillPaginate::Collection" do
5
- ThinkingSphinx::Collection.instance_methods.should include("previous_page")
6
- ThinkingSphinx::Collection.instance_methods.should include("next_page")
7
- ThinkingSphinx::Collection.instance_methods.should include("current_page")
8
- ThinkingSphinx::Collection.instance_methods.should include("total_pages")
9
- ThinkingSphinx::Collection.instance_methods.should include("total_entries")
10
- ThinkingSphinx::Collection.instance_methods.should include("offset")
5
+ instance_methods = ThinkingSphinx::Collection.instance_methods.collect { |m| m.to_s }
6
+ instance_methods.should include("previous_page")
7
+ instance_methods.should include("next_page")
8
+ instance_methods.should include("current_page")
9
+ instance_methods.should include("total_pages")
10
+ instance_methods.should include("total_entries")
11
+ instance_methods.should include("offset")
11
12
 
12
13
  ThinkingSphinx::Collection.ancestors.should include(Array)
13
14
  end
@@ -10,11 +10,11 @@ describe ThinkingSphinx::Configuration do
10
10
 
11
11
  it "should use the Merb environment value if set" do
12
12
  unless defined?(Merb)
13
- module Merb; end
13
+ module ::Merb; end
14
14
  end
15
-
15
+
16
16
  ThinkingSphinx::Configuration.stub_method(:defined? => true)
17
- Merb.stub_method(:environment => "merb_production")
17
+ Merb.stub!(:environment => "merb_production")
18
18
  ThinkingSphinx::Configuration.environment.should == "merb_production"
19
19
 
20
20
  Object.send(:remove_const, :Merb)
@@ -48,7 +48,9 @@ describe ThinkingSphinx::Configuration do
48
48
  "morphology" => "stem_ru",
49
49
  "charset_type" => "latin1",
50
50
  "charset_table" => "table",
51
- "ignore_chars" => "e"
51
+ "ignore_chars" => "e",
52
+ "searchd_binary_name" => "sphinx-searchd",
53
+ "indexer_binary_name" => "sphinx-indexer"
52
54
  }
53
55
  }
54
56
 
@@ -62,7 +64,7 @@ describe ThinkingSphinx::Configuration do
62
64
  config.send(:parse_config)
63
65
 
64
66
  %w(config_file searchd_log_file query_log_file pid_file searchd_file_path
65
- address port).each do |key|
67
+ address port searchd_binary_name indexer_binary_name).each do |key|
66
68
  config.send(key).should == @settings["development"][key]
67
69
  end
68
70
  end
@@ -71,7 +73,16 @@ describe ThinkingSphinx::Configuration do
71
73
  FileUtils.rm "#{RAILS_ROOT}/config/sphinx.yml"
72
74
  end
73
75
  end
74
-
76
+
77
+ describe "block configuration" do
78
+ it "should let the user set-up a custom app_root" do
79
+ ThinkingSphinx::Configuration.configure do |config|
80
+ config.app_root = "/here/somewhere"
81
+ end
82
+ ThinkingSphinx::Configuration.instance.app_root.should == "/here/somewhere"
83
+ end
84
+ end
85
+
75
86
  describe "initialisation" do
76
87
  it "should have a default bin_path of nothing" do
77
88
  ThinkingSphinx::Configuration.instance.bin_path.should == ""
@@ -93,6 +104,81 @@ describe ThinkingSphinx::Configuration do
93
104
  end
94
105
  end
95
106
 
107
+ describe "index options" do
108
+ before :each do
109
+ @settings = {
110
+ "development" => {"disable_range" => true}
111
+ }
112
+
113
+ open("#{RAILS_ROOT}/config/sphinx.yml", "w") do |f|
114
+ f.write YAML.dump(@settings)
115
+ end
116
+
117
+ @config = ThinkingSphinx::Configuration.instance
118
+ @config.send(:parse_config)
119
+ end
120
+
121
+ it "should collect disable_range" do
122
+ @config.index_options[:disable_range].should be_true
123
+ end
124
+ end
125
+
126
+ describe "#load_models" do
127
+ before :each do
128
+ @config = ThinkingSphinx::Configuration.instance
129
+ @config.model_directories = ['']
130
+
131
+ @file_name = 'a.rb'
132
+ @model_name_lower = 'a'
133
+ @class_name = 'A'
134
+
135
+ @file_name.stub!(:gsub).and_return(@model_name_lower)
136
+ @model_name_lower.stub!(:camelize).and_return(@class_name)
137
+ Dir.stub(:[]).and_return([@file_name])
138
+ end
139
+
140
+ it "should load the files by guessing the file name" do
141
+ @class_name.should_receive(:constantize).and_return(true)
142
+
143
+ @config.load_models
144
+ end
145
+
146
+ it "should not raise errors if the model name is nil" do
147
+ @file_name.stub!(:gsub).and_return(nil)
148
+
149
+ lambda {
150
+ @config.load_models
151
+ }.should_not raise_error
152
+ end
153
+
154
+ it "should not raise errors if the file name does not represent a class name" do
155
+ @class_name.should_receive(:constantize).and_raise(NameError)
156
+
157
+ lambda {
158
+ @config.load_models
159
+ }.should_not raise_error
160
+ end
161
+
162
+ it "should retry if the first pass fails and contains a directory" do
163
+ @model_name_lower.stub!(:gsub!).and_return(true, nil)
164
+ @class_name.stub(:constantize).and_raise(LoadError)
165
+ @model_name_lower.should_receive(:camelize).twice
166
+
167
+ lambda {
168
+ @config.load_models
169
+ }.should_not raise_error
170
+ end
171
+
172
+ it "should catch database errors with a warning" do
173
+ @class_name.should_receive(:constantize).and_raise(Mysql::Error)
174
+ @config.should_receive(:puts).with('Warning: Error loading a.rb')
175
+
176
+ lambda {
177
+ @config.load_models
178
+ }.should_not raise_error
179
+ end
180
+ end
181
+
96
182
  it "should insert set index options into the configuration file" do
97
183
  config = ThinkingSphinx::Configuration.instance
98
184
  ThinkingSphinx::Configuration::IndexOptions.each do |option|
@@ -133,4 +219,4 @@ describe ThinkingSphinx::Configuration do
133
219
  }
134
220
  file.should_not match(/index alpha_core\s+\{\s+[^\}]*prefix_fields\s+=[^\}]*\}/m)
135
221
  end
136
- end
222
+ end