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

.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


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 55

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

    failure_message_for_should 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_for_should_not 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

.have_facet_field_bodyObject

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


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
56
57
58
59
60
61
62
63
# File 'lib/rspec-solr/have_facet_field_matcher.rb', line 12

def self.have_facet_field_body 
  lambda { |expected_facet_field_name|
    
    match_for_should 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_for_should_not 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_for_should 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_for_should_not 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

.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


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 83

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

    failure_message_for_should 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_for_should_not 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

.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


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 27

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

    failure_message_for_should 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_for_should_not 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

Instance Method Details

#have_documentsObject

Determine if the receiver has Solr documents 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
# File 'lib/rspec-solr/have_documents_matcher.rb', line 6

def have_documents
  # Placeholder method for documentation purposes; 
  # the actual method is defined using RSpec's matcher DSL
end

#have_facet_fieldObject

Determine if the receiver has the facet_field



5
6
7
8
# File 'lib/rspec-solr/have_facet_field_matcher.rb', line 5

def have_facet_field
  # Placeholder method for documentation purposes; 
  # the actual method is defined using RSpec's matcher DSL
end

#have_fewer_results_thanObject

Determine if the receiver has fewer Solr docs than the expected NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



13
14
15
16
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 13

def have_fewer_results_than
  # Placeholder method for documentation purposes; 
  # the actual method is defined using RSpec's matcher DSL
end

#have_more_results_thanObject

Determine if the receiver has more Solr docs than the expected NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



20
21
22
23
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 20

def have_more_results_than
  # Placeholder method for documentation purposes; 
  # the actual method is defined using RSpec's matcher DSL
end

#have_the_same_number_of_results_asObject

Determine if the receiver has the same number of Solr docs as the expected NOTE: this is about the TOTAL number of Solr documents matching the queries, not solely the number of docs in THESE responses



6
7
8
9
# File 'lib/rspec-solr/compare_num_docs_matcher.rb', line 6

def have_the_same_number_of_results_as
  # Placeholder method for documentation purposes; 
  # the actual method is defined using RSpec's matcher DSL
end