Skip to content

Commit

Permalink
have(n).documents works
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed Aug 8, 2012
1 parent f42ab6b commit 4d7981b
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/rspec-solr.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "rspec-solr/matchers"
require "rspec-solr/have_docs_matcher"
require "rspec-solr/num_docs_matcher"

module RSpecSolr::Matchers

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ def have_documents
# the actual method is defined using RSpec's matcher DSL
end

# Determine if the receiver (a Solr response object) has at least
# one document
# Define .have_documents
# Determine if the receiver (a Solr response object) has at least one document
rspec_namespace.define :have_documents do |document_matcher|
match do |solr_resp|
if !document_matcher
solr_resp["response"]["docs"].size > 0
else
# TODO: test this
solr_resp.documents.any? { |doc| document_matcher.include? doc }
end
end
Expand All @@ -36,6 +37,7 @@ def have_documents
if !document_matcher
"expected documents in Solr response #{solr_resp["response"]}"
else
# TODO: test this
"expected documents #{document_matcher.to_s} in #{solr_resp["response"]["docs"]}"
end

Expand All @@ -45,11 +47,14 @@ def have_documents
if !document_matcher
"did not expect documents, but Solr response had #{solr_resp["response"]["docs"]}"
else
# TODO: test this
"did not expect #{document_matcher.to_s} to be in #{solr_resp["response"]["docs"]}"
end
end

end
end # define :have_documents

# TODO: does below work?

def document finders = {}
DocumentFinder.new finders
Expand Down
55 changes: 55 additions & 0 deletions lib/rspec-solr/num_docs_matcher.rb
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
133 changes: 133 additions & 0 deletions spec/number_of_documents_spec.rb
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
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@ def format(result)

$LOAD_PATH.unshift(File.dirname(__FILE__))

Dir['./spec/support/**/*'].each {|f| require f}

#RSpec.configure do |config|
#end
22 changes: 22 additions & 0 deletions spec/support/matchers.rb
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

0 comments on commit 4d7981b

Please sign in to comment.