Module: RSpecSolr::Matchers

Defined in:
lib/rspec-solr/have_documents_matcher.rb,
lib/rspec-solr/compare_num_docs_matcher.rb,
lib/rspec-solr/have_facet_field_matcher.rb,
lib/rspec-solr.rb

Overview

Custom RSpec Matchers for Solr responses

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.facet_field_body?Boolean

this is the lambda used to determine if the receiver (a Solr response object) has non-empty values for the facet field

as the expected RSpecSolr::SolrResponseHash

Returns:

  • (Boolean)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rspec-solr/have_facet_field_matcher.rb', line 5

def self.facet_field_body?
  lambda do |expected_facet_field_name|
    match do |solr_resp|
      if @facet_val
        solr_resp.has_facet_field_with_value?(expected_facet_field_name, @facet_val)
      else
        solr_resp.has_facet_field?(expected_facet_field_name)
      end
    end

    match_when_negated do |solr_resp|
      # we should fail if we are looking for a specific facet value but the facet field isn't present in the response
      @has_field = solr_resp.has_facet_field?(expected_facet_field_name)
      if @facet_val
        if @has_field
          !solr_resp.has_facet_field_with_value?(expected_facet_field_name, @facet_val)
        else
          false
        end
      else
        !@has_field
      end
    end

    failure_message do |solr_resp|
      if @facet_val
        "expected facet field #{expected_facet_field_name} with value #{@facet_val} in Solr response: #{solr_resp}"
      else
        "expected facet field #{expected_facet_field_name} with values in Solr response: #{solr_resp}"
      end
    end

    failure_message_when_negated do |solr_resp|
      if @facet_val
        if @has_field
          "expected facet field #{expected_facet_field_name} not to have value #{@facet_val} in Solr response: #{solr_resp}"
        else
          "expected facet field #{expected_facet_field_name} in Solr response: #{solr_resp}"
        end
      else
        "expected no #{expected_facet_field_name} facet field in Solr response: #{solr_resp}"
      end
    end

    chain :with_value do |val|
      @facet_val = val
    end

    diffable
  end
end

.fewer_results_than_bodyObject

this is the lambda used to determine if the receiver (a Solr response object) has fewer total documents

than the expected RSpecSolr::SolrResponseHash


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 35

def self.fewer_results_than_body
  lambda do |expected_solr_resp_hash|
    match do |actual_solr_resp_hash|
      actual_solr_resp_hash.size < expected_solr_resp_hash.size
    end

    failure_message do |actual_solr_resp_hash|
      "expected more than #{actual_solr_resp_hash.size} documents in Solr response #{expected_solr_resp_hash.num_docs_partial_output_str}"
    end

    failure_message_when_negated do |actual_solr_resp_hash|
      "expected #{actual_solr_resp_hash.size} or fewer documents in Solr response #{expected_solr_resp_hash.num_docs_partial_output_str}"
    end

    diffable
  end
end

.more_results_than_bodyObject

this is the lambda used to determine if the receiver (a Solr response object) has more total documents

than the expected RSpecSolr::SolrResponseHash


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 65

def self.more_results_than_body
  lambda do |expected_solr_resp_hash|
    match do |actual_solr_resp_hash|
      actual_solr_resp_hash.size > expected_solr_resp_hash.size
    end

    failure_message do |actual_solr_resp_hash|
      "expected fewer than #{actual_solr_resp_hash.size} documents in Solr response #{expected_solr_resp_hash.num_docs_partial_output_str}"
    end

    failure_message_when_negated do |actual_solr_resp_hash|
      "expected #{actual_solr_resp_hash.size} or more documents in Solr response #{expected_solr_resp_hash.num_docs_partial_output_str}"
    end

    diffable
  end
end

.same_number_of_results_bodyObject

this is the lambda used to determine if the receiver (a Solr response object) has the same number of total documents

as the expected RSpecSolr::SolrResponseHash


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 5

def self.same_number_of_results_body
  lambda do |expected_solr_resp_hash|
    match do |actual_solr_resp_hash|
      actual_solr_resp_hash.size == expected_solr_resp_hash.size
    end

    failure_message do |actual_solr_resp_hash|
      "expected #{actual_solr_resp_hash.size} documents in Solr response #{expected_solr_resp_hash.num_docs_partial_output_str}"
    end

    failure_message_when_negated do |actual_solr_resp_hash|
      "expected (not #{actual_solr_resp_hash.size}) documents in Solr response #{expected_solr_resp_hash.num_docs_partial_output_str}"
    end

    diffable
  end
end

Instance Method Details

#have_documentsObject

Determine if the receiver (a Solr response object) has at least one document NOTE: this is about the TOTAL number of Solr documents matching the query, not solely the number of docs in THIS response



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rspec-solr/have_documents_matcher.rb', line 6

RSpec::Matchers.define :have_documents do
  match do |solr_resp|
    solr_resp['response']['numFound'] > 0
  end

  failure_message do |solr_resp|
    "expected documents in Solr response #{solr_resp['response']}"
  end

  failure_message_when_negated do |solr_resp|
    "did not expect documents, but Solr response had #{solr_resp['response']['numFound']}"
  end
end

#have_facet_fieldObject

Determine if the receiver (a Solr response object) has the same number of total documents as the expected RSpecSolr::SolrResponseHash NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



60
61
# File 'lib/rspec-solr/have_facet_field_matcher.rb', line 60

RSpec::Matchers.define :have_facet_field, &facet_field_body?
e

#have_fewer_documents_thanObject

Determine if the receiver (a Solr response object) has fewer total documents than the expected RSpecSolr::SolrResponseHash NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



61
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 61

RSpec::Matchers.define :have_fewer_documents_than, &fewer_results_than_body

#have_fewer_results_thanObject

Determine if the receiver (a Solr response object) has fewer total documents than the expected RSpecSolr::SolrResponseHash NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



56
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 56

RSpec::Matchers.define :have_fewer_results_than, &fewer_results_than_body

#have_more_documents_thanObject

Determine if the receiver (a Solr response object) has more total documents than the expected RSpecSolr::SolrResponseHash NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



91
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 91

RSpec::Matchers.define :have_more_documents_than, &more_results_than_body

#have_more_results_thanObject

Determine if the receiver (a Solr response object) has more total documents than the expected RSpecSolr::SolrResponseHash NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



86
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 86

RSpec::Matchers.define :have_more_results_than, &more_results_than_body

#have_the_same_number_of_documents_asObject

Determine if the receiver (a Solr response object) has the same number of total documents as the expected RSpecSolr::SolrResponseHash NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



31
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 31

RSpec::Matchers.define :have_the_same_number_of_documents_as,  &same_number_of_results_body

#have_the_same_number_of_results_asObject

Determine if the receiver (a Solr response object) has the same number of total documents as the expected RSpecSolr::SolrResponseHash NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



26
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 26

RSpec::Matchers.define :have_the_same_number_of_results_as, &same_number_of_results_body