warp-thinking-sphinx 1.2.12 → 1.3.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (144) hide show
  1. data/README.textile +21 -4
  2. data/VERSION +1 -0
  3. data/features/abstract_inheritance.feature +10 -0
  4. data/features/alternate_primary_key.feature +1 -1
  5. data/features/attribute_updates.feature +22 -5
  6. data/features/deleting_instances.feature +3 -0
  7. data/features/facets.feature +6 -0
  8. data/features/facets_across_model.feature +2 -2
  9. data/features/searching_across_models.feature +1 -1
  10. data/features/searching_by_index.feature +40 -0
  11. data/features/sphinx_scopes.feature +7 -0
  12. data/features/step_definitions/alpha_steps.rb +14 -1
  13. data/features/step_definitions/beta_steps.rb +1 -1
  14. data/features/step_definitions/common_steps.rb +12 -2
  15. data/features/step_definitions/facet_steps.rb +5 -1
  16. data/features/step_definitions/scope_steps.rb +4 -0
  17. data/features/step_definitions/sphinx_steps.rb +8 -4
  18. data/features/sti_searching.feature +5 -0
  19. data/features/support/{db/database.example.yml → database.example.yml} +0 -0
  20. data/features/support/db/fixtures/foxes.rb +3 -0
  21. data/features/support/db/fixtures/music.rb +4 -0
  22. data/features/support/db/fixtures/robots.rb +1 -1
  23. data/features/support/db/fixtures/tags.rb +1 -1
  24. data/features/support/db/migrations/create_alphas.rb +1 -0
  25. data/features/support/db/migrations/create_genres.rb +3 -0
  26. data/features/support/db/migrations/create_music.rb +6 -0
  27. data/features/support/db/migrations/create_robots.rb +1 -2
  28. data/features/support/env.rb +16 -1
  29. data/features/support/models/alpha.rb +12 -0
  30. data/features/support/models/comment.rb +3 -3
  31. data/features/support/models/fox.rb +5 -0
  32. data/features/support/models/genre.rb +3 -0
  33. data/features/support/models/medium.rb +5 -0
  34. data/features/support/models/music.rb +8 -0
  35. data/features/support/models/post.rb +2 -1
  36. data/features/support/models/robot.rb +4 -0
  37. data/lib/cucumber/thinking_sphinx/external_world.rb +8 -0
  38. data/lib/cucumber/thinking_sphinx/internal_world.rb +126 -0
  39. data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
  40. data/lib/thinking_sphinx.rb +56 -37
  41. data/lib/thinking_sphinx/active_record.rb +257 -192
  42. data/lib/thinking_sphinx/active_record/attribute_updates.rb +10 -12
  43. data/lib/thinking_sphinx/active_record/delta.rb +0 -26
  44. data/lib/thinking_sphinx/active_record/scopes.rb +37 -1
  45. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +1 -1
  46. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +18 -11
  47. data/lib/thinking_sphinx/attribute.rb +19 -4
  48. data/lib/thinking_sphinx/auto_version.rb +22 -0
  49. data/lib/thinking_sphinx/configuration.rb +57 -59
  50. data/lib/thinking_sphinx/context.rb +74 -0
  51. data/lib/thinking_sphinx/deltas.rb +0 -2
  52. data/lib/thinking_sphinx/deltas/default_delta.rb +14 -20
  53. data/lib/thinking_sphinx/deploy/capistrano.rb +1 -1
  54. data/lib/thinking_sphinx/facet_search.rb +3 -1
  55. data/lib/thinking_sphinx/index.rb +77 -19
  56. data/lib/thinking_sphinx/index/builder.rb +2 -2
  57. data/lib/thinking_sphinx/search.rb +47 -9
  58. data/lib/thinking_sphinx/search_methods.rb +22 -4
  59. data/lib/thinking_sphinx/source.rb +9 -8
  60. data/lib/thinking_sphinx/source/sql.rb +5 -3
  61. data/lib/thinking_sphinx/tasks.rb +13 -57
  62. data/lib/thinking_sphinx/test.rb +52 -0
  63. data/rails/init.rb +4 -2
  64. data/spec/{lib/thinking_sphinx → thinking_sphinx}/active_record/delta_spec.rb +4 -6
  65. data/spec/{lib/thinking_sphinx → thinking_sphinx}/active_record/has_many_association_spec.rb +0 -0
  66. data/spec/thinking_sphinx/active_record/scopes_spec.rb +177 -0
  67. data/spec/thinking_sphinx/active_record_spec.rb +622 -0
  68. data/spec/{lib/thinking_sphinx → thinking_sphinx}/association_spec.rb +0 -0
  69. data/spec/{lib/thinking_sphinx → thinking_sphinx}/attribute_spec.rb +39 -0
  70. data/spec/thinking_sphinx/auto_version_spec.rb +39 -0
  71. data/spec/{lib/thinking_sphinx → thinking_sphinx}/configuration_spec.rb +27 -61
  72. data/spec/thinking_sphinx/context_spec.rb +119 -0
  73. data/spec/{lib/thinking_sphinx → thinking_sphinx}/core/array_spec.rb +0 -0
  74. data/spec/{lib/thinking_sphinx → thinking_sphinx}/core/string_spec.rb +0 -0
  75. data/spec/{lib/thinking_sphinx → thinking_sphinx}/excerpter_spec.rb +0 -0
  76. data/spec/{lib/thinking_sphinx → thinking_sphinx}/facet_search_spec.rb +0 -0
  77. data/spec/{lib/thinking_sphinx → thinking_sphinx}/facet_spec.rb +0 -0
  78. data/spec/{lib/thinking_sphinx → thinking_sphinx}/field_spec.rb +0 -0
  79. data/spec/{lib/thinking_sphinx → thinking_sphinx}/index/builder_spec.rb +24 -0
  80. data/spec/{lib/thinking_sphinx → thinking_sphinx}/index/faux_column_spec.rb +0 -0
  81. data/spec/thinking_sphinx/index_spec.rb +183 -0
  82. data/spec/{lib/thinking_sphinx → thinking_sphinx}/rails_additions_spec.rb +0 -0
  83. data/spec/{lib/thinking_sphinx → thinking_sphinx}/search_methods_spec.rb +0 -0
  84. data/spec/{lib/thinking_sphinx → thinking_sphinx}/search_spec.rb +41 -0
  85. data/spec/{lib/thinking_sphinx → thinking_sphinx}/source_spec.rb +1 -1
  86. data/spec/thinking_sphinx_spec.rb +204 -0
  87. data/tasks/distribution.rb +6 -20
  88. data/tasks/testing.rb +8 -19
  89. metadata +117 -142
  90. data/VERSION.yml +0 -4
  91. data/features/a.rb +0 -17
  92. data/features/datetime_deltas.feature +0 -66
  93. data/features/delayed_delta_indexing.feature +0 -37
  94. data/features/step_definitions/datetime_delta_steps.rb +0 -15
  95. data/features/step_definitions/delayed_delta_indexing_steps.rb +0 -7
  96. data/features/support/db/active_record.rb +0 -40
  97. data/features/support/db/fixtures/delayed_betas.rb +0 -10
  98. data/features/support/db/fixtures/thetas.rb +0 -10
  99. data/features/support/db/migrations/create_delayed_betas.rb +0 -17
  100. data/features/support/db/migrations/create_thetas.rb +0 -5
  101. data/features/support/db/mysql.rb +0 -3
  102. data/features/support/db/postgresql.rb +0 -3
  103. data/features/support/models/delayed_beta.rb +0 -7
  104. data/features/support/models/theta.rb +0 -7
  105. data/features/support/post_database.rb +0 -43
  106. data/features/support/z.rb +0 -19
  107. data/lib/thinking_sphinx/deltas/datetime_delta.rb +0 -50
  108. data/lib/thinking_sphinx/deltas/delayed_delta.rb +0 -30
  109. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +0 -24
  110. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +0 -27
  111. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +0 -26
  112. data/spec/lib/thinking_sphinx/active_record/scopes_spec.rb +0 -96
  113. data/spec/lib/thinking_sphinx/active_record_spec.rb +0 -353
  114. data/spec/lib/thinking_sphinx/deltas/job_spec.rb +0 -32
  115. data/spec/lib/thinking_sphinx/index_spec.rb +0 -45
  116. data/spec/lib/thinking_sphinx_spec.rb +0 -162
  117. data/vendor/after_commit/LICENSE +0 -20
  118. data/vendor/after_commit/README +0 -16
  119. data/vendor/after_commit/Rakefile +0 -22
  120. data/vendor/after_commit/init.rb +0 -8
  121. data/vendor/after_commit/lib/after_commit.rb +0 -45
  122. data/vendor/after_commit/lib/after_commit/active_record.rb +0 -114
  123. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +0 -103
  124. data/vendor/after_commit/test/after_commit_test.rb +0 -53
  125. data/vendor/delayed_job/lib/delayed/job.rb +0 -251
  126. data/vendor/delayed_job/lib/delayed/message_sending.rb +0 -7
  127. data/vendor/delayed_job/lib/delayed/performable_method.rb +0 -55
  128. data/vendor/delayed_job/lib/delayed/worker.rb +0 -54
  129. data/vendor/riddle/lib/riddle.rb +0 -30
  130. data/vendor/riddle/lib/riddle/client.rb +0 -635
  131. data/vendor/riddle/lib/riddle/client/filter.rb +0 -53
  132. data/vendor/riddle/lib/riddle/client/message.rb +0 -66
  133. data/vendor/riddle/lib/riddle/client/response.rb +0 -84
  134. data/vendor/riddle/lib/riddle/configuration.rb +0 -33
  135. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +0 -48
  136. data/vendor/riddle/lib/riddle/configuration/index.rb +0 -142
  137. data/vendor/riddle/lib/riddle/configuration/indexer.rb +0 -19
  138. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +0 -17
  139. data/vendor/riddle/lib/riddle/configuration/searchd.rb +0 -25
  140. data/vendor/riddle/lib/riddle/configuration/section.rb +0 -43
  141. data/vendor/riddle/lib/riddle/configuration/source.rb +0 -23
  142. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +0 -34
  143. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +0 -28
  144. data/vendor/riddle/lib/riddle/controller.rb +0 -53
@@ -504,4 +504,43 @@ describe ThinkingSphinx::Attribute do
504
504
  @statement.should match(/SELECT cricket_team_id, id FROM tags/)
505
505
  end
506
506
  end
507
+
508
+ describe '#live_value' do
509
+ before :each do
510
+ @attribute = ThinkingSphinx::Attribute.new @source, [
511
+ stub('column', :__stack => [], :__name => "col_name")
512
+ ]
513
+ @instance = stub('model')
514
+ end
515
+
516
+ it "should translate boolean values to integers" do
517
+ @instance.stub!(:col_name => true)
518
+ @attribute.live_value(@instance).should == 1
519
+
520
+ @instance.stub!(:col_name => false)
521
+ @attribute.live_value(@instance).should == 0
522
+ end
523
+
524
+ it "should translate timestamps to integers" do
525
+ now = Time.now
526
+ @instance.stub!(:col_name => now)
527
+ @attribute.live_value(@instance).should == now.to_i
528
+ end
529
+
530
+ it "should translate dates to timestamp integers" do
531
+ today = Date.today
532
+ @instance.stub!(:col_name => today)
533
+ @attribute.live_value(@instance).should == today.to_time.to_i
534
+ end
535
+
536
+ it "should translate nils to 0" do
537
+ @instance.stub!(:col_name => nil)
538
+ @attribute.live_value(@instance).should == 0
539
+ end
540
+
541
+ it "should return integers as integers" do
542
+ @instance.stub!(:col_name => 42)
543
+ @attribute.live_value(@instance).should == 42
544
+ end
545
+ end
507
546
  end
@@ -0,0 +1,39 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::AutoVersion do
4
+ describe '.detect' do
5
+ before :each do
6
+ @controller = ThinkingSphinx::Configuration.instance.controller
7
+ end
8
+
9
+ it "should require 0.9.8 if that is the detected version" do
10
+ ThinkingSphinx::AutoVersion.should_receive(:require).
11
+ with('riddle/0.9.8')
12
+
13
+ @controller.stub!(:sphinx_version => '0.9.8')
14
+ ThinkingSphinx::AutoVersion.detect
15
+ end
16
+
17
+ it "should require 0.9.9 if that is the detected version" do
18
+ ThinkingSphinx::AutoVersion.should_receive(:require).
19
+ with('riddle/0.9.9')
20
+
21
+ @controller.stub!(:sphinx_version => '0.9.9')
22
+ ThinkingSphinx::AutoVersion.detect
23
+ end
24
+
25
+ it "should output a warning if the detected version is something else" do
26
+ STDERR.should_receive(:puts)
27
+
28
+ @controller.stub!(:sphinx_version => '0.9.7')
29
+ ThinkingSphinx::AutoVersion.detect
30
+ end
31
+
32
+ it "should output a warning if the version cannot be determined" do
33
+ STDERR.should_receive(:puts)
34
+
35
+ @controller.stub!(:sphinx_version => nil)
36
+ ThinkingSphinx::AutoVersion.detect
37
+ end
38
+ end
39
+ end
@@ -3,8 +3,8 @@ require 'spec/spec_helper'
3
3
  describe ThinkingSphinx::Configuration do
4
4
  describe "environment class method" do
5
5
  before :each do
6
- ThinkingSphinx::Configuration.send(:class_variable_set, :@@environment, nil)
7
-
6
+ Thread.current[:thinking_sphinx_environment] = nil
7
+
8
8
  ENV["RAILS_ENV"] = nil
9
9
  end
10
10
 
@@ -50,7 +50,8 @@ describe ThinkingSphinx::Configuration do
50
50
  "charset_table" => "table",
51
51
  "ignore_chars" => "e",
52
52
  "searchd_binary_name" => "sphinx-searchd",
53
- "indexer_binary_name" => "sphinx-indexer"
53
+ "indexer_binary_name" => "sphinx-indexer",
54
+ "index_exact_words" => true
54
55
  }
55
56
  }
56
57
 
@@ -129,65 +130,11 @@ describe ThinkingSphinx::Configuration do
129
130
  end
130
131
  end
131
132
 
132
- describe "#load_models" do
133
- before :each do
134
- @config = ThinkingSphinx::Configuration.instance
135
- @config.model_directories = ['']
136
-
137
- @file_name = 'a.rb'
138
- @model_name_lower = 'a'
139
- @class_name = 'A'
140
-
141
- @file_name.stub!(:gsub).and_return(@model_name_lower)
142
- @model_name_lower.stub!(:camelize).and_return(@class_name)
143
- Dir.stub(:[]).and_return([@file_name])
144
- end
145
-
146
- it "should load the files by guessing the file name" do
147
- @class_name.should_receive(:constantize).and_return(true)
148
-
149
- @config.load_models
150
- end
151
-
152
- it "should not raise errors if the model name is nil" do
153
- @file_name.stub!(:gsub).and_return(nil)
154
-
155
- lambda {
156
- @config.load_models
157
- }.should_not raise_error
158
- end
159
-
160
- it "should not raise errors if the file name does not represent a class name" do
161
- @class_name.should_receive(:constantize).and_raise(NameError)
162
-
163
- lambda {
164
- @config.load_models
165
- }.should_not raise_error
166
- end
167
-
168
- it "should retry if the first pass fails and contains a directory" do
169
- @model_name_lower.stub!(:gsub!).and_return(true, nil)
170
- @class_name.stub(:constantize).and_raise(LoadError)
171
- @model_name_lower.should_receive(:camelize).twice
172
-
173
- lambda {
174
- @config.load_models
175
- }.should_not raise_error
176
- end
177
-
178
- it "should catch database errors with a warning" do
179
- @class_name.should_receive(:constantize).and_raise(Mysql::Error)
180
- @config.should_receive(:puts).with('Warning: Error loading a.rb')
181
-
182
- lambda {
183
- @config.load_models
184
- }.should_not raise_error
185
- end
186
- end
187
-
188
133
  it "should insert set index options into the configuration file" do
189
134
  config = ThinkingSphinx::Configuration.instance
135
+
190
136
  ThinkingSphinx::Configuration::IndexOptions.each do |option|
137
+ config.reset
191
138
  config.index_options[option.to_sym] = "something"
192
139
  config.build
193
140
 
@@ -200,15 +147,34 @@ describe ThinkingSphinx::Configuration do
200
147
 
201
148
  it "should insert set source options into the configuration file" do
202
149
  config = ThinkingSphinx::Configuration.instance
150
+ config.reset
151
+
152
+ config.source_options[:sql_query_pre] = ["something"]
203
153
  ThinkingSphinx::Configuration::SourceOptions.each do |option|
204
- config.source_options[option.to_sym] = "something"
154
+ config.source_options[option.to_sym] ||= "something"
205
155
  config.build
206
156
 
207
157
  file = open(config.config_file) { |f| f.read }
208
158
  file.should match(/#{option}\s+= something/)
209
159
 
210
- config.source_options[option.to_sym] = nil
160
+ config.source_options.delete option.to_sym
211
161
  end
162
+
163
+ config.source_options[:sql_query_pre] = nil
164
+ end
165
+
166
+ it "should not blow away delta or utf options if sql pre is specified in config" do
167
+ config = ThinkingSphinx::Configuration.instance
168
+ config.reset
169
+
170
+ config.source_options[:sql_query_pre] = ["a pre query"]
171
+ config.build
172
+ file = open(config.config_file) { |f| f.read }
173
+
174
+ file.should match(/sql_query_pre = a pre query\n\s*sql_query_pre = UPDATE `\w+` SET `delta` = 0 WHERE `delta` = 1/im)
175
+ file.should match(/sql_query_pre = a pre query\n\s*sql_query_pre = \n/im)
176
+
177
+ config.source_options[:sql_query_pre] = nil
212
178
  end
213
179
 
214
180
  it "should set any explicit prefixed or infixed fields" do
@@ -0,0 +1,119 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Context do
4
+ before :each do
5
+ @context = ThinkingSphinx::Context.new
6
+ end
7
+
8
+ describe '#prepare' do
9
+ before :each do
10
+ @config = ThinkingSphinx::Configuration.instance
11
+ @config.model_directories = ['']
12
+
13
+ @file_name = 'a.rb'
14
+ @model_name_lower = 'a'
15
+ @class_name = 'A'
16
+
17
+ @file_name.stub!(:gsub).and_return(@model_name_lower)
18
+ @model_name_lower.stub!(:camelize).and_return(@class_name)
19
+ Dir.stub(:[]).and_return([@file_name])
20
+ end
21
+
22
+ it "should load the files by guessing the file name" do
23
+ @class_name.should_receive(:constantize).and_return(true)
24
+
25
+ @context.prepare
26
+ end
27
+
28
+ it "should not raise errors if the model name is nil" do
29
+ @file_name.stub!(:gsub).and_return(nil)
30
+
31
+ lambda {
32
+ @context.prepare
33
+ }.should_not raise_error
34
+ end
35
+
36
+ it "should not raise errors if the file name does not represent a class name" do
37
+ @class_name.should_receive(:constantize).and_raise(NameError)
38
+
39
+ lambda {
40
+ @context.prepare
41
+ }.should_not raise_error
42
+ end
43
+
44
+ it "should retry if the first pass fails and contains a directory" do
45
+ @model_name_lower.stub!(:gsub!).and_return(true, nil)
46
+ @class_name.stub(:constantize).and_raise(LoadError)
47
+ @model_name_lower.should_receive(:camelize).twice
48
+
49
+ lambda {
50
+ @context.prepare
51
+ }.should_not raise_error
52
+ end
53
+
54
+ it "should catch database errors with a warning" do
55
+ @class_name.should_receive(:constantize).and_raise(Mysql::Error)
56
+ STDERR.should_receive(:puts).with('Warning: Error loading a.rb')
57
+
58
+ lambda {
59
+ @context.prepare
60
+ }.should_not raise_error
61
+ end
62
+ end
63
+
64
+ describe '#define_indexes' do
65
+ it "should call define_indexes on all known indexed models" do
66
+ @context.stub!(:indexed_models => ['Alpha', 'Beta'])
67
+ Alpha.should_receive(:define_indexes)
68
+ Beta.should_receive(:define_indexes)
69
+
70
+ @context.define_indexes
71
+ end
72
+ end
73
+
74
+ describe '#add_indexed_model' do
75
+ before :each do
76
+ @context.indexed_models.clear
77
+ end
78
+
79
+ it "should add the model to the collection" do
80
+ @context.add_indexed_model 'Alpha'
81
+
82
+ @context.indexed_models.should == ['Alpha']
83
+ end
84
+
85
+ it "should not duplicate models in the collection" do
86
+ @context.add_indexed_model 'Alpha'
87
+ @context.add_indexed_model 'Alpha'
88
+
89
+ @context.indexed_models.should == ['Alpha']
90
+ end
91
+
92
+ it "should keep the collection in alphabetical order" do
93
+ @context.add_indexed_model 'Beta'
94
+ @context.add_indexed_model 'Alpha'
95
+
96
+ @context.indexed_models.should == ['Alpha', 'Beta']
97
+ end
98
+
99
+ it "should translate classes to their names" do
100
+ @context.add_indexed_model Alpha
101
+
102
+ @context.indexed_models.should == ['Alpha']
103
+ end
104
+ end
105
+
106
+ describe '#superclass_indexed_models' do
107
+ it "should return indexed model names" do
108
+ @context.stub!(:indexed_models => ['Alpha', 'Beta'])
109
+
110
+ @context.superclass_indexed_models.should == ['Alpha', 'Beta']
111
+ end
112
+
113
+ it "should not include classes which have indexed superclasses" do
114
+ @context.stub!(:indexed_models => ['Parent', 'Person'])
115
+
116
+ @context.superclass_indexed_models.should == ['Person']
117
+ end
118
+ end
119
+ end
@@ -452,4 +452,28 @@ describe ThinkingSphinx::Index::Builder do
452
452
  @index.delta_object.should be_a_kind_of(ThinkingSphinx::Deltas::DefaultDelta)
453
453
  end
454
454
  end
455
+
456
+ context 'index options' do
457
+ before :each do
458
+ @index = ThinkingSphinx::Index::Builder.generate(Person) do
459
+ indexes first_name
460
+
461
+ set_property :index_exact_words => true
462
+ end
463
+ end
464
+
465
+ it "should track the index_exact_words option to the index" do
466
+ @index.local_options[:index_exact_words].should be_true
467
+ end
468
+ end
469
+
470
+ context 'with an explicit name' do
471
+ it "should set the index's name using the provided value" do
472
+ index = ThinkingSphinx::Index::Builder.generate(Person, 'custom') do
473
+ indexes first_name
474
+ end
475
+
476
+ index.name.should == 'custom'
477
+ end
478
+ end
455
479
  end
@@ -0,0 +1,183 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Index do
4
+ describe "prefix_fields method" do
5
+ before :each do
6
+ @index = ThinkingSphinx::Index.new(Person)
7
+
8
+ @field_a = stub('field', :prefixes => true)
9
+ @field_b = stub('field', :prefixes => false)
10
+ @field_c = stub('field', :prefixes => true)
11
+
12
+ @index.stub!(:fields => [@field_a, @field_b, @field_c])
13
+ end
14
+
15
+ it "should return fields that are flagged as prefixed" do
16
+ @index.prefix_fields.should include(@field_a)
17
+ @index.prefix_fields.should include(@field_c)
18
+ end
19
+
20
+ it "should not return fields that aren't flagged as prefixed" do
21
+ @index.prefix_fields.should_not include(@field_b)
22
+ end
23
+ end
24
+
25
+ describe "infix_fields method" do
26
+ before :each do
27
+ @index = ThinkingSphinx::Index.new(Person)
28
+
29
+ @field_a = stub('field', :infixes => true)
30
+ @field_b = stub('field', :infixes => false)
31
+ @field_c = stub('field', :infixes => true)
32
+
33
+ @index.stub!(:fields => [@field_a, @field_b, @field_c])
34
+ end
35
+
36
+ it "should return fields that are flagged as infixed" do
37
+ @index.infix_fields.should include(@field_a)
38
+ @index.infix_fields.should include(@field_c)
39
+ end
40
+
41
+ it "should not return fields that aren't flagged as infixed" do
42
+ @index.infix_fields.should_not include(@field_b)
43
+ end
44
+ end
45
+
46
+ describe '.name_for' do
47
+ it "should return the model's name downcased" do
48
+ ThinkingSphinx::Index.name_for(Alpha).should == 'alpha'
49
+ end
50
+
51
+ it "should separate words by underscores" do
52
+ ThinkingSphinx::Index.name_for(ActiveRecord).should == 'active_record'
53
+ end
54
+
55
+ it "should separate namespaces by underscores" do
56
+ ThinkingSphinx::Index.name_for(ActiveRecord::Base).
57
+ should == 'active_record_base'
58
+ end
59
+ end
60
+
61
+ describe '#name' do
62
+ it "should return the downcased name of the index's model" do
63
+ ThinkingSphinx::Index.new(Alpha).name.should == 'alpha'
64
+ end
65
+
66
+ it "should return a custom name if one is set" do
67
+ index = ThinkingSphinx::Index.new(Alpha)
68
+ index.name = 'custom'
69
+ index.name.should == 'custom'
70
+ end
71
+ end
72
+
73
+ describe '#core_name' do
74
+ it "should take the index's name and append _core" do
75
+ ThinkingSphinx::Index.new(Alpha).core_name.should == 'alpha_core'
76
+ end
77
+ end
78
+
79
+ describe '#delta_name' do
80
+ it "should take the index's name and append _delta" do
81
+ ThinkingSphinx::Index.new(Alpha).delta_name.should == 'alpha_delta'
82
+ end
83
+ end
84
+
85
+ describe '#all_names' do
86
+ it "should return the core index name by default" do
87
+ ThinkingSphinx::Index.new(Alpha).all_names.should == ['alpha_core']
88
+ end
89
+
90
+ it "should return both core and delta names if deltas are enabled" do
91
+ index = ThinkingSphinx::Index.new(Alpha)
92
+ index.delta_object = stub('delta')
93
+
94
+ index.all_names.should == ['alpha_core', 'alpha_delta']
95
+ end
96
+
97
+ it "should respect custom names" do
98
+ index = ThinkingSphinx::Index.new(Alpha)
99
+ index.name = 'custom'
100
+
101
+ index.all_names.should == ['custom_core']
102
+ end
103
+
104
+ it "should respect custom names when deltas are enabled" do
105
+ index = ThinkingSphinx::Index.new(Alpha)
106
+ index.name = 'custom'
107
+ index.delta_object = stub('delta')
108
+
109
+ index.all_names.should == ['custom_core', 'custom_delta']
110
+ end
111
+ end
112
+
113
+ describe '#to_riddle' do
114
+ it "should return two Riddle indexes if deltas are disabled" do
115
+ index = ThinkingSphinx::Index.new(Alpha)
116
+
117
+ index.to_riddle(0).length.should == 2
118
+ end
119
+
120
+ it "should return three Riddle indexes if deltas are enabled" do
121
+ index = ThinkingSphinx::Index.new(Beta)
122
+ index.delta_object = stub('delta')
123
+
124
+ index.to_riddle(0).length.should == 3
125
+ end
126
+
127
+ it "should include a distributed index" do
128
+ index = ThinkingSphinx::Index.new(Alpha)
129
+
130
+ index.to_riddle(0).last.
131
+ should be_a(Riddle::Configuration::DistributedIndex)
132
+ end
133
+
134
+ context 'core index' do
135
+ it "should use the core name" do
136
+ @index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
137
+ @index.name.should == 'alpha_core'
138
+ end
139
+
140
+ it "should not try to set disable_range on the index" do
141
+ ThinkingSphinx::Configuration.instance.
142
+ index_options[:disable_range] = true
143
+
144
+ lambda {
145
+ @index = ThinkingSphinx::Index.new(Alpha).to_riddle(0).first
146
+ }.should_not raise_error(NoMethodError)
147
+ end
148
+ end
149
+
150
+ context 'delta index' do
151
+ before :each do
152
+ index = ThinkingSphinx::Index.new(Beta)
153
+ index.delta_object = stub('delta')
154
+ @index = index.to_riddle(0)[1]
155
+ end
156
+
157
+ it "should use the delta name" do
158
+ @index.name.should == 'beta_delta'
159
+ end
160
+ end
161
+
162
+ context 'distributed index' do
163
+ it "should use the index's name" do
164
+ index = ThinkingSphinx::Index.new(Alpha)
165
+
166
+ index.to_riddle(0).last.name.should == 'alpha'
167
+ end
168
+
169
+ it "should add the core index" do
170
+ index = ThinkingSphinx::Index.new(Alpha)
171
+
172
+ index.to_riddle(0).last.local_indexes.should include('alpha_core')
173
+ end
174
+
175
+ it "should add the delta index if there is one" do
176
+ index = ThinkingSphinx::Index.new(Beta)
177
+ index.delta_object = stub('delta')
178
+
179
+ index.to_riddle(0).last.local_indexes.should include('beta_delta')
180
+ end
181
+ end
182
+ end
183
+ end