rspec-solr 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/MATCHERS.rdoc CHANGED
@@ -76,13 +76,15 @@ TODO:
76
76
  === Matchers
77
77
  * include().as_first
78
78
  * include().as_first.document
79
- * include().as_first.results
80
- * include().as_first.anything.can.go.here
81
79
  * include().in_first(n)
82
- * include().in_first(n).documents
83
- * include().in_first(n).results
84
- * include().in_first(n).anything_can_go_here
85
80
  * include(Array).in_first(n).results
81
+ * include(Hash).in_each_of_first(n).documents
82
+
83
+ Note that the following are equivalent:
84
+ * include().blah.document
85
+ * include().blah.documents
86
+ * include().blah.result
87
+ * include().blah.results
86
88
 
87
89
  TODO:
88
90
  * include_at_least(3).of_these_documents().in_first(3).results
@@ -94,6 +96,7 @@ See above for information on how to specify specific documents
94
96
  * rspec-solr_resp_hash.should include(["111", "222"]).as_first.documents
95
97
  * rspec-solr_resp_hash.should include([{"title" => "title1"}, {"title" => "title2"}]).in_first(3).results
96
98
  * rspec-solr_resp_hash.should include("fld1" => "val1").in_first(3)
99
+ * rspec-solr_resp_hash.should include("title" => /cooking/).in_first(3).results
97
100
 
98
101
 
99
102
  == MATCHING RELATIVE ORDER OF SPECIFIC DOCUMENTS
@@ -15,6 +15,14 @@ module RSpec
15
15
 
16
16
  alias_method :as_first, :in_first
17
17
 
18
+ # chain method for .should include().in_each_of_first(n)
19
+ # sets @min_for_last_matching_doc_ix for use in perform_match method
20
+ # @return self - "each method must return self in order to chain methods together"
21
+ def in_each_of_first(num=1)
22
+ @min_for_last_matching_doc_pos = num
23
+ self
24
+ end
25
+
18
26
  # chain method for .should include().before()
19
27
  # sets @before_expected for use in perform_match method
20
28
  # @return self - "each method must return self in order to chain methods together"
@@ -30,6 +38,8 @@ module RSpec
30
38
  # FIXME: DRY up these messages across cases and across should and should_not
31
39
  if @before_expected
32
40
  "expected #{@actual.inspect} to #{name_to_sentence} #{doc_label_str(@expected)}#{expected_to_sentence} before #{doc_label_str(@before_expected)} matching #{@before_expected.inspect}"
41
+ elsif @min_for_last_matching_doc_pos
42
+ "expected each of the first #{@min_for_last_matching_doc_pos.to_s} documents to #{name_to_sentence}#{expected_to_sentence}: #{@actual.inspect}"
33
43
  elsif @max_doc_position
34
44
  "expected #{@actual.inspect} to #{name_to_sentence} #{doc_label_str(@expected)}#{expected_to_sentence} in first #{@max_doc_position.to_s} results"
35
45
  else
@@ -42,6 +52,8 @@ module RSpec
42
52
  assert_ivars :@actual, :@expected
43
53
  if @before_expected
44
54
  "expected #{@actual.inspect} not to #{name_to_sentence} #{doc_label_str(@expected)}#{expected_to_sentence} before #{doc_label_str(@before_expected)} matching #{@before_expected.inspect}"
55
+ elsif @min_for_last_matching_doc_pos
56
+ "expected some of the first #{@min_for_last_matching_doc_pos.to_s} documents not to #{name_to_sentence}#{expected_to_sentence}: #{@actual.inspect}"
45
57
  elsif @max_doc_position
46
58
  "expected #{@actual.inspect} not to #{name_to_sentence} #{doc_label_str(@expected)}#{expected_to_sentence} in first #{@max_doc_position.to_s} results"
47
59
  else
@@ -56,7 +68,7 @@ private
56
68
  def perform_match(predicate, hash_predicate, actuals, expecteds)
57
69
  expecteds.send(predicate) do |expected|
58
70
  if comparing_doc_to_solr_resp_hash?(actuals, expected)
59
- if (@before_expected)
71
+ if @before_expected
60
72
  before_ix = actuals.get_first_doc_index(@before_expected)
61
73
  if before_ix
62
74
  @max_doc_position = before_ix + 1
@@ -65,7 +77,11 @@ private
65
77
  @max_doc_position = -1
66
78
  end
67
79
  end
68
- actuals.has_document?(expected, @max_doc_position)
80
+ if @min_for_last_matching_doc_pos
81
+ actuals.has_document?(expected, @min_for_last_matching_doc_pos, true)
82
+ else
83
+ actuals.has_document?(expected, @max_doc_position)
84
+ end
69
85
  elsif comparing_hash_values?(actuals, expected)
70
86
  expected.send(hash_predicate) {|k,v| actuals[k] == v}
71
87
  elsif comparing_hash_keys?(actuals, expected)
@@ -82,10 +98,14 @@ private
82
98
  end
83
99
 
84
100
  def method_missing(method, *args, &block)
85
- @collection_name = method
86
- @args = args
87
- @block = block
88
- self
101
+ if (method =~ /documents?/ || method =~ /results?/)
102
+ @collection_name = method
103
+ @args = args
104
+ @block = block
105
+ self
106
+ else
107
+ super.method_missing
108
+ end
89
109
  end
90
110
 
91
111
  # @return [String] 'documents' or 'document' as indicated by expectation
@@ -38,34 +38,51 @@ class RSpecSolr
38
38
  # [{"title" => "warm fuzzies"}, {"title" => "cool fuzzies"}] implies we expect at least one Solr doc in this response matching each Hash in the Array
39
39
  # [{"title" => /rm fuzz/}, {"title" => /^cool/}] implies we expect at least one Solr doc in this response matching each Hash in the Array
40
40
  # @param [FixNum] max_doc_position maximum acceptable position (1-based) of document in results. (e.g. if 2, it must be the 1st or 2nd doc in the results)
41
- def has_document?(expected_doc, max_doc_position = nil)
41
+ def has_document?(expected_doc, max_doc_position = nil, all_must_match = nil)
42
42
  if expected_doc.is_a?(Hash)
43
- # we are happy if any doc meets all of our expectations
44
- docs.any? { |doc|
45
- expected_doc.all? { | exp_fname, exp_vals |
46
- doc.include?(exp_fname) &&
47
- # exp_vals can be a String or an Array
48
- # if it's an Array, then all expected values must be present
49
- Array(exp_vals).all? { | exp_val |
50
- # a doc's fld values can be a String or an Array
51
- case exp_val
52
- when Regexp
53
- Array(doc[exp_fname]).any? { |val| val =~ exp_val }
54
- else
55
- Array(doc[exp_fname]).include?(exp_val)
56
- end
57
- } &&
43
+ if all_must_match
44
+ first_non_match = docs.find_index { |doc|
45
+ !doc_matches_all_criteria(doc, expected_doc)
46
+ } >= max_doc_position
47
+ else
48
+ # we are happy if any doc meets all of our expectations
49
+ docs.any? { |doc|
50
+ doc_matches_all_criteria(doc, expected_doc) &&
58
51
  # satisfy doc's position in the results
59
52
  (max_doc_position ? docs.find_index(doc) < max_doc_position : true)
60
53
  }
61
- }
54
+ end
62
55
  elsif expected_doc.is_a?(String)
56
+ if all_must_match
57
+ raise ArgumentError, "in_each_of_first(n) requires a Hash argument to include() method"
58
+ end
63
59
  has_document?({self.id_field => expected_doc}, max_doc_position)
64
60
  elsif expected_doc.is_a?(Array)
61
+ if all_must_match
62
+ raise ArgumentError, "in_each_of_first(n) requires a Hash argument to include() method"
63
+ end
65
64
  expected_doc.all? { |exp| has_document?(exp, max_doc_position) }
66
65
  end
67
66
  end
68
67
 
68
+ # return true if the document contains all the key value pairs in the expectations_hash
69
+ def doc_matches_all_criteria(doc, expectations_hash)
70
+ expectations_hash.all? { | exp_fname, exp_vals |
71
+ doc.include?(exp_fname) &&
72
+ # exp_vals can be a String or an Array
73
+ # if it's an Array, then all expected values must be present
74
+ Array(exp_vals).all? { | exp_val |
75
+ # a doc's fld values can be a String or an Array
76
+ case exp_val
77
+ when Regexp
78
+ Array(doc[exp_fname]).any? { |val| val =~ exp_val }
79
+ else
80
+ Array(doc[exp_fname]).include?(exp_val)
81
+ end
82
+ }
83
+ }
84
+ end
85
+
69
86
  # @return the index of the first document that meets the expectations in THIS response
70
87
  # @param expected_doc what should be matched in a document in THIS response
71
88
  # @example expected_doc Hash implies ALL key/value pairs will be matched in a SINGLE Solr document
@@ -1,3 +1,3 @@
1
1
  class RSpecSolr
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,75 @@
1
+ require 'spec_helper'
2
+ require 'rspec-solr'
3
+
4
+ describe RSpecSolr do
5
+
6
+ # fixtures below
7
+
8
+ context "#include().in_each_of_first(n)" do
9
+ context "should" do
10
+ it "passes when all docs in range meet criteria" do
11
+ @solr_resp.should include("fld" => "val").in_each_of_first(2)
12
+ @solr_resp.should include("fld" => "val").in_each_of_first(2).documents
13
+ @solr_resp.should include("fld" => "val").in_each_of_first(2).results
14
+ end
15
+ it "fails when doc in range doesn't meet criteria" do
16
+ lambda {
17
+ @solr_resp.should include("fld2" => "val2").in_each_of_first(2)
18
+ }.should fail_matching('expected each of the first 2 documents to include {"fld2"=>"val2"}')
19
+ lambda {
20
+ @solr_resp.should include("fld" => "val").in_each_of_first(3)
21
+ }.should fail_matching('expected each of the first 3 documents to include {"fld"=>"val"}')
22
+ end
23
+ end
24
+ context "should_NOT" do
25
+ it "fails when all docs in range meet criteria" do
26
+ lambda {
27
+ @solr_resp.should_not include("fld" => "val").in_each_of_first(2)
28
+ }.should fail_matching('expected some of the first 2 documents not to include {"fld"=>"val"}')
29
+ lambda {
30
+ @solr_resp.should_not include("fld" => "val").in_each_of_first(2).documents
31
+ }.should fail_matching('expected some of the first 2 documents not to include {"fld"=>"val"}')
32
+ lambda {
33
+ @solr_resp.should_not include("fld" => "val").in_each_of_first(2).results
34
+ }.should fail_matching('expected some of the first 2 documents not to include {"fld"=>"val"}')
35
+ end
36
+ it "passes when doc in range doesn't meet criteria" do
37
+ @solr_resp.should_not include("fld" => "val").in_each_of_first(3)
38
+ @solr_resp.should_not include("fld2" => "val2").in_each_of_first(2)
39
+ end
40
+ end
41
+ it "should expect a Hash for the include" do
42
+ expect {
43
+ @solr_resp.should include("111").in_each_of_first(1)
44
+ }.to raise_error(ArgumentError, "in_each_of_first(n) requires a Hash argument to include() method")
45
+ expect {
46
+ @solr_resp.should include(["111", "222"]).in_each_of_first(1)
47
+ }.to raise_error(ArgumentError, "in_each_of_first(n) requires a Hash argument to include() method")
48
+ expect {
49
+ @solr_resp.should_not include("111").in_each_of_first(1)
50
+ }.to raise_error(ArgumentError, "in_each_of_first(n) requires a Hash argument to include() method")
51
+ expect {
52
+ @solr_resp.should include("fld" => "val").in_each_of_first(1)
53
+ }.to_not raise_error(ArgumentError)
54
+ expect {
55
+ @solr_resp.should include({"fld" => "val", "fld2" => "val2"}).in_each_of_first(1)
56
+ }.to_not raise_error(ArgumentError)
57
+ end
58
+ end
59
+
60
+ before(:all) do
61
+ @solr_resp = RSpecSolr::SolrResponseHash.new({ "response" =>
62
+ { "numFound" => 5,
63
+ "start" => 0,
64
+ "docs" =>
65
+ [ {"id"=>"111", "fld"=>"val", "fld2"=>"val2"},
66
+ {"id"=>"222", "fld"=>"val"},
67
+ {"id"=>"333"},
68
+ {"id"=>"444", "fld"=>["val1", "val2", "val3"]},
69
+ {"id"=>"555"}
70
+ ]
71
+ }
72
+ })
73
+ end
74
+
75
+ end
@@ -110,13 +110,12 @@ describe RSpecSolr do
110
110
  end
111
111
  end # include().in_first (no n set)
112
112
 
113
- context "#include().in_first(n).any_chained.names" do
114
- context "should #include().in_first(n).any_chained.names" do
113
+ context "#include().in_first(n).allowed_chained.names" do
114
+ context "should #include().in_first(n).allowed_chained.names" do
115
115
  it "passes when document(s) meet criteria" do
116
116
  @solr_resp_5_docs.should include("222").in_first(2).results
117
117
  @solr_resp_5_docs.should include("222").in_first(2).documents
118
- @solr_resp_5_docs.should include("222").in_first(2).solr.documents
119
- @solr_resp_5_docs.should include("fld2"=>"val2").in_first.solr.document
118
+ @solr_resp_5_docs.should include("fld2"=>"val2").in_first.document
120
119
  end
121
120
  it "fails when document(s) don't meet criteria" do
122
121
  lambda {
@@ -124,20 +123,25 @@ describe RSpecSolr do
124
123
  }.should fail_matching('} to include document {"fld"=>["val1", "val2", "val3"]} in first 1')
125
124
  end
126
125
  end
127
- context "should_NOT #include().in_first(n).any_chained.names" do
126
+ context "should_NOT #include().in_first(n).allowed_chained.names" do
128
127
  it "fails when document(s) meet criteria" do
129
128
  lambda {
130
- @solr_resp_5_docs.should_not include("222").in_first(2).solr.documents
129
+ @solr_resp_5_docs.should_not include("222").in_first(2).documents
131
130
  }.should fail_matching('not to include document "222" in first 2 ')
132
131
  lambda {
133
- @solr_resp_5_docs.should_not include("fld2"=>"val2").in_first.solr.document
132
+ @solr_resp_5_docs.should_not include("fld2"=>"val2").in_first.document
134
133
  }.should fail_matching('not to include document {"fld2"=>"val2"} in first 1')
135
134
  end
136
135
  it "passes when document(s) don't meet criteria" do
137
136
  @solr_resp_5_docs.should_not include("fld"=>["val1", "val2", "val3"]).in_first.result
138
137
  end
139
138
  end
140
- end # include().in_first().any_chained_names
139
+ it "should raise a NoMethodError exception when it gets nonsense after include" do
140
+ expect {
141
+ @solr_resp.should include("111").bad_method(2)
142
+ }.to raise_error(NoMethodError, /bad_method/)
143
+ end
144
+ end # include().in_first().allowed_chained.names
141
145
 
142
146
  context "#include().as_first" do
143
147
  context "should include().as_first" do
@@ -183,12 +187,12 @@ describe RSpecSolr do
183
187
  end
184
188
 
185
189
 
186
- context "#include().as_first().any_chained.names" do
187
- context "should #include().as_first(n).any_chained.names" do
190
+ context "#include().as_first().allowed_chained.names" do
191
+ context "should #include().as_first(n).allowed_chained.names" do
188
192
  it "passes when document(s) meet criteria" do
189
193
  @solr_resp_5_docs.should include(["111", "222"]).as_first(2).results
190
- @solr_resp_5_docs.should include(["111", "222"]).as_first(2).solr.documents
191
- @solr_resp_5_docs.should include("fld2"=>"val2").as_first.solr.document
194
+ @solr_resp_5_docs.should include(["111", "222"]).as_first(2).documents
195
+ @solr_resp_5_docs.should include("fld2"=>"val2").as_first.document
192
196
  end
193
197
  it "fails when document(s) don't meet criteria" do
194
198
  lambda {
@@ -199,17 +203,17 @@ describe RSpecSolr do
199
203
  context "should_NOT #include().as_first(n).any_chained.names" do
200
204
  it "fails when document(s) meet criteria" do
201
205
  lambda {
202
- @solr_resp_5_docs.should_not include(["111", "222"]).as_first(2).solr.documents
206
+ @solr_resp_5_docs.should_not include(["111", "222"]).as_first(2).documents
203
207
  }.should fail_matching('not to include documents ["111", "222"] in first 2')
204
208
  lambda {
205
- @solr_resp_5_docs.should_not include("fld2"=>"val2").as_first.solr.document
209
+ @solr_resp_5_docs.should_not include("fld2"=>"val2").as_first.document
206
210
  }.should fail_matching('not to include document {"fld2"=>"val2"} in first 1')
207
211
  end
208
212
  it "passes when document(s) don't meet criteria" do
209
213
  @solr_resp_5_docs.should_not include("fld"=>["val1", "val2", "val3"]).as_first.result
210
214
  end
211
215
  end
212
- end # include().as_first().any_chained_names
216
+ end # include().as_first().allowed_chained.names
213
217
 
214
218
  before(:all) do
215
219
  @solr_resp_5_docs = RSpecSolr::SolrResponseHash.new({ "response" =>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-solr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-16 00:00:00.000000000 Z
13
+ date: 2012-10-20 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rspec
@@ -158,6 +158,7 @@ files:
158
158
  - spec/have_documents_spec.rb
159
159
  - spec/include_before_spec.rb
160
160
  - spec/include_document_spec.rb
161
+ - spec/include_in_each_of_first_n_spec.rb
161
162
  - spec/include_in_first_n_spec.rb
162
163
  - spec/number_of_documents_spec.rb
163
164
  - spec/solr_response_hash_spec.rb
@@ -177,7 +178,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
177
178
  version: '0'
178
179
  segments:
179
180
  - 0
180
- hash: -4597988063406856522
181
+ hash: -2465877972703506306
181
182
  required_rubygems_version: !ruby/object:Gem::Requirement
182
183
  none: false
183
184
  requirements:
@@ -186,7 +187,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
186
187
  version: '0'
187
188
  segments:
188
189
  - 0
189
- hash: -4597988063406856522
190
+ hash: -2465877972703506306
190
191
  requirements: []
191
192
  rubyforge_project:
192
193
  rubygems_version: 1.8.24
@@ -198,6 +199,7 @@ test_files:
198
199
  - spec/have_documents_spec.rb
199
200
  - spec/include_before_spec.rb
200
201
  - spec/include_document_spec.rb
202
+ - spec/include_in_each_of_first_n_spec.rb
201
203
  - spec/include_in_first_n_spec.rb
202
204
  - spec/number_of_documents_spec.rb
203
205
  - spec/solr_response_hash_spec.rb