sayso-thinking-sphinx 2.0.3.001

Sign up to get free protection for your applications and to get access to all the features.
Files changed (164) hide show
  1. data/LICENCE +20 -0
  2. data/README.textile +251 -0
  3. data/VERSION +1 -0
  4. data/features/abstract_inheritance.feature +10 -0
  5. data/features/alternate_primary_key.feature +27 -0
  6. data/features/attribute_transformation.feature +22 -0
  7. data/features/attribute_updates.feature +77 -0
  8. data/features/deleting_instances.feature +67 -0
  9. data/features/direct_attributes.feature +11 -0
  10. data/features/excerpts.feature +21 -0
  11. data/features/extensible_delta_indexing.feature +9 -0
  12. data/features/facets.feature +88 -0
  13. data/features/facets_across_model.feature +29 -0
  14. data/features/field_sorting.feature +18 -0
  15. data/features/handling_edits.feature +94 -0
  16. data/features/retry_stale_indexes.feature +24 -0
  17. data/features/searching_across_models.feature +20 -0
  18. data/features/searching_by_index.feature +40 -0
  19. data/features/searching_by_model.feature +168 -0
  20. data/features/searching_with_find_arguments.feature +56 -0
  21. data/features/sphinx_detection.feature +25 -0
  22. data/features/sphinx_scopes.feature +68 -0
  23. data/features/step_definitions/alpha_steps.rb +16 -0
  24. data/features/step_definitions/beta_steps.rb +7 -0
  25. data/features/step_definitions/common_steps.rb +197 -0
  26. data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
  27. data/features/step_definitions/facet_steps.rb +96 -0
  28. data/features/step_definitions/find_arguments_steps.rb +36 -0
  29. data/features/step_definitions/gamma_steps.rb +15 -0
  30. data/features/step_definitions/scope_steps.rb +19 -0
  31. data/features/step_definitions/search_steps.rb +94 -0
  32. data/features/step_definitions/sphinx_steps.rb +35 -0
  33. data/features/sti_searching.feature +19 -0
  34. data/features/support/env.rb +27 -0
  35. data/features/support/lib/generic_delta_handler.rb +8 -0
  36. data/features/thinking_sphinx/database.example.yml +3 -0
  37. data/features/thinking_sphinx/db/fixtures/alphas.rb +10 -0
  38. data/features/thinking_sphinx/db/fixtures/authors.rb +1 -0
  39. data/features/thinking_sphinx/db/fixtures/betas.rb +11 -0
  40. data/features/thinking_sphinx/db/fixtures/boxes.rb +9 -0
  41. data/features/thinking_sphinx/db/fixtures/categories.rb +1 -0
  42. data/features/thinking_sphinx/db/fixtures/cats.rb +3 -0
  43. data/features/thinking_sphinx/db/fixtures/comments.rb +24 -0
  44. data/features/thinking_sphinx/db/fixtures/developers.rb +31 -0
  45. data/features/thinking_sphinx/db/fixtures/dogs.rb +3 -0
  46. data/features/thinking_sphinx/db/fixtures/extensible_betas.rb +10 -0
  47. data/features/thinking_sphinx/db/fixtures/foxes.rb +3 -0
  48. data/features/thinking_sphinx/db/fixtures/gammas.rb +10 -0
  49. data/features/thinking_sphinx/db/fixtures/music.rb +4 -0
  50. data/features/thinking_sphinx/db/fixtures/people.rb +1001 -0
  51. data/features/thinking_sphinx/db/fixtures/posts.rb +6 -0
  52. data/features/thinking_sphinx/db/fixtures/robots.rb +14 -0
  53. data/features/thinking_sphinx/db/fixtures/tags.rb +27 -0
  54. data/features/thinking_sphinx/db/migrations/create_alphas.rb +8 -0
  55. data/features/thinking_sphinx/db/migrations/create_animals.rb +5 -0
  56. data/features/thinking_sphinx/db/migrations/create_authors.rb +3 -0
  57. data/features/thinking_sphinx/db/migrations/create_authors_posts.rb +6 -0
  58. data/features/thinking_sphinx/db/migrations/create_betas.rb +5 -0
  59. data/features/thinking_sphinx/db/migrations/create_boxes.rb +5 -0
  60. data/features/thinking_sphinx/db/migrations/create_categories.rb +3 -0
  61. data/features/thinking_sphinx/db/migrations/create_comments.rb +10 -0
  62. data/features/thinking_sphinx/db/migrations/create_developers.rb +7 -0
  63. data/features/thinking_sphinx/db/migrations/create_extensible_betas.rb +5 -0
  64. data/features/thinking_sphinx/db/migrations/create_gammas.rb +3 -0
  65. data/features/thinking_sphinx/db/migrations/create_genres.rb +3 -0
  66. data/features/thinking_sphinx/db/migrations/create_music.rb +6 -0
  67. data/features/thinking_sphinx/db/migrations/create_people.rb +13 -0
  68. data/features/thinking_sphinx/db/migrations/create_posts.rb +5 -0
  69. data/features/thinking_sphinx/db/migrations/create_robots.rb +4 -0
  70. data/features/thinking_sphinx/db/migrations/create_taggings.rb +5 -0
  71. data/features/thinking_sphinx/db/migrations/create_tags.rb +4 -0
  72. data/features/thinking_sphinx/models/alpha.rb +23 -0
  73. data/features/thinking_sphinx/models/andrew.rb +17 -0
  74. data/features/thinking_sphinx/models/animal.rb +5 -0
  75. data/features/thinking_sphinx/models/author.rb +3 -0
  76. data/features/thinking_sphinx/models/beta.rb +13 -0
  77. data/features/thinking_sphinx/models/box.rb +8 -0
  78. data/features/thinking_sphinx/models/cat.rb +3 -0
  79. data/features/thinking_sphinx/models/category.rb +4 -0
  80. data/features/thinking_sphinx/models/comment.rb +10 -0
  81. data/features/thinking_sphinx/models/developer.rb +20 -0
  82. data/features/thinking_sphinx/models/dog.rb +3 -0
  83. data/features/thinking_sphinx/models/extensible_beta.rb +9 -0
  84. data/features/thinking_sphinx/models/fox.rb +5 -0
  85. data/features/thinking_sphinx/models/gamma.rb +5 -0
  86. data/features/thinking_sphinx/models/genre.rb +3 -0
  87. data/features/thinking_sphinx/models/medium.rb +5 -0
  88. data/features/thinking_sphinx/models/music.rb +8 -0
  89. data/features/thinking_sphinx/models/person.rb +24 -0
  90. data/features/thinking_sphinx/models/post.rb +21 -0
  91. data/features/thinking_sphinx/models/robot.rb +12 -0
  92. data/features/thinking_sphinx/models/tag.rb +3 -0
  93. data/features/thinking_sphinx/models/tagging.rb +4 -0
  94. data/lib/cucumber/thinking_sphinx/external_world.rb +12 -0
  95. data/lib/cucumber/thinking_sphinx/internal_world.rb +127 -0
  96. data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
  97. data/lib/thinking-sphinx.rb +1 -0
  98. data/lib/thinking_sphinx.rb +301 -0
  99. data/lib/thinking_sphinx/action_controller.rb +31 -0
  100. data/lib/thinking_sphinx/active_record.rb +352 -0
  101. data/lib/thinking_sphinx/active_record/attribute_updates.rb +52 -0
  102. data/lib/thinking_sphinx/active_record/delta.rb +92 -0
  103. data/lib/thinking_sphinx/active_record/has_many_association.rb +36 -0
  104. data/lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb +21 -0
  105. data/lib/thinking_sphinx/active_record/log_subscriber.rb +61 -0
  106. data/lib/thinking_sphinx/active_record/scopes.rb +93 -0
  107. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +87 -0
  108. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +58 -0
  109. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +153 -0
  110. data/lib/thinking_sphinx/association.rb +169 -0
  111. data/lib/thinking_sphinx/attribute.rb +389 -0
  112. data/lib/thinking_sphinx/auto_version.rb +38 -0
  113. data/lib/thinking_sphinx/bundled_search.rb +44 -0
  114. data/lib/thinking_sphinx/class_facet.rb +16 -0
  115. data/lib/thinking_sphinx/configuration.rb +355 -0
  116. data/lib/thinking_sphinx/context.rb +76 -0
  117. data/lib/thinking_sphinx/core/string.rb +15 -0
  118. data/lib/thinking_sphinx/deltas.rb +28 -0
  119. data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
  120. data/lib/thinking_sphinx/deploy/capistrano.rb +101 -0
  121. data/lib/thinking_sphinx/excerpter.rb +23 -0
  122. data/lib/thinking_sphinx/facet.rb +127 -0
  123. data/lib/thinking_sphinx/facet_search.rb +166 -0
  124. data/lib/thinking_sphinx/field.rb +82 -0
  125. data/lib/thinking_sphinx/index.rb +157 -0
  126. data/lib/thinking_sphinx/index/builder.rb +312 -0
  127. data/lib/thinking_sphinx/index/faux_column.rb +118 -0
  128. data/lib/thinking_sphinx/join.rb +37 -0
  129. data/lib/thinking_sphinx/property.rb +185 -0
  130. data/lib/thinking_sphinx/railtie.rb +46 -0
  131. data/lib/thinking_sphinx/search.rb +950 -0
  132. data/lib/thinking_sphinx/search_methods.rb +439 -0
  133. data/lib/thinking_sphinx/source.rb +163 -0
  134. data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
  135. data/lib/thinking_sphinx/source/sql.rb +148 -0
  136. data/lib/thinking_sphinx/tasks.rb +139 -0
  137. data/lib/thinking_sphinx/test.rb +55 -0
  138. data/spec/thinking_sphinx/active_record/delta_spec.rb +128 -0
  139. data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +72 -0
  140. data/spec/thinking_sphinx/active_record/scopes_spec.rb +176 -0
  141. data/spec/thinking_sphinx/active_record_spec.rb +576 -0
  142. data/spec/thinking_sphinx/adapters/abstract_adapter_spec.rb +145 -0
  143. data/spec/thinking_sphinx/association_spec.rb +216 -0
  144. data/spec/thinking_sphinx/attribute_spec.rb +560 -0
  145. data/spec/thinking_sphinx/auto_version_spec.rb +63 -0
  146. data/spec/thinking_sphinx/configuration_spec.rb +288 -0
  147. data/spec/thinking_sphinx/context_spec.rb +128 -0
  148. data/spec/thinking_sphinx/core/array_spec.rb +9 -0
  149. data/spec/thinking_sphinx/core/string_spec.rb +9 -0
  150. data/spec/thinking_sphinx/excerpter_spec.rb +49 -0
  151. data/spec/thinking_sphinx/facet_search_spec.rb +170 -0
  152. data/spec/thinking_sphinx/facet_spec.rb +359 -0
  153. data/spec/thinking_sphinx/field_spec.rb +127 -0
  154. data/spec/thinking_sphinx/index/builder_spec.rb +508 -0
  155. data/spec/thinking_sphinx/index/faux_column_spec.rb +36 -0
  156. data/spec/thinking_sphinx/index_spec.rb +183 -0
  157. data/spec/thinking_sphinx/search_methods_spec.rb +156 -0
  158. data/spec/thinking_sphinx/search_spec.rb +1387 -0
  159. data/spec/thinking_sphinx/source_spec.rb +253 -0
  160. data/spec/thinking_sphinx/test_spec.rb +20 -0
  161. data/spec/thinking_sphinx_spec.rb +203 -0
  162. data/tasks/distribution.rb +33 -0
  163. data/tasks/testing.rb +80 -0
  164. metadata +509 -0
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'ThinkingSphinx::ActiveRecord::HasManyAssociation' do
4
+ describe "search method" do
5
+ before :each do
6
+ Friendship.stub!(: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!(:attributes => [])
14
+
15
+ lambda { @person.friendships.search "test" }.should raise_error(RuntimeError)
16
+ end
17
+
18
+ it "should add a filter for the attribute into a normal search call" do
19
+ Friendship.should_receive(:search) do |query, options|
20
+ options[:with][:person_id].should == @person.id
21
+ end
22
+
23
+ @person.friendships.search "test"
24
+ end
25
+
26
+ it "should define indexes for the reflection class" do
27
+ Friendship.should_receive(:define_indexes)
28
+
29
+ @person.friendships.search 'test'
30
+ end
31
+ end
32
+
33
+ describe "search method for has_many :through" do
34
+ before :each do
35
+ Person.stub!(:search => true)
36
+
37
+ @person = Person.find(:first)
38
+ @index = Person.sphinx_indexes.first
39
+ end
40
+
41
+ it "should raise an error if the required attribute doesn't exist" do
42
+ @index.stub!(:attributes => [])
43
+
44
+ lambda { @person.friends.search "test" }.should raise_error(RuntimeError)
45
+ end
46
+
47
+ it "should add a filter for the attribute into a normal search call" do
48
+ Person.should_receive(:search).with do |query, options|
49
+ options[:with][:friendly_ids].should == @person.id
50
+ end
51
+
52
+ @person.friends.search "test"
53
+ end
54
+ end
55
+
56
+ describe 'filtering sphinx scopes' do
57
+ before :each do
58
+ Friendship.stub!(:search => Friendship)
59
+
60
+ @person = Person.find(:first)
61
+ end
62
+
63
+ it "should add a filter for the attribute in a sphinx scope call" do
64
+ Friendship.should_receive(:search).with do |options|
65
+ options[:with][:person_id].should == @person.id
66
+ Friendship
67
+ end
68
+
69
+ @person.friendships.reverse
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,176 @@
1
+ require 'spec_helper'
2
+
3
+ describe ThinkingSphinx::ActiveRecord::Scopes do
4
+ after :each do
5
+ Alpha.remove_sphinx_scopes
6
+ end
7
+
8
+ it "should be included into models with indexes" do
9
+ Alpha.included_modules.should include(ThinkingSphinx::ActiveRecord::Scopes)
10
+ end
11
+
12
+ it "should not be included into models without indexes" do
13
+ Gamma.included_modules.should_not include(
14
+ ThinkingSphinx::ActiveRecord::Scopes
15
+ )
16
+ end
17
+
18
+ describe '.sphinx_scope' do
19
+ before :each do
20
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
21
+ end
22
+
23
+ it "should define a method on the model" do
24
+ Alpha.should respond_to(:by_name)
25
+ end
26
+ end
27
+
28
+ describe '.sphinx_scopes' do
29
+ before :each do
30
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
31
+ end
32
+
33
+ it "should return an array of defined scope names as symbols" do
34
+ Alpha.sphinx_scopes.should == [:by_name]
35
+ end
36
+ end
37
+
38
+ describe '.default_sphinx_scope' do
39
+ before :each do
40
+ Alpha.sphinx_scope(:scope_used_as_default_scope) { {:conditions => {:name => 'name'}} }
41
+ Alpha.default_sphinx_scope :scope_used_as_default_scope
42
+ end
43
+
44
+ it "should return an array of defined scope names as symbols" do
45
+ Alpha.sphinx_scopes.should == [:scope_used_as_default_scope]
46
+ end
47
+
48
+ it "should have a default_sphinx_scope" do
49
+ Alpha.has_default_sphinx_scope?.should be_true
50
+ end
51
+ end
52
+
53
+ describe '.remove_sphinx_scopes' do
54
+ before :each do
55
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
56
+ Alpha.remove_sphinx_scopes
57
+ end
58
+
59
+ it "should remove sphinx scope methods" do
60
+ Alpha.should_not respond_to(:by_name)
61
+ end
62
+
63
+ it "should empty the list of sphinx scopes" do
64
+ Alpha.sphinx_scopes.should be_empty
65
+ end
66
+ end
67
+
68
+ describe '.example_default_scope' do
69
+ before :each do
70
+ Alpha.sphinx_scope(:foo_scope){ {:conditions => {:name => 'foo'}} }
71
+ Alpha.default_sphinx_scope :foo_scope
72
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
73
+ Alpha.sphinx_scope(:by_foo) { |foo| {:conditions => {:foo => foo}} }
74
+ end
75
+
76
+ it "should return a ThinkingSphinx::Search object" do
77
+ Alpha.search.should be_a(ThinkingSphinx::Search)
78
+ end
79
+
80
+ it "should apply the default scope options to the underlying search object" do
81
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
82
+ search.search.options[:conditions].should == {:name => 'foo'}
83
+ end
84
+
85
+ it "should apply the default scope options and scope options to the underlying search object" do
86
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
87
+ search.by_foo('foo').search.options[:conditions].should == {:foo => 'foo', :name => 'foo'}
88
+ end
89
+
90
+ it "should apply the default scope options before other scope options to the underlying search object" do
91
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
92
+ search.by_name('bar').search.options[:conditions].should == {:name => 'bar'}
93
+ end
94
+ end
95
+
96
+ describe '.example_scope' do
97
+ before :each do
98
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
99
+ Alpha.sphinx_scope(:by_foo) { |foo| {:conditions => {:foo => foo}} }
100
+ Alpha.sphinx_scope(:with_betas) { {:classes => [Beta]} }
101
+ end
102
+
103
+ it "should return a ThinkingSphinx::Search object" do
104
+ Alpha.by_name('foo').should be_a(ThinkingSphinx::Search)
105
+ end
106
+
107
+ it "should set the classes option" do
108
+ Alpha.by_name('foo').options[:classes].should == [Alpha]
109
+ end
110
+
111
+ it "should be able to be called on a ThinkingSphinx::Search object" do
112
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
113
+ lambda {
114
+ search.by_name('foo')
115
+ }.should_not raise_error
116
+ end
117
+
118
+ it "should return the search object it gets called upon" do
119
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
120
+ search.by_name('foo').should == search
121
+ end
122
+
123
+ it "should apply the scope options to the underlying search object" do
124
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
125
+ search.by_name('foo').options[:conditions].should == {:name => 'foo'}
126
+ end
127
+
128
+ it "should combine hash option scopes such as :conditions" do
129
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
130
+ search.by_name('foo').by_foo('bar').options[:conditions].
131
+ should == {:name => 'foo', :foo => 'bar'}
132
+ end
133
+
134
+ it "should combine array option scopes such as :classes" do
135
+ search = ThinkingSphinx::Search.new(:classes => [Alpha])
136
+ search.with_betas.options[:classes].should == [Alpha, Beta]
137
+ end
138
+ end
139
+
140
+ describe '.search_count_with_scope' do
141
+ before :each do
142
+ @config = ThinkingSphinx::Configuration.instance
143
+ @client = Riddle::Client.new
144
+
145
+ @config.stub!(:client => @client)
146
+ @client.stub!(:query => {:matches => [], :total_found => 43})
147
+ Alpha.sphinx_scope(:by_name) { |name| {:conditions => {:name => name}} }
148
+ Alpha.sphinx_scope(:ids_only) { {:ids_only => true} }
149
+ end
150
+
151
+ it "should return the total number of results" do
152
+ Alpha.by_name('foo').search_count.should == 43
153
+ end
154
+
155
+ it "should not make any calls to the database" do
156
+ Alpha.should_not_receive(:find)
157
+
158
+ Alpha.by_name('foo').search_count
159
+ end
160
+
161
+ it "should not leave the :ids_only option set and the results populated if it was not set before" do
162
+ stored_scope = Alpha.by_name('foo')
163
+ stored_scope.search_count
164
+ stored_scope.options[:ids_only].should be_false
165
+ stored_scope.populated?.should be_false
166
+ end
167
+
168
+ it "should leave the :ids_only option set and the results populated if it was set before" do
169
+ stored_scope = Alpha.by_name('foo').ids_only
170
+ stored_scope.search_count
171
+ stored_scope.options[:ids_only].should be_true
172
+ stored_scope.populated?.should be_true
173
+ end
174
+ end
175
+
176
+ end
@@ -0,0 +1,576 @@
1
+ require 'spec_helper'
2
+
3
+ describe ThinkingSphinx::ActiveRecord do
4
+ before :each do
5
+ @existing_alpha_indexes = Alpha.sphinx_indexes.clone
6
+ @existing_beta_indexes = Beta.sphinx_indexes.clone
7
+
8
+ Alpha.send :defined_indexes=, false
9
+ Beta.send :defined_indexes=, false
10
+
11
+ Alpha.sphinx_indexes.clear
12
+ Beta.sphinx_indexes.clear
13
+ end
14
+
15
+ after :each do
16
+ Alpha.sphinx_indexes.replace @existing_alpha_indexes
17
+ Beta.sphinx_indexes.replace @existing_beta_indexes
18
+
19
+ Alpha.send :defined_indexes=, true
20
+ Beta.send :defined_indexes=, true
21
+
22
+ Alpha.sphinx_index_blocks.clear
23
+ Beta.sphinx_index_blocks.clear
24
+ end
25
+
26
+ describe '.define_index' do
27
+ it "should do nothing if indexes are disabled" do
28
+ ThinkingSphinx.define_indexes = false
29
+ ThinkingSphinx::Index.should_not_receive(:new)
30
+
31
+ Alpha.define_index { }
32
+ Alpha.define_indexes
33
+
34
+ ThinkingSphinx.define_indexes = true
35
+ end
36
+
37
+ it "should not evaluate the index block automatically" do
38
+ lambda {
39
+ Alpha.define_index { raise StandardError }
40
+ }.should_not raise_error
41
+ end
42
+
43
+ it "should add the model to the context collection" do
44
+ Alpha.define_index { indexes :name }
45
+
46
+ ThinkingSphinx.context.indexed_models.should include("Alpha")
47
+ end
48
+
49
+ it "should die quietly if there is a database error" do
50
+ ThinkingSphinx::Index::Builder.stub(:generate) { raise Mysql::Error }
51
+ Alpha.define_index { indexes :name }
52
+
53
+ lambda {
54
+ Alpha.define_indexes
55
+ }.should_not raise_error
56
+ end
57
+
58
+ it "should die noisily if there is a non-database error" do
59
+ ThinkingSphinx::Index::Builder.stub(:generate) { raise StandardError }
60
+ Alpha.define_index { indexes :name }
61
+
62
+ lambda {
63
+ Alpha.define_indexes
64
+ }.should raise_error
65
+ end
66
+
67
+ it "should set the index's name using the parameter if provided" do
68
+ Alpha.define_index('custom') { indexes :name }
69
+ Alpha.define_indexes
70
+
71
+ Alpha.sphinx_indexes.first.name.should == 'custom'
72
+ end
73
+
74
+ context 'callbacks' do
75
+ it "should add a before_validation callback to define_indexes" do
76
+ Alpha.should_receive(:before_validation).with(:define_indexes)
77
+
78
+ Alpha.define_index { }
79
+ end
80
+
81
+ it "should not add a before_validation callback twice" do
82
+ Alpha.should_receive(:before_validation).with(:define_indexes).once
83
+
84
+ Alpha.define_index { }
85
+ Alpha.define_index { }
86
+ end
87
+
88
+ it "should add a before_destroy callback to define_indexes" do
89
+ Alpha.should_receive(:before_destroy).with(:define_indexes)
90
+
91
+ Alpha.define_index { }
92
+ end
93
+
94
+ it "should not add a before_destroy callback twice" do
95
+ Alpha.should_receive(:before_destroy).with(:define_indexes).once
96
+
97
+ Alpha.define_index { }
98
+ Alpha.define_index { }
99
+ end
100
+
101
+ it "should add a toggle_deleted callback when defined" do
102
+ Alpha.should_receive(:after_destroy).with(:toggle_deleted)
103
+
104
+ Alpha.define_index { indexes :name }
105
+ Alpha.define_indexes
106
+ end
107
+
108
+ it "should not add toggle_deleted callback more than once" do
109
+ Alpha.should_receive(:after_destroy).with(:toggle_deleted).once
110
+
111
+ Alpha.define_index { indexes :name }
112
+ Alpha.define_index { indexes :name }
113
+ Alpha.define_indexes
114
+ end
115
+
116
+ it "should add a update_attribute_values callback when defined" do
117
+ Alpha.should_receive(:after_save).with(:update_attribute_values)
118
+
119
+ Alpha.define_index { indexes :name }
120
+ Alpha.define_indexes
121
+ end
122
+
123
+ it "should not add update_attribute_values callback more than once" do
124
+ Alpha.should_receive(:after_save).with(:update_attribute_values).once
125
+
126
+ Alpha.define_index { indexes :name }
127
+ Alpha.define_index { indexes :name }
128
+ Alpha.define_indexes
129
+ end
130
+
131
+ it "should add a toggle_delta callback if deltas are enabled" do
132
+ Beta.should_receive(:before_save).with(:toggle_delta)
133
+
134
+ Beta.define_index {
135
+ indexes :name
136
+ set_property :delta => true
137
+ }
138
+ Beta.define_indexes
139
+ end
140
+
141
+ it "should not add a toggle_delta callback if deltas are disabled" do
142
+ Alpha.should_not_receive(:before_save).with(:toggle_delta)
143
+
144
+ Alpha.define_index { indexes :name }
145
+ Alpha.define_indexes
146
+ end
147
+
148
+ it "should add the toggle_delta callback if deltas are disabled in other indexes" do
149
+ Beta.should_receive(:before_save).with(:toggle_delta).once
150
+
151
+ Beta.define_index { indexes :name }
152
+ Beta.define_index('foo') {
153
+ indexes :name
154
+ set_property :delta => true
155
+ }
156
+ Beta.define_indexes
157
+ end
158
+
159
+ it "should only add the toggle_delta callback once" do
160
+ Beta.should_receive(:before_save).with(:toggle_delta).once
161
+
162
+ Beta.define_index {
163
+ indexes :name
164
+ set_property :delta => true
165
+ }
166
+ Beta.define_index {
167
+ indexes :name
168
+ set_property :delta => true
169
+ }
170
+ Beta.define_indexes
171
+ end
172
+
173
+ it "should add an index_delta callback if deltas are enabled" do
174
+ Beta.stub!(:after_save => true)
175
+ Beta.should_receive(:after_commit).with(:index_delta)
176
+
177
+ Beta.define_index {
178
+ indexes :name
179
+ set_property :delta => true
180
+ }
181
+ Beta.define_indexes
182
+ end
183
+
184
+ it "should not add an index_delta callback if deltas are disabled" do
185
+ Alpha.should_not_receive(:after_commit).with(:index_delta)
186
+
187
+ Alpha.define_index { indexes :name }
188
+ Alpha.define_indexes
189
+ end
190
+
191
+ it "should add the index_delta callback if deltas are disabled in other indexes" do
192
+ Beta.stub!(:after_commit => true)
193
+ Beta.should_receive(:after_commit).with(:index_delta).once
194
+
195
+ Beta.define_index { indexes :name }
196
+ Beta.define_index('foo') {
197
+ indexes :name
198
+ set_property :delta => true
199
+ }
200
+ Beta.define_indexes
201
+ end
202
+
203
+ it "should only add the index_delta callback once" do
204
+ Beta.stub!(:after_commit => true)
205
+ Beta.should_receive(:after_commit).with(:index_delta).once
206
+
207
+ Beta.define_index {
208
+ indexes :name
209
+ set_property :delta => true
210
+ }
211
+ Beta.define_index {
212
+ indexes :name
213
+ set_property :delta => true
214
+ }
215
+ Beta.define_indexes
216
+ end
217
+ end
218
+ end
219
+
220
+ describe '.define_indexes' do
221
+ it "should process define_index blocks" do
222
+ Beta.define_index { indexes :name }
223
+ Beta.sphinx_indexes.length.should == 0
224
+
225
+ Beta.define_indexes
226
+ Beta.sphinx_indexes.length.should == 1
227
+ end
228
+
229
+ it "should not re-add indexes" do
230
+ Beta.define_index { indexes :name }
231
+ Beta.define_indexes
232
+ Beta.define_indexes
233
+
234
+ Beta.sphinx_indexes.length.should == 1
235
+ end
236
+ end
237
+
238
+ describe '.source_of_sphinx_index' do
239
+ it "should return self if model defines an index" do
240
+ Person.source_of_sphinx_index.should == Person
241
+ end
242
+
243
+ it "should return the parent if model inherits an index" do
244
+ Admin::Person.source_of_sphinx_index.should == Person
245
+ end
246
+ end
247
+
248
+ describe '.to_crc32' do
249
+ it "should return an integer" do
250
+ Person.to_crc32.should be_a_kind_of(Integer)
251
+ end
252
+ end
253
+
254
+ describe '.to_crc32s' do
255
+ it "should return an array" do
256
+ Person.to_crc32s.should be_a_kind_of(Array)
257
+ end
258
+ end
259
+
260
+ describe "toggle_deleted method" do
261
+ before :each do
262
+ ThinkingSphinx.stub!(:sphinx_running? => true)
263
+
264
+ @configuration = ThinkingSphinx::Configuration.instance
265
+ @configuration.stub!(
266
+ :address => "an address",
267
+ :port => 123
268
+ )
269
+ @client = Riddle::Client.new
270
+ @client.stub!(:update => true)
271
+ @person = Person.find(:first)
272
+
273
+ @configuration.stub!(:client => @client)
274
+ Person.sphinx_indexes.each { |index| index.stub!(:delta? => false) }
275
+ Person.stub!(:search_for_id => true)
276
+ end
277
+
278
+ it "should update the core index's deleted flag if in core index" do
279
+ @client.should_receive(:update).with(
280
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
281
+ )
282
+
283
+ @person.toggle_deleted
284
+ end
285
+
286
+ it "shouldn't update the core index's deleted flag if the record isn't in it" do
287
+ Person.stub!(:search_for_id => false)
288
+ @client.should_not_receive(:update).with(
289
+ "person_core", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
290
+ )
291
+
292
+ @person.toggle_deleted
293
+ end
294
+
295
+ it "shouldn't attempt to update the deleted flag if sphinx isn't running" do
296
+ ThinkingSphinx.stub!(:sphinx_running? => false)
297
+ @client.should_not_receive(:update)
298
+ Person.should_not_receive(:search_for_id)
299
+
300
+ @person.toggle_deleted
301
+ end
302
+
303
+ it "should update the delta index's deleted flag if delta indexes are enabled and the instance's delta is true" do
304
+ ThinkingSphinx.deltas_enabled = true
305
+ Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
306
+ @person.delta = true
307
+ @client.should_receive(:update).with(
308
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
309
+ )
310
+
311
+ @person.toggle_deleted
312
+ end
313
+
314
+ it "should not update the delta index's deleted flag if delta indexes are enabled and the instance's delta is false" do
315
+ ThinkingSphinx.deltas_enabled = true
316
+ Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
317
+ @person.delta = false
318
+ @client.should_not_receive(:update).with(
319
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
320
+ )
321
+
322
+ @person.toggle_deleted
323
+ end
324
+
325
+ 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
326
+ ThinkingSphinx.deltas_enabled = true
327
+ Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
328
+ @person.delta = 0
329
+ @client.should_not_receive(:update).with(
330
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
331
+ )
332
+
333
+ @person.toggle_deleted
334
+ end
335
+
336
+ it "shouldn't update the delta index if delta indexes are disabled" do
337
+ ThinkingSphinx.deltas_enabled = true
338
+ @client.should_not_receive(:update).with(
339
+ "person_delta", ["sphinx_deleted"], {@person.sphinx_document_id => [1]}
340
+ )
341
+
342
+ @person.toggle_deleted
343
+ end
344
+
345
+ it "should not update either index if updates are disabled" do
346
+ ThinkingSphinx.updates_enabled = false
347
+ ThinkingSphinx.deltas_enabled = true
348
+ Person.sphinx_indexes.each { |index| index.stub!(:delta? => true) }
349
+ @person.delta = true
350
+ @client.should_not_receive(:update)
351
+
352
+ @person.toggle_deleted
353
+ end
354
+ end
355
+
356
+ describe "sphinx_indexes in the inheritance chain (STI)" do
357
+ it "should hand defined indexes on a class down to its child classes" do
358
+ Child.sphinx_indexes.should include(*Person.sphinx_indexes)
359
+ end
360
+
361
+ it "should allow associations to other STI models" do
362
+ source = Child.sphinx_indexes.last.sources.first
363
+ sql = source.to_riddle_for_core(0, 0).sql_query
364
+ sql.gsub!('$start', '0').gsub!('$end', '100')
365
+ lambda {
366
+ Child.connection.execute(sql)
367
+ }.should_not raise_error(ActiveRecord::StatementInvalid)
368
+ end
369
+ end
370
+
371
+ describe '#sphinx_document_id' do
372
+ before :each do
373
+ Alpha.define_index { indexes :name }
374
+ Beta.define_index { indexes :name }
375
+ end
376
+
377
+ it "should return values with the expected offset" do
378
+ person = Person.find(:first)
379
+ model_count = ThinkingSphinx.context.indexed_models.length
380
+ Person.stub!(:sphinx_offset => 3)
381
+
382
+ (person.id * model_count + 3).should == person.sphinx_document_id
383
+ end
384
+ end
385
+
386
+ describe '#primary_key_for_sphinx' do
387
+ before :each do
388
+ @person = Person.find(:first)
389
+ end
390
+
391
+ after :each do
392
+ Person.clear_primary_key_for_sphinx
393
+ end
394
+
395
+ after :each do
396
+ Person.set_sphinx_primary_key nil
397
+ end
398
+
399
+ it "should return the id by default" do
400
+ @person.primary_key_for_sphinx.should == @person.id
401
+ end
402
+
403
+ it "should use the sphinx primary key to determine the value" do
404
+ Person.set_sphinx_primary_key :first_name
405
+ @person.primary_key_for_sphinx.should == @person.first_name
406
+ end
407
+
408
+ it "should not use accessor methods but the attributes hash" do
409
+ id = @person.id
410
+ @person.stub!(:id => 'unique_hash')
411
+ @person.primary_key_for_sphinx.should == id
412
+ end
413
+
414
+ it "should be inherited by subclasses" do
415
+ Person.set_sphinx_primary_key :first_name
416
+ Parent.superclass.custom_primary_key_for_sphinx?
417
+ Parent.primary_key_for_sphinx.should == Person.primary_key_for_sphinx
418
+ end
419
+ end
420
+
421
+ describe '.sphinx_index_names' do
422
+ it "should return the core index" do
423
+ Alpha.define_index { indexes :name }
424
+ Alpha.define_indexes
425
+ Alpha.sphinx_index_names.should == ['alpha_core']
426
+ end
427
+
428
+ it "should return the delta index if enabled" do
429
+ Beta.define_index {
430
+ indexes :name
431
+ set_property :delta => true
432
+ }
433
+ Beta.define_indexes
434
+
435
+ Beta.sphinx_index_names.should == ['beta_core', 'beta_delta']
436
+ end
437
+
438
+ it "should return the superclass with an index definition" do
439
+ Parent.sphinx_index_names.should == ['person_core', 'person_delta']
440
+ end
441
+ end
442
+
443
+ describe '.indexed_by_sphinx?' do
444
+ it "should return true if there is at least one index on the model" do
445
+ Alpha.define_index { indexes :name }
446
+ Alpha.define_indexes
447
+
448
+ Alpha.should be_indexed_by_sphinx
449
+ end
450
+
451
+ it "should return false if there are no indexes on the model" do
452
+ Gamma.should_not be_indexed_by_sphinx
453
+ end
454
+ end
455
+
456
+ describe '.delta_indexed_by_sphinx?' do
457
+ it "should return true if there is at least one delta index on the model" do
458
+ Beta.define_index {
459
+ indexes :name
460
+ set_property :delta => true
461
+ }
462
+ Beta.define_indexes
463
+
464
+ Beta.should be_delta_indexed_by_sphinx
465
+ end
466
+
467
+ it "should return false if there are no delta indexes on the model" do
468
+ Alpha.define_index { indexes :name }
469
+ Alpha.define_indexes
470
+
471
+ Alpha.should_not be_delta_indexed_by_sphinx
472
+ end
473
+ end
474
+
475
+ describe '.delete_in_index' do
476
+ before :each do
477
+ @client = stub('client')
478
+ ThinkingSphinx.stub!(:sphinx_running? => true)
479
+ ThinkingSphinx::Configuration.instance.stub!(:client => @client)
480
+ Alpha.stub!(:search_for_id => true)
481
+ end
482
+
483
+ it "should not update if the document isn't in the given index" do
484
+ Alpha.stub!(:search_for_id => false)
485
+ @client.should_not_receive(:update)
486
+
487
+ Alpha.delete_in_index('alpha_core', 42)
488
+ end
489
+
490
+ it "should direct the update to the supplied index" do
491
+ @client.should_receive(:update) do |index, attributes, values|
492
+ index.should == 'custom_index_core'
493
+ end
494
+
495
+ Alpha.delete_in_index('custom_index_core', 42)
496
+ end
497
+
498
+ it "should set the sphinx_deleted flag to true" do
499
+ @client.should_receive(:update) do |index, attributes, values|
500
+ attributes.should == ['sphinx_deleted']
501
+ values.should == {42 => [1]}
502
+ end
503
+
504
+ Alpha.delete_in_index('alpha_core', 42)
505
+ end
506
+ end
507
+
508
+ describe '.core_index_names' do
509
+ it "should return each index's core name" do
510
+ Alpha.define_index('foo') { indexes :name }
511
+ Alpha.define_index('bar') { indexes :name }
512
+ Alpha.define_indexes
513
+
514
+ Alpha.core_index_names.should == ['foo_core', 'bar_core']
515
+ end
516
+ end
517
+
518
+ describe '.delta_index_names' do
519
+ it "should return index delta names, for indexes with deltas enabled" do
520
+ Alpha.define_index('foo') { indexes :name }
521
+ Alpha.define_index('bar') { indexes :name }
522
+ Alpha.define_indexes
523
+ Alpha.sphinx_indexes.first.delta_object = stub('delta')
524
+
525
+ Alpha.delta_index_names.should == ['foo_delta']
526
+ end
527
+ end
528
+
529
+ describe '.sphinx_offset' do
530
+ before :each do
531
+ @context = ThinkingSphinx.context
532
+ end
533
+
534
+ it "should return the index of the model's name in all known indexed models" do
535
+ @context.stub!(:indexed_models => ['Alpha', 'Beta'])
536
+
537
+ Alpha.sphinx_offset.should == 0
538
+ Beta.sphinx_offset.should == 1
539
+ end
540
+
541
+ it "should ignore classes that have indexed superclasses" do
542
+ @context.stub!(:indexed_models => ['Alpha', 'Parent', 'Person'])
543
+
544
+ Person.sphinx_offset.should == 1
545
+ end
546
+
547
+ it "should respect first known indexed parents" do
548
+ @context.stub!(:indexed_models => ['Alpha', 'Parent', 'Person'])
549
+
550
+ Parent.sphinx_offset.should == 1
551
+ end
552
+ end
553
+
554
+ describe '.has_sphinx_indexes?' do
555
+ it "should return true if there are sphinx indexes defined" do
556
+ Alpha.sphinx_indexes.replace [stub('index')]
557
+ Alpha.sphinx_index_blocks.replace []
558
+
559
+ Alpha.should have_sphinx_indexes
560
+ end
561
+
562
+ it "should return true if there are sphinx index blocks defined" do
563
+ Alpha.sphinx_indexes.replace []
564
+ Alpha.sphinx_index_blocks.replace [stub('lambda')]
565
+
566
+ Alpha.should have_sphinx_indexes
567
+ end
568
+
569
+ it "should return false if there are no sphinx indexes or blocks" do
570
+ Alpha.sphinx_indexes.clear
571
+ Alpha.sphinx_index_blocks.clear
572
+
573
+ Alpha.should_not have_sphinx_indexes
574
+ end
575
+ end
576
+ end