sunspot-rails-tester 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@sunspot-rails-tester --create
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Justin Ko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # sunspot-rails-tester
2
+
3
+ This gem allows you to "turn on" solr for certain portions
4
+ of your tests. For the code that does not use solr, you
5
+ would want to "stub" sunspot to avoid unneeded indexing.
6
+
7
+ Here is an example RSpec 2 spec_helper.rb:
8
+
9
+ $original_sunspot_session = Sunspot.session
10
+
11
+ RSpec.configure do |config|
12
+ config.mock_with :rspec
13
+
14
+ config.before do
15
+ Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session)
16
+ end
17
+
18
+ config.before :solr => true do
19
+ Sunspot::Rails::Tester.fork_original_sunspot_session
20
+ Sunspot.session = $original_sunspot_session
21
+ Sunspot.remove_all!
22
+ end
23
+ end
24
+
25
+ Let's go through what the above code does.
26
+
27
+ * `$original_sunspot_session` stores the _original_ sunspot
28
+ session. By default, sunspot_rails uses the `SessionProxy::ThreadLocalSessionProxy`.
29
+
30
+ * In the first `before` block, we set the session to a stub session for
31
+ _every_ example. `Sunspot::Rails::StubSessionProxy` is just a dummy class
32
+ that skips indexing.
33
+
34
+ * In the second `before` block, we use RSpec 2's metadata feature by
35
+ adding `:solr => true`. Any example or example group with this metadata
36
+ will run the _original_ sunspot session.
37
+ `Sunspot::Rails::Tester.fork_original_sunspot_session` starts the solr instance
38
+ if it's not running.
39
+
40
+ Here is an example spec that utilizes sunspot-rails-tester:
41
+
42
+ require 'spec_helper'
43
+
44
+ describe 'search page' do
45
+ it 'highlights the active tab in the navigation' do
46
+ # uses the stub session
47
+ end
48
+
49
+ it 'finds and displays a person', :solr => true do
50
+ # uses actual solr - indexing will happen
51
+ end
52
+ end
53
+
54
+ ## Thanks
55
+
56
+ The following articles served as guidance and inspiration for this gem:
57
+
58
+ * [http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/](http://blog.kabisa.nl/2010/02/03/running-cucumber-features-with-sunspot_rails/)
59
+ * [http://opensoul.org/blog/archives/2010/04/07/cucumber-and-sunspot/](http://opensoul.org/blog/archives/2010/04/07/cucumber-and-sunspot/)
60
+
61
+ ## Copyright
62
+
63
+ Copyright (c) 2011 Justin Ko. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,70 @@
1
+ require 'net/http'
2
+ require 'forwardable'
3
+
4
+ module Sunspot
5
+ module Rails
6
+ class Tester
7
+ VERSION = '0.0.1'
8
+
9
+ class << self
10
+ extend Forwardable
11
+
12
+ attr_accessor :server, :started, :pid
13
+
14
+ def fork_original_sunspot_session
15
+ unless started?
16
+ self.server = Sunspot::Rails::Server.new
17
+ self.started = Time.now
18
+ self.pid = fork do
19
+ $stderr.reopen('/dev/null')
20
+ $stdout.reopen('/dev/null')
21
+ sunspot.run
22
+ end
23
+ kill_at_exit
24
+ give_feedback
25
+ end
26
+ end
27
+
28
+ def started?
29
+ not server.nil?
30
+ end
31
+
32
+ def kill_at_exit
33
+ at_exit { Process.kill('TERM', pid) }
34
+ end
35
+
36
+ def give_feedback
37
+ puts 'Sunspot server is starting...' while starting
38
+ puts "Sunspot server took #{seconds} seconds to start"
39
+ end
40
+
41
+ def starting
42
+ sleep(1)
43
+ Net::HTTP.get_response(URI.parse(uri))
44
+ false
45
+ rescue Errno::ECONNREFUSED
46
+ true
47
+ end
48
+
49
+ def seconds
50
+ '%.2f' % (Time.now - started)
51
+ end
52
+
53
+ def uri
54
+ "http://#{hostname}:#{port}#{path}"
55
+ end
56
+
57
+ def_delegators :configuration, :hostname, :port, :path
58
+
59
+ def configuration
60
+ server.send(:configuration)
61
+ end
62
+
63
+ def clear
64
+ self.server = nil
65
+ end
66
+ end
67
+
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,2 @@
1
+ require 'sunspot/rails'
2
+ require 'sunspot/rails/tester'
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+
5
+ require 'sunspot-rails-tester'
6
+
7
+ RSpec.configure do |config|
8
+ config.color_enabled = true
9
+ end
@@ -0,0 +1,106 @@
1
+ require 'spec_helper'
2
+
3
+ module Sunspot
4
+ module Rails
5
+ describe Tester do
6
+ let(:tester) { described_class }
7
+
8
+ describe '.fork_original_sunspot_session' do
9
+ let(:server) { double('sunspot_rails_server') }
10
+ let(:pid) { 5555 }
11
+
12
+ before do
13
+ Sunspot::Rails::Server.should_receive(:new).and_return(server)
14
+ tester.should_receive(:fork).and_return(pid)
15
+ tester.should_receive(:kill_at_exit)
16
+ tester.should_receive(:give_feedback)
17
+ end
18
+
19
+ after { tester.clear }
20
+
21
+ it 'sets the "server" attribute' do
22
+ tester.fork_original_sunspot_session
23
+ tester.server.should eq(server)
24
+ end
25
+
26
+ it 'sets the "started" attribute' do
27
+ tester.fork_original_sunspot_session
28
+ tester.started.should be_an_instance_of(Time)
29
+ end
30
+
31
+ it 'sets the "pid" attribute' do
32
+ tester.fork_original_sunspot_session
33
+ tester.pid.should eq(pid)
34
+ end
35
+ end
36
+
37
+ describe '.started?' do
38
+ context 'given the "server" attribute is nil' do
39
+ specify { tester.should_not be_started }
40
+ end
41
+
42
+ context 'given the "server" attribute is not nil' do
43
+ before { tester.server = :not_nil }
44
+ specify { tester.should be_started }
45
+ end
46
+ end
47
+
48
+ describe '.starting' do
49
+ let(:uri) { double('uri') }
50
+
51
+ before do
52
+ tester.should_receive(:sleep)
53
+ tester.should_receive(:uri).and_return(uri)
54
+ URI.should_receive(:parse).with(uri)
55
+ end
56
+
57
+ context 'given the "uri" is available' do
58
+ it 'returns false' do
59
+ Net::HTTP.should_receive(:get_response)
60
+ tester.starting.should be_false
61
+ end
62
+ end
63
+
64
+ context 'given the "uri" is not available' do
65
+ it 'returns true' do
66
+ Net::HTTP.should_receive(:get_response).and_raise(Errno::ECONNREFUSED)
67
+ tester.starting.should be_true
68
+ end
69
+ end
70
+ end
71
+
72
+ describe '.seconds' do
73
+ context 'given the "started" attribute is set to 5 seconds ago' do
74
+ before { tester.started = Time.now - 5 }
75
+ specify { tester.seconds.should eq('5.00') }
76
+ end
77
+ end
78
+
79
+ describe '.uri' do
80
+ context 'given hostname|port|path is set to: localhost|5555|/solr' do
81
+ let(:configuration) do
82
+ double 'configuration', :hostname => 'localhost',
83
+ :port => 5555,
84
+ :path => '/solr'
85
+ end
86
+
87
+ before { tester.stub(:configuration).and_return(configuration) }
88
+ specify { tester.uri.should eq('http://localhost:5555/solr') }
89
+ end
90
+ end
91
+
92
+ describe '.clear' do
93
+ context 'given the "server" attribute is not nil' do
94
+ before { tester.server = :not_nil }
95
+
96
+ it 'sets it to nil' do
97
+ tester.server.should eq(:not_nil)
98
+ tester.clear
99
+ tester.server.should be_nil
100
+ end
101
+ end
102
+ end
103
+
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'sunspot/rails/tester'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'sunspot-rails-tester'
7
+ s.version = Sunspot::Rails::Tester::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.author = 'Justin Ko'
10
+ s.email = 'jko170@gmail.com'
11
+ s.homepage = 'https://github.com/justinko/sunspot-rails-tester'
12
+ s.summary = 'Stub sunspot when you want, and enable it when you want'
13
+ s.description = 'Enable sunspot during testing for *real* integration tests'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_path = 'lib'
19
+
20
+ s.add_dependency 'sunspot_rails', '~> 1.2.1'
21
+
22
+ s.add_development_dependency 'rspec', '~> 2.5'
23
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sunspot-rails-tester
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Justin Ko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-11 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: sunspot_rails
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 1.2.1
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: "2.5"
36
+ type: :development
37
+ version_requirements: *id002
38
+ description: Enable sunspot during testing for *real* integration tests
39
+ email: jko170@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - .rvmrc
49
+ - Gemfile
50
+ - LICENSE
51
+ - README.md
52
+ - Rakefile
53
+ - lib/sunspot-rails-tester.rb
54
+ - lib/sunspot/rails/tester.rb
55
+ - spec/spec_helper.rb
56
+ - spec/sunspot/rails/tester_spec.rb
57
+ - sunspot-rails-tester.gemspec
58
+ has_rdoc: true
59
+ homepage: https://github.com/justinko/sunspot-rails-tester
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.6.2
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Stub sunspot when you want, and enable it when you want
86
+ test_files:
87
+ - spec/spec_helper.rb
88
+ - spec/sunspot/rails/tester_spec.rb