initforthe-thinking-sphinx 1.1.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +141 -0
  3. data/lib/thinking_sphinx.rb +215 -0
  4. data/lib/thinking_sphinx/active_record.rb +278 -0
  5. data/lib/thinking_sphinx/active_record/attribute_updates.rb +48 -0
  6. data/lib/thinking_sphinx/active_record/delta.rb +87 -0
  7. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  8. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  9. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +42 -0
  10. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +54 -0
  11. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +135 -0
  12. data/lib/thinking_sphinx/association.rb +164 -0
  13. data/lib/thinking_sphinx/attribute.rb +268 -0
  14. data/lib/thinking_sphinx/class_facet.rb +15 -0
  15. data/lib/thinking_sphinx/collection.rb +148 -0
  16. data/lib/thinking_sphinx/configuration.rb +262 -0
  17. data/lib/thinking_sphinx/core/string.rb +15 -0
  18. data/lib/thinking_sphinx/deltas.rb +30 -0
  19. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  20. data/lib/thinking_sphinx/deltas/default_delta.rb +68 -0
  21. data/lib/thinking_sphinx/deltas/delayed_delta.rb +27 -0
  22. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  23. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  24. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  25. data/lib/thinking_sphinx/deploy/capistrano.rb +82 -0
  26. data/lib/thinking_sphinx/facet.rb +108 -0
  27. data/lib/thinking_sphinx/facet_collection.rb +59 -0
  28. data/lib/thinking_sphinx/field.rb +82 -0
  29. data/lib/thinking_sphinx/index.rb +99 -0
  30. data/lib/thinking_sphinx/index/builder.rb +287 -0
  31. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  32. data/lib/thinking_sphinx/property.rb +160 -0
  33. data/lib/thinking_sphinx/rails_additions.rb +136 -0
  34. data/lib/thinking_sphinx/search.rb +727 -0
  35. data/lib/thinking_sphinx/search/facets.rb +104 -0
  36. data/lib/thinking_sphinx/source.rb +150 -0
  37. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  38. data/lib/thinking_sphinx/source/sql.rb +126 -0
  39. data/lib/thinking_sphinx/tasks.rb +162 -0
  40. data/rails/init.rb +14 -0
  41. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  42. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  43. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  44. data/spec/unit/thinking_sphinx/active_record_spec.rb +329 -0
  45. data/spec/unit/thinking_sphinx/association_spec.rb +246 -0
  46. data/spec/unit/thinking_sphinx/attribute_spec.rb +338 -0
  47. data/spec/unit/thinking_sphinx/collection_spec.rb +15 -0
  48. data/spec/unit/thinking_sphinx/configuration_spec.rb +222 -0
  49. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  50. data/spec/unit/thinking_sphinx/facet_collection_spec.rb +64 -0
  51. data/spec/unit/thinking_sphinx/facet_spec.rb +302 -0
  52. data/spec/unit/thinking_sphinx/field_spec.rb +154 -0
  53. data/spec/unit/thinking_sphinx/index/builder_spec.rb +355 -0
  54. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  55. data/spec/unit/thinking_sphinx/index_spec.rb +45 -0
  56. data/spec/unit/thinking_sphinx/rails_additions_spec.rb +191 -0
  57. data/spec/unit/thinking_sphinx/search_spec.rb +228 -0
  58. data/spec/unit/thinking_sphinx/source_spec.rb +217 -0
  59. data/spec/unit/thinking_sphinx_spec.rb +151 -0
  60. data/tasks/distribution.rb +67 -0
  61. data/tasks/rails.rake +1 -0
  62. data/tasks/testing.rb +78 -0
  63. data/vendor/after_commit/LICENSE +20 -0
  64. data/vendor/after_commit/README +16 -0
  65. data/vendor/after_commit/Rakefile +22 -0
  66. data/vendor/after_commit/init.rb +8 -0
  67. data/vendor/after_commit/lib/after_commit.rb +45 -0
  68. data/vendor/after_commit/lib/after_commit/active_record.rb +114 -0
  69. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  70. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  71. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  72. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  73. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  74. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  75. data/vendor/riddle/lib/riddle.rb +30 -0
  76. data/vendor/riddle/lib/riddle/client.rb +619 -0
  77. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  78. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  79. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  80. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  81. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  82. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  83. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  84. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  85. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  86. data/vendor/riddle/lib/riddle/configuration/section.rb +43 -0
  87. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  88. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  89. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  90. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  91. metadata +190 -0
@@ -0,0 +1,14 @@
1
+ require 'thinking_sphinx'
2
+ require 'action_controller/dispatcher'
3
+
4
+ ActionController::Dispatcher.to_prepare :thinking_sphinx do
5
+ # Force internationalisation to be loaded.
6
+ if Rails::VERSION::STRING.to_f > 2.2
7
+ I18n.backend.reload!
8
+ I18n.backend.available_locales
9
+ elsif Rails::VERSION::STRING.to_f > 2.1
10
+ I18n.backend.load_translations(*I18n.load_path)
11
+ end
12
+
13
+ ThinkingSphinx::Configuration.instance.load_models
14
+ end
@@ -0,0 +1,136 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "ThinkingSphinx::ActiveRecord::Delta" do
4
+ it "should call the toggle_delta method after a save" do
5
+ @beta = Beta.new(:name => 'beta')
6
+ @beta.stub_method(:toggle_delta => true)
7
+
8
+ @beta.save
9
+
10
+ @beta.should have_received(:toggle_delta)
11
+ end
12
+
13
+ it "should call the toggle_delta method after a save!" do
14
+ @beta = Beta.new(:name => 'beta')
15
+ @beta.stub_method(:toggle_delta => true)
16
+
17
+ @beta.save!
18
+
19
+ @beta.should have_received(:toggle_delta)
20
+ end
21
+
22
+ describe "suspended_delta method" do
23
+ before :each do
24
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
25
+ Person.sphinx_indexes.first.delta_object.stub_method(:` => "")
26
+ end
27
+
28
+ it "should execute the argument block with deltas disabled" do
29
+ ThinkingSphinx.should_receive(:deltas_enabled=).once.with(false)
30
+ ThinkingSphinx.should_receive(:deltas_enabled=).once.with(true)
31
+ lambda { Person.suspended_delta { raise 'i was called' } }.should(
32
+ raise_error(Exception)
33
+ )
34
+ end
35
+
36
+ it "should restore deltas_enabled to its original setting" do
37
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
38
+ ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
39
+ Person.suspended_delta { 'no-op' }
40
+ end
41
+
42
+ it "should restore deltas_enabled to its original setting even if there was an exception" do
43
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
44
+ ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
45
+ lambda { Person.suspended_delta { raise 'bad error' } }.should(
46
+ raise_error(Exception)
47
+ )
48
+ end
49
+
50
+ it "should reindex by default after the code block is run" do
51
+ Person.should_receive(:index_delta)
52
+ Person.suspended_delta { 'no-op' }
53
+ end
54
+
55
+ it "should not reindex after the code block if false is passed in" do
56
+ Person.should_not_receive(:index_delta)
57
+ Person.suspended_delta(false) { 'no-op' }
58
+ end
59
+ end
60
+
61
+ describe "toggle_delta method" do
62
+ it "should set the delta value to true" do
63
+ @person = Person.new
64
+
65
+ @person.delta.should be_false
66
+ @person.send(:toggle_delta)
67
+ @person.delta.should be_true
68
+ end
69
+ end
70
+
71
+ describe "index_delta method" do
72
+ before :each do
73
+ ThinkingSphinx::Configuration.stub_method(:environment => "spec")
74
+ ThinkingSphinx.stub_method(:deltas_enabled? => true, :sphinx_running? => true)
75
+ Person.delta_object.stub_methods(:` => "", :toggled => true)
76
+
77
+ @person = Person.new
78
+ @person.stub_method(
79
+ :in_both_indexes? => false,
80
+ :sphinx_document_id => 1
81
+ )
82
+
83
+ @client = Riddle::Client.new
84
+ @client.stub!(:update => true)
85
+ Riddle::Client.stub_method(:new => @client)
86
+ end
87
+
88
+ it "shouldn't index if delta indexing is disabled" do
89
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
90
+
91
+ @person.send(:index_delta)
92
+
93
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
94
+ @client.should_not have_received(:update)
95
+ end
96
+
97
+ it "shouldn't index if index updating is disabled" do
98
+ ThinkingSphinx.stub_method(:updates_enabled? => false)
99
+
100
+ @person.send(:index_delta)
101
+
102
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
103
+ end
104
+
105
+ it "shouldn't index if the environment is 'test'" do
106
+ ThinkingSphinx.unstub_method(:deltas_enabled?)
107
+ ThinkingSphinx.deltas_enabled = nil
108
+ ThinkingSphinx::Configuration.stub_method(:environment => "test")
109
+
110
+ @person.send(:index_delta)
111
+
112
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
113
+ end
114
+
115
+ it "should call indexer for the delta index" do
116
+ @person.send(:index_delta)
117
+
118
+ Person.sphinx_indexes.first.delta_object.should have_received(:`).with(
119
+ "#{ThinkingSphinx::Configuration.instance.bin_path}indexer --config #{ThinkingSphinx::Configuration.instance.config_file} --rotate person_delta"
120
+ )
121
+ end
122
+
123
+ it "shouldn't update the deleted attribute if not in the index" do
124
+ @client.should_not_receive(:update)
125
+
126
+ @person.send(:index_delta)
127
+ end
128
+
129
+ it "should update the deleted attribute if in the core index" do
130
+ @person.stub_method(:in_both_indexes? => true)
131
+ @client.should_receive(:update)
132
+
133
+ @person.send(:index_delta)
134
+ end
135
+ end
136
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe 'ThinkingSphinx::ActiveRecord::HasManyAssociation' do
4
+ describe "search method" do
5
+ before :each do
6
+ Friendship.stub_method(:search => true)
7
+
8
+ @person = Person.find(:first)
9
+ @index = Friendship.sphinx_indexes.first
10
+ end
11
+
12
+ it "should raise an error if the required attribute doesn't exist" do
13
+ @index.stub_method(:attributes => [])
14
+
15
+ lambda { @person.friendships.search "test" }.should raise_error(RuntimeError)
16
+
17
+ @index.unstub_method(:attributes)
18
+ end
19
+
20
+ it "should add a filter for the attribute into a normal search call" do
21
+ @person.friendships.search "test"
22
+
23
+ Friendship.should have_received(:search).with(
24
+ "test", :with => {:person_id => @person.id}
25
+ )
26
+ end
27
+ end
28
+
29
+ describe "search method for has_many :through" do
30
+ before :each do
31
+ Person.stub_method(:search => true)
32
+
33
+ @person = Person.find(:first)
34
+ @index = Person.sphinx_indexes.first
35
+ end
36
+
37
+ it "should raise an error if the required attribute doesn't exist" do
38
+ @index.stub_method(:attributes => [])
39
+
40
+ lambda { @person.friends.search "test" }.should raise_error(RuntimeError)
41
+
42
+ @index.unstub_method(:attributes)
43
+ end
44
+
45
+ it "should add a filter for the attribute into a normal search call" do
46
+ @person.friends.search "test"
47
+
48
+ Person.should have_received(:search).with(
49
+ "test", :with => {:friendly_ids => @person.id}
50
+ )
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,107 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "ThinkingSphinx::ActiveRecord::Search" do
4
+ it "should add search_for_ids to ActiveRecord::Base" do
5
+ ActiveRecord::Base.should respond_to("search_for_ids")
6
+ end
7
+
8
+ it "should add search_for_ids to ActiveRecord::Base" do
9
+ ActiveRecord::Base.should respond_to("search")
10
+ end
11
+
12
+ it "should add search_count to ActiveRecord::Base" do
13
+ ActiveRecord::Base.should respond_to("search_count")
14
+ end
15
+
16
+ it "should add search_for_id to ActiveRecord::Base" do
17
+ ActiveRecord::Base.should respond_to("search_for_id")
18
+ end
19
+
20
+ describe "search_for_ids method" do
21
+ before :each do
22
+ ThinkingSphinx::Search.stub_method(:search_for_ids => true)
23
+ end
24
+
25
+ it "should call ThinkingSphinx::Search#search_for_ids with the class option set" do
26
+ Person.search_for_ids("search")
27
+
28
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
29
+ "search", :class => Person
30
+ )
31
+ end
32
+
33
+ it "should override the class option" do
34
+ Person.search_for_ids("search", :class => Friendship)
35
+
36
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
37
+ "search", :class => Person
38
+ )
39
+ end
40
+ end
41
+
42
+ describe "search method" do
43
+ before :each do
44
+ ThinkingSphinx::Search.stub_method(:search => true)
45
+ end
46
+
47
+ it "should call ThinkingSphinx::Search#search with the class option set" do
48
+ Person.search("search")
49
+
50
+ ThinkingSphinx::Search.should have_received(:search).with(
51
+ "search", :class => Person
52
+ )
53
+ end
54
+
55
+ it "should override the class option" do
56
+ Person.search("search", :class => Friendship)
57
+
58
+ ThinkingSphinx::Search.should have_received(:search).with(
59
+ "search", :class => Person
60
+ )
61
+ end
62
+ end
63
+
64
+ describe "search_for_id method" do
65
+ before :each do
66
+ ThinkingSphinx::Search.stub_method(:search_for_id => true)
67
+ end
68
+
69
+ it "should call ThinkingSphinx::Search#search with the class option set" do
70
+ Person.search_for_id(10)
71
+
72
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
73
+ 10, :class => Person
74
+ )
75
+ end
76
+
77
+ it "should override the class option" do
78
+ Person.search_for_id(10, :class => Friendship)
79
+
80
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
81
+ 10, :class => Person
82
+ )
83
+ end
84
+ end
85
+
86
+ describe "search_count method" do
87
+ before :each do
88
+ ThinkingSphinx::Search.stub_method(:count => true)
89
+ end
90
+
91
+ it "should call ThinkingSphinx::Search#search with the class option set" do
92
+ Person.search_count("search")
93
+
94
+ ThinkingSphinx::Search.should have_received(:count).with(
95
+ "search", :class => Person
96
+ )
97
+ end
98
+
99
+ it "should override the class option" do
100
+ Person.search_count("search", :class => Friendship)
101
+
102
+ ThinkingSphinx::Search.should have_received(:count).with(
103
+ "search", :class => Person
104
+ )
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,329 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "ThinkingSphinx::ActiveRecord" do
4
+ describe "define_index method" do
5
+ before :each do
6
+ module ::TestModule
7
+ class TestModel < ActiveRecord::Base; end
8
+ end
9
+
10
+ TestModule::TestModel.stub_methods(
11
+ :before_save => true,
12
+ :after_commit => true,
13
+ :after_destroy => true
14
+ )
15
+
16
+ @index = ThinkingSphinx::Index.stub_instance(:delta? => false)
17
+ ThinkingSphinx::Index::Builder.stub_method(:generate => @index)
18
+ end
19
+
20
+ after :each do
21
+ # Remove the class so we can redefine it
22
+ TestModule.send(:remove_const, :TestModel)
23
+
24
+ ThinkingSphinx.indexed_models.delete "TestModule::TestModel"
25
+ end
26
+
27
+ it "should do nothing if indexes are disabled" do
28
+ ThinkingSphinx.stub_method(:define_indexes? => false)
29
+
30
+ TestModule::TestModel.define_index {}
31
+ ThinkingSphinx::Index.should_not have_received(:new)
32
+
33
+ ThinkingSphinx.unstub_method(:define_indexes?)
34
+ end
35
+
36
+ it "should add a new index to the model" do
37
+ TestModule::TestModel.define_index {}
38
+
39
+ TestModule::TestModel.sphinx_indexes.length.should == 1
40
+ end
41
+
42
+ it "should add to ThinkingSphinx.indexed_models if the model doesn't already exist in the array" do
43
+ TestModule::TestModel.define_index do; end
44
+
45
+ ThinkingSphinx.indexed_models.should include("TestModule::TestModel")
46
+ end
47
+
48
+ it "shouldn't add to ThinkingSphinx.indexed_models if the model already exists in the array" do
49
+ TestModule::TestModel.define_index do; end
50
+
51
+ ThinkingSphinx.indexed_models.select { |model|
52
+ model == "TestModule::TestModel"
53
+ }.length.should == 1
54
+
55
+ TestModule::TestModel.define_index do; end
56
+
57
+ ThinkingSphinx.indexed_models.select { |model|
58
+ model == "TestModule::TestModel"
59
+ }.length.should == 1
60
+ end
61
+
62
+ it "should add before_save and after_commit hooks to the model if delta indexing is enabled" do
63
+ @index.stub_method(:delta? => true)
64
+
65
+ TestModule::TestModel.define_index do; end
66
+
67
+ TestModule::TestModel.should have_received(:before_save).with(:toggle_delta)
68
+ TestModule::TestModel.should have_received(:after_commit).with(:index_delta)
69
+ end
70
+
71
+ it "should not add before_save and after_commit hooks to the model if delta indexing is disabled" do
72
+ TestModule::TestModel.define_index do; end
73
+
74
+ TestModule::TestModel.should_not have_received(:before_save).with(:toggle_delta)
75
+ TestModule::TestModel.should_not have_received(:after_commit).with(:index_delta)
76
+ end
77
+
78
+ it "should add an after_destroy hook with delta indexing enabled" do
79
+ @index.stub_method(:delta? => true)
80
+
81
+ TestModule::TestModel.define_index do; end
82
+
83
+ TestModule::TestModel.should have_received(:after_destroy).with(:toggle_deleted)
84
+ end
85
+
86
+ it "should add an after_destroy hook with delta indexing disabled" do
87
+ TestModule::TestModel.define_index do; end
88
+
89
+ TestModule::TestModel.should have_received(:after_destroy).with(:toggle_deleted)
90
+ end
91
+
92
+ it "should return the new index" do
93
+ TestModule::TestModel.define_index.should == @index
94
+ end
95
+
96
+ it "should die quietly if there is a database error" do
97
+ ThinkingSphinx::Index::Builder.stub_method_to_raise(:generate => Mysql::Error)
98
+
99
+ lambda {
100
+ TestModule::TestModel.define_index
101
+ }.should_not raise_error
102
+ end
103
+
104
+ it "should die noisily if there is a non-database error" do
105
+ ThinkingSphinx::Index::Builder.stub_method_to_raise(:generate => StandardError)
106
+
107
+ lambda {
108
+ TestModule::TestModel.define_index
109
+ }.should raise_error
110
+ end
111
+ end
112
+
113
+ describe "index methods" do
114
+ before(:all) do
115
+ @person = Person.find(:first)
116
+ end
117
+
118
+ describe "in_both_indexes?" do
119
+ it "should return true if in core and delta indexes" do
120
+ @person.should_receive(:in_core_index?).and_return(true)
121
+ @person.should_receive(:in_delta_index?).and_return(true)
122
+ @person.in_both_indexes?.should be_true
123
+ end
124
+
125
+ it "should return false if in one index and not the other" do
126
+ @person.should_receive(:in_core_index?).and_return(true)
127
+ @person.should_receive(:in_delta_index?).and_return(false)
128
+ @person.in_both_indexes?.should be_false
129
+ end
130
+ end
131
+
132
+ describe "in_core_index?" do
133
+ it "should call in_index? with core" do
134
+ @person.should_receive(:in_index?).with('core')
135
+ @person.in_core_index?
136
+ end
137
+ end
138
+
139
+ describe "in_delta_index?" do
140
+ it "should call in_index? with delta" do
141
+ @person.should_receive(:in_index?).with('delta')
142
+ @person.in_delta_index?
143
+ end
144
+ end
145
+
146
+ describe "in_index?" do
147
+ it "should return true if in the specified index" do
148
+ @person.should_receive(:sphinx_document_id).and_return(1)
149
+ @person.should_receive(:sphinx_index_name).and_return('person_core')
150
+ Person.should_receive(:search_for_id).with(1, 'person_core').and_return(true)
151
+
152
+ @person.in_index?('core').should be_true
153
+ end
154
+ end
155
+ end
156
+
157
+ describe "source_of_sphinx_index method" do
158
+ it "should return self if model defines an index" do
159
+ Person.source_of_sphinx_index.should == Person
160
+ end
161
+
162
+ it "should return the parent if model inherits an index" do
163
+ Admin::Person.source_of_sphinx_index.should == Person
164
+ end
165
+ end
166
+
167
+ describe "to_crc32 method" do
168
+ it "should return an integer" do
169
+ Person.to_crc32.should be_a_kind_of(Integer)
170
+ end
171
+ end
172
+
173
+ describe "to_crc32s method" do
174
+ it "should return an array" do
175
+ Person.to_crc32s.should be_a_kind_of(Array)
176
+ end
177
+ end
178
+
179
+ describe "toggle_deleted method" do
180
+ before :each do
181
+ ThinkingSphinx.stub_method(:sphinx_running? => true)
182
+
183
+ @configuration = ThinkingSphinx::Configuration.instance
184
+ @configuration.stub_methods(
185
+ :address => "an address",
186
+ :port => 123
187
+ )
188
+ @client = Riddle::Client.new
189
+ @client.stub!(:update => true)
190
+ @person = Person.find(:first)
191
+
192
+ Riddle::Client.stub_method(:new => @client)
193
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => false) }
194
+ @person.stub_method(:in_core_index? => true)
195
+ end
196
+
197
+ it "should create a client using the Configuration's address and port" do
198
+ @person.toggle_deleted
199
+
200
+ Riddle::Client.should have_received(:new).with(
201
+ @configuration.address, @configuration.port
202
+ )
203
+ end
204
+
205
+ it "should update the core index's deleted flag if in core index" do
206
+ @client.should_receive(:update).with(
207
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
208
+ )
209
+
210
+ @person.toggle_deleted
211
+ end
212
+
213
+ it "shouldn't update the core index's deleted flag if the record isn't in it" do
214
+ @person.stub_method(:in_core_index? => false)
215
+ @client.should_not_receive(:update).with(
216
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
217
+ )
218
+
219
+ @person.toggle_deleted
220
+ end
221
+
222
+ it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
223
+ ThinkingSphinx.stub_method(:sphinx_running? => false)
224
+ @client.should_not_receive(:update)
225
+
226
+ @person.toggle_deleted
227
+
228
+ @person.should_not have_received(:in_core_index?)
229
+ end
230
+
231
+ it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
232
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
233
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
234
+ @person.delta = true
235
+ @client.should_receive(:update).with(
236
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
237
+ )
238
+
239
+ @person.toggle_deleted
240
+ end
241
+
242
+ it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
243
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
244
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
245
+ @person.delta = false
246
+ @client.should_not_receive(:update).with(
247
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
248
+ )
249
+
250
+ @person.toggle_deleted
251
+ end
252
+
253
+ 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
254
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
255
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
256
+ @person.delta = 0
257
+ @client.should_not_receive(:update).with(
258
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
259
+ )
260
+
261
+ @person.toggle_deleted
262
+ end
263
+
264
+ it "shouldn't update the delta index if delta indexes are disabled" do
265
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
266
+ @client.should_not_receive(:update).with(
267
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
268
+ )
269
+
270
+ @person.toggle_deleted
271
+ end
272
+
273
+ it "should not update the delta index if delta indexing is disabled" do
274
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
275
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
276
+ @person.delta = true
277
+ @client.should_not_receive(:update).with(
278
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
279
+ )
280
+
281
+ @person.toggle_deleted
282
+ end
283
+
284
+ it "should not update either index if updates are disabled" do
285
+ ThinkingSphinx.stub_methods(
286
+ :updates_enabled? => false,
287
+ :deltas_enabled => true
288
+ )
289
+ Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
290
+ @person.delta = true
291
+ @client.should_not_receive(:update)
292
+
293
+ @person.toggle_deleted
294
+ end
295
+ end
296
+
297
+ describe "sphinx_indexes in the inheritance chain (STI)" do
298
+ it "should hand defined indexes on a class down to its child classes" do
299
+ Child.sphinx_indexes.should include(*Person.sphinx_indexes)
300
+ end
301
+
302
+ it "should allow associations to other STI models" do
303
+ source = Child.sphinx_indexes.last.sources.first
304
+ sql = source.to_riddle_for_core(0, 0).sql_query
305
+ sql.gsub!('$start', '0').gsub!('$end', '100')
306
+ lambda {
307
+ Child.connection.execute(sql)
308
+ }.should_not raise_error(ActiveRecord::StatementInvalid)
309
+ end
310
+ end
311
+
312
+ it "should return the sphinx document id as expected" do
313
+ person = Person.find(:first)
314
+ model_count = ThinkingSphinx.indexed_models.length
315
+ offset = ThinkingSphinx.indexed_models.index("Person")
316
+
317
+ (person.id * model_count + offset).should == person.sphinx_document_id
318
+
319
+ alpha = Alpha.find(:first)
320
+ offset = ThinkingSphinx.indexed_models.index("Alpha")
321
+
322
+ (alpha.id * model_count + offset).should == alpha.sphinx_document_id
323
+
324
+ beta = Beta.find(:first)
325
+ offset = ThinkingSphinx.indexed_models.index("Beta")
326
+
327
+ (beta.id * model_count + offset).should == beta.sphinx_document_id
328
+ end
329
+ end