RSpec-Solr Matchers for RSpec-Solr::SolrResponseHash

(rspec-solr_resp_hash..should … or rspec-solr_resp_hash..should_not …)

MATCHING WHETHER RESPONSE HAS DOCUMENTS OR NOT

NOTE:  this is about the TOTAL number of Solr documents that match ("numFound"), NOT solely the documents returned in this response

Matcher

  • have_documents

Usage

  • rspec-solr_resp_hash.should have_documents

  • rspec-solr_resp_hash.should_not have_documents

MATCHING NUMBER OF DOCUMENTS

NOTE:  this is about the TOTAL number of Solr documents that match ("numFound"), NOT solely the documents returned in this response

Matchers

  • have(2).documents

  • have_exactly(4).documents

  • have_at_least(3).documents

  • have_at_most(3).documents

Usage

  • rspec-solr_resp_hash.should have(3).documents

  • rspec-solr_resp_hash.should_not have(2).documents

  • rspec-solr_resp_hash.should have_at_least(3).documents

  • rspec-solr_resp_hash.should have_at_most(4).documents

MATCHING SPECIFIC DOCUMENTS IN RESPONSE

NOTE:  this is about the Solr documents returned in THIS response

Matcher

  • include()

Usage

Specifying Single Document

String
  • (“idval”)

NOTE: value of the unique id field (defaults to 'id') in the Solr document
      To change the id field name, use my_solr_response_hash.id_field='my_id_fldname'
Hash
  • (“fldname” => “value”)

  • (“fldname” => /regex_to_match_val/)

  • (“fld1” => “val1”, “fld2” => /val2_regex/)

NOTE: single Solr document must satisfy all key value pairs
  • (“fldname” => [“val1”, /val2_regex/, “val3”])

NOTE: all of the Array values must be present for the fld in a single Solr document
      should_not for Array implies NONE of the values should be present in a single document

More Ideas (TODO):

  • include_title(“val”) (i.e. include_anyFieldName(“val”) )

Specifying Multiple Documents

Array
  • by id strings: ([“id1”, “id2”, “id3”])

  • by hashes: ([=> “green is best”, => /blue/, => “val”])

NOTE: you cannot do this:  ([{"title" => ["Solr doc 1 title", "Solr doc 2 title"]} ]) to specify multiple documents
  • by mix of id strings and hashes: ([=> “green is best”, “id3”, => “steinbeck”])

Full Examples

  • rspec-solr_resp_hash.should include(“fld1” => “val1”)

  • rspec-solr_resp_hash.should include(“fld1” => /regex_for_val/)

  • rspec-solr_resp_hash.should include(“f1” => “v1”, “f2” => [“val1”, “val2”, /regex_for_val/])

  • rspec-solr_resp_hash.should include(“idval”)

  • rspec-solr_resp_hash.should include([“id1”, “id2”, “id3”])

  • rspec-solr_resp_hash.should include([=> “title1”, => “title2”])

  • rspec-solr_resp_hash.should include([=> “title1”, => “title2”, “id8”])

MATCHING SPECIFIC DOCUMENTS OCCURRING IN FIRST N RESULTS

NOTE:  this is about the Solr documents returned in THIS response

Matchers

  • include().as_first

  • include().as_first.document

  • include().in_first(n)

  • include(Array).in_first(n).results

  • include(Hash).in_each_of_first(n).documents

Note that the following are equivalent:

  • include().blah.document

  • include().blah.documents

  • include().blah.result

  • include().blah.results

TODO:

  • include_at_least(3).of_these_documents().in_first(3).results

  • start_with()

Usage

See above for information on how to specify specific documents

  • rspec-solr_resp_hash.should include(“111”).as_first.document

  • rspec-solr_resp_hash.should include([“111”, “222”]).as_first.documents

  • rspec-solr_resp_hash.should include([=> “title1”, => “title2”]).in_first(3).results

  • rspec-solr_resp_hash.should include(“fld1” => “val1”).in_first(3)

  • rspec-solr_resp_hash.should include(“title” => /cooking/).in_first(3).results

MATCHING RELATIVE ORDER OF SPECIFIC DOCUMENTS

NOTE:  this is about the Solr documents returned in THIS response

Matcher

  • include().before()

NOTE: documents are specified the same way inside both sets of parens (see Usage for examples and see above re: specifying documents)

TODO: Potential Syntax:

  • include().before_first_occurrence_of()

  • include().within(3).of_document()

  • subject.document(:title => ‘vala’).should come_before(subject.document(:title =>‘valb’))

  • subject.should have_result_field_ordered(“title”, “vala”, “valb”)

Usage

  • rspec-solr_resp_hash.should include(“111”).before(“222”)

  • rspec-solr_resp_hash.should include(“fld”=>“val”).before(“fld”=>[“val1”, “val2”, “val3”])

  • rspec-solr_resp_hash.should include([=> “title1”, => “title2”]).before(“title” => “title3”)

COMPARING TOTAL RESULTS OF TWO RESPONSES

NOTE:  this is about the TOTAL number of Solr documents that match ("numFound"), NOT solely the documents returned in THESE responses

Matchers

  • have_more_results_than()

  • have_fewer_results_than()

  • have_the_same_number_of_results_as()

  • have_more_documents_than()

  • have_fewer_documents_than()

  • have_the_same_number_of_documents_as()

Usage

  • rspec-solr_resp_hash1.should have_more_results_than(rspec-solr_resp_hash2)

  • rspec-solr_resp_hash1.should have_fewer_results_than(rspec-solr_resp_hash2)

  • rspec-solr_resp_hash1.should have_the_same_number_of_results_as(rspec-solr_resp_hash2)

Alternate (allows more granularity)

  • rspec-solr_resp_hash1.size.should > rspec-solr_resp_hash2.size

  • rspec-solr_resp_hash1.size.should >= rspec-solr_resp_hash2.size

  • rspec-solr_resp_hash1.size.should < rspec-solr_resp_hash2.size

  • rspec-solr_resp_hash1.size.should <= rspec-solr_resp_hash2.size

  • rspec-solr_resp_hash1.size.should == rspec-solr_resp_hash2.size

  • rspec-solr_resp_hash1.size.should be_within(delta).of(rspec-solr_resp_hash2.size)

MATCHING FACET VALUES IN RESPONSE

NOTE:  this is about the facet counts returned in THIS response

Matchers

  • have_facet_field()

  • have_facet_field().with_value()

Usage

  • rspec-solr_resp_hash.should have_facet_field(“author”)

  • rspec-solr_resp_hash.should have_facet_field(“author”).with_value(“Steinbeck, John”)

TODO:

  • facets with arrarr and without (Solr option to get better facet formatting)

  • have_facet_field().with_value().with_count()

  • more ideas

    • facet(:format => “Book”)

    • facets(:format => [“Image”, “Map”])

    • include_facet().before_facet()

    • include_facets().before_facet()

    • include_facet().before_facets()

    • include_facets().before_facets()