sunspot_test 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem "jeweler"
5
+ gem "rspec"
6
+ end
data/HISTORY CHANGED
@@ -1,3 +1,6 @@
1
+ 0.3.0 - June 11, 2011
2
+ * Renaming SunspotTestHelper to SunspotTest (thanks ersatzryan)
3
+ * Enabling testing within rspec specs
1
4
  0.2.0 - May 26, 2011
2
5
  * Moved requires from "sunspot_test" to "sunspot_test/cucumber" so we didn't conflict with bundler's autorequire.
3
6
  * Added non-@search functionality which stubs out the session and consequently speeds up the non-searching tests.
data/README.rdoc CHANGED
@@ -31,12 +31,30 @@ If you feature is not tagged with @search the environment will use a sunspot tes
31
31
 
32
32
  Starting solr will depend on your settings in <b>config/sunspot.yml</b> (though this configuration file is optional). If you run into issues remember to look for existing java processes, starting solr may conflict with existing instances. You can also check out http://collectiveidea.com/blog/archives/2011/05/25/testing-with-sunspot-and-cucumber/ which contains a little more information.
33
33
 
34
+ == Can it do RSpec?
35
+
36
+ In spec_helper.rb
37
+
38
+ require 'sunspot_test/rspec'
39
+
40
+ Then within your specs, you'll need to tag and explicitly commit changes to solr:
41
+
42
+ require 'spec_helper'
43
+
44
+ describe "searching", :search => true do
45
+ it 'returns book results' do
46
+ book = Factory(:book, :title => "Of Mice and Men")
47
+ Sunspot.commit
48
+ Book.search { keywords "mice"}.results.should == [book]
49
+ end
50
+ end
51
+
34
52
  == Configuring the timeout
35
53
 
36
54
  The test suite will try and launch a solr process and wait for 15 seconds for the process to launch. You can configure this timeout by setting the following in env.rb:
37
55
 
38
56
 
39
- SunspotTestHelper.startup_timeout = 60 # will wait 60 seconds for the solr process to start
57
+ SunspotTest.startup_timeout = 60 # will wait 60 seconds for the solr process to start
40
58
 
41
59
 
42
60
  == Note on Patches/Pull Requests
data/Rakefile CHANGED
@@ -11,7 +11,6 @@ begin
11
11
  gem.homepage = "https://github.com/collectiveidea/sunspot_test"
12
12
  gem.authors = ["Zach Moazeni"]
13
13
  gem.add_dependency "sunspot_rails", ">= 1.2.1"
14
- gem.add_development_dependency "rspec", ">= 1.2.9"
15
14
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
15
  end
17
16
  Jeweler::GemcutterTasks.new
@@ -19,39 +18,39 @@ rescue LoadError
19
18
  puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
19
  end
21
20
 
22
- require 'spec/rake/spectask'
23
- Spec::Rake::SpecTask.new(:spec) do |spec|
24
- spec.libs << 'lib' << 'spec'
25
- spec.spec_files = FileList['spec/**/*_spec.rb']
26
- end
27
-
28
- Spec::Rake::SpecTask.new(:rcov) do |spec|
29
- spec.libs << 'lib' << 'spec'
30
- spec.pattern = 'spec/**/*_spec.rb'
31
- spec.rcov = true
32
- end
33
-
34
- begin
35
- require 'cucumber/rake/task'
36
- Cucumber::Rake::Task.new(:features)
37
-
38
- task :features => :check_dependencies
39
- rescue LoadError
40
- task :features do
41
- abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
42
- end
43
- end
44
-
45
- task :spec => :check_dependencies
46
-
47
- task :default => :spec
48
-
49
- require 'rake/rdoctask'
50
- Rake::RDocTask.new do |rdoc|
51
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
52
-
53
- rdoc.rdoc_dir = 'rdoc'
54
- rdoc.title = "sunspot_test #{version}"
55
- rdoc.rdoc_files.include('README*')
56
- rdoc.rdoc_files.include('lib/**/*.rb')
57
- end
21
+ # require 'spec/rake/spectask'
22
+ # Spec::Rake::SpecTask.new(:spec) do |spec|
23
+ # spec.libs << 'lib' << 'spec'
24
+ # spec.spec_files = FileList['spec/**/*_spec.rb']
25
+ # end
26
+ #
27
+ # Spec::Rake::SpecTask.new(:rcov) do |spec|
28
+ # spec.libs << 'lib' << 'spec'
29
+ # spec.pattern = 'spec/**/*_spec.rb'
30
+ # spec.rcov = true
31
+ # end
32
+ #
33
+ # begin
34
+ # require 'cucumber/rake/task'
35
+ # Cucumber::Rake::Task.new(:features)
36
+ #
37
+ # task :features => :check_dependencies
38
+ # rescue LoadError
39
+ # task :features do
40
+ # abort "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
41
+ # end
42
+ # end
43
+ #
44
+ # task :spec => :check_dependencies
45
+ #
46
+ # task :default => :spec
47
+ #
48
+ # require 'rake/rdoctask'
49
+ # Rake::RDocTask.new do |rdoc|
50
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
51
+ #
52
+ # rdoc.rdoc_dir = 'rdoc'
53
+ # rdoc.title = "sunspot_test #{version}"
54
+ # rdoc.rdoc_files.include('README*')
55
+ # rdoc.rdoc_files.include('lib/**/*.rb')
56
+ # end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/lib/sunspot_test.rb CHANGED
@@ -0,0 +1,78 @@
1
+ require 'sunspot/rails'
2
+ require 'net/http'
3
+
4
+ module SunspotTest
5
+ class TimeOutError < StandardError; end;
6
+ class << self
7
+
8
+ attr_writer :solr_startup_timeout
9
+ attr_writer :server
10
+
11
+ def solr_startup_timeout
12
+ @solr_startup_timeout || 15
13
+ end
14
+
15
+ def setup_solr
16
+ unstub
17
+ start_sunspot_server
18
+ end
19
+
20
+ def server
21
+ @server ||= Sunspot::Rails::Server.new
22
+ end
23
+
24
+ def start_sunspot_server
25
+ unless @server
26
+ pid = fork do
27
+ STDERR.reopen("/dev/null")
28
+ STDOUT.reopen("/dev/null")
29
+ server.run
30
+ end
31
+
32
+ at_exit { Process.kill("TERM", pid) }
33
+
34
+ wait_until_solr_starts
35
+ end
36
+ end
37
+
38
+ # Stubs Sunspot calls to Solr server
39
+ def stub
40
+ unless @session_stubbed
41
+ Sunspot.session = Sunspot::Rails::StubSessionProxy.new(original_sunspot_session)
42
+ @session_stubbed = true
43
+ end
44
+ end
45
+
46
+ # Resets Sunspot to call Solr server, opposite of stub
47
+ def unstub
48
+ if @session_stubbed
49
+ Sunspot.session = original_sunspot_session
50
+ @session_stubbed = false
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def original_sunspot_session
57
+ @original_sunspot_session ||= Sunspot.session
58
+ end
59
+
60
+ def wait_until_solr_starts
61
+ (solr_startup_timeout * 10).times do
62
+ break if solr_running?
63
+ sleep(0.1)
64
+ end
65
+ raise TimeOutError, "Solr failed to start after #{solr_startup_timeout} seconds" unless solr_running?
66
+ end
67
+
68
+ def solr_running?
69
+ begin
70
+ solr_ping_uri = URI.parse("#{Sunspot.session.config.solr.url}/ping")
71
+ Net::HTTP.get(solr_ping_uri)
72
+ true # Solr Running
73
+ rescue
74
+ false # Solr Not Running
75
+ end
76
+ end
77
+ end
78
+ end
@@ -1,29 +1,15 @@
1
- require 'net/http'
2
-
3
- $original_sunspot_session = Sunspot.session
1
+ require 'sunspot_test'
4
2
 
5
3
  Before("@searchrunning") do
6
4
  $sunspot = true
7
5
  end
8
6
 
9
7
  Before("~@search") do
10
- Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session)
8
+ SunspotTest.stub
11
9
  end
12
10
 
13
11
  Before("@search") do
14
- unless $sunspot
15
- $sunspot = Sunspot::Rails::Server.new
16
- pid = fork do
17
- STDERR.reopen('/dev/null')
18
- STDOUT.reopen('/dev/null')
19
- $sunspot.run
20
- end
21
- # shut down the Solr server
22
- at_exit { Process.kill('TERM', pid) }
23
- SunspotTestHelper.wait_until_solr_starts
24
- end
25
-
26
- Sunspot.session = $original_sunspot_session
12
+ SunspotTest.setup_solr
27
13
  Sunspot.remove_all!
28
14
  Sunspot.commit
29
15
  end
@@ -31,24 +17,3 @@ end
31
17
  AfterStep('@search') do
32
18
  Sunspot.commit
33
19
  end
34
-
35
- module SunspotTestHelper
36
- @@startup_timeout = 15
37
- def self.startup_timeout; @@startup_timeout; end
38
- def self.startup_timeout=(seconds); @@startup_timeout = seconds; end
39
-
40
- def self.wait_until_solr_starts
41
- solr_started = false
42
- ping_uri = URI.parse("#{Sunspot.session.config.solr.url}/ping")
43
- (@@startup_timeout * 10).times do
44
- begin
45
- Net::HTTP.get(ping_uri)
46
- solr_started = true
47
- break
48
- rescue
49
- sleep(0.1)
50
- end
51
- end
52
- raise "Solr failed to start after 15 seconds" unless solr_started
53
- end
54
- end
@@ -0,0 +1,13 @@
1
+ require 'sunspot_test'
2
+
3
+ RSpec.configure do |c|
4
+ c.before(:all) do
5
+ SunspotTest.stub
6
+ end
7
+
8
+ c.before(:all, :search => true) do
9
+ SunspotTest.setup_solr
10
+ Sunspot.remove_all!
11
+ Sunspot.commit
12
+ end
13
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,5 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'sunspot_test'
4
- require 'spec'
5
- require 'spec/autorun'
6
3
 
7
- Spec::Runner.configure do |config|
8
-
9
- end
4
+ require 'sunspot'
5
+ require 'sunspot_test'
@@ -1,4 +1,160 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
- describe "SunspotTest" do
3
+ describe SunspotTest do
4
+ describe ".solr_startup_timeout" do
5
+ after(:all) { SunspotTest.solr_startup_timeout = 15 }
6
+ it "defaults to 15" do
7
+ SunspotTest.solr_startup_timeout.should eq(15)
8
+ end
9
+
10
+ it "can be set using .solr_startup_timeout=" do
11
+ SunspotTest.solr_startup_timeout = 20
12
+ SunspotTest.solr_startup_timeout.should eq(20)
13
+ end
14
+ end
15
+
16
+ describe "setup_solr" do
17
+ it "calls unstub and start_sunspot_server" do
18
+ SunspotTest.should_receive(:unstub)
19
+ SunspotTest.should_receive(:start_sunspot_server)
20
+ SunspotTest.setup_solr
21
+ end
22
+ end
23
+
24
+ describe "server" do
25
+ it "creates a new Sunspot Rails Server" do
26
+ Sunspot::Rails::Server.should_receive(:new)
27
+ SunspotTest.server
28
+ end
29
+ end
30
+
31
+ describe ".start_sunspot_server" do
32
+ context "if server is already started" do
33
+ before(:each) { SunspotTest.server = true }
34
+
35
+ it "does not try to spin up another server" do
36
+ Kernel.should_not_receive(:fork)
37
+ SunspotTest.start_sunspot_server
38
+ end
39
+ end
40
+
41
+ context "if the server has NOT been started" do
42
+ before(:all) do
43
+ SunspotTest.stub!(:wait_until_solr_starts) { true }
44
+ end
45
+
46
+ before(:each) do
47
+ SunspotTest.server = nil
48
+
49
+ fake_server = mock("server")
50
+ SunspotTest.stub!(:server) { fake_server }
51
+ end
52
+
53
+ # was getting funky issues when the test was broken up
54
+ it "forks the process, redirects stdout/stderr, runs server, sets exit hook, waits for server" do
55
+
56
+ SunspotTest.should_receive(:fork) do |&block|
57
+ STDERR.should_receive(:reopen).with("/dev/null")
58
+ STDOUT.should_receive(:reopen).with("/dev/null")
59
+ SunspotTest.server.should_receive(:run)
60
+ block.call
61
+ end
62
+
63
+ SunspotTest.should_receive(:at_exit) do |&block|
64
+ Process.should_receive(:kill)
65
+ block.call
66
+ end
67
+
68
+ SunspotTest.should_receive(:wait_until_solr_starts)
69
+ SunspotTest.start_sunspot_server
70
+ end
71
+ end
72
+
73
+ describe ".stub" do
74
+ context "if the session is already stubbed" do
75
+ before(:each) { SunspotTest.instance_variable_set(:@session_stubbed, true) }
76
+
77
+ it "does nothing" do
78
+ Sunspot::Rails::StubSessionProxy.should_not_receive(:new)
79
+
80
+ SunspotTest.stub
81
+ end
82
+ end
83
+
84
+ context "if the session is not currently stubbed" do
85
+ before(:each) { SunspotTest.instance_variable_set(:@session_stubbed, false) }
86
+
87
+ it "stubs the Sunspot session and sets @session_stubbed to true" do
88
+ Sunspot::Rails::StubSessionProxy.should_receive(:new)
89
+
90
+ SunspotTest.stub
91
+ end
92
+
93
+ it "sets @session_stubbed to true" do
94
+ SunspotTest.stub
95
+ SunspotTest.instance_variable_get(:@session_stubbed).should eq(true)
96
+ end
97
+ end
98
+ end
99
+
100
+ describe ".unstub" do
101
+ context "if the session is not stubbed" do
102
+ before(:each) { SunspotTest.instance_variable_set(:@session_stubbed, false) }
103
+ it "does nothing" do
104
+ SunspotTest.should_not_receive(:original_sunspot_session)
105
+ SunspotTest.unstub
106
+ end
107
+ end
108
+
109
+ context "if the session is currently stubbed" do
110
+ before(:each) { SunspotTest.instance_variable_set(:@session_stubbed, true) }
111
+
112
+ it "sets the Sunspot session to the original session" do
113
+ SunspotTest.should_receive(:original_sunspot_session)
114
+
115
+ SunspotTest.unstub
116
+ end
117
+
118
+ it "sets @session_stubbed to true" do
119
+ SunspotTest.unstub
120
+ SunspotTest.instance_variable_get(:@session_stubbed).should eq(false)
121
+ end
122
+ end
123
+ end
124
+
125
+ describe ".solr_running" do
126
+ context "if solr is running" do
127
+ before(:each) { Net::HTTP.stub!(:get).and_return(true) }
128
+
129
+ it "returns true" do
130
+ SunspotTest.send(:solr_running?).should eq(true)
131
+ end
132
+ end
133
+
134
+ context "if solr is not running" do
135
+ it "returns false" do
136
+ SunspotTest.send(:solr_running?).should eq(false)
137
+ end
138
+ end
139
+ end
140
+ describe ".wait_until_solr_starts" do
141
+ context "if solr never starts" do
142
+ before(:each) { SunspotTest.solr_startup_timeout = 2 }
143
+
144
+ it "calls solr_running? until solr_startup_timeout raises TimeOutError" do
145
+ SunspotTest.should_receive(:solr_running?).exactly((SunspotTest.solr_startup_timeout * 10) + 1).times
146
+
147
+ lambda { SunspotTest.send(:wait_until_solr_starts) }.should raise_error SunspotTest::TimeOutError, "Solr failed to start after #{SunspotTest.solr_startup_timeout} seconds"
148
+ end
149
+ end
150
+
151
+ context "if solr is started" do
152
+ it "calls solr_running? and returns" do
153
+ SunspotTest.should_receive(:solr_running?).exactly(2).times.and_return(true)
154
+
155
+ lambda { SunspotTest.send(:wait_until_solr_starts) }.should_not raise_error
156
+ end
157
+ end
158
+ end
159
+ end
4
160
  end
data/sunspot_test.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sunspot_test}
8
- s.version = "0.2.0"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Zach Moazeni"]
12
- s.date = %q{2011-05-26}
12
+ s.date = %q{2011-06-11}
13
13
  s.description = %q{Testing sunspot with cucumber can be a pain. This gem will automatically start/stop solr with cucumber scenarios tagged with @search}
14
14
  s.email = %q{zach@collectiveidea.com}
15
15
  s.extra_rdoc_files = [
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  ]
19
19
  s.files = [
20
20
  ".document",
21
+ "Gemfile",
21
22
  "HISTORY",
22
23
  "LICENSE",
23
24
  "README.rdoc",
@@ -26,7 +27,7 @@ Gem::Specification.new do |s|
26
27
  "features/support/env.rb",
27
28
  "lib/sunspot_test.rb",
28
29
  "lib/sunspot_test/cucumber.rb",
29
- "spec/spec.opts",
30
+ "lib/sunspot_test/rspec.rb",
30
31
  "spec/spec_helper.rb",
31
32
  "spec/sunspot_test_spec.rb",
32
33
  "sunspot_test.gemspec"
@@ -40,15 +41,18 @@ Gem::Specification.new do |s|
40
41
  s.specification_version = 3
41
42
 
42
43
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
44
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
45
+ s.add_development_dependency(%q<rspec>, [">= 0"])
43
46
  s.add_runtime_dependency(%q<sunspot_rails>, [">= 1.2.1"])
44
- s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
45
47
  else
48
+ s.add_dependency(%q<jeweler>, [">= 0"])
49
+ s.add_dependency(%q<rspec>, [">= 0"])
46
50
  s.add_dependency(%q<sunspot_rails>, [">= 1.2.1"])
47
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
48
51
  end
49
52
  else
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ s.add_dependency(%q<rspec>, [">= 0"])
50
55
  s.add_dependency(%q<sunspot_rails>, [">= 1.2.1"])
51
- s.add_dependency(%q<rspec>, [">= 1.2.9"])
52
56
  end
53
57
  end
54
58
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sunspot_test
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 0.2.0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Zach Moazeni
@@ -15,41 +15,53 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-26 00:00:00 -04:00
18
+ date: 2011-06-11 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: sunspot_rails
23
22
  prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
23
+ name: jeweler
24
+ type: :development
25
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
26
  none: false
26
27
  requirements:
27
28
  - - ">="
28
29
  - !ruby/object:Gem::Version
29
- hash: 29
30
+ hash: 3
30
31
  segments:
31
- - 1
32
- - 2
33
- - 1
34
- version: 1.2.1
35
- type: :runtime
36
- version_requirements: *id001
32
+ - 0
33
+ version: "0"
34
+ requirement: *id001
37
35
  - !ruby/object:Gem::Dependency
36
+ prerelease: false
38
37
  name: rspec
38
+ type: :development
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ hash: 3
45
+ segments:
46
+ - 0
47
+ version: "0"
48
+ requirement: *id002
49
+ - !ruby/object:Gem::Dependency
39
50
  prerelease: false
40
- requirement: &id002 !ruby/object:Gem::Requirement
51
+ name: sunspot_rails
52
+ type: :runtime
53
+ version_requirements: &id003 !ruby/object:Gem::Requirement
41
54
  none: false
42
55
  requirements:
43
56
  - - ">="
44
57
  - !ruby/object:Gem::Version
45
- hash: 13
58
+ hash: 29
46
59
  segments:
47
60
  - 1
48
61
  - 2
49
- - 9
50
- version: 1.2.9
51
- type: :development
52
- version_requirements: *id002
62
+ - 1
63
+ version: 1.2.1
64
+ requirement: *id003
53
65
  description: Testing sunspot with cucumber can be a pain. This gem will automatically start/stop solr with cucumber scenarios tagged with @search
54
66
  email: zach@collectiveidea.com
55
67
  executables: []
@@ -61,6 +73,7 @@ extra_rdoc_files:
61
73
  - README.rdoc
62
74
  files:
63
75
  - .document
76
+ - Gemfile
64
77
  - HISTORY
65
78
  - LICENSE
66
79
  - README.rdoc
@@ -69,7 +82,7 @@ files:
69
82
  - features/support/env.rb
70
83
  - lib/sunspot_test.rb
71
84
  - lib/sunspot_test/cucumber.rb
72
- - spec/spec.opts
85
+ - lib/sunspot_test/rspec.rb
73
86
  - spec/spec_helper.rb
74
87
  - spec/sunspot_test_spec.rb
75
88
  - sunspot_test.gemspec
data/spec/spec.opts DELETED
@@ -1 +0,0 @@
1
- --color