sunspot_rails 0.10.9 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,45 @@
1
+ module Sunspot
2
+ module Rails
3
+ module Spec
4
+ module Extension
5
+ def self.included(base)
6
+ base.class_eval do
7
+ class_inheritable_accessor :sunspot_integration
8
+ extend ClassMethods
9
+ end
10
+ end
11
+
12
+ def integrate_sunspot?
13
+ self.class.integrate_sunspot?
14
+ end
15
+
16
+ def mock_sunspot
17
+ [ :index, :remove_from_index ].each do |method_name|
18
+ Sunspot.stub!(method_name)
19
+ end
20
+ end
21
+
22
+ end
23
+
24
+ module ClassMethods
25
+ def integrate_sunspot( integrate = true )
26
+ self.sunspot_integration = integrate
27
+ end
28
+
29
+ def integrate_sunspot?
30
+ !!self.sunspot_integration
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ module ActiveSupport
38
+ class TestCase
39
+ before(:each) do
40
+ mock_sunspot unless integrate_sunspot?
41
+ end
42
+
43
+ include Sunspot::Rails::Spec::Extension
44
+ end
45
+ end
@@ -1,9 +1,6 @@
1
1
  require 'sunspot'
2
2
 
3
- Sunspot.config.solr.url = URI::HTTP.build(:host => Sunspot::Rails.configuration.hostname,
4
- :port => Sunspot::Rails.configuration.port,
5
- :path => Sunspot::Rails.configuration.path).to_s
6
-
3
+ Sunspot.session = Sunspot::Rails::SessionProxy.instance
7
4
  Sunspot::Adapters::InstanceAdapter.register(Sunspot::Rails::Adapters::ActiveRecordInstanceAdapter, ActiveRecord::Base)
8
5
  Sunspot::Adapters::DataAccessor.register(Sunspot::Rails::Adapters::ActiveRecordDataAccessor, ActiveRecord::Base)
9
6
  ActiveRecord::Base.module_eval { include(Sunspot::Rails::Searchable) }
@@ -1,53 +1,102 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe Sunspot::Rails::Configuration, "default values" do
4
4
  before(:each) do
5
- File.should_receive(:exist?).and_return(false)
5
+ File.should_receive(:exist?).at_least(:once).and_return(false)
6
+ @config = Sunspot::Rails::Configuration.new
6
7
  end
7
8
 
8
9
  it "should handle the 'hostname' property when not set" do
9
- config = Sunspot::Rails::Configuration.new
10
- config.hostname.should == 'localhost'
10
+ @config.hostname.should == 'localhost'
11
11
  end
12
12
 
13
13
  it "should handle the 'path' property when not set" do
14
- config = Sunspot::Rails::Configuration.new
15
- config.path.should == '/solr'
14
+ @config.path.should == '/solr'
16
15
  end
17
16
 
18
17
  it "should handle the 'port' property when not set" do
19
- config = Sunspot::Rails::Configuration.new
20
- config.port.should == 8983
18
+ @config.port.should == 8983
19
+ end
20
+
21
+ it "should handle the 'log_level' property when not set" do
22
+ @config.log_level.should == 'INFO'
23
+ end
24
+
25
+ it "should handle the 'log_file' property" do
26
+ @config.log_file.should =~ /log\/solr_test.log/
27
+ end
28
+
29
+ it "should handle the 'solr_home' property when not set" do
30
+ Rails.should_receive(:root).at_least(1).and_return('/some/path')
31
+ @config.solr_home.should == '/some/path/solr'
32
+ end
33
+
34
+ it "should handle the 'data_path' property when not set" do
35
+ Rails.should_receive(:root).at_least(1).and_return('/some/path')
36
+ @config.data_path.should == '/some/path/solr/data/test'
37
+ end
38
+
39
+ it "should handle the 'pid_path' property when not set" do
40
+ Rails.should_receive(:root).at_least(1).and_return('/some/path')
41
+ @config.pid_path.should == '/some/path/solr/pids/test'
42
+ end
43
+
44
+ it "should handle the 'solr_home' property when not set" do
45
+ @config.solr_home.should_not == nil
21
46
  end
22
47
 
23
48
  it "should handle the 'auto_commit_after_request' propery when not set" do
24
- config = Sunspot::Rails::Configuration.new
25
- config.auto_commit_after_request?.should == true
49
+ @config.auto_commit_after_request?.should == true
50
+ end
51
+
52
+ it "should handle the 'auto_commit_after_delete_request' propery when not set" do
53
+ @config.auto_commit_after_delete_request?.should == false
26
54
  end
27
55
  end
28
56
 
29
57
  describe Sunspot::Rails::Configuration, "user settings" do
30
58
  before(:each) do
31
- Rails.stub!(:env => 'config_test')
59
+ ::Rails.stub!(:env => 'config_test')
60
+ @config = Sunspot::Rails::Configuration.new
32
61
  end
33
62
 
34
- it "should handle the 'hostname' property when not set" do
35
- config = Sunspot::Rails::Configuration.new
36
- config.hostname.should == 'some.host'
63
+ it "should handle the 'hostname' property when set" do
64
+ @config.hostname.should == 'some.host'
37
65
  end
38
66
 
39
- it "should handle the 'port' property when not set" do
40
- config = Sunspot::Rails::Configuration.new
41
- config.port.should == 1234
67
+ it "should handle the 'port' property when set" do
68
+ @config.port.should == 1234
42
69
  end
43
70
 
44
71
  it "should handle the 'path' property when set" do
45
- config = Sunspot::Rails::Configuration.new
46
- config.path.should == '/solr/idx'
72
+ @config.path.should == '/solr/idx'
73
+ end
74
+
75
+ it "should handle the 'log_level' propery when set" do
76
+ @config.log_level.should == 'WARNING'
77
+ end
78
+
79
+ it "should handle the 'solr_home' propery when set" do
80
+ @config.solr_home.should == '/my_superior_path'
81
+ end
82
+
83
+ it "should handle the 'data_path' property when set" do
84
+ @config.data_path.should == '/my_superior_path/data'
85
+ end
86
+
87
+ it "should handle the 'pid_path' property when set" do
88
+ @config.pid_path.should == '/my_superior_path/pids'
89
+ end
90
+
91
+ it "should handle the 'solr_home' property when set" do
92
+ @config.solr_home.should == '/my_superior_path'
47
93
  end
48
94
 
49
95
  it "should handle the 'auto_commit_after_request' propery when set" do
50
- config = Sunspot::Rails::Configuration.new
51
- config.auto_commit_after_request?.should == false
96
+ @config.auto_commit_after_request?.should == false
97
+ end
98
+
99
+ it "should handle the 'auto_commit_after_delete_request' propery when set" do
100
+ @config.auto_commit_after_delete_request?.should == true
52
101
  end
53
102
  end
@@ -0,0 +1,8 @@
1
+ class Author < ActiveRecord::Base
2
+ set_table_name :writers
3
+ set_primary_key :writer_id
4
+
5
+ searchable do
6
+ string :name
7
+ end
8
+ end
@@ -3,7 +3,7 @@ class PostWithAuto < ActiveRecord::Base
3
3
  'posts'
4
4
  end
5
5
 
6
- searchable do
6
+ searchable :ignore_attribute_changes_of => [ :updated_at ] do
7
7
  string :title
8
8
  end
9
9
  end
@@ -1,4 +1,4 @@
1
1
  test: &test
2
2
  :adapter: sqlite3
3
- :dbfile: vendor/plugins/sunspot_rails/spec/test.db
3
+ :database: db/test.db
4
4
  development: *test
@@ -1,7 +1,7 @@
1
1
  # Be sure to restart your server when you modify this file
2
2
 
3
3
  # Specifies gem version of Rails to use when vendor/rails is not present
4
- RAILS_GEM_VERSION = ENV['RAILS_GEM_VERSION'] || '2.3.2' unless defined? RAILS_GEM_VERSION
4
+ # RAILS_GEM_VERSION = ENV['RAILS_GEM_VERSION'] || '2.3.3' unless defined? RAILS_GEM_VERSION
5
5
 
6
6
  # Bootstrap the Rails environment, frameworks, and default configuration
7
7
  require File.join(File.dirname(__FILE__), 'boot')
@@ -19,6 +19,7 @@ Rails::Initializer.run do |config|
19
19
  # config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
20
20
  # config.gem "sqlite3-ruby", :lib => "sqlite3"
21
21
  # config.gem "aws-s3", :lib => "aws/s3"
22
+ config.gem "qoobaa-sqlite3-ruby", :lib => 'sqlite3', :source => "http://gems.github.com" if RUBY_VERSION > '1.9'
22
23
 
23
24
  # Only load the plugins named here, in the order given (default is alphabetical).
24
25
  # :all can be used as a placeholder for all plugins not explicitly named
@@ -11,4 +11,9 @@ config_test:
11
11
  hostname: some.host
12
12
  port: 1234
13
13
  path: /solr/idx
14
+ log_level: WARNING
15
+ data_path: /my_superior_path/data
16
+ pid_path: /my_superior_path/pids
17
+ solr_home: /my_superior_path
14
18
  auto_commit_after_request: false
19
+ auto_commit_after_delete_request: true
@@ -0,0 +1,20 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :posts, :force => true do |t|
3
+ t.string :title
4
+ t.text :body
5
+ t.references :blog
6
+ t.timestamps
7
+ end
8
+
9
+ create_table :blogs, :force => true do |t|
10
+ t.string :name
11
+ t.string :subdomain
12
+ t.timestamps
13
+ end
14
+
15
+ create_table :writers, :force => true, :primary_key => :writer_id do |t|
16
+ t.string :name
17
+ t.timestamps
18
+ end
19
+
20
+ end
@@ -0,0 +1 @@
1
+ S
@@ -1,6 +1,8 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe 'searchable with lifecycle' do
4
+ integrate_sunspot
5
+
4
6
  describe 'on create' do
5
7
  before :each do
6
8
  @post = PostWithAuto.create
@@ -23,7 +25,7 @@ describe 'searchable with lifecycle' do
23
25
  PostWithAuto.search { with :title, 'Test 1' }.results.should == [@post]
24
26
  end
25
27
  end
26
-
28
+
27
29
  describe 'on destroy' do
28
30
  before :each do
29
31
  @post = PostWithAuto.create
@@ -36,3 +38,16 @@ describe 'searchable with lifecycle' do
36
38
  end
37
39
  end
38
40
  end
41
+
42
+ describe 'searchable with lifecycle - ignoring specific attributes' do
43
+ integrate_sunspot
44
+
45
+ before(:each) do
46
+ @post = PostWithAuto.create
47
+ end
48
+
49
+ it "should not reindex the object on an update_at change, because it is marked as to-ignore" do
50
+ Sunspot.should_not_receive(:index).with(@post)
51
+ @post.update_attribute :updated_at, 123.seconds.from_now
52
+ end
53
+ end
@@ -1,6 +1,8 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe 'ActiveRecord mixin' do
4
+ integrate_sunspot
5
+
4
6
  describe 'index()' do
5
7
  before :each do
6
8
  @post = Post.create!
@@ -105,7 +107,30 @@ describe 'ActiveRecord mixin' do
105
107
  end
106
108
 
107
109
  it 'should find ActiveRecord objects with an integer, not a string' do
108
- Post.should_receive(:find).with([@post.id.to_i]).and_return([@post])
110
+ Post.should_receive(:find).with([@post.id.to_i], anything()).and_return([@post])
111
+ Post.search do
112
+ with :title, 'Test Post'
113
+ end.results.should == [@post]
114
+ end
115
+
116
+ it 'should use the include option on the data accessor when specified' do
117
+ Post.should_receive(:find).with(anything(), hash_including(:include => [:blog])).and_return([@post])
118
+ Post.search do
119
+ with :title, 'Test Post'
120
+ data_accessor_for(Post).include = [:blog]
121
+ end.results.should == [@post]
122
+ end
123
+
124
+ it 'should use the select option on the data accessor when specified' do
125
+ Post.should_receive(:find).with(anything(), hash_including(:select => 'title, published_at')).and_return([@post])
126
+ Post.search do
127
+ with :title, 'Test Post'
128
+ data_accessor_for(Post).select = [:title, :published_at]
129
+ end.results.should == [@post]
130
+ end
131
+
132
+ it 'should not use the select option on the data accessor when not specified' do
133
+ Post.should_receive(:find).with(anything(), hash_not_including(:select)).and_return([@post])
109
134
  Post.search do
110
135
  with :title, 'Test Post'
111
136
  end.results.should == [@post]
@@ -237,10 +262,21 @@ describe 'ActiveRecord mixin' do
237
262
  Post.should_receive(:all).with do |params|
238
263
  params[:limit].should == 500
239
264
  params[:include].should == []
265
+ params[:conditions].should == ['posts.id > ?', 0]
266
+ params[:order].should == 'id'
240
267
  end.and_return(@posts)
241
268
  Post.reindex
242
269
  end
243
270
 
271
+ it "should set the conditions using the overridden table attributes" do
272
+ @posts = Array.new(10) { Author.create }
273
+ Author.should_receive(:all).with do |params|
274
+ params[:conditions].should == ['writers.writer_id > ?', 0]
275
+ params[:order].should == 'writer_id'
276
+ end.and_return(@posts)
277
+ Author.reindex
278
+ end
279
+
244
280
  it "should count the number of records to index" do
245
281
  Post.should_receive(:count).and_return(10)
246
282
  Post.reindex
@@ -1,4 +1,4 @@
1
- require File.join(File.dirname(__FILE__), 'spec_helper')
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  describe 'request lifecycle', :type => :controller do
4
4
  before(:each) do
@@ -27,4 +27,26 @@ describe 'request lifecycle', :type => :controller do
27
27
  Sunspot.should_receive(:commit_if_dirty)
28
28
  post :create, :post => { :title => 'Test 1' }
29
29
  end
30
+
31
+ ### auto_commit_if_delete_dirty
32
+
33
+ it 'should automatically commit after each delete if specified' do
34
+ @configuration.user_configuration = { 'auto_commit_after_request' => false,
35
+ 'auto_commit_after_delete_request' => true }
36
+ Sunspot.should_receive(:commit_if_delete_dirty)
37
+ post :create, :post => { :title => 'Test 1' }
38
+ end
39
+
40
+ it 'should not automatically commit on delete if configuration is set to false' do
41
+ @configuration.user_configuration = { 'auto_commit_after_request' => false,
42
+ 'auto_commit_after_delete_request' => false }
43
+ Sunspot.should_not_receive(:commit_if_delete_dirty)
44
+ post :create, :post => { :title => 'Test 1' }
45
+ end
46
+
47
+ it 'should not automatically commit on delete if configuration is not specified' do
48
+ @configuration.user_configuration = { 'auto_commit_after_request' => false }
49
+ Sunspot.should_not_receive(:commit_if_delete_dirty)
50
+ post :create, :post => { :title => 'Test 1' }
51
+ end
30
52
  end
@@ -11,4 +11,10 @@ ActiveRecord::Schema.define(:version => 0) do
11
11
  t.string :subdomain
12
12
  t.timestamps
13
13
  end
14
+
15
+ create_table :writers, :force => true, :primary_key => :writer_id do |t|
16
+ t.string :name
17
+ t.timestamps
18
+ end
19
+
14
20
  end
@@ -0,0 +1,124 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ describe Sunspot::Rails::Server do
4
+ before(:each) do
5
+ @sunspot_configuration = Sunspot::Rails::Configuration.new
6
+ end
7
+
8
+ describe "rake task commands" do
9
+ before(:each) do
10
+ Sunspot::Rails::Server.should_receive(:pid_path).and_return('/tmp')
11
+ end
12
+
13
+ it "should generate and execute the start command" do
14
+ Sunspot::Rails::Server.should_receive(:start_command).and_return('sunspot-start')
15
+ Sunspot::Rails::Server.should_respond_to_and_receive(:bootstrap_neccessary?).and_return(false)
16
+ Kernel.should_receive(:system).with('sunspot-start').and_return(true)
17
+ Sunspot::Rails::Server.start.should == true
18
+ end
19
+
20
+ it "should generate and execute the stop command" do
21
+ Sunspot::Rails::Server.should_receive(:stop_command).and_return('sunspot-stop')
22
+ Sunspot::Rails::Server.should_not_receive(:bootstrap_neccessary?)
23
+ Kernel.should_receive(:system).with('sunspot-stop').and_return(true)
24
+ Sunspot::Rails::Server.stop.should == true
25
+ end
26
+
27
+ it "should generate and execute the run command" do
28
+ Sunspot::Rails::Server.should_receive(:run_command).and_return('sunspot-run')
29
+ Sunspot::Rails::Server.should_respond_to_and_receive(:bootstrap_neccessary?).and_return(false)
30
+ Kernel.should_receive(:system).with('sunspot-run').and_return(true)
31
+ Sunspot::Rails::Server.run.should == true
32
+ end
33
+ end
34
+
35
+ describe "bootstraping" do
36
+ before(:each) do
37
+ @temp_dir = File.join( Dir.tmpdir, 'solr_rspec', Time.now.to_i.to_s, rand(1000).to_s )
38
+ Sunspot::Rails::Server.should_receive(:solr_home).at_least(1).and_return( @temp_dir )
39
+ end
40
+
41
+ it "should require bootstraping" do
42
+ Sunspot::Rails::Server.bootstrap_neccessary?.should == true
43
+ end
44
+
45
+ it "should not require bootstrapping again" do
46
+ Sunspot::Rails::Server.bootstrap_neccessary?.should == true
47
+ Sunspot::Rails::Server.bootstrap
48
+ Sunspot::Rails::Server.bootstrap_neccessary?.should == false
49
+ end
50
+ end
51
+
52
+ describe "delegate methods" do
53
+ before(:each) do
54
+ Sunspot::Rails::Server.should_receive(:configuration).at_least(1).and_return(@sunspot_configuration)
55
+ end
56
+
57
+ it "should delegate the port method to the configuration" do
58
+ @sunspot_configuration.should_respond_to_and_receive(:port).and_return(1234)
59
+ Sunspot::Rails::Server.port.should == 1234
60
+ end
61
+
62
+ it "should delegate the solr_home method to the configuration" do
63
+ @sunspot_configuration.should_respond_to_and_receive(:solr_home).and_return('/some/path')
64
+ Sunspot::Rails::Server.solr_home.should == '/some/path'
65
+ end
66
+
67
+ it "should delegate the log_level method to the configuration" do
68
+ @sunspot_configuration.should_respond_to_and_receive(:log_level).and_return('LOG_LEVEL')
69
+ Sunspot::Rails::Server.log_level.should == 'LOG_LEVEL'
70
+ end
71
+
72
+ it "should delegate the log_dir method to the configuration" do
73
+ @sunspot_configuration.should_respond_to_and_receive(:log_file).and_return('log_file')
74
+ Sunspot::Rails::Server.log_file.should =~ /log_file/
75
+ end
76
+
77
+ end
78
+
79
+ describe "protected methods" do
80
+ it "should generate the start command" do
81
+ Sunspot::Rails::Server.should_respond_to_and_receive(:port).and_return('1')
82
+ Sunspot::Rails::Server.should_respond_to_and_receive(:solr_home).and_return('home')
83
+ Sunspot::Rails::Server.should_respond_to_and_receive(:data_path).and_return('data')
84
+ Sunspot::Rails::Server.should_respond_to_and_receive(:log_level).and_return('LOG')
85
+ Sunspot::Rails::Server.should_respond_to_and_receive(:log_file).and_return('log_file')
86
+ Sunspot::Rails::Server.send(:start_command).should == \
87
+ [ 'sunspot-solr', 'start', '-p', '1', '-d', 'data', '-s', 'home', '-l', 'LOG', '--log-file', 'log_file' ]
88
+ end
89
+
90
+ it "should generate the stop command" do
91
+ Sunspot::Rails::Server.send(:stop_command).should == [ 'sunspot-solr', 'stop' ]
92
+ end
93
+
94
+ it "should generate the run command" do
95
+ Sunspot::Rails::Server.should_respond_to_and_receive(:port).and_return('1')
96
+ Sunspot::Rails::Server.should_respond_to_and_receive(:solr_home).and_return('home')
97
+ Sunspot::Rails::Server.should_respond_to_and_receive(:data_path).and_return('data')
98
+ Sunspot::Rails::Server.should_respond_to_and_receive(:log_level).and_return('LOG')
99
+ Sunspot::Rails::Server.should_respond_to_and_receive(:log_file).and_return('log_file')
100
+ Sunspot::Rails::Server.send(:run_command).should == \
101
+ [ 'sunspot-solr', 'run', '-p', '1', '-d', 'data', '-s', 'home', '-l', 'LOG', '-lf', 'log_file' ]
102
+ end
103
+
104
+ it "should generate the path for config files" do
105
+ Sunspot::Rails::Server.should_receive(:solr_home).and_return('/solr/home')
106
+ Sunspot::Rails::Server.config_path.should == '/solr/home/conf'
107
+ end
108
+
109
+ it "should generate the path for custom libraries" do
110
+ Sunspot::Rails::Server.should_receive(:solr_home).and_return('/solr/home')
111
+ Sunspot::Rails::Server.lib_path.should == '/solr/home/lib'
112
+ end
113
+
114
+ it "should generate the path for the index data" do
115
+ Sunspot::Rails::Server.should_receive(:solr_home).and_return('/solr/home')
116
+ Sunspot::Rails::Server.data_path.should == '/solr/home/data/test'
117
+ end
118
+
119
+ it "should generate the path for pid files" do
120
+ Sunspot::Rails::Server.should_receive(:solr_home).and_return('/solr/home')
121
+ Sunspot::Rails::Server.pid_path.should == '/solr/home/pids/test'
122
+ end
123
+ end
124
+ end