ryanb-thinking_sphinx 0.9.8

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 (38) hide show
  1. data/LICENCE +20 -0
  2. data/README +60 -0
  3. data/lib/riddle.rb +26 -0
  4. data/lib/riddle/client.rb +639 -0
  5. data/lib/riddle/client/filter.rb +44 -0
  6. data/lib/riddle/client/message.rb +65 -0
  7. data/lib/riddle/client/response.rb +84 -0
  8. data/lib/test.rb +46 -0
  9. data/lib/thinking_sphinx.rb +102 -0
  10. data/lib/thinking_sphinx/active_record.rb +141 -0
  11. data/lib/thinking_sphinx/active_record/delta.rb +97 -0
  12. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  13. data/lib/thinking_sphinx/active_record/search.rb +50 -0
  14. data/lib/thinking_sphinx/association.rb +144 -0
  15. data/lib/thinking_sphinx/attribute.rb +284 -0
  16. data/lib/thinking_sphinx/configuration.rb +283 -0
  17. data/lib/thinking_sphinx/field.rb +200 -0
  18. data/lib/thinking_sphinx/index.rb +340 -0
  19. data/lib/thinking_sphinx/index/builder.rb +195 -0
  20. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  21. data/lib/thinking_sphinx/rails_additions.rb +56 -0
  22. data/lib/thinking_sphinx/search.rb +482 -0
  23. data/lib/thinking_sphinx/tasks.rb +86 -0
  24. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +207 -0
  25. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  26. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  27. data/spec/unit/thinking_sphinx/active_record_spec.rb +236 -0
  28. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  29. data/spec/unit/thinking_sphinx/attribute_spec.rb +360 -0
  30. data/spec/unit/thinking_sphinx/configuration_spec.rb +493 -0
  31. data/spec/unit/thinking_sphinx/field_spec.rb +219 -0
  32. data/spec/unit/thinking_sphinx/index/builder_spec.rb +33 -0
  33. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +68 -0
  34. data/spec/unit/thinking_sphinx/index_spec.rb +277 -0
  35. data/spec/unit/thinking_sphinx/search_spec.rb +190 -0
  36. data/spec/unit/thinking_sphinx_spec.rb +129 -0
  37. data/tasks/thinking_sphinx_tasks.rake +1 -0
  38. metadata +103 -0
@@ -0,0 +1,236 @@
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.stub_method(:new => @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::Index.unstub_method(:new)
25
+ end
26
+
27
+ it "should return nil and do nothing if indexes are disabled" do
28
+ ThinkingSphinx.stub_method(:define_indexes? => false)
29
+
30
+ TestModule::TestModel.define_index {}.should be_nil
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 do; end
38
+
39
+ TestModule::TestModel.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)
68
+ TestModule::TestModel.should have_received(:after_commit)
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)
75
+ TestModule::TestModel.should_not have_received(:after_commit)
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
+ end
96
+
97
+ describe "to_crc32 method" do
98
+ it "should return an integer" do
99
+ Person.to_crc32.should be_a_kind_of(Integer)
100
+ end
101
+ end
102
+
103
+ describe "in_core_index? method" do
104
+ it "should return the model's corresponding search_for_id value" do
105
+ Person.stub_method(:search_for_id => :searching_for_id)
106
+
107
+ person = Person.find(:first)
108
+ person.in_core_index?.should == :searching_for_id
109
+ Person.should have_received(:search_for_id).with(person.id, "person_core")
110
+ end
111
+ end
112
+
113
+ describe "toggle_deleted method" do
114
+ before :each do
115
+ @configuration = ThinkingSphinx::Configuration.stub_instance(
116
+ :address => "an address",
117
+ :port => 123
118
+ )
119
+ @client = Riddle::Client.stub_instance(:update => true)
120
+ @person = Person.new
121
+
122
+ ThinkingSphinx::Configuration.stub_method(:new => @configuration)
123
+ Riddle::Client.stub_method(:new => @client)
124
+ Person.indexes.each { |index| index.stub_method(:delta? => false) }
125
+ @person.stub_method(:in_core_index? => true)
126
+ end
127
+
128
+ it "should create a client using the Configuration's address and port" do
129
+ @person.toggle_deleted
130
+
131
+ Riddle::Client.should have_received(:new).with(
132
+ @configuration.address, @configuration.port
133
+ )
134
+ end
135
+
136
+ it "should update the core index's deleted flag if in core index" do
137
+ @person.toggle_deleted
138
+
139
+ @client.should have_received(:update).with(
140
+ "person_core", ["sphinx_deleted"], {@person.id => 1}
141
+ )
142
+ end
143
+
144
+ it "shouldn't update the core index's deleted flag if the record isn't in it" do
145
+ @person.stub_method(:in_core_index? => false)
146
+
147
+ @person.toggle_deleted
148
+
149
+ @client.should_not have_received(:update).with(
150
+ "person_core", ["sphinx_deleted"], {@person.id => 1}
151
+ )
152
+ end
153
+
154
+ it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
155
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
156
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
157
+ @person.delta = true
158
+
159
+ @person.toggle_deleted
160
+
161
+ @client.should have_received(:update).with(
162
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
163
+ )
164
+ end
165
+
166
+ it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
167
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
168
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
169
+ @person.delta = false
170
+
171
+ @person.toggle_deleted
172
+
173
+ @client.should_not have_received(:update).with(
174
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
175
+ )
176
+ end
177
+
178
+ 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
179
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
180
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
181
+ @person.delta = 0
182
+
183
+ @person.toggle_deleted
184
+
185
+ @client.should_not have_received(:update).with(
186
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
187
+ )
188
+ end
189
+
190
+ it "shouldn't update the delta index if delta indexes are disabled" do
191
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
192
+ @person.toggle_deleted
193
+
194
+ @client.should_not have_received(:update).with(
195
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
196
+ )
197
+ end
198
+
199
+ it "should not update the delta index if delta indexing is disabled" do
200
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
201
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
202
+ @person.delta = true
203
+
204
+ @person.toggle_deleted
205
+
206
+ @client.should_not have_received(:update).with(
207
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
208
+ )
209
+ end
210
+
211
+ it "should not update either index if updates are disabled" do
212
+ ThinkingSphinx.stub_methods(
213
+ :updates_enabled? => false,
214
+ :deltas_enabled => true
215
+ )
216
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
217
+ @person.delta = true
218
+
219
+ @person.toggle_deleted
220
+
221
+ @client.should_not have_received(:update)
222
+ end
223
+ end
224
+
225
+ describe "indexes in the inheritance chain (STI)" do
226
+ it "should hand defined indexes on a class down to its child classes" do
227
+ Child.indexes.should include(*Person.indexes)
228
+ end
229
+
230
+ it "should allow associations to other STI models" do
231
+ Child.indexes.last.link!
232
+ sql = Child.indexes.last.to_sql.gsub('$start', '0').gsub('$end', '100')
233
+ lambda { Child.connection.execute(sql) }.should_not raise_error(ActiveRecord::StatementInvalid)
234
+ end
235
+ end
236
+ end
@@ -0,0 +1,247 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Association do
4
+ describe "class-level children method" do
5
+ before :each do
6
+ @normal_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
7
+ :options => {:polymorphic => false}
8
+ )
9
+ @normal_association = ThinkingSphinx::Association.stub_instance
10
+ @poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
11
+ :options => {:polymorphic => true},
12
+ :macro => :has_many,
13
+ :name => "polly",
14
+ :active_record => "AR"
15
+ )
16
+ @non_poly_reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance
17
+
18
+ Person.stub_method(:reflect_on_association => @normal_reflection)
19
+ ThinkingSphinx::Association.stub_methods(
20
+ :new => @normal_association,
21
+ :polymorphic_classes => [Person, Person],
22
+ :casted_options => {:casted => :options}
23
+ )
24
+ ::ActiveRecord::Reflection::AssociationReflection.stub_method(
25
+ :new => @non_poly_reflection
26
+ )
27
+ end
28
+
29
+ it "should return an empty array if no association exists" do
30
+ Person.stub_method(:reflect_on_association => nil)
31
+
32
+ ThinkingSphinx::Association.children(Person, :assoc).should == []
33
+ end
34
+
35
+ it "should return a single association instance in an array if assocation isn't polymorphic" do
36
+ ThinkingSphinx::Association.children(Person, :assoc).should == [@normal_association]
37
+ end
38
+
39
+ it "should return multiple association instances for polymorphic associations" do
40
+ Person.stub_method(:reflect_on_association => @poly_reflection)
41
+
42
+ ThinkingSphinx::Association.children(Person, :assoc).should ==
43
+ [@normal_association, @normal_association]
44
+ end
45
+
46
+ it "should generate non-polymorphic 'casted' associations for each polymorphic possibility" do
47
+ Person.stub_method(:reflect_on_association => @poly_reflection)
48
+
49
+ ThinkingSphinx::Association.children(Person, :assoc)
50
+
51
+ ThinkingSphinx::Association.should have_received(:casted_options).with(
52
+ Person, @poly_reflection
53
+ ).twice
54
+
55
+ ::ActiveRecord::Reflection::AssociationReflection.should have_received(:new).with(
56
+ :has_many, :polly_Person, {:casted => :options}, "AR"
57
+ ).twice
58
+
59
+ ThinkingSphinx::Association.should have_received(:new).with(
60
+ nil, @non_poly_reflection
61
+ ).twice
62
+ end
63
+ end
64
+
65
+ describe "instance-level children method" do
66
+ it "should return the children associations for the given association" do
67
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
68
+ :klass => :klass
69
+ )
70
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
71
+ ThinkingSphinx::Association.stub_method(:children => :result)
72
+
73
+ @association.children(:assoc).should == :result
74
+ ThinkingSphinx::Association.should have_received(:children).with(:klass, :assoc, @association)
75
+ end
76
+ end
77
+
78
+ describe "join_to method" do
79
+ before :each do
80
+ @parent_join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
81
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance
82
+ @parent = ThinkingSphinx::Association.stub_instance(:join_to => true, :join => nil)
83
+ @base_join = Object.stub_instance(:joins => [:a, :b, :c])
84
+
85
+ ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_method(:new => @join)
86
+ end
87
+
88
+ it "should call the parent's join_to if parent has no join" do
89
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
90
+
91
+ @assoc.join_to(@base_join)
92
+
93
+ @parent.should have_received(:join_to).with(@base_join)
94
+ end
95
+
96
+ it "should not call the parent's join_to if it already has a join" do
97
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
98
+ @parent.stub_method(:join => @parent_join)
99
+
100
+ @assoc.join_to(@base_join)
101
+
102
+ @parent.should_not have_received(:join_to)
103
+ end
104
+
105
+ it "should define the join association with a JoinAssociation instance" do
106
+ @assoc = ThinkingSphinx::Association.new(@parent, :ref)
107
+
108
+ @assoc.join_to(@base_join).should == @join
109
+ @assoc.join.should == @join
110
+ end
111
+ end
112
+
113
+ describe "to_sql method" do
114
+ before :each do
115
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
116
+ :klass => Person
117
+ )
118
+ @association = ThinkingSphinx::Association.new(nil, @reflection)
119
+ @parent = Object.stub_instance(:aliased_table_name => "ALIAS TABLE NAME")
120
+ @join = ::ActiveRecord::Associations::ClassMethods::JoinDependency::JoinAssociation.stub_instance(
121
+ :association_join => "full association join SQL",
122
+ :parent => @parent
123
+ )
124
+ @association.join = @join
125
+ end
126
+
127
+ it "should return the join's association join value" do
128
+ @association.to_sql.should == "full association join SQL"
129
+ end
130
+
131
+ it "should replace ::ts_join_alias:: with the aliased table name" do
132
+ @join.stub_method(:association_join => "text with ::ts_join_alias:: gone")
133
+
134
+ @association.to_sql.should == "text with `ALIAS TABLE NAME` gone"
135
+ end
136
+ end
137
+
138
+ describe "is_many? method" do
139
+ before :each do
140
+ @parent = ThinkingSphinx::Association.stub_instance(
141
+ :is_many? => :parent_is_many
142
+ )
143
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
144
+ :macro => :has_many
145
+ )
146
+ end
147
+
148
+ it "should return true if association is either a has_many or a habtm" do
149
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
150
+ association.is_many?.should be_true
151
+
152
+ @reflection.stub_method(:macro => :has_and_belongs_to_many)
153
+ association.is_many?.should be_true
154
+ end
155
+
156
+ it "should return the parent value if not a has many or habtm and there is a parent" do
157
+ association = ThinkingSphinx::Association.new(@parent, @reflection)
158
+ @reflection.stub_method(:macro => :belongs_to)
159
+ association.is_many?.should == :parent_is_many
160
+ end
161
+
162
+ it "should return false if no parent and not a has many or habtm" do
163
+ association = ThinkingSphinx::Association.new(nil, @reflection)
164
+ @reflection.stub_method(:macro => :belongs_to)
165
+ association.is_many?.should be_false
166
+ end
167
+ end
168
+
169
+ describe "ancestors method" do
170
+ it "should return an array of associations - including all parents" do
171
+ parent = ThinkingSphinx::Association.stub_instance(:ancestors => [:all, :ancestors])
172
+ association = ThinkingSphinx::Association.new(parent, @reflection)
173
+ association.ancestors.should == [:all, :ancestors, association]
174
+ end
175
+ end
176
+
177
+ describe "polymorphic_classes method" do
178
+ it "should return all the polymorphic result types as classes" do
179
+ Person.connection.stub_method(:select_all => [
180
+ {"person_type" => "Person"},
181
+ {"person_type" => "Friendship"}
182
+ ])
183
+ ref = Object.stub_instance(
184
+ :active_record => Person,
185
+ :options => {:foreign_type => "person_type"}
186
+ )
187
+
188
+ ThinkingSphinx::Association.send(:polymorphic_classes, ref).should == [Person, Friendship]
189
+ end
190
+ end
191
+
192
+ describe "casted_options method" do
193
+ before :each do
194
+ @options = {
195
+ :foreign_key => "thing_id",
196
+ :foreign_type => "thing_type",
197
+ :polymorphic => true
198
+ }
199
+ @reflection = ::ActiveRecord::Reflection::AssociationReflection.stub_instance(
200
+ :options => @options
201
+ )
202
+ end
203
+
204
+ it "should return a new options set for a specific class" do
205
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
206
+ :polymorphic => nil,
207
+ :class_name => "Person",
208
+ :foreign_key => "thing_id",
209
+ :foreign_type => "thing_type",
210
+ :conditions => "::ts_join_alias::.`thing_type` = 'Person'"
211
+ }
212
+ end
213
+
214
+ it "should append to existing Array of conditions" do
215
+ @options[:conditions] = ["first condition"]
216
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
217
+ :polymorphic => nil,
218
+ :class_name => "Person",
219
+ :foreign_key => "thing_id",
220
+ :foreign_type => "thing_type",
221
+ :conditions => ["first condition", "::ts_join_alias::.`thing_type` = 'Person'"]
222
+ }
223
+ end
224
+
225
+ it "should merge to an existing Hash of conditions" do
226
+ @options[:conditions] = {"field" => "value"}
227
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
228
+ :polymorphic => nil,
229
+ :class_name => "Person",
230
+ :foreign_key => "thing_id",
231
+ :foreign_type => "thing_type",
232
+ :conditions => {"field" => "value", "thing_type" => "Person"}
233
+ }
234
+ end
235
+
236
+ it "should append to an existing String of conditions" do
237
+ @options[:conditions] = "first condition"
238
+ ThinkingSphinx::Association.send(:casted_options, Person, @reflection).should == {
239
+ :polymorphic => nil,
240
+ :class_name => "Person",
241
+ :foreign_key => "thing_id",
242
+ :foreign_type => "thing_type",
243
+ :conditions => "first condition AND ::ts_join_alias::.`thing_type` = 'Person'"
244
+ }
245
+ end
246
+ end
247
+ end