-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
222 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
begin | ||
require 'rspec-expectations' | ||
rescue LoadError | ||
end | ||
|
||
# Custom RSpec Matchers for Solr responses | ||
module RSpecSolr::Matchers | ||
|
||
if defined?(::RSpec) | ||
rspec_namespace = ::RSpec::Matchers | ||
elsif defined?(::Spec) | ||
rspec_namespace = ::Spec::Matchers | ||
else | ||
raise NameError, "Cannot find Spec (rspec 1.x) or RSpec (rspec 2.x)" | ||
end | ||
|
||
# Define .have(n).documents | ||
# Determine if the receiver (a Solr response object) has the expected number of documents | ||
rspec_namespace.define :have do |num_docs| | ||
@num_expected = case num_docs | ||
when :no then 0 | ||
when String then num_docs.to_i | ||
else num_docs | ||
end | ||
|
||
match do |solr_resp| | ||
@num_actual = solr_resp["response"]["docs"].size | ||
@num_actual == @num_expected | ||
end | ||
|
||
chain :documents do | ||
end | ||
|
||
chain :document do | ||
end | ||
|
||
failure_message_for_should do |solr_resp| | ||
if @num_expected == 1 | ||
"expected #{@num_expected} document; got #{@num_actual}" | ||
else | ||
"expected #{@num_expected} documents; got #{@num_actual}" | ||
end | ||
end | ||
|
||
failure_message_for_should_not do |solr_resp| | ||
if @num_expected == 1 | ||
"expected not to have #{@num_expected} document; got #{@num_actual}" | ||
else | ||
"expected not to have #{@num_expected} documents; got #{@num_actual}" | ||
end | ||
end | ||
|
||
end # define :have(n).documents | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
require 'rspec-solr' | ||
|
||
describe RSpecSolr do | ||
|
||
context "should have(n)_documents" do | ||
|
||
it "pluralizes 'documents'" do | ||
@solr_resp_1_doc.should have(1).document | ||
end | ||
|
||
it "passes if target response has n documents" do | ||
@solr_resp_5_docs.should have(5).documents | ||
@solr_resp_no_docs.should have(0).documents | ||
end | ||
|
||
it "converts :no to 0" do | ||
@solr_resp_no_docs.should have(:no).documents | ||
end | ||
|
||
it "converts a String argument to Integer" do | ||
@solr_resp_5_docs.should have('5').documents | ||
end | ||
|
||
it "fails if target response has < n documents" do | ||
lambda { | ||
@solr_resp_5_docs.should have(6).documents | ||
}.should fail_with("expected 6 documents; got 5") | ||
lambda { | ||
@solr_resp_no_docs.should have(1).document | ||
}.should fail_with("expected 1 document; got 0") | ||
end | ||
|
||
it "fails if target response has > n documents" do | ||
lambda { | ||
@solr_resp_5_docs.should have(4).documents | ||
}.should fail_with("expected 4 documents; got 5") | ||
lambda { | ||
@solr_resp_1_doc.should have(0).documents | ||
}.should fail_with("expected 0 documents; got 1") | ||
end | ||
|
||
end | ||
|
||
context "should_not have(n).documents" do | ||
it "passes if target response has < n documents" do | ||
@solr_resp_5_docs.should_not have(6).documents | ||
@solr_resp_1_doc.should_not have(2).documents | ||
@solr_resp_no_docs.should_not have(1).document | ||
end | ||
|
||
it "passes if target response has > n documents" do | ||
@solr_resp_5_docs.should_not have(4).documents | ||
@solr_resp_1_doc.should_not have(0).documents | ||
@solr_resp_no_docs.should_not have(-1).documents | ||
end | ||
|
||
it "fails if target response has n documents" do | ||
lambda { | ||
@solr_resp_5_docs.should_not have(5).documents | ||
}.should fail_with("expected not to have 5 documents; got 5") | ||
end | ||
end | ||
|
||
# context "should have_exactly(n).documents" do | ||
# it "passes if target response has n documents" do | ||
# @solr_resp_5_docs.should have_exactly(5).documents | ||
# @solr_resp_no_docs.should have_exactly(0).documents | ||
# end | ||
# it "converts :no to 0" do | ||
# @solr_resp_no_docs.should have_exactly(:no).documents | ||
# end | ||
# it "fails if target response has < n documents" do | ||
# lambda { | ||
# @solr_resp_5_docs.should have_exacty(6).documents | ||
# }.should fail_with("expected 6 documents, got 5") | ||
# lambda { | ||
# @solr_resp_no_docs.should have_exactly(1).document | ||
# }.should fail_with("expected 1 document, got 0") | ||
# end | ||
# | ||
# it "fails if target response has > n documents" do | ||
# lambda { | ||
# @solr_resp_5_docs.should have_exactly(4).documents | ||
# }.should fail_with("expected 4 documents, got 5") | ||
# lambda { | ||
# @solr_resp_1_doc.should have_exactly(0).documents | ||
# }.should fail_with("expected 0 documents, got 1") | ||
# end | ||
# end | ||
# | ||
|
||
|
||
# TODO: check for should_not failure messages | ||
# | ||
# it "failure message for should_not" do | ||
# expect {@solr_resp_w_docs.should_not have_documents}. | ||
# to raise_error(RSpec::Expectations::ExpectationNotMetError, /FIXME did not expect documents, but Solr response had /) | ||
# end | ||
# | ||
|
||
|
||
before(:all) do | ||
@solr_resp_1_doc = { "response" => | ||
{ "numFound" => 5, | ||
"start" => 0, | ||
"docs" => [ {"id"=>"111"} ] | ||
} | ||
} | ||
|
||
@solr_resp_5_docs = { "response" => | ||
{ "numFound" => 5, | ||
"start" => 0, | ||
"docs" => | ||
[ {"id"=>"111"}, | ||
{"id"=>"222"}, | ||
{"id"=>"333"}, | ||
{"id"=>"444"}, | ||
{"id"=>"555"} | ||
] | ||
} | ||
} | ||
|
||
@solr_resp_no_docs = { "response" => | ||
{ "numFound" => 0, | ||
"start" => 0, | ||
"docs" => [] | ||
} | ||
} | ||
end | ||
|
||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# This file shamelessly copied from rspec-expecations/spec/support/matchers.rb | ||
RSpec::Matchers.define :include_method do |expected| | ||
match do |actual| | ||
actual.map { |m| m.to_s }.include?(expected.to_s) | ||
end | ||
end | ||
|
||
module RSpec | ||
module Matchers | ||
def fail | ||
raise_error(RSpec::Expectations::ExpectationNotMetError) | ||
end | ||
|
||
def fail_with(message) | ||
raise_error(RSpec::Expectations::ExpectationNotMetError, message) | ||
end | ||
|
||
def fail_matching(message) | ||
raise_error(RSpec::Expectations::ExpectationNotMetError, /#{Regexp.escape(message)}/) | ||
end | ||
end | ||
end |