DrMark-thinking-sphinx 0.9.7

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 +52 -0
  3. data/lib/riddle.rb +22 -0
  4. data/lib/riddle/client.rb +593 -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 +82 -0
  10. data/lib/thinking_sphinx/active_record.rb +138 -0
  11. data/lib/thinking_sphinx/active_record/delta.rb +90 -0
  12. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  13. data/lib/thinking_sphinx/active_record/search.rb +43 -0
  14. data/lib/thinking_sphinx/association.rb +140 -0
  15. data/lib/thinking_sphinx/attribute.rb +282 -0
  16. data/lib/thinking_sphinx/configuration.rb +277 -0
  17. data/lib/thinking_sphinx/field.rb +198 -0
  18. data/lib/thinking_sphinx/index.rb +334 -0
  19. data/lib/thinking_sphinx/index/builder.rb +212 -0
  20. data/lib/thinking_sphinx/index/faux_column.rb +97 -0
  21. data/lib/thinking_sphinx/rails_additions.rb +56 -0
  22. data/lib/thinking_sphinx/search.rb +455 -0
  23. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +185 -0
  24. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  25. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +81 -0
  26. data/spec/unit/thinking_sphinx/active_record_spec.rb +201 -0
  27. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  28. data/spec/unit/thinking_sphinx/attribute_spec.rb +356 -0
  29. data/spec/unit/thinking_sphinx/configuration_spec.rb +476 -0
  30. data/spec/unit/thinking_sphinx/field_spec.rb +215 -0
  31. data/spec/unit/thinking_sphinx/index/builder_spec.rb +33 -0
  32. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +41 -0
  33. data/spec/unit/thinking_sphinx/index_spec.rb +230 -0
  34. data/spec/unit/thinking_sphinx/search_spec.rb +163 -0
  35. data/spec/unit/thinking_sphinx_spec.rb +107 -0
  36. data/tasks/thinking_sphinx_tasks.rake +1 -0
  37. data/tasks/thinking_sphinx_tasks.rb +86 -0
  38. metadata +90 -0
@@ -0,0 +1,185 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "ThinkingSphinx::ActiveRecord::Delta" do
4
+ describe "after_commit callback" do
5
+ before :each do
6
+ Person.stub_method(:write_inheritable_array => true)
7
+ end
8
+
9
+ # This spec only passes with ActiveRecord 2.0.2 or earlier.
10
+ # it "should add callbacks" do
11
+ # Person.after_commit :toggle_delta
12
+ #
13
+ # Person.should have_received(:write_inheritable_array).with(
14
+ # :after_commit, [:toggle_delta]
15
+ # )
16
+ # end
17
+
18
+ it "should have an after_commit method by default" do
19
+ Person.instance_methods.should include("after_commit")
20
+ end
21
+ end
22
+
23
+ describe "save_with_after_commit_callback method" do
24
+ before :each do
25
+ @person = Person.new
26
+ @person.stub_methods(
27
+ :save_without_after_commit_callback => true,
28
+ :callback => true
29
+ )
30
+ end
31
+
32
+ it "should call the normal save method" do
33
+ @person.save
34
+
35
+ @person.should have_received(:save_without_after_commit_callback)
36
+ end
37
+
38
+ it "should call the callbacks if the save was successful" do
39
+ @person.save
40
+
41
+ @person.should have_received(:callback).with(:after_commit)
42
+ end
43
+
44
+ it "shouldn't call the callbacks if the save failed" do
45
+ @person.stub_method(:save_without_after_commit_callback => false)
46
+
47
+ @person.save
48
+
49
+ @person.should_not have_received(:callback)
50
+ end
51
+
52
+ it "should return the normal save's result" do
53
+ @person.save.should be_true
54
+
55
+ @person.stub_method(:save_without_after_commit_callback => false)
56
+
57
+ @person.save.should be_false
58
+ end
59
+ end
60
+
61
+ describe "save_with_after_commit_callback! method" do
62
+ before :each do
63
+ @person = Person.new
64
+ @person.stub_methods(
65
+ :save_without_after_commit_callback! => true,
66
+ :callback => true
67
+ )
68
+ end
69
+
70
+ it "should call the normal save! method" do
71
+ @person.save!
72
+
73
+ @person.should have_received(:save_without_after_commit_callback!)
74
+ end
75
+
76
+ it "should call the callbacks if the save! was successful" do
77
+ @person.save!
78
+
79
+ @person.should have_received(:callback).with(:after_commit)
80
+ end
81
+
82
+ it "shouldn't call the callbacks if the save! failed" do
83
+ @person.stub_method(:save_without_after_commit_callback! => false)
84
+
85
+ @person.save!
86
+
87
+ @person.should_not have_received(:callback)
88
+ end
89
+
90
+ it "should return the normal save's result" do
91
+ @person.save!.should be_true
92
+
93
+ @person.stub_method(:save_without_after_commit_callback! => false)
94
+
95
+ @person.save!.should be_false
96
+ end
97
+ end
98
+
99
+ describe "destroy_with_after_commit_callback method" do
100
+ before :each do
101
+ @person = Person.new
102
+ @person.stub_methods(
103
+ :destroy_without_after_commit_callback => true,
104
+ :callback => true
105
+ )
106
+ end
107
+
108
+ it "should call the normal destroy method" do
109
+ @person.destroy
110
+
111
+ @person.should have_received(:destroy_without_after_commit_callback)
112
+ end
113
+
114
+ it "should call the callbacks if the destroy was successful" do
115
+ @person.destroy
116
+
117
+ @person.should have_received(:callback).with(:after_commit)
118
+ end
119
+
120
+ it "shouldn't call the callbacks if the destroy failed" do
121
+ @person.stub_method(:destroy_without_after_commit_callback => false)
122
+
123
+ @person.destroy
124
+
125
+ @person.should_not have_received(:callback)
126
+ end
127
+
128
+ it "should return the normal save's result" do
129
+ @person.destroy.should be_true
130
+
131
+ @person.stub_method(:destroy_without_after_commit_callback => false)
132
+
133
+ @person.destroy.should be_false
134
+ end
135
+ end
136
+
137
+ describe "toggle_delta method" do
138
+ it "should set the delta value to true" do
139
+ @person = Person.new
140
+
141
+ @person.delta.should be_false
142
+ @person.send(:toggle_delta)
143
+ @person.delta.should be_true
144
+ end
145
+ end
146
+
147
+ describe "index_delta method" do
148
+ before :each do
149
+ ThinkingSphinx::Configuration.stub_method(:environment => "spec")
150
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
151
+
152
+ @person = Person.new
153
+ @person.stub_method(:system => true)
154
+ end
155
+
156
+ after :each do
157
+ ThinkingSphinx::Configuration.unstub_method(:environment)
158
+ ThinkingSphinx.unstub_method(:deltas_enabled?)
159
+ end
160
+
161
+ it "shouldn't index if delta indexing is disabled" do
162
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
163
+
164
+ @person.send(:index_delta)
165
+
166
+ @person.should_not have_received(:system)
167
+ end
168
+
169
+ it "shouldn't index if the environment is 'test'" do
170
+ ThinkingSphinx::Configuration.stub_method(:environment => "test")
171
+
172
+ @person.send(:index_delta)
173
+
174
+ @person.should_not have_received(:system)
175
+ end
176
+
177
+ it "should call indexer for the delta index" do
178
+ @person.send(:index_delta)
179
+
180
+ @person.should have_received(:system).with(
181
+ "indexer --config #{ThinkingSphinx::Configuration.new.config_file} --rotate person_delta"
182
+ )
183
+ end
184
+ end
185
+ 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.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.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,81 @@
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.methods.should include("search_for_ids")
6
+ end
7
+
8
+ it "should add search_for_ids to ActiveRecord::Base" do
9
+ ActiveRecord::Base.methods.should include("search")
10
+ end
11
+
12
+ it "should add search_for_id to ActiveRecord::Base" do
13
+ ActiveRecord::Base.methods.should include("search_for_id")
14
+ end
15
+
16
+ describe "search_for_ids method" do
17
+ before :each do
18
+ ThinkingSphinx::Search.stub_method(:search_for_ids => true)
19
+ end
20
+
21
+ it "should call ThinkingSphinx::Search#search_for_ids with the class option set" do
22
+ Person.search_for_ids("search")
23
+
24
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
25
+ "search", :class => Person
26
+ )
27
+ end
28
+
29
+ it "should override the class option" do
30
+ Person.search_for_ids("search", :class => Friendship)
31
+
32
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
33
+ "search", :class => Person
34
+ )
35
+ end
36
+ end
37
+
38
+ describe "search method" do
39
+ before :each do
40
+ ThinkingSphinx::Search.stub_method(:search => true)
41
+ end
42
+
43
+ it "should call ThinkingSphinx::Search#search with the class option set" do
44
+ Person.search("search")
45
+
46
+ ThinkingSphinx::Search.should have_received(:search).with(
47
+ "search", :class => Person
48
+ )
49
+ end
50
+
51
+ it "should override the class option" do
52
+ Person.search("search", :class => Friendship)
53
+
54
+ ThinkingSphinx::Search.should have_received(:search).with(
55
+ "search", :class => Person
56
+ )
57
+ end
58
+ end
59
+
60
+ describe "search_for_id method" do
61
+ before :each do
62
+ ThinkingSphinx::Search.stub_method(:search_for_id => true)
63
+ end
64
+
65
+ it "should call ThinkingSphinx::Search#search with the class option set" do
66
+ Person.search_for_id(10)
67
+
68
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
69
+ 10, :class => Person
70
+ )
71
+ end
72
+
73
+ it "should override the class option" do
74
+ Person.search_for_id(10, :class => Friendship)
75
+
76
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
77
+ 10, :class => Person
78
+ )
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,201 @@
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
+ # after :each do
129
+ # ThinkingSphinx::Configuration.unstub_method(:new)
130
+ # Riddle::Client.unstub_method(:new)
131
+ # Person.indexes.each { |index| index.unstub_method(:delta?) }
132
+ # end
133
+
134
+ it "should create a client using the Configuration's address and port" do
135
+ @person.toggle_deleted
136
+
137
+ Riddle::Client.should have_received(:new).with(
138
+ @configuration.address, @configuration.port
139
+ )
140
+ end
141
+
142
+ it "should update the core index's deleted flag if in core index" do
143
+ @person.toggle_deleted
144
+
145
+ @client.should have_received(:update).with(
146
+ "person_core", ["sphinx_deleted"], {@person.id => 1}
147
+ )
148
+ end
149
+
150
+ it "shouldn't update the core index's deleted flag if the record isn't in it" do
151
+ @person.stub_method(:in_core_index? => false)
152
+
153
+ @person.toggle_deleted
154
+
155
+ @client.should_not have_received(:update).with(
156
+ "person_core", ["sphinx_deleted"], {@person.id => 1}
157
+ )
158
+ end
159
+
160
+ it "should update the delta index's deleted flag if delta indexing is enabled and the instance's delta is true" do
161
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
162
+ @person.delta = true
163
+
164
+ @person.toggle_deleted
165
+
166
+ @client.should have_received(:update).with(
167
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
168
+ )
169
+ end
170
+
171
+ it "should not update the delta index's deleted flag if delta indexing is enabled and the instance's delta is false" do
172
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
173
+ @person.delta = false
174
+
175
+ @person.toggle_deleted
176
+
177
+ @client.should_not have_received(:update).with(
178
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
179
+ )
180
+ end
181
+
182
+ it "should not update the delta index's deleted flag if delta indexing is enabled and the instance's delta is equivalent to false" do
183
+ Person.indexes.each { |index| index.stub_method(:delta? => true) }
184
+ @person.delta = 0
185
+
186
+ @person.toggle_deleted
187
+
188
+ @client.should_not have_received(:update).with(
189
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
190
+ )
191
+ end
192
+
193
+ it "shouldn't update the delta index if delta indexing is disabled" do
194
+ @person.toggle_deleted
195
+
196
+ @client.should_not have_received(:update).with(
197
+ "person_delta", ["sphinx_deleted"], {@person.id => 1}
198
+ )
199
+ end
200
+ end
201
+ end