dpickett-thinking-sphinx 1.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE +20 -0
- data/README +107 -0
- data/lib/thinking_sphinx/active_record/delta.rb +74 -0
- data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
- data/lib/thinking_sphinx/active_record/search.rb +57 -0
- data/lib/thinking_sphinx/active_record.rb +245 -0
- data/lib/thinking_sphinx/adapters/abstract_adapter.rb +34 -0
- data/lib/thinking_sphinx/adapters/mysql_adapter.rb +53 -0
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +129 -0
- data/lib/thinking_sphinx/association.rb +144 -0
- data/lib/thinking_sphinx/attribute.rb +254 -0
- data/lib/thinking_sphinx/class_facet.rb +20 -0
- data/lib/thinking_sphinx/collection.rb +142 -0
- data/lib/thinking_sphinx/configuration.rb +236 -0
- data/lib/thinking_sphinx/core/string.rb +22 -0
- data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +65 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
- data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
- data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
- data/lib/thinking_sphinx/deltas.rb +22 -0
- data/lib/thinking_sphinx/facet.rb +58 -0
- data/lib/thinking_sphinx/facet_collection.rb +45 -0
- data/lib/thinking_sphinx/field.rb +172 -0
- data/lib/thinking_sphinx/index/builder.rb +233 -0
- data/lib/thinking_sphinx/index/faux_column.rb +110 -0
- data/lib/thinking_sphinx/index.rb +432 -0
- data/lib/thinking_sphinx/rails_additions.rb +133 -0
- data/lib/thinking_sphinx/search.rb +654 -0
- data/lib/thinking_sphinx/tasks.rb +128 -0
- data/lib/thinking_sphinx.rb +145 -0
- data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
- data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
- data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
- data/spec/unit/thinking_sphinx/active_record_spec.rb +256 -0
- data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
- data/spec/unit/thinking_sphinx/attribute_spec.rb +212 -0
- data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
- data/spec/unit/thinking_sphinx/configuration_spec.rb +136 -0
- data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
- data/spec/unit/thinking_sphinx/field_spec.rb +145 -0
- data/spec/unit/thinking_sphinx/index/builder_spec.rb +5 -0
- data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
- data/spec/unit/thinking_sphinx/index_spec.rb +54 -0
- data/spec/unit/thinking_sphinx/search_spec.rb +59 -0
- data/spec/unit/thinking_sphinx_spec.rb +129 -0
- data/tasks/distribution.rb +48 -0
- data/tasks/rails.rake +1 -0
- data/tasks/testing.rb +86 -0
- data/vendor/after_commit/LICENSE +20 -0
- data/vendor/after_commit/README +16 -0
- data/vendor/after_commit/Rakefile +22 -0
- data/vendor/after_commit/init.rb +5 -0
- data/vendor/after_commit/lib/after_commit/active_record.rb +91 -0
- data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
- data/vendor/after_commit/lib/after_commit.rb +42 -0
- data/vendor/after_commit/test/after_commit_test.rb +53 -0
- data/vendor/delayed_job/lib/delayed/job.rb +251 -0
- data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
- data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
- data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
- data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
- data/vendor/riddle/lib/riddle/client/message.rb +65 -0
- data/vendor/riddle/lib/riddle/client/response.rb +84 -0
- data/vendor/riddle/lib/riddle/client.rb +619 -0
- data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
- data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
- data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
- data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
- data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
- data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
- data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
- data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
- data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
- data/vendor/riddle/lib/riddle/configuration.rb +33 -0
- data/vendor/riddle/lib/riddle/controller.rb +44 -0
- data/vendor/riddle/lib/riddle.rb +30 -0
- metadata +158 -0
@@ -0,0 +1,256 @@
|
|
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.indexed_models.delete "TestModule::TestModel"
|
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.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)
|
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 "toggle_deleted method" do
|
104
|
+
before :each do
|
105
|
+
ThinkingSphinx.stub_method(:sphinx_running? => true)
|
106
|
+
|
107
|
+
@configuration = ThinkingSphinx::Configuration.instance
|
108
|
+
@configuration.stub_methods(
|
109
|
+
:address => "an address",
|
110
|
+
:port => 123
|
111
|
+
)
|
112
|
+
@client = Riddle::Client.stub_instance(:update => true)
|
113
|
+
@person = Person.find(:first)
|
114
|
+
|
115
|
+
Riddle::Client.stub_method(:new => @client)
|
116
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => false) }
|
117
|
+
@person.stub_method(:in_core_index? => true)
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should create a client using the Configuration's address and port" do
|
121
|
+
@person.toggle_deleted
|
122
|
+
|
123
|
+
Riddle::Client.should have_received(:new).with(
|
124
|
+
@configuration.address, @configuration.port
|
125
|
+
)
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should update the core index's deleted flag if in core index" do
|
129
|
+
@person.toggle_deleted
|
130
|
+
|
131
|
+
@client.should have_received(:update).with(
|
132
|
+
"person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
133
|
+
)
|
134
|
+
end
|
135
|
+
|
136
|
+
it "shouldn't update the core index's deleted flag if the record isn't in it" do
|
137
|
+
@person.stub_method(:in_core_index? => false)
|
138
|
+
|
139
|
+
@person.toggle_deleted
|
140
|
+
|
141
|
+
@client.should_not have_received(:update).with(
|
142
|
+
"person_core", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
143
|
+
)
|
144
|
+
end
|
145
|
+
|
146
|
+
it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
|
147
|
+
ThinkingSphinx.stub_method(:sphinx_running? => false)
|
148
|
+
|
149
|
+
@person.toggle_deleted
|
150
|
+
|
151
|
+
@person.should_not have_received(:in_core_index?)
|
152
|
+
@client.should_not have_received(:update)
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
|
156
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
157
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
158
|
+
@person.delta = true
|
159
|
+
|
160
|
+
@person.toggle_deleted
|
161
|
+
|
162
|
+
@client.should have_received(:update).with(
|
163
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
164
|
+
)
|
165
|
+
end
|
166
|
+
|
167
|
+
it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
|
168
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
169
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
170
|
+
@person.delta = false
|
171
|
+
|
172
|
+
@person.toggle_deleted
|
173
|
+
|
174
|
+
@client.should_not have_received(:update).with(
|
175
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
176
|
+
)
|
177
|
+
end
|
178
|
+
|
179
|
+
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
|
180
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
181
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
182
|
+
@person.delta = 0
|
183
|
+
|
184
|
+
@person.toggle_deleted
|
185
|
+
|
186
|
+
@client.should_not have_received(:update).with(
|
187
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
188
|
+
)
|
189
|
+
end
|
190
|
+
|
191
|
+
it "shouldn't update the delta index if delta indexes are disabled" do
|
192
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => true)
|
193
|
+
@person.toggle_deleted
|
194
|
+
|
195
|
+
@client.should_not have_received(:update).with(
|
196
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
197
|
+
)
|
198
|
+
end
|
199
|
+
|
200
|
+
it "should not update the delta index if delta indexing is disabled" do
|
201
|
+
ThinkingSphinx.stub_method(:deltas_enabled? => false)
|
202
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
203
|
+
@person.delta = true
|
204
|
+
|
205
|
+
@person.toggle_deleted
|
206
|
+
|
207
|
+
@client.should_not have_received(:update).with(
|
208
|
+
"person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => 1}
|
209
|
+
)
|
210
|
+
end
|
211
|
+
|
212
|
+
it "should not update either index if updates are disabled" do
|
213
|
+
ThinkingSphinx.stub_methods(
|
214
|
+
:updates_enabled? => false,
|
215
|
+
:deltas_enabled => true
|
216
|
+
)
|
217
|
+
Person.sphinx_indexes.each { |index| index.stub_method(:delta? => true) }
|
218
|
+
@person.delta = true
|
219
|
+
|
220
|
+
@person.toggle_deleted
|
221
|
+
|
222
|
+
@client.should_not have_received(:update)
|
223
|
+
end
|
224
|
+
end
|
225
|
+
|
226
|
+
describe "sphinx_indexes in the inheritance chain (STI)" do
|
227
|
+
it "should hand defined indexes on a class down to its child classes" do
|
228
|
+
Child.sphinx_indexes.should include(*Person.sphinx_indexes)
|
229
|
+
end
|
230
|
+
|
231
|
+
it "should allow associations to other STI models" do
|
232
|
+
Child.sphinx_indexes.last.link!
|
233
|
+
sql = Child.sphinx_indexes.last.to_riddle_for_core(0, 0).sql_query
|
234
|
+
sql.gsub!('$start', '0').gsub!('$end', '100')
|
235
|
+
lambda { Child.connection.execute(sql) }.should_not raise_error(ActiveRecord::StatementInvalid)
|
236
|
+
end
|
237
|
+
end
|
238
|
+
|
239
|
+
it "should return the sphinx document id as expected" do
|
240
|
+
person = Person.find(:first)
|
241
|
+
model_count = ThinkingSphinx.indexed_models.length
|
242
|
+
offset = ThinkingSphinx.indexed_models.index("Person")
|
243
|
+
|
244
|
+
(person.id * model_count + offset).should == person.sphinx_document_id
|
245
|
+
|
246
|
+
alpha = Alpha.find(:first)
|
247
|
+
offset = ThinkingSphinx.indexed_models.index("Alpha")
|
248
|
+
|
249
|
+
(alpha.id * model_count + offset).should == alpha.sphinx_document_id
|
250
|
+
|
251
|
+
beta = Beta.find(:first)
|
252
|
+
offset = ThinkingSphinx.indexed_models.index("Beta")
|
253
|
+
|
254
|
+
(beta.id * model_count + offset).should == beta.sphinx_document_id
|
255
|
+
end
|
256
|
+
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
|