pduey-sunspot_rails 1.2.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/.gitignore +5 -0
  2. data/.rspec +1 -0
  3. data/History.txt +54 -0
  4. data/LICENSE +18 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +282 -0
  7. data/Rakefile +18 -0
  8. data/TESTING.md +35 -0
  9. data/TODO +8 -0
  10. data/VERSION.yml +4 -0
  11. data/dev_tasks/rdoc.rake +24 -0
  12. data/dev_tasks/release.rake +4 -0
  13. data/dev_tasks/spec.rake +27 -0
  14. data/dev_tasks/todo.rake +4 -0
  15. data/generators/sunspot/sunspot_generator.rb +9 -0
  16. data/generators/sunspot/templates/sunspot.yml +18 -0
  17. data/install.rb +1 -0
  18. data/lib/generators/sunspot_rails.rb +9 -0
  19. data/lib/generators/sunspot_rails/install/install_generator.rb +13 -0
  20. data/lib/generators/sunspot_rails/install/templates/config/sunspot.yml +17 -0
  21. data/lib/sunspot/rails.rb +63 -0
  22. data/lib/sunspot/rails/adapters.rb +83 -0
  23. data/lib/sunspot/rails/configuration.rb +323 -0
  24. data/lib/sunspot/rails/init.rb +5 -0
  25. data/lib/sunspot/rails/log_subscriber.rb +33 -0
  26. data/lib/sunspot/rails/railtie.rb +36 -0
  27. data/lib/sunspot/rails/railties/controller_runtime.rb +36 -0
  28. data/lib/sunspot/rails/request_lifecycle.rb +36 -0
  29. data/lib/sunspot/rails/searchable.rb +420 -0
  30. data/lib/sunspot/rails/server.rb +173 -0
  31. data/lib/sunspot/rails/solr_instrumentation.rb +20 -0
  32. data/lib/sunspot/rails/solr_logging.rb +62 -0
  33. data/lib/sunspot/rails/spec_helper.rb +26 -0
  34. data/lib/sunspot/rails/stub_session_proxy.rb +142 -0
  35. data/lib/sunspot/rails/tasks.rb +66 -0
  36. data/lib/sunspot_rails.rb +12 -0
  37. data/spec/configuration_spec.rb +173 -0
  38. data/spec/model_lifecycle_spec.rb +63 -0
  39. data/spec/model_spec.rb +478 -0
  40. data/spec/rails2/.gitignore +2 -0
  41. data/spec/rails2/Gemfile +15 -0
  42. data/spec/rails2/Rakefile +17 -0
  43. data/spec/rails2/app/controllers/application.rb +10 -0
  44. data/spec/rails2/app/controllers/application_controller.rb +10 -0
  45. data/spec/rails2/app/controllers/posts_controller.rb +6 -0
  46. data/spec/rails2/app/models/author.rb +8 -0
  47. data/spec/rails2/app/models/blog.rb +12 -0
  48. data/spec/rails2/app/models/location.rb +2 -0
  49. data/spec/rails2/app/models/photo_post.rb +2 -0
  50. data/spec/rails2/app/models/post.rb +11 -0
  51. data/spec/rails2/app/models/post_with_auto.rb +10 -0
  52. data/spec/rails2/app/models/post_with_conditional_index.rb +13 -0
  53. data/spec/rails2/app/models/post_with_default_scope.rb +11 -0
  54. data/spec/rails2/config/boot.rb +116 -0
  55. data/spec/rails2/config/database.yml +4 -0
  56. data/spec/rails2/config/environment.rb +41 -0
  57. data/spec/rails2/config/environments/development.rb +27 -0
  58. data/spec/rails2/config/environments/test.rb +27 -0
  59. data/spec/rails2/config/initializers/new_rails_defaults.rb +19 -0
  60. data/spec/rails2/config/initializers/session_store.rb +15 -0
  61. data/spec/rails2/config/preinitializer.rb +19 -0
  62. data/spec/rails2/config/routes.rb +43 -0
  63. data/spec/rails2/config/sunspot.yml +19 -0
  64. data/spec/rails2/db/schema.rb +27 -0
  65. data/spec/rails2/script/console +3 -0
  66. data/spec/rails3/.gitignore +4 -0
  67. data/spec/rails3/Gemfile +15 -0
  68. data/spec/rails3/README +281 -0
  69. data/spec/rails3/Rakefile +7 -0
  70. data/spec/rails3/app/controllers/application.rb +4 -0
  71. data/spec/rails3/app/controllers/application_controller.rb +4 -0
  72. data/spec/rails3/app/controllers/posts_controller.rb +6 -0
  73. data/spec/rails3/app/models/author.rb +8 -0
  74. data/spec/rails3/app/models/blog.rb +12 -0
  75. data/spec/rails3/app/models/location.rb +2 -0
  76. data/spec/rails3/app/models/photo_post.rb +2 -0
  77. data/spec/rails3/app/models/post.rb +11 -0
  78. data/spec/rails3/app/models/post_with_auto.rb +10 -0
  79. data/spec/rails3/app/models/post_with_conditional_index.rb +13 -0
  80. data/spec/rails3/app/models/post_with_default_scope.rb +11 -0
  81. data/spec/rails3/config.ru +4 -0
  82. data/spec/rails3/config/application.rb +46 -0
  83. data/spec/rails3/config/boot.rb +13 -0
  84. data/spec/rails3/config/database.yml +22 -0
  85. data/spec/rails3/config/environment.rb +5 -0
  86. data/spec/rails3/config/environments/development.rb +19 -0
  87. data/spec/rails3/config/environments/test.rb +19 -0
  88. data/spec/rails3/config/initializers/backtrace_silencers.rb +7 -0
  89. data/spec/rails3/config/initializers/inflections.rb +10 -0
  90. data/spec/rails3/config/initializers/mime_types.rb +5 -0
  91. data/spec/rails3/config/initializers/new_rails_defaults.rb +19 -0
  92. data/spec/rails3/config/initializers/secret_token.rb +7 -0
  93. data/spec/rails3/config/initializers/session_store.rb +8 -0
  94. data/spec/rails3/config/locales/en.yml +5 -0
  95. data/spec/rails3/config/routes.rb +59 -0
  96. data/spec/rails3/config/sunspot.yml +19 -0
  97. data/spec/rails3/db/schema.rb +27 -0
  98. data/spec/rails3/script/console +3 -0
  99. data/spec/rails3/script/rails +6 -0
  100. data/spec/rails3/solr/.gitignore +1 -0
  101. data/spec/rails3/solr/conf/elevate.xml +36 -0
  102. data/spec/rails3/solr/conf/schema.xml +238 -0
  103. data/spec/rails3/solr/conf/solrconfig.xml +938 -0
  104. data/spec/rails3/solr/conf/spellings.txt +2 -0
  105. data/spec/rails3/solr/conf/stopwords.txt +58 -0
  106. data/spec/rails3/solr/conf/synonyms.txt +31 -0
  107. data/spec/rails3/solr/lib/lucene-spatial-2.9.1.jar +0 -0
  108. data/spec/rails3/solr/lib/solr-spatial-light-0.0.6.jar +0 -0
  109. data/spec/request_lifecycle_spec.rb +61 -0
  110. data/spec/schema.rb +27 -0
  111. data/spec/server_spec.rb +37 -0
  112. data/spec/session_spec.rb +24 -0
  113. data/spec/spec_helper.rb +46 -0
  114. data/spec/stub_session_proxy_spec.rb +122 -0
  115. data/sunspot_rails.gemspec +44 -0
  116. metadata +322 -0
@@ -0,0 +1,12 @@
1
+ # This needs to be loaded before sunspot/search/paginated_collection
2
+ # or #to_json gets defined in Object breaking delegation to Array via
3
+ # method_missing
4
+ require 'active_support/core_ext/object/to_json' if Rails::VERSION::MAJOR == 3
5
+
6
+ require 'sunspot/rails'
7
+
8
+ if Rails::VERSION::MAJOR == 3
9
+ require 'sunspot/rails/railtie'
10
+ else
11
+ require 'sunspot/rails/init'
12
+ end
@@ -0,0 +1,173 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe Sunspot::Rails::Configuration, "default values without a sunspot.yml" do
4
+ before(:each) do
5
+ File.stub!(:exist?).and_return(false) # simulate sunspot.yml not existing
6
+ @config = Sunspot::Rails::Configuration.new
7
+ end
8
+
9
+ it "should handle the 'hostname' property when not set" do
10
+ @config.hostname.should == 'localhost'
11
+ end
12
+
13
+ it "should handle the 'path' property when not set" do
14
+ @config.path.should == '/solr'
15
+ end
16
+
17
+ describe "port" do
18
+ it "should default to port 8981 in test" do
19
+ ::Rails.stub!(:env => 'test')
20
+ @config = Sunspot::Rails::Configuration.new
21
+ @config.port.should == 8981
22
+ end
23
+ it "should default to port 8982 in development" do
24
+ ::Rails.stub!(:env => 'development')
25
+ @config = Sunspot::Rails::Configuration.new
26
+ @config.port.should == 8982
27
+ end
28
+ it "should default to 8983 in production" do
29
+ ::Rails.stub!(:env => 'production')
30
+ @config = Sunspot::Rails::Configuration.new
31
+ @config.port.should == 8983
32
+ end
33
+ it "should generally default to 8983" do
34
+ ::Rails.stub!(:env => 'staging')
35
+ @config = Sunspot::Rails::Configuration.new
36
+ @config.port.should == 8983
37
+ end
38
+ end
39
+
40
+ it "should handle the 'log_level' property when not set" do
41
+ @config.log_level.should == 'INFO'
42
+ end
43
+
44
+ it "should handle the 'log_file' property" do
45
+ @config.log_file.should =~ /log\/solr_test.log/
46
+ end
47
+
48
+ it "should handle the 'solr_home' property when not set" do
49
+ Rails.should_receive(:root).at_least(1).and_return('/some/path')
50
+ @config.solr_home.should == '/some/path/solr'
51
+ end
52
+
53
+ it "should handle the 'data_path' property when not set" do
54
+ Rails.should_receive(:root).at_least(1).and_return('/some/path')
55
+ @config.data_path.should == '/some/path/solr/data/test'
56
+ end
57
+
58
+ it "should handle the 'pid_dir' property when not set" do
59
+ Rails.should_receive(:root).at_least(1).and_return('/some/path')
60
+ @config.pid_dir.should == '/some/path/solr/pids/test'
61
+ end
62
+
63
+ it "should handle the 'auto_commit_after_request' propery when not set" do
64
+ @config.auto_commit_after_request?.should == true
65
+ end
66
+
67
+ it "should handle the 'auto_commit_after_delete_request' propery when not set" do
68
+ @config.auto_commit_after_delete_request?.should == false
69
+ end
70
+ end
71
+
72
+ describe Sunspot::Rails::Configuration, "user provided sunspot.yml" do
73
+ before(:each) do
74
+ ::Rails.stub!(:env => 'config_test')
75
+ @config = Sunspot::Rails::Configuration.new
76
+ end
77
+
78
+ it "should handle the 'hostname' property when set" do
79
+ @config.hostname.should == 'some.host'
80
+ end
81
+
82
+ it "should handle the 'port' property when set" do
83
+ @config.port.should == 1234
84
+ end
85
+
86
+ it "should handle the 'path' property when set" do
87
+ @config.path.should == '/solr/idx'
88
+ end
89
+
90
+ it "should handle the 'log_level' propery when set" do
91
+ @config.log_level.should == 'WARNING'
92
+ end
93
+
94
+ it "should handle the 'solr_home' propery when set" do
95
+ @config.solr_home.should == '/my_superior_path'
96
+ end
97
+
98
+ it "should handle the 'data_path' property when set" do
99
+ @config.data_path.should == '/my_superior_path/data'
100
+ end
101
+
102
+ it "should handle the 'pid_dir' property when set" do
103
+ @config.pid_dir.should == '/my_superior_path/pids'
104
+ end
105
+
106
+ it "should handle the 'solr_home' property when set" do
107
+ @config.solr_home.should == '/my_superior_path'
108
+ end
109
+
110
+ it "should handle the 'auto_commit_after_request' propery when set" do
111
+ @config.auto_commit_after_request?.should == false
112
+ end
113
+
114
+ it "should handle the 'auto_commit_after_delete_request' propery when set" do
115
+ @config.auto_commit_after_delete_request?.should == true
116
+ end
117
+ end
118
+
119
+
120
+ describe Sunspot::Rails::Configuration, "with ENV['SOLR_URL'] overriding sunspot.yml" do
121
+ before(:all) do
122
+ ENV['SOLR_URL'] = 'http://environment.host:5432/solr/env'
123
+ end
124
+
125
+ before(:each) do
126
+ ::Rails.stub!(:env => 'config_test')
127
+ @config = Sunspot::Rails::Configuration.new
128
+ end
129
+
130
+ after(:all) do
131
+ ENV.delete('SOLR_URL')
132
+ end
133
+
134
+ it "should handle the 'hostname' property when set" do
135
+ @config.hostname.should == 'environment.host'
136
+ end
137
+
138
+ it "should handle the 'port' property when set" do
139
+ @config.port.should == 5432
140
+ end
141
+
142
+ it "should handle the 'path' property when set" do
143
+ @config.path.should == '/solr/env'
144
+ end
145
+ end
146
+
147
+ describe Sunspot::Rails::Configuration, "with ENV['WEBSOLR_URL'] overriding sunspot.yml" do
148
+ before(:all) do
149
+ ENV['WEBSOLR_URL'] = 'http://index.websolr.test/solr/a1b2c3d4e5f'
150
+ end
151
+
152
+ before(:each) do
153
+ ::Rails.stub!(:env => 'config_test')
154
+ @config = Sunspot::Rails::Configuration.new
155
+ end
156
+
157
+ after(:all) do
158
+ ENV.delete('WEBSOLR_URL')
159
+ end
160
+
161
+ it "should handle the 'hostname' property when set" do
162
+ @config.hostname.should == 'index.websolr.test'
163
+ end
164
+
165
+ it "should handle the 'port' property when set" do
166
+ @config.port.should == 80
167
+ end
168
+
169
+ it "should handle the 'path' property when set" do
170
+ @config.path.should == '/solr/a1b2c3d4e5f'
171
+ end
172
+ end
173
+
@@ -0,0 +1,63 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe 'searchable with lifecycle' do
4
+ describe 'on create' do
5
+ before :each do
6
+ @post = PostWithAuto.create
7
+ Sunspot.commit
8
+ end
9
+
10
+ it 'should automatically index' do
11
+ PostWithAuto.search.results.should == [@post]
12
+ end
13
+ end
14
+
15
+ describe 'on update' do
16
+ before :each do
17
+ @post = PostWithAuto.create
18
+ @post.update_attributes(:title => 'Test 1')
19
+ Sunspot.commit
20
+ end
21
+
22
+ it 'should automatically update index' do
23
+ PostWithAuto.search { with :title, 'Test 1' }.results.should == [@post]
24
+ end
25
+
26
+ it "should index model if relevant attribute changed" do
27
+ @post = PostWithAuto.create!
28
+ @post.title = 'new title'
29
+ @post.should_receive :solr_index
30
+ @post.save!
31
+ end
32
+
33
+ it "should not index model if relevant attribute not changed" do
34
+ @post = PostWithAuto.create!
35
+ @post.updated_at = Date.tomorrow
36
+ @post.should_not_receive :solr_index
37
+ @post.save!
38
+ end
39
+ end
40
+
41
+ describe 'on destroy' do
42
+ before :each do
43
+ @post = PostWithAuto.create
44
+ @post.destroy
45
+ Sunspot.commit
46
+ end
47
+
48
+ it 'should automatically remove it from the index' do
49
+ PostWithAuto.search_ids.should be_empty
50
+ end
51
+ end
52
+ end
53
+
54
+ describe 'searchable with lifecycle - ignoring specific attributes' do
55
+ before(:each) do
56
+ @post = PostWithAuto.create
57
+ end
58
+
59
+ it "should not reindex the object on an update_at change, because it is marked as to-ignore" do
60
+ Sunspot.should_not_receive(:index).with(@post)
61
+ @post.update_attribute :updated_at, 123.seconds.from_now
62
+ end
63
+ end
@@ -0,0 +1,478 @@
1
+ require File.expand_path('spec_helper', File.dirname(__FILE__))
2
+
3
+ describe 'ActiveRecord mixin' do
4
+ describe 'index()' do
5
+ before :each do
6
+ @post = Post.create!
7
+ @post.index
8
+ end
9
+
10
+ it 'should not commit the model' do
11
+ Post.search.results.should be_empty
12
+ end
13
+
14
+ it 'should index the model' do
15
+ Sunspot.commit
16
+ Post.search.results.should == [@post]
17
+ end
18
+
19
+ it "should not blow up if there's a default scope specifying order" do
20
+ posts = Array.new(2) { |j| PostWithDefaultScope.create! :title => (10-j).to_s }
21
+ lambda { PostWithDefaultScope.index(:batch_size => 1) }.should_not raise_error
22
+ end
23
+ end
24
+
25
+ describe 'index() with conditional indexing' do
26
+ describe 'when conditional is false' do
27
+ before :each do
28
+ @post = PostWithConditionalIndex.create!(:title => nil)
29
+ @post.index
30
+ Sunspot.commit
31
+ end
32
+
33
+ it 'should not index the model' do
34
+ PostWithConditionalIndex.search.results.should be_empty
35
+ end
36
+ end
37
+
38
+ describe 'when conditional is true' do
39
+ before :each do
40
+ @post = PostWithConditionalIndex.create!(:title => 'Present')
41
+ @post.index
42
+ Sunspot.commit
43
+ end
44
+
45
+ it 'should index the model' do
46
+ PostWithConditionalIndex.search.results.should == [@post]
47
+ end
48
+ end
49
+ end
50
+
51
+ describe 'single table inheritence' do
52
+ before :each do
53
+ @post = PhotoPost.create!
54
+ end
55
+
56
+ it 'should not break auto-indexing' do
57
+ @post.title = 'Title'
58
+ lambda { @post.save! }.should_not raise_error
59
+ end
60
+ end
61
+
62
+ describe 'index!()' do
63
+ before :each do
64
+ @post = Post.create!
65
+ @post.index!
66
+ end
67
+
68
+ it 'should immediately index and commit' do
69
+ Post.search.results.should == [@post]
70
+ end
71
+ end
72
+
73
+ describe 'index!() with conditional indexing' do
74
+ describe 'when conditional is false' do
75
+ before :each do
76
+ @post = PostWithConditionalIndex.create!(:title => nil)
77
+ @post.index!
78
+ end
79
+
80
+ it 'should not index the model' do
81
+ PostWithConditionalIndex.search.results.should be_empty
82
+ end
83
+ end
84
+
85
+ describe 'when conditional is true' do
86
+ before :each do
87
+ @post = PostWithConditionalIndex.create!(:title => 'Present')
88
+ @post.index!
89
+ end
90
+
91
+ it 'should index the model' do
92
+ PostWithConditionalIndex.search.results.should == [@post]
93
+ end
94
+ end
95
+ end
96
+
97
+ describe 'remove_from_index()' do
98
+ before :each do
99
+ @post = Post.create!
100
+ @post.index!
101
+ @post.remove_from_index
102
+ end
103
+
104
+ it 'should not commit immediately' do
105
+ Post.search.results.should == [@post]
106
+ end
107
+
108
+ it 'should remove the model from the index' do
109
+ Sunspot.commit
110
+ Post.search.results.should be_empty
111
+ end
112
+ end
113
+
114
+ describe 'remove_from_index() with conditional indexing' do
115
+ before :each do
116
+ @post = PostWithConditionalIndex.create!(:title => 'Present')
117
+ @post.index!
118
+ end
119
+
120
+ describe 'when conditional is false' do
121
+ before :each do
122
+ @post.title = nil
123
+ end
124
+
125
+ it 'should not remove the model from the index' do
126
+ @post.remove_from_index
127
+ Sunspot.commit
128
+ PostWithConditionalIndex.search.results.should == [@post]
129
+ end
130
+ end
131
+
132
+ describe 'when conditional is true' do
133
+ it 'should remove the model from the index' do
134
+ @post.remove_from_index
135
+ Sunspot.commit
136
+ PostWithConditionalIndex.search.results.should be_empty
137
+ end
138
+ end
139
+ end
140
+
141
+ describe 'remove_from_index!()' do
142
+ before :each do
143
+ @post = Post.create!
144
+ @post.index!
145
+ @post.remove_from_index!
146
+ end
147
+
148
+ it 'should immediately remove the model and commit' do
149
+ Post.search.results.should be_empty
150
+ end
151
+ end
152
+
153
+ describe 'remove_from_index!() with conditional indexing' do
154
+ before :each do
155
+ @post = PostWithConditionalIndex.create!(:title => 'Present')
156
+ @post.index!
157
+ end
158
+
159
+ describe 'when conditional is false' do
160
+ before :each do
161
+ @post.title = nil
162
+ end
163
+
164
+ it 'should not remove the model from the index' do
165
+ @post.remove_from_index!
166
+ PostWithConditionalIndex.search.results.should == [@post]
167
+ end
168
+ end
169
+
170
+ describe 'when conditional is true' do
171
+ it 'should remove the model from the index' do
172
+ @post.remove_from_index!
173
+ PostWithConditionalIndex.search.results.should be_empty
174
+ end
175
+ end
176
+ end
177
+
178
+ describe 'remove_all_from_index' do
179
+ before :each do
180
+ @posts = Array.new(2) { Post.create! }.each { |post| Sunspot.index(post) }
181
+ Sunspot.commit
182
+ Post.remove_all_from_index
183
+ end
184
+
185
+ it 'should not commit immediately' do
186
+ Post.search.results.to_set.should == @posts.to_set
187
+ end
188
+
189
+ it 'should remove all instances from the index' do
190
+ Sunspot.commit
191
+ Post.search.results.should be_empty
192
+ end
193
+ end
194
+
195
+ describe 'remove_all_from_index!' do
196
+ before :each do
197
+ Array.new(2) { Post.create! }.each { |post| Sunspot.index(post) }
198
+ Sunspot.commit
199
+ Post.remove_all_from_index!
200
+ end
201
+
202
+ it 'should remove all instances from the index and commit immediately' do
203
+ Post.search.results.should be_empty
204
+ end
205
+ end
206
+
207
+ describe 'search()' do
208
+ before :each do
209
+ @post = Post.create!(:title => 'Test Post')
210
+ @post.index!
211
+ end
212
+
213
+ it 'should return results specified by search' do
214
+ Post.search do
215
+ with :title, 'Test Post'
216
+ end.results.should == [@post]
217
+ end
218
+
219
+ it 'should not return results excluded by search' do
220
+ Post.search do
221
+ with :title, 'Bogus Post'
222
+ end.results.should be_empty
223
+ end
224
+
225
+ it 'should use the include option on the data accessor when specified' do
226
+ Post.should_receive(:all).with(hash_including(:include => [:blog])).and_return([@post])
227
+ Post.search do
228
+ with :title, 'Test Post'
229
+ data_accessor_for(Post).include = [:blog]
230
+ end.results.should == [@post]
231
+ end
232
+
233
+ it 'should pass :include option from search call to data accessor' do
234
+ Post.should_receive(:all).with(hash_including(:include => [:blog])).and_return([@post])
235
+ Post.search(:include => [:blog]) do
236
+ with :title, 'Test Post'
237
+ end.results.should == [@post]
238
+ end
239
+
240
+ it 'should use the select option from search call to data accessor' do
241
+ Post.should_receive(:all).with(hash_including(:select => 'title, published_at')).and_return([@post])
242
+ Post.search(:select => 'title, published_at') do
243
+ with :title, 'Test Post'
244
+ end.results.should == [@post]
245
+ end
246
+
247
+ it 'should not allow bogus options to search' do
248
+ lambda { Post.search(:bogus => :option) }.should raise_error(ArgumentError)
249
+ end
250
+
251
+ it 'should use the select option on the data accessor when specified' do
252
+ Post.should_receive(:all).with(hash_including(:select => 'title, published_at')).and_return([@post])
253
+ Post.search do
254
+ with :title, 'Test Post'
255
+ data_accessor_for(Post).select = [:title, :published_at]
256
+ end.results.should == [@post]
257
+ end
258
+
259
+ it 'should not use the select option on the data accessor when not specified' do
260
+ Post.should_receive(:all).with(hash_not_including(:select)).and_return([@post])
261
+ Post.search do
262
+ with :title, 'Test Post'
263
+ end.results.should == [@post]
264
+ end
265
+
266
+ it 'should gracefully handle nonexistent records' do
267
+ post2 = Post.create!(:title => 'Test Post')
268
+ post2.index!
269
+ post2.destroy
270
+ Post.search do
271
+ with :title, 'Test Post'
272
+ end.results.should == [@post]
273
+ end
274
+
275
+ it 'should use an ActiveRecord object for coordinates' do
276
+ post = Post.new(:title => 'Test Post')
277
+ post.location = Location.create!(:lat => 40.0, :lng => -70.0)
278
+ post.save
279
+ post.index!
280
+ Post.search { with(:location).near(40.0, -70.0) }.results.should == [post]
281
+ end
282
+
283
+ end
284
+
285
+ describe 'search_ids()' do
286
+ before :each do
287
+ @posts = Array.new(2) { Post.create! }.each { |post| post.index }
288
+ Sunspot.commit
289
+ end
290
+
291
+ it 'should return IDs' do
292
+ Post.search_ids.to_set.should == @posts.map { |post| post.id }.to_set
293
+ end
294
+ end
295
+
296
+ describe 'searchable?()' do
297
+ it 'should not be true for models that have not been configured for search' do
298
+ Location.should_not be_searchable
299
+ end
300
+
301
+ it 'should be true for models that have been configured for search' do
302
+ Post.should be_searchable
303
+ end
304
+ end
305
+
306
+ describe 'index_orphans()' do
307
+ before :each do
308
+ @posts = Array.new(2) { Post.create }.each { |post| post.index }
309
+ Sunspot.commit
310
+ @posts.first.destroy
311
+ end
312
+
313
+ it 'should return IDs of objects that are in the index but not the database' do
314
+ Post.index_orphans.should == [@posts.first.id]
315
+ end
316
+ end
317
+
318
+ describe 'clean_index_orphans()' do
319
+ before :each do
320
+ @posts = Array.new(2) { Post.create }.each { |post| post.index }
321
+ Sunspot.commit
322
+ @posts.first.destroy
323
+ end
324
+
325
+ it 'should remove orphans from the index' do
326
+ Post.clean_index_orphans
327
+ Sunspot.commit
328
+ Post.search.results.should == [@posts.last]
329
+ end
330
+ end
331
+
332
+ describe 'reindex()' do
333
+ before :each do
334
+ @posts = Array.new(2) { Post.create }
335
+ end
336
+
337
+ it 'should index all instances' do
338
+ Post.reindex(:batch_size => nil)
339
+ Sunspot.commit
340
+ Post.search.results.to_set.should == @posts.to_set
341
+ end
342
+
343
+ it 'should remove all currently indexed instances' do
344
+ old_post = Post.create!
345
+ old_post.index!
346
+ old_post.destroy
347
+ Post.reindex
348
+ Sunspot.commit
349
+ Post.search.results.to_set.should == @posts.to_set
350
+ end
351
+ end
352
+
353
+ describe 'reindex() with real data' do
354
+ before :each do
355
+ @posts = Array.new(2) { Post.create }
356
+ end
357
+
358
+ it 'should index all instances' do
359
+ Post.reindex(:batch_size => nil)
360
+ Sunspot.commit
361
+ Post.search.results.to_set.should == @posts.to_set
362
+ end
363
+
364
+ it 'should remove all currently indexed instances' do
365
+ old_post = Post.create!
366
+ old_post.index!
367
+ old_post.destroy
368
+ Post.reindex
369
+ Sunspot.commit
370
+ Post.search.results.to_set.should == @posts.to_set
371
+ end
372
+
373
+ describe "using batch sizes" do
374
+ it 'should index with a specified batch size' do
375
+ Post.reindex(:batch_size => 1)
376
+ Sunspot.commit
377
+ Post.search.results.to_set.should == @posts.to_set
378
+ end
379
+ end
380
+ end
381
+
382
+ describe 'reindex() with conditional indexing' do
383
+ before :each do
384
+ @non_indexed_post = PostWithConditionalIndex.create!(:title => nil)
385
+ @indexed_post = PostWithConditionalIndex.create!(:title => 'Present')
386
+ end
387
+
388
+ describe "when not using batches" do
389
+ it 'should not include non indexed records' do
390
+ PostWithConditionalIndex.reindex(:batch_size => nil)
391
+ Sunspot.commit
392
+ PostWithConditionalIndex.search.results.should == [@indexed_post]
393
+ end
394
+ end
395
+
396
+ describe "when using batches" do
397
+ it 'should not include non indexed records' do
398
+ PostWithConditionalIndex.reindex(:batch_size => 1)
399
+ Sunspot.commit
400
+ PostWithConditionalIndex.search.results.should == [@indexed_post]
401
+ end
402
+ end
403
+ end
404
+
405
+ describe "reindex()" do
406
+
407
+ before(:each) do
408
+ @posts = Array.new(2) { Post.create }
409
+ end
410
+
411
+ describe "when not using batches" do
412
+
413
+ it "should select all if the batch_size is nil" do
414
+ Post.should_receive(:all).with(:include => []).and_return([])
415
+ Post.reindex(:batch_size => nil)
416
+ end
417
+
418
+ it "should search for models with includes" do
419
+ Post.should_receive(:all).with(:include => :author).and_return([])
420
+ Post.reindex(:batch_size => nil, :include => :author)
421
+ end
422
+
423
+ end
424
+
425
+ describe "when using batches" do
426
+ it "should commit after indexing each batch" do
427
+ Sunspot.should_receive(:commit).twice
428
+ Post.reindex(:batch_size => 1)
429
+ end
430
+
431
+ it "should commit after indexing everything" do
432
+ Sunspot.should_receive(:commit).once
433
+ Post.reindex(:batch_commit => false)
434
+ end
435
+ end
436
+ end
437
+
438
+ describe "more_like_this()" do
439
+ before(:each) do
440
+ @posts = [
441
+ Post.create!(:title => 'Post123', :body => "one two three"),
442
+ Post.create!(:title => 'Post345', :body => "three four five"),
443
+ Post.create!(:title => 'Post456', :body => "four five six"),
444
+ Post.create!(:title => 'Post234', :body => "two three four"),
445
+ ]
446
+ @posts_with_auto = [
447
+ PostWithAuto.create!(:body => "one two three"),
448
+ PostWithAuto.create!(:body => "four five six")
449
+ ]
450
+ @posts.each { |p| p.index! }
451
+ end
452
+
453
+ it "should return results" do
454
+ @posts.first.more_like_this.results.should == [@posts[3], @posts[1]]
455
+ end
456
+
457
+ it "should return results for specified classes" do
458
+ @posts.first.more_like_this(Post, PostWithAuto).results.to_set.should ==
459
+ Set[@posts_with_auto[0], @posts[1], @posts[3]]
460
+ end
461
+ end
462
+
463
+ describe 'more_like_this_ids()' do
464
+ before :each do
465
+ @posts = [
466
+ Post.create!(:title => 'Post123', :body => "one two three"),
467
+ Post.create!(:title => 'Post345', :body => "three four five"),
468
+ Post.create!(:title => 'Post456', :body => "four five six"),
469
+ Post.create!(:title => 'Post234', :body => "two three four"),
470
+ ]
471
+ @posts.each { |p| p.index! }
472
+ end
473
+
474
+ it 'should return IDs' do
475
+ @posts.first.more_like_this_ids.to_set.should == [@posts[3], @posts[1]].map { |post| post.id }.to_set
476
+ end
477
+ end
478
+ end