Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 360aad70788dd6474d0f343fa9c84e339b3538bd @justinko committed May 11, 2011
Showing with 308 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +1 −0 .rvmrc
  3. +3 −0 Gemfile
  4. +20 −0 LICENSE
  5. +63 −0 README.md
  6. +7 −0 Rakefile
  7. +2 −0 lib/sunspot-rails-tester.rb
  8. +70 −0 lib/sunspot/rails/tester.rb
  9. +9 −0 spec/spec_helper.rb
  10. +106 −0 spec/sunspot/rails/tester_spec.rb
  11. +23 −0 sunspot-rails-tester.gemspec
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
1 .rvmrc
@@ -0,0 +1 @@
+rvm use 1.9.2@sunspot-rails-tester --create
@@ -0,0 +1,3 @@
+source :rubygems
+
+gemspec
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Justin Ko
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,63 @@
+# sunspot-rails-tester
+
+This gem allows you to "turn on" solr for certain portions
+of your tests. For the code that does not use solr, you
+would want to "stub" sunspot to avoid unneeded indexing.
+
+Here is an example RSpec 2 spec_helper.rb:
+
+ $original_sunspot_session = Sunspot.session
+
+ RSpec.configure do |config|
+ config.mock_with :rspec
+
+ config.before do
+ Sunspot.session = Sunspot::Rails::StubSessionProxy.new($original_sunspot_session)
+ end
+
+ config.before :solr => true do
+ Sunspot::Rails::Tester.fork_original_sunspot_session
+ Sunspot.session = $original_sunspot_session
+ Sunspot.remove_all!
+ end
+ end
+
+Let's go through what the above code does.
+
+* `$original_sunspot_session` stores the _original_ sunspot
+ session. By default, sunspot_rails uses the `SessionProxy::ThreadLocalSessionProxy`.
+
+* In the first `before` block, we set the session to a stub session for
+ _every_ example. `Sunspot::Rails::StubSessionProxy` is just a dummy class
+ that skips indexing.
+
+* In the second `before` block, we use RSpec 2's metadata feature by
+ adding `:solr => true`. Any example or example group with this metadata
+ will run the _original_ sunspot session.
+ `Sunspot::Rails::Tester.fork_original_sunspot_session` starts the solr instance
+ if it's not running.
+
+Here is an example spec that utilizes sunspot-rails-tester:
+
+ require 'spec_helper'
+
+ describe 'search page' do
+ it 'highlights the active tab in the navigation' do
+ # uses the stub session
+ end
+
+ it 'finds and displays a person', :solr => true do
+ # uses actual solr - indexing will happen
+ end
+ end
+
+## Thanks
+
+The following articles served as guidance and inspiration for this gem:
+
+* [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/)
+* [http://opensoul.org/blog/archives/2010/04/07/cucumber-and-sunspot/](http://opensoul.org/blog/archives/2010/04/07/cucumber-and-sunspot/)
+
+## Copyright
+
+Copyright (c) 2011 Justin Ko. See LICENSE for details.
@@ -0,0 +1,7 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
+
+task :default => :spec
@@ -0,0 +1,2 @@
+require 'sunspot/rails'
+require 'sunspot/rails/tester'
@@ -0,0 +1,70 @@
+require 'net/http'
+require 'forwardable'
+
+module Sunspot
+ module Rails
+ class Tester
+ VERSION = '0.0.1'
+
+ class << self
+ extend Forwardable
+
+ attr_accessor :server, :started, :pid
+
+ def fork_original_sunspot_session
+ unless started?
+ self.server = Sunspot::Rails::Server.new
+ self.started = Time.now
+ self.pid = fork do
+ $stderr.reopen('/dev/null')
+ $stdout.reopen('/dev/null')
+ sunspot.run
+ end
+ kill_at_exit
+ give_feedback
+ end
+ end
+
+ def started?
+ not server.nil?
+ end
+
+ def kill_at_exit
+ at_exit { Process.kill('TERM', pid) }
+ end
+
+ def give_feedback
+ puts 'Sunspot server is starting...' while starting
+ puts "Sunspot server took #{seconds} seconds to start"
+ end
+
+ def starting
+ sleep(1)
+ Net::HTTP.get_response(URI.parse(uri))
+ false
+ rescue Errno::ECONNREFUSED
+ true
+ end
+
+ def seconds
+ '%.2f' % (Time.now - started)
+ end
+
+ def uri
+ "http://#{hostname}:#{port}#{path}"
+ end
+
+ def_delegators :configuration, :hostname, :port, :path
+
+ def configuration
+ server.send(:configuration)
+ end
+
+ def clear
+ self.server = nil
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,9 @@
+require 'rubygems'
+require 'bundler'
+Bundler.setup
+
+require 'sunspot-rails-tester'
+
+RSpec.configure do |config|
+ config.color_enabled = true
+end
@@ -0,0 +1,106 @@
+require 'spec_helper'
+
+module Sunspot
+ module Rails
+ describe Tester do
+ let(:tester) { described_class }
+
+ describe '.fork_original_sunspot_session' do
+ let(:server) { double('sunspot_rails_server') }
+ let(:pid) { 5555 }
+
+ before do
+ Sunspot::Rails::Server.should_receive(:new).and_return(server)
+ tester.should_receive(:fork).and_return(pid)
+ tester.should_receive(:kill_at_exit)
+ tester.should_receive(:give_feedback)
+ end
+
+ after { tester.clear }
+
+ it 'sets the "server" attribute' do
+ tester.fork_original_sunspot_session
+ tester.server.should eq(server)
+ end
+
+ it 'sets the "started" attribute' do
+ tester.fork_original_sunspot_session
+ tester.started.should be_an_instance_of(Time)
+ end
+
+ it 'sets the "pid" attribute' do
+ tester.fork_original_sunspot_session
+ tester.pid.should eq(pid)
+ end
+ end
+
+ describe '.started?' do
+ context 'given the "server" attribute is nil' do
+ specify { tester.should_not be_started }
+ end
+
+ context 'given the "server" attribute is not nil' do
+ before { tester.server = :not_nil }
+ specify { tester.should be_started }
+ end
+ end
+
+ describe '.starting' do
+ let(:uri) { double('uri') }
+
+ before do
+ tester.should_receive(:sleep)
+ tester.should_receive(:uri).and_return(uri)
+ URI.should_receive(:parse).with(uri)
+ end
+
+ context 'given the "uri" is available' do
+ it 'returns false' do
+ Net::HTTP.should_receive(:get_response)
+ tester.starting.should be_false
+ end
+ end
+
+ context 'given the "uri" is not available' do
+ it 'returns true' do
+ Net::HTTP.should_receive(:get_response).and_raise(Errno::ECONNREFUSED)
+ tester.starting.should be_true
+ end
+ end
+ end
+
+ describe '.seconds' do
+ context 'given the "started" attribute is set to 5 seconds ago' do
+ before { tester.started = Time.now - 5 }
+ specify { tester.seconds.should eq('5.00') }
+ end
+ end
+
+ describe '.uri' do
+ context 'given hostname|port|path is set to: localhost|5555|/solr' do
+ let(:configuration) do
+ double 'configuration', :hostname => 'localhost',
+ :port => 5555,
+ :path => '/solr'
+ end
+
+ before { tester.stub(:configuration).and_return(configuration) }
+ specify { tester.uri.should eq('http://localhost:5555/solr') }
+ end
+ end
+
+ describe '.clear' do
+ context 'given the "server" attribute is not nil' do
+ before { tester.server = :not_nil }
+
+ it 'sets it to nil' do
+ tester.server.should eq(:not_nil)
+ tester.clear
+ tester.server.should be_nil
+ end
+ end
+ end
+
+ end
+ end
+end
@@ -0,0 +1,23 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path('../lib', __FILE__)
+require 'sunspot/rails/tester'
+
+Gem::Specification.new do |s|
+ s.name = 'sunspot-rails-tester'
+ s.version = Sunspot::Rails::Tester::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.author = 'Justin Ko'
+ s.email = 'jko170@gmail.com'
+ s.homepage = ""
+ s.summary = 'Stub sunspot when you want, and enable it when you want'
+ s.description = 'Enable sunspot during testing for *real* integration tests'
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_path = 'lib'
+
+ s.add_dependency 'sunspot_rails', '~> 1.2.1'
+
+ s.add_development_dependency 'rspec', '~> 2.5'
+end

0 comments on commit 360aad7

Please sign in to comment.