sherpa99-thinking-sphinx 1.1.4

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 (145) hide show
  1. data/LICENCE +20 -0
  2. data/README +107 -0
  3. data/README.textile +107 -0
  4. data/Rakefile +4 -0
  5. data/contribute.rb +328 -0
  6. data/cucumber.yml +1 -0
  7. data/features/a.rb +17 -0
  8. data/features/attribute_transformation.feature +22 -0
  9. data/features/datetime_deltas.feature +55 -0
  10. data/features/delayed_delta_indexing.feature +37 -0
  11. data/features/deleting_instances.feature +52 -0
  12. data/features/facets.feature +26 -0
  13. data/features/handling_edits.feature +67 -0
  14. data/features/retry_stale_indexes.feature +24 -0
  15. data/features/searching_across_models.feature +20 -0
  16. data/features/searching_by_model.feature +118 -0
  17. data/features/searching_with_find_arguments.feature +56 -0
  18. data/features/sphinx_detection.feature +16 -0
  19. data/features/step_definitions/alpha_steps.rb +3 -0
  20. data/features/step_definitions/beta_steps.rb +11 -0
  21. data/features/step_definitions/cat_steps.rb +3 -0
  22. data/features/step_definitions/common_steps.rb +154 -0
  23. data/features/step_definitions/datetime_delta_steps.rb +11 -0
  24. data/features/step_definitions/delayed_delta_indexing_steps.rb +7 -0
  25. data/features/step_definitions/facet_steps.rb +30 -0
  26. data/features/step_definitions/find_arguments_steps.rb +36 -0
  27. data/features/step_definitions/gamma_steps.rb +15 -0
  28. data/features/step_definitions/search_steps.rb +66 -0
  29. data/features/step_definitions/sphinx_steps.rb +23 -0
  30. data/features/support/db/active_record.rb +40 -0
  31. data/features/support/db/database.example.yml +4 -0
  32. data/features/support/db/migrations/create_alphas.rb +18 -0
  33. data/features/support/db/migrations/create_animals.rb +9 -0
  34. data/features/support/db/migrations/create_betas.rb +15 -0
  35. data/features/support/db/migrations/create_boxes.rb +13 -0
  36. data/features/support/db/migrations/create_comments.rb +13 -0
  37. data/features/support/db/migrations/create_delayed_betas.rb +28 -0
  38. data/features/support/db/migrations/create_developers.rb +39 -0
  39. data/features/support/db/migrations/create_gammas.rb +14 -0
  40. data/features/support/db/migrations/create_people.rb +1014 -0
  41. data/features/support/db/migrations/create_posts.rb +6 -0
  42. data/features/support/db/migrations/create_thetas.rb +16 -0
  43. data/features/support/db/mysql.rb +4 -0
  44. data/features/support/db/postgresql.rb +4 -0
  45. data/features/support/env.rb +6 -0
  46. data/features/support/models/alpha.rb +9 -0
  47. data/features/support/models/animal.rb +5 -0
  48. data/features/support/models/beta.rb +7 -0
  49. data/features/support/models/box.rb +8 -0
  50. data/features/support/models/cat.rb +3 -0
  51. data/features/support/models/comment.rb +3 -0
  52. data/features/support/models/delayed_beta.rb +7 -0
  53. data/features/support/models/developer.rb +8 -0
  54. data/features/support/models/gamma.rb +5 -0
  55. data/features/support/models/person.rb +8 -0
  56. data/features/support/models/post.rb +8 -0
  57. data/features/support/models/theta.rb +7 -0
  58. data/features/support/post_database.rb +37 -0
  59. data/features/support/z.rb +19 -0
  60. data/ginger_scenarios.rb +24 -0
  61. data/init.rb +12 -0
  62. data/lib/thinking_sphinx.rb +144 -0
  63. data/lib/thinking_sphinx/active_record.rb +245 -0
  64. data/lib/thinking_sphinx/active_record/delta.rb +74 -0
  65. data/lib/thinking_sphinx/active_record/has_many_association.rb +29 -0
  66. data/lib/thinking_sphinx/active_record/search.rb +57 -0
  67. data/lib/thinking_sphinx/adapters/abstract_adapter.rb +34 -0
  68. data/lib/thinking_sphinx/adapters/mysql_adapter.rb +53 -0
  69. data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +129 -0
  70. data/lib/thinking_sphinx/association.rb +144 -0
  71. data/lib/thinking_sphinx/attribute.rb +258 -0
  72. data/lib/thinking_sphinx/collection.rb +142 -0
  73. data/lib/thinking_sphinx/configuration.rb +236 -0
  74. data/lib/thinking_sphinx/core/string.rb +22 -0
  75. data/lib/thinking_sphinx/deltas.rb +22 -0
  76. data/lib/thinking_sphinx/deltas/datetime_delta.rb +50 -0
  77. data/lib/thinking_sphinx/deltas/default_delta.rb +65 -0
  78. data/lib/thinking_sphinx/deltas/delayed_delta.rb +25 -0
  79. data/lib/thinking_sphinx/deltas/delayed_delta/delta_job.rb +24 -0
  80. data/lib/thinking_sphinx/deltas/delayed_delta/flag_as_deleted_job.rb +27 -0
  81. data/lib/thinking_sphinx/deltas/delayed_delta/job.rb +26 -0
  82. data/lib/thinking_sphinx/facet.rb +58 -0
  83. data/lib/thinking_sphinx/facet_collection.rb +44 -0
  84. data/lib/thinking_sphinx/field.rb +172 -0
  85. data/lib/thinking_sphinx/index.rb +414 -0
  86. data/lib/thinking_sphinx/index/builder.rb +233 -0
  87. data/lib/thinking_sphinx/index/faux_column.rb +110 -0
  88. data/lib/thinking_sphinx/rails_additions.rb +133 -0
  89. data/lib/thinking_sphinx/search.rb +638 -0
  90. data/lib/thinking_sphinx/tasks.rb +128 -0
  91. data/rails/init.rb +6 -0
  92. data/spec/fixtures/data.sql +32 -0
  93. data/spec/fixtures/database.yml.default +3 -0
  94. data/spec/fixtures/models.rb +81 -0
  95. data/spec/fixtures/structure.sql +84 -0
  96. data/spec/spec_helper.rb +54 -0
  97. data/spec/sphinx_helper.rb +109 -0
  98. data/spec/unit/thinking_sphinx/active_record/delta_spec.rb +136 -0
  99. data/spec/unit/thinking_sphinx/active_record/has_many_association_spec.rb +53 -0
  100. data/spec/unit/thinking_sphinx/active_record/search_spec.rb +107 -0
  101. data/spec/unit/thinking_sphinx/active_record_spec.rb +256 -0
  102. data/spec/unit/thinking_sphinx/association_spec.rb +247 -0
  103. data/spec/unit/thinking_sphinx/attribute_spec.rb +212 -0
  104. data/spec/unit/thinking_sphinx/collection_spec.rb +14 -0
  105. data/spec/unit/thinking_sphinx/configuration_spec.rb +136 -0
  106. data/spec/unit/thinking_sphinx/core/string_spec.rb +9 -0
  107. data/spec/unit/thinking_sphinx/field_spec.rb +145 -0
  108. data/spec/unit/thinking_sphinx/index/builder_spec.rb +5 -0
  109. data/spec/unit/thinking_sphinx/index/faux_column_spec.rb +30 -0
  110. data/spec/unit/thinking_sphinx/index_spec.rb +54 -0
  111. data/spec/unit/thinking_sphinx/search_spec.rb +59 -0
  112. data/spec/unit/thinking_sphinx_spec.rb +129 -0
  113. data/tasks/distribution.rb +48 -0
  114. data/tasks/rails.rake +1 -0
  115. data/tasks/testing.rb +86 -0
  116. data/thinking-sphinx.gemspec +232 -0
  117. data/vendor/after_commit/LICENSE +20 -0
  118. data/vendor/after_commit/README +16 -0
  119. data/vendor/after_commit/Rakefile +22 -0
  120. data/vendor/after_commit/init.rb +5 -0
  121. data/vendor/after_commit/lib/after_commit.rb +42 -0
  122. data/vendor/after_commit/lib/after_commit/active_record.rb +91 -0
  123. data/vendor/after_commit/lib/after_commit/connection_adapters.rb +103 -0
  124. data/vendor/after_commit/test/after_commit_test.rb +53 -0
  125. data/vendor/delayed_job/lib/delayed/job.rb +251 -0
  126. data/vendor/delayed_job/lib/delayed/message_sending.rb +7 -0
  127. data/vendor/delayed_job/lib/delayed/performable_method.rb +55 -0
  128. data/vendor/delayed_job/lib/delayed/worker.rb +54 -0
  129. data/vendor/riddle/lib/riddle.rb +30 -0
  130. data/vendor/riddle/lib/riddle/client.rb +619 -0
  131. data/vendor/riddle/lib/riddle/client/filter.rb +53 -0
  132. data/vendor/riddle/lib/riddle/client/message.rb +65 -0
  133. data/vendor/riddle/lib/riddle/client/response.rb +84 -0
  134. data/vendor/riddle/lib/riddle/configuration.rb +33 -0
  135. data/vendor/riddle/lib/riddle/configuration/distributed_index.rb +48 -0
  136. data/vendor/riddle/lib/riddle/configuration/index.rb +142 -0
  137. data/vendor/riddle/lib/riddle/configuration/indexer.rb +19 -0
  138. data/vendor/riddle/lib/riddle/configuration/remote_index.rb +17 -0
  139. data/vendor/riddle/lib/riddle/configuration/searchd.rb +25 -0
  140. data/vendor/riddle/lib/riddle/configuration/section.rb +37 -0
  141. data/vendor/riddle/lib/riddle/configuration/source.rb +23 -0
  142. data/vendor/riddle/lib/riddle/configuration/sql_source.rb +34 -0
  143. data/vendor/riddle/lib/riddle/configuration/xml_source.rb +28 -0
  144. data/vendor/riddle/lib/riddle/controller.rb +44 -0
  145. metadata +248 -0
@@ -0,0 +1,136 @@
1
+ require 'spec/spec_helper'
2
+
3
+ describe "ThinkingSphinx::ActiveRecord::Delta" do
4
+ it "should call the toggle_delta method after a save" do
5
+ @beta = Beta.new(:name => 'beta')
6
+ @beta.stub_method(:toggle_delta => true)
7
+
8
+ @beta.save
9
+
10
+ @beta.should have_received(:toggle_delta)
11
+ end
12
+
13
+ it "should call the toggle_delta method after a save!" do
14
+ @beta = Beta.new(:name => 'beta')
15
+ @beta.stub_method(:toggle_delta => true)
16
+
17
+ @beta.save!
18
+
19
+ @beta.should have_received(:toggle_delta)
20
+ end
21
+
22
+ describe "suspended_delta method" do
23
+ before :each do
24
+ ThinkingSphinx.stub_method(:deltas_enabled? => true)
25
+ Person.sphinx_indexes.first.delta_object.stub_method(:` => "")
26
+ end
27
+
28
+ it "should execute the argument block with deltas disabled" do
29
+ ThinkingSphinx.should_receive(:deltas_enabled=).once.with(false)
30
+ ThinkingSphinx.should_receive(:deltas_enabled=).once.with(true)
31
+ lambda { Person.suspended_delta { raise 'i was called' } }.should(
32
+ raise_error(Exception)
33
+ )
34
+ end
35
+
36
+ it "should restore deltas_enabled to its original setting" do
37
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
38
+ ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
39
+ Person.suspended_delta { 'no-op' }
40
+ end
41
+
42
+ it "should restore deltas_enabled to its original setting even if there was an exception" do
43
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
44
+ ThinkingSphinx.should_receive(:deltas_enabled=).twice.with(false)
45
+ lambda { Person.suspended_delta { raise 'bad error' } }.should(
46
+ raise_error(Exception)
47
+ )
48
+ end
49
+
50
+ it "should reindex by default after the code block is run" do
51
+ Person.should_receive(:index_delta)
52
+ Person.suspended_delta { 'no-op' }
53
+ end
54
+
55
+ it "should not reindex after the code block if false is passed in" do
56
+ Person.should_not_receive(:index_delta)
57
+ Person.suspended_delta(false) { 'no-op' }
58
+ end
59
+ end
60
+
61
+ describe "toggle_delta method" do
62
+ it "should set the delta value to true" do
63
+ @person = Person.new
64
+
65
+ @person.delta.should be_false
66
+ @person.send(:toggle_delta)
67
+ @person.delta.should be_true
68
+ end
69
+ end
70
+
71
+ describe "index_delta method" do
72
+ before :each do
73
+ ThinkingSphinx::Configuration.stub_method(:environment => "spec")
74
+ ThinkingSphinx.stub_method(:deltas_enabled? => true, :sphinx_running? => true)
75
+ Person.delta_object.stub_methods(:` => "", :toggled => true)
76
+
77
+ @person = Person.new
78
+ @person.stub_method(
79
+ :in_core_index? => false,
80
+ :sphinx_document_id => 1
81
+ )
82
+
83
+ @client = Riddle::Client.stub_instance(:update => true)
84
+ Riddle::Client.stub_method(:new => @client)
85
+ end
86
+
87
+ it "shouldn't index if delta indexing is disabled" do
88
+ ThinkingSphinx.stub_method(:deltas_enabled? => false)
89
+
90
+ @person.send(:index_delta)
91
+
92
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
93
+ @client.should_not have_received(:update)
94
+ end
95
+
96
+ it "shouldn't index if index updating is disabled" do
97
+ ThinkingSphinx.stub_method(:updates_enabled? => false)
98
+
99
+ @person.send(:index_delta)
100
+
101
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
102
+ end
103
+
104
+ it "shouldn't index if the environment is 'test'" do
105
+ ThinkingSphinx.unstub_method(:deltas_enabled?)
106
+ ThinkingSphinx.deltas_enabled = nil
107
+ ThinkingSphinx::Configuration.stub_method(:environment => "test")
108
+
109
+ @person.send(:index_delta)
110
+
111
+ Person.sphinx_indexes.first.delta_object.should_not have_received(:`)
112
+ end
113
+
114
+ it "should call indexer for the delta index" do
115
+ @person.send(:index_delta)
116
+
117
+ Person.sphinx_indexes.first.delta_object.should have_received(:`).with(
118
+ "#{ThinkingSphinx::Configuration.instance.bin_path}indexer --config #{ThinkingSphinx::Configuration.instance.config_file} --rotate person_delta"
119
+ )
120
+ end
121
+
122
+ it "shouldn't update the deleted attribute if not in the index" do
123
+ @person.send(:index_delta)
124
+
125
+ @client.should_not have_received(:update)
126
+ end
127
+
128
+ it "should update the deleted attribute if in the core index" do
129
+ @person.stub_method(:in_core_index? => true)
130
+
131
+ @person.send(:index_delta)
132
+
133
+ @client.should have_received(:update)
134
+ end
135
+ end
136
+ 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.sphinx_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.sphinx_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,107 @@
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_count to ActiveRecord::Base" do
13
+ ActiveRecord::Base.methods.should include("search_count")
14
+ end
15
+
16
+ it "should add search_for_id to ActiveRecord::Base" do
17
+ ActiveRecord::Base.methods.should include("search_for_id")
18
+ end
19
+
20
+ describe "search_for_ids method" do
21
+ before :each do
22
+ ThinkingSphinx::Search.stub_method(:search_for_ids => true)
23
+ end
24
+
25
+ it "should call ThinkingSphinx::Search#search_for_ids with the class option set" do
26
+ Person.search_for_ids("search")
27
+
28
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
29
+ "search", :class => Person
30
+ )
31
+ end
32
+
33
+ it "should override the class option" do
34
+ Person.search_for_ids("search", :class => Friendship)
35
+
36
+ ThinkingSphinx::Search.should have_received(:search_for_ids).with(
37
+ "search", :class => Person
38
+ )
39
+ end
40
+ end
41
+
42
+ describe "search method" do
43
+ before :each do
44
+ ThinkingSphinx::Search.stub_method(:search => true)
45
+ end
46
+
47
+ it "should call ThinkingSphinx::Search#search with the class option set" do
48
+ Person.search("search")
49
+
50
+ ThinkingSphinx::Search.should have_received(:search).with(
51
+ "search", :class => Person
52
+ )
53
+ end
54
+
55
+ it "should override the class option" do
56
+ Person.search("search", :class => Friendship)
57
+
58
+ ThinkingSphinx::Search.should have_received(:search).with(
59
+ "search", :class => Person
60
+ )
61
+ end
62
+ end
63
+
64
+ describe "search_for_id method" do
65
+ before :each do
66
+ ThinkingSphinx::Search.stub_method(:search_for_id => true)
67
+ end
68
+
69
+ it "should call ThinkingSphinx::Search#search with the class option set" do
70
+ Person.search_for_id(10)
71
+
72
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
73
+ 10, :class => Person
74
+ )
75
+ end
76
+
77
+ it "should override the class option" do
78
+ Person.search_for_id(10, :class => Friendship)
79
+
80
+ ThinkingSphinx::Search.should have_received(:search_for_id).with(
81
+ 10, :class => Person
82
+ )
83
+ end
84
+ end
85
+
86
+ describe "search_count method" do
87
+ before :each do
88
+ ThinkingSphinx::Search.stub_method(:count => true)
89
+ end
90
+
91
+ it "should call ThinkingSphinx::Search#search with the class option set" do
92
+ Person.search_count("search")
93
+
94
+ ThinkingSphinx::Search.should have_received(:count).with(
95
+ "search", :class => Person
96
+ )
97
+ end
98
+
99
+ it "should override the class option" do
100
+ Person.search_count("search", :class => Friendship)
101
+
102
+ ThinkingSphinx::Search.should have_received(:count).with(
103
+ "search", :class => Person
104
+ )
105
+ end
106
+ end
107
+ end
@@ -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