freelancing-god-thinking-sphinx 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. data/LICENCE +20 -0
  2. data/README +25 -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 +79 -0
  10. data/lib/thinking_sphinx/active_record.rb +115 -0
  11. data/lib/thinking_sphinx/active_record/delta.rb +86 -0
  12. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  13. data/lib/thinking_sphinx/active_record/search.rb +36 -0
  14. data/lib/thinking_sphinx/association.rb +140 -0
  15. data/lib/thinking_sphinx/attribute.rb +279 -0
  16. data/lib/thinking_sphinx/configuration.rb +275 -0
  17. data/lib/thinking_sphinx/field.rb +186 -0
  18. data/lib/thinking_sphinx/index.rb +234 -0
  19. data/lib/thinking_sphinx/index/builder.rb +197 -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 +413 -0
  23. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +184 -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 +0 -0
  26. data/spec/unit/thinking_sphinx/active_record_spec.rb +85 -0
  27. data/spec/unit/thinking_sphinx/association_spec.rb +0 -0
  28. data/spec/unit/thinking_sphinx/attribute_spec.rb +73 -0
  29. data/spec/unit/thinking_sphinx/configuration_spec.rb +7 -0
  30. data/spec/unit/thinking_sphinx/field_spec.rb +51 -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 +5 -0
  34. data/spec/unit/thinking_sphinx/search_spec.rb +121 -0
  35. data/spec/unit/thinking_sphinx_spec.rb +82 -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,184 @@
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
+ after :each do
10
+ Person.unstub_method(:write_inheritable_array)
11
+ end
12
+
13
+ it "should add callbacks" do
14
+ Person.after_commit :toggle_delta
15
+
16
+ Person.should have_received(:write_inheritable_array).with(
17
+ :after_commit, [:toggle_delta]
18
+ )
19
+ end
20
+ end
21
+
22
+ describe "save_with_after_commit_callback method" do
23
+ before :each do
24
+ @person = Person.new
25
+ @person.stub_methods(
26
+ :save_without_after_commit_callback => true,
27
+ :callback => true
28
+ )
29
+ end
30
+
31
+ it "should call the normal save method" do
32
+ @person.save
33
+
34
+ @person.should have_received(:save_without_after_commit_callback)
35
+ end
36
+
37
+ it "should call the callbacks if the save was successful" do
38
+ @person.save
39
+
40
+ @person.should have_received(:callback).with(:after_commit)
41
+ end
42
+
43
+ it "shouldn't call the callbacks if the save failed" do
44
+ @person.stub_method(:save_without_after_commit_callback => false)
45
+
46
+ @person.save
47
+
48
+ @person.should_not have_received(:callback)
49
+ end
50
+
51
+ it "should return the normal save's result" do
52
+ @person.save.should be_true
53
+
54
+ @person.stub_method(:save_without_after_commit_callback => false)
55
+
56
+ @person.save.should be_false
57
+ end
58
+ end
59
+
60
+ describe "save_with_after_commit_callback! method" do
61
+ before :each do
62
+ @person = Person.new
63
+ @person.stub_methods(
64
+ :save_without_after_commit_callback! => true,
65
+ :callback => true
66
+ )
67
+ end
68
+
69
+ it "should call the normal save! method" do
70
+ @person.save!
71
+
72
+ @person.should have_received(:save_without_after_commit_callback!)
73
+ end
74
+
75
+ it "should call the callbacks if the save! was successful" do
76
+ @person.save!
77
+
78
+ @person.should have_received(:callback).with(:after_commit)
79
+ end
80
+
81
+ it "shouldn't call the callbacks if the save! failed" do
82
+ @person.stub_method(:save_without_after_commit_callback! => false)
83
+
84
+ @person.save!
85
+
86
+ @person.should_not have_received(:callback)
87
+ end
88
+
89
+ it "should return the normal save's result" do
90
+ @person.save!.should be_true
91
+
92
+ @person.stub_method(:save_without_after_commit_callback! => false)
93
+
94
+ @person.save!.should be_false
95
+ end
96
+ end
97
+
98
+ describe "destroy_with_after_commit_callback method" do
99
+ before :each do
100
+ @person = Person.new
101
+ @person.stub_methods(
102
+ :destroy_without_after_commit_callback => true,
103
+ :callback => true
104
+ )
105
+ end
106
+
107
+ it "should call the normal destroy method" do
108
+ @person.destroy
109
+
110
+ @person.should have_received(:destroy_without_after_commit_callback)
111
+ end
112
+
113
+ it "should call the callbacks if the destroy was successful" do
114
+ @person.destroy
115
+
116
+ @person.should have_received(:callback).with(:after_commit)
117
+ end
118
+
119
+ it "shouldn't call the callbacks if the destroy failed" do
120
+ @person.stub_method(:destroy_without_after_commit_callback => false)
121
+
122
+ @person.destroy
123
+
124
+ @person.should_not have_received(:callback)
125
+ end
126
+
127
+ it "should return the normal save's result" do
128
+ @person.destroy.should be_true
129
+
130
+ @person.stub_method(:destroy_without_after_commit_callback => false)
131
+
132
+ @person.destroy.should be_false
133
+ end
134
+ end
135
+
136
+ describe "toggle_delta method" do
137
+ it "should set the delta value to true" do
138
+ @person = Person.new
139
+
140
+ @person.delta.should be_false
141
+ @person.send(:toggle_delta)
142
+ @person.delta.should be_true
143
+ end
144
+ end
145
+
146
+ describe "index_delta method" do
147
+ before :each do
148
+ ThinkingSphinx::Configuration.stub_method(:environment => "spec")
149
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
150
+
151
+ @person = Person.new
152
+ @person.stub_method(:system => true)
153
+ end
154
+
155
+ after :each do
156
+ ThinkingSphinx::Configuration.unstub_method(:environment)
157
+ ThinkingSphinx.unstub_method(:deltas_enabled?)
158
+ end
159
+
160
+ it "shouldn't index if delta indexing is disabled" do
161
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
162
+
163
+ @person.send(:index_delta)
164
+
165
+ @person.should_not have_received(:system)
166
+ end
167
+
168
+ it "shouldn't index if the environment is 'test'" do
169
+ ThinkingSphinx::Configuration.stub_method(:environment => "test")
170
+
171
+ @person.send(:index_delta)
172
+
173
+ @person.should_not have_received(:system)
174
+ end
175
+
176
+ it "should call indexer for the delta index" do
177
+ @person.send(:index_delta)
178
+
179
+ @person.should have_received(:system).with(
180
+ "indexer --config #{ThinkingSphinx::Configuration.new.config_file} --rotate person_delta"
181
+ )
182
+ end
183
+ end
184
+ 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,85 @@
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
+ )
14
+
15
+ @index = ThinkingSphinx::Index.stub_instance(:delta? => false)
16
+ ThinkingSphinx::Index.stub_method(:new => @index)
17
+ end
18
+
19
+ after :each do
20
+ # Remove the class so we can redefine it
21
+ TestModule.send(:remove_const, :TestModel)
22
+ end
23
+
24
+ it "should return nil and do nothing if indexes are disabled" do
25
+ ThinkingSphinx.stub_method(:define_indexes? => false)
26
+
27
+ TestModule::TestModel.define_index {}.should be_nil
28
+ ThinkingSphinx::Index.should_not have_received(:new)
29
+
30
+ ThinkingSphinx.unstub_method(:define_indexes?)
31
+ end
32
+
33
+ it "should add a new index to the model" do
34
+ TestModule::TestModel.define_index do; end
35
+
36
+ TestModule::TestModel.indexes.length.should == 1
37
+ end
38
+
39
+ it "should add to ThinkingSphinx.indexed_models if the model doesn't already exist in the array" do
40
+ TestModule::TestModel.define_index do; end
41
+
42
+ ThinkingSphinx.indexed_models.should include("TestModule::TestModel")
43
+ end
44
+
45
+ it "shouldn't add to ThinkingSphinx.indexed_models if the model already exists in the array" do
46
+ TestModule::TestModel.define_index do; end
47
+
48
+ ThinkingSphinx.indexed_models.select { |model|
49
+ model == "TestModule::TestModel"
50
+ }.length.should == 1
51
+
52
+ TestModule::TestModel.define_index do; end
53
+
54
+ ThinkingSphinx.indexed_models.select { |model|
55
+ model == "TestModule::TestModel"
56
+ }.length.should == 1
57
+ end
58
+
59
+ it "should add before_save and after_commit hooks to the model if delta indexing is enabled" do
60
+ @index.stub_method(:delta? => true)
61
+
62
+ TestModule::TestModel.define_index do; end
63
+
64
+ TestModule::TestModel.should have_received(:before_save)
65
+ TestModule::TestModel.should have_received(:after_commit)
66
+ end
67
+
68
+ it "should not add before_save and after_commit hooks to the model if delta indexing is disabled" do
69
+ TestModule::TestModel.define_index do; end
70
+
71
+ TestModule::TestModel.should_not have_received(:before_save)
72
+ TestModule::TestModel.should_not have_received(:after_commit)
73
+ end
74
+
75
+ it "should return the new index" do
76
+ TestModule::TestModel.define_index.should == @index
77
+ end
78
+ end
79
+
80
+ describe "to_crc32 method" do
81
+ it "should return an integer" do
82
+ Person.to_crc32.should be_a_kind_of(Integer)
83
+ end
84
+ end
85
+ end
File without changes
@@ -0,0 +1,73 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Attribute do
4
+ describe "to_select_sql method" do
5
+ before :each do
6
+ @index = Person.indexes.first
7
+ @index.link!
8
+ end
9
+
10
+ it "should concat with spaces if there's more than one non-integer column" do
11
+ @index.attributes[0].to_select_sql.should match(/CONCAT_WS\(' ', /)
12
+ end
13
+
14
+ it "should concat with spaces if there's more than one association for a non-integer column" do
15
+ @index.attributes[1].to_select_sql.should match(/CONCAT_WS\(' ', /)
16
+ end
17
+
18
+ it "should concat with commas if there's multiple integer columns" do
19
+ @index.attributes[2].to_select_sql.should match(/CONCAT_WS\(',', /)
20
+ end
21
+
22
+ it "should concat with commas if there's more than one association for an integer column" do
23
+ @index.attributes[3].to_select_sql.should match(/CONCAT_WS\(',', /)
24
+ end
25
+
26
+ it "should group with spaces if there's string columns from a has_many or has_and_belongs_to_many association" do
27
+ @index.attributes[4].to_select_sql.should match(/GROUP_CONCAT\(.+ SEPARATOR ' '\)/)
28
+ end
29
+
30
+ it "should group with commas if there's integer columns from a has_many or has_and_belongs_to_many association" do
31
+ @index.attributes[5].to_select_sql.should match(/GROUP_CONCAT\(.+ SEPARATOR ','\)/)
32
+ end
33
+
34
+ it "should convert datetime values to timestamps" do
35
+ @index.attributes[6].to_select_sql.should match(/UNIX_TIMESTAMP/)
36
+ end
37
+ end
38
+
39
+ describe "to_group_sql method" do
40
+ before :each do
41
+ @attribute = ThinkingSphinx::Attribute.new([])
42
+ @attribute.stub_method(:is_many? => false, :is_string? => false)
43
+
44
+ ThinkingSphinx.stub_method(:use_group_by_shortcut? => false)
45
+ end
46
+
47
+ it "should return nil if is_many?" do
48
+ @attribute.stub_method(:is_many? => true)
49
+
50
+ @attribute.to_group_sql.should be_nil
51
+ end
52
+
53
+ it "should return nil if is_string?" do
54
+ @attribute.stub_method(:is_string? => true)
55
+
56
+ @attribute.to_group_sql.should be_nil
57
+ end
58
+
59
+ it "should return nil if group_by shortcut is allowed" do
60
+ ThinkingSphinx.stub_method(:use_group_by_shortcut? => true)
61
+
62
+ @attribute.to_group_sql.should be_nil
63
+ end
64
+
65
+ it "should return an array if neither is_many? or shortcut allowed" do
66
+ @attribute.to_group_sql.should be_a_kind_of(Array)
67
+ end
68
+
69
+ after :each do
70
+ ThinkingSphinx.unstub_method(:use_group_by_shortcut?)
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Configuration do
4
+ describe "build method" do
5
+
6
+ end
7
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe ThinkingSphinx::Field do
4
+ describe "to_select_sql method" do
5
+ before :each do
6
+ @index = Person.indexes.first
7
+ @index.link!
8
+ end
9
+
10
+ it "should concat with spaces if there are multiple columns" do
11
+ @index.fields.first.to_select_sql.should match(/CONCAT_WS\(' ', /)
12
+ end
13
+
14
+ it "should concat with spaces if a column has more than one association" do
15
+ @index.fields[1].to_select_sql.should match(/CONCAT_WS\(' ', /)
16
+ end
17
+
18
+ it "should group if any association for any column is a has_many or has_and_belongs_to_many" do
19
+ @index.fields[2].to_select_sql.should match(/GROUP_CONCAT/)
20
+ end
21
+ end
22
+
23
+ describe "to_group_sql method" do
24
+ before :each do
25
+ @field = ThinkingSphinx::Field.new([])
26
+ @field.stub_methods(:is_many? => false)
27
+
28
+ ThinkingSphinx.stub_method(:use_group_by_shortcut? => false)
29
+ end
30
+
31
+ it "should return nil if is_many?" do
32
+ @field.stub_method(:is_many? => true)
33
+
34
+ @field.to_group_sql.should be_nil
35
+ end
36
+
37
+ it "should return nil if group_by shortcut is allowed" do
38
+ ThinkingSphinx.stub_method(:use_group_by_shortcut? => true)
39
+
40
+ @field.to_group_sql.should be_nil
41
+ end
42
+
43
+ it "should return an array if neither is_many? or shortcut allowed" do
44
+ @field.to_group_sql.should be_a_kind_of(Array)
45
+ end
46
+
47
+ after :each do
48
+ ThinkingSphinx.unstub_method(:use_group_by_shortcut?)
49
+ end
50
+ end
51
+ end