rspec-solr 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +18 -0
- data/.rvmrc +1 -0
- data/.travis.yml +10 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +5 -0
- data/README.rdoc +31 -0
- data/Rakefile +19 -0
- data/expectation_matchers.rdoc +148 -0
- data/lib/rspec-solr/have_documents_matcher.rb +34 -0
- data/lib/rspec-solr/include_documents_matcher.rb +105 -0
- data/lib/rspec-solr/solr_response_hash.rb +130 -0
- data/lib/rspec-solr/version.rb +3 -0
- data/lib/rspec-solr.rb +7 -0
- data/lib/tasks/ci.rake +5 -0
- data/lib/tasks/doc.rake +21 -0
- data/lib/tasks/spec.rake +5 -0
- data/rspec-solr.gemspec +35 -0
- data/spec/have_documents_spec.rb +60 -0
- data/spec/include_before_spec.rb +107 -0
- data/spec/include_document_spec.rb +423 -0
- data/spec/include_in_first_n_spec.rb +229 -0
- data/spec/number_of_documents_spec.rb +250 -0
- data/spec/solr_response_hash_spec.rb +115 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/matchers.rb +22 -0
- metadata +202 -0
@@ -0,0 +1,229 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec-solr'
|
3
|
+
|
4
|
+
describe RSpecSolr do
|
5
|
+
|
6
|
+
# fixtures below
|
7
|
+
|
8
|
+
context "#include().in_first(n)" do
|
9
|
+
context "should include().in_first(n)" do
|
10
|
+
it "passes when include arg is String and doc meets criteria" do
|
11
|
+
@solr_resp_5_docs.should include("222").in_first(3)
|
12
|
+
@solr_resp_5_docs.should include("111").in_first(1)
|
13
|
+
@solr_resp_5_docs.should include("222").in_first(2)
|
14
|
+
end
|
15
|
+
it "fails when include arg is String but doc comes too late" do
|
16
|
+
lambda {
|
17
|
+
@solr_resp_5_docs.should include("222").in_first(1)
|
18
|
+
}.should fail_matching('} to include document "222" in first 1')
|
19
|
+
end
|
20
|
+
it "passes when include arg is Hash and doc meets criteria" do
|
21
|
+
@solr_resp_5_docs.should include("fld" => ["val1", "val2", "val3"]).in_first(4)
|
22
|
+
end
|
23
|
+
it "fails when include arg is Hash but doc comes too late" do
|
24
|
+
lambda {
|
25
|
+
@solr_resp_5_docs.should include("fld" => ["val1", "val2", "val3"]).in_first(2)
|
26
|
+
}.should fail_matching('} to include document {"fld"=>["val1", "val2", "val3"]} in first 2')
|
27
|
+
end
|
28
|
+
it "passes when include arg is Array and all docs meet criteria" do
|
29
|
+
@solr_resp_5_docs.should include(["111", "222"]).in_first(2)
|
30
|
+
@solr_resp_5_docs.should include(["333", {"fld2"=>"val2"}]).in_first(4)
|
31
|
+
end
|
32
|
+
it "fails when include arg is Array but at least one doc comes too late" do
|
33
|
+
lambda {
|
34
|
+
@solr_resp_5_docs.should include(["111", "444"]).in_first(2)
|
35
|
+
}.should fail_matching('} to include documents ["111", "444"] in first 2')
|
36
|
+
lambda {
|
37
|
+
@solr_resp_5_docs.should include(["333", {"fld2"=>"val2"}]).in_first(2)
|
38
|
+
}.should fail_matching('} to include documents ["333", {"fld2"=>"val2"}] in first 2')
|
39
|
+
end
|
40
|
+
it "fails when there is no matching result" do
|
41
|
+
lambda {
|
42
|
+
@solr_resp_5_docs.should include("666").in_first(6)
|
43
|
+
}.should fail_matching('} to include document "666" in first 6')
|
44
|
+
lambda {
|
45
|
+
@solr_resp_5_docs.should include("fld"=>"not there").in_first(3)
|
46
|
+
}.should fail_matching('} to include document {"fld"=>"not there"} in first 3')
|
47
|
+
lambda {
|
48
|
+
@solr_resp_5_docs.should include("666", "777").in_first(6)
|
49
|
+
}.should fail_matching('} to include documents "666" and "777" in first 6')
|
50
|
+
end
|
51
|
+
end # should include().in_first(n)
|
52
|
+
|
53
|
+
context "should_NOT include().in_first(n)" do
|
54
|
+
it "fails when include arg is String and doc meets criteria" do
|
55
|
+
lambda {
|
56
|
+
@solr_resp_5_docs.should_not include("222").in_first(2)
|
57
|
+
}.should fail_matching('not to include document "222" in first 2')
|
58
|
+
end
|
59
|
+
it "passes when include arg is String but doc comes too late" do
|
60
|
+
@solr_resp_5_docs.should_not include("222").in_first(1)
|
61
|
+
end
|
62
|
+
it "fails when include arg is Hash and doc meets criteria" do
|
63
|
+
lambda {
|
64
|
+
@solr_resp_5_docs.should_not include("fld" => ["val1", "val2", "val3"]).in_first(4)
|
65
|
+
}.should fail_matching('not to include document {"fld"=>["val1", "val2", "val3"]} in first 4')
|
66
|
+
end
|
67
|
+
it "passes when include arg is Hash but doc comes too late" do
|
68
|
+
@solr_resp_5_docs.should_not include("fld" => ["val1", "val2", "val3"]).in_first(2)
|
69
|
+
end
|
70
|
+
it "fails when include arg is Array and all docs meet criteria" do
|
71
|
+
lambda {
|
72
|
+
@solr_resp_5_docs.should_not include("111", "222").in_first(2)
|
73
|
+
}.should fail_matching('not to include documents "111" and "222" in first 2')
|
74
|
+
lambda {
|
75
|
+
@solr_resp_5_docs.should_not include(["333", {"fld2"=>"val2"}]).in_first(4)
|
76
|
+
}.should fail_matching('not to include documents ["333", {"fld2"=>"val2"}] in first 4')
|
77
|
+
end
|
78
|
+
it "passes when include arg is Array but at least one doc comes too late" do
|
79
|
+
@solr_resp_5_docs.should_not include(["111", "444"]).in_first(2)
|
80
|
+
@solr_resp_5_docs.should_not include(["333", {"fld2"=>"val2"}]).in_first(2)
|
81
|
+
end
|
82
|
+
it "passes when there is no matching result" do
|
83
|
+
@solr_resp_5_docs.should_not include("666").in_first(6)
|
84
|
+
@solr_resp_5_docs.should_not include("fld"=>"not there").in_first(3)
|
85
|
+
@solr_resp_5_docs.should_not include("666", "777").in_first(6)
|
86
|
+
end
|
87
|
+
end # should_NOT include().in_first(n)
|
88
|
+
end # include().in_first(n)
|
89
|
+
|
90
|
+
context "#include().in_first (no n set) implies n=1" do
|
91
|
+
context "should #include().in_first" do
|
92
|
+
it "passes when matching document is first" do
|
93
|
+
@solr_resp_5_docs.should include("111").in_first
|
94
|
+
end
|
95
|
+
it "fails when matching document is not first" do
|
96
|
+
lambda {
|
97
|
+
@solr_resp_5_docs.should include("fld"=>["val1", "val2", "val3"]).in_first
|
98
|
+
}.should fail_matching('} to include document {"fld"=>["val1", "val2", "val3"]} in first 1')
|
99
|
+
end
|
100
|
+
end
|
101
|
+
context "should_NOT include().in_first" do
|
102
|
+
it "fails when matching document is first" do
|
103
|
+
lambda {
|
104
|
+
@solr_resp_5_docs.should_not include("111").in_first
|
105
|
+
}.should fail_matching('not to include document "111" in first 1')
|
106
|
+
end
|
107
|
+
it "passes when matching document is not first" do
|
108
|
+
@solr_resp_5_docs.should_not include("fld"=>["val1", "val2", "val3"]).in_first
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end # include().in_first (no n set)
|
112
|
+
|
113
|
+
context "#include().in_first(n).any_chained.names" do
|
114
|
+
context "should #include().in_first(n).any_chained.names" do
|
115
|
+
it "passes when document(s) meet criteria" do
|
116
|
+
@solr_resp_5_docs.should include("222").in_first(2).results
|
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
|
120
|
+
end
|
121
|
+
it "fails when document(s) don't meet criteria" do
|
122
|
+
lambda {
|
123
|
+
@solr_resp_5_docs.should include("fld"=>["val1", "val2", "val3"]).in_first.result
|
124
|
+
}.should fail_matching('} to include document {"fld"=>["val1", "val2", "val3"]} in first 1')
|
125
|
+
end
|
126
|
+
end
|
127
|
+
context "should_NOT #include().in_first(n).any_chained.names" do
|
128
|
+
it "fails when document(s) meet criteria" do
|
129
|
+
lambda {
|
130
|
+
@solr_resp_5_docs.should_not include("222").in_first(2).solr.documents
|
131
|
+
}.should fail_matching('not to include document "222" in first 2 ')
|
132
|
+
lambda {
|
133
|
+
@solr_resp_5_docs.should_not include("fld2"=>"val2").in_first.solr.document
|
134
|
+
}.should fail_matching('not to include document {"fld2"=>"val2"} in first 1')
|
135
|
+
end
|
136
|
+
it "passes when document(s) don't meet criteria" do
|
137
|
+
@solr_resp_5_docs.should_not include("fld"=>["val1", "val2", "val3"]).in_first.result
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end # include().in_first().any_chained_names
|
141
|
+
|
142
|
+
context "#include().as_first" do
|
143
|
+
context "should include().as_first" do
|
144
|
+
it "passes when the first document meets the criteria" do
|
145
|
+
@solr_resp_5_docs.should include("111").as_first
|
146
|
+
@solr_resp_5_docs.should include("fld"=>"val").as_first
|
147
|
+
@solr_resp_5_docs.should include("fld"=>"val", "fld2"=>"val2").as_first
|
148
|
+
end
|
149
|
+
it "fails when a document meets the criteria but is not the first" do
|
150
|
+
lambda {
|
151
|
+
@solr_resp_5_docs.should include("222").as_first
|
152
|
+
}.should fail_matching('} to include document "222" in first 1')
|
153
|
+
lambda {
|
154
|
+
@solr_resp_5_docs.should include("fld" => ["val1", "val2", "val3"]).as_first
|
155
|
+
}.should fail_matching('} to include document {"fld"=>["val1", "val2", "val3"]} in first 1')
|
156
|
+
end
|
157
|
+
it "fails when there is no matching result" do
|
158
|
+
lambda {
|
159
|
+
@solr_resp_5_docs.should include("96").as_first
|
160
|
+
}.should fail_matching('} to include document "96" in first 1')
|
161
|
+
end
|
162
|
+
end
|
163
|
+
context "should_NOT include().as_first" do
|
164
|
+
it "fails when the first document meets the criteria" do
|
165
|
+
lambda {
|
166
|
+
@solr_resp_5_docs.should_not include("111").as_first
|
167
|
+
}.should fail_matching('not to include document "111" in first 1')
|
168
|
+
lambda {
|
169
|
+
@solr_resp_5_docs.should_not include("fld"=>"val").as_first
|
170
|
+
}.should fail_matching('not to include document {"fld"=>"val"} in first 1')
|
171
|
+
lambda {
|
172
|
+
@solr_resp_5_docs.should_not include("fld"=>"val", "fld2"=>"val2").as_first
|
173
|
+
}.should fail_matching('not to include document {"fld"=>"val", "fld2"=>"val2"} in first 1')
|
174
|
+
end
|
175
|
+
it "passes when a document meets the criteria but is not the first" do
|
176
|
+
@solr_resp_5_docs.should_not include("222").as_first.document
|
177
|
+
@solr_resp_5_docs.should_not include("fld" => ["val1", "val2", "val3"]).as_first
|
178
|
+
end
|
179
|
+
it "passes when there is no matching result" do
|
180
|
+
@solr_resp_5_docs.should_not include("96").as_first
|
181
|
+
end
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
context "#include().as_first().any_chained.names" do
|
187
|
+
context "should #include().as_first(n).any_chained.names" do
|
188
|
+
it "passes when document(s) meet criteria" do
|
189
|
+
@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
|
192
|
+
end
|
193
|
+
it "fails when document(s) don't meet criteria" do
|
194
|
+
lambda {
|
195
|
+
@solr_resp_5_docs.should include("fld"=>["val1", "val2", "val3"]).as_first.result
|
196
|
+
}.should fail_matching ('} to include document {"fld"=>["val1", "val2", "val3"]} in first 1')
|
197
|
+
end
|
198
|
+
end
|
199
|
+
context "should_NOT #include().as_first(n).any_chained.names" do
|
200
|
+
it "fails when document(s) meet criteria" do
|
201
|
+
lambda {
|
202
|
+
@solr_resp_5_docs.should_not include(["111", "222"]).as_first(2).solr.documents
|
203
|
+
}.should fail_matching('not to include documents ["111", "222"] in first 2')
|
204
|
+
lambda {
|
205
|
+
@solr_resp_5_docs.should_not include("fld2"=>"val2").as_first.solr.document
|
206
|
+
}.should fail_matching('not to include document {"fld2"=>"val2"} in first 1')
|
207
|
+
end
|
208
|
+
it "passes when document(s) don't meet criteria" do
|
209
|
+
@solr_resp_5_docs.should_not include("fld"=>["val1", "val2", "val3"]).as_first.result
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end # include().as_first().any_chained_names
|
213
|
+
|
214
|
+
before(:all) do
|
215
|
+
@solr_resp_5_docs = RSpecSolr::SolrResponseHash.new({ "response" =>
|
216
|
+
{ "numFound" => 5,
|
217
|
+
"start" => 0,
|
218
|
+
"docs" =>
|
219
|
+
[ {"id"=>"111", "fld"=>"val", "fld2"=>"val2"},
|
220
|
+
{"id"=>"222"},
|
221
|
+
{"id"=>"333", "fld"=>"val"},
|
222
|
+
{"id"=>"444", "fld"=>["val1", "val2", "val3"]},
|
223
|
+
{"id"=>"555"}
|
224
|
+
]
|
225
|
+
}
|
226
|
+
})
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
@@ -0,0 +1,250 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec-solr'
|
3
|
+
|
4
|
+
describe RSpecSolr do
|
5
|
+
|
6
|
+
context "shouldn't break RSpec #have matcher" do
|
7
|
+
it "for Array" do
|
8
|
+
[1,2,3].should have(3).items
|
9
|
+
[1,2,3].should_not have(4).items
|
10
|
+
[1,2,3].should have_at_least(2).items
|
11
|
+
[1,2,3].should have_at_least(1).item
|
12
|
+
[1,2,3].should have_at_most(5).items
|
13
|
+
end
|
14
|
+
|
15
|
+
it "for Hash" do
|
16
|
+
{:k1 => 'v1', :k2 => 'v2', :k3 => 'v3'}.should have_exactly(3).items
|
17
|
+
{:k1 => 'v1', :k2 => 'v2', :k3 => 'v3'}.should_not have(4).items
|
18
|
+
{:k1 => 'v1', :k2 => 'v2', :k3 => 'v3'}.should have_at_least(2).items
|
19
|
+
{:k1 => 'v1', :k2 => 'v2', :k3 => 'v3'}.should have_at_most(5).items
|
20
|
+
end
|
21
|
+
|
22
|
+
it "for String" do
|
23
|
+
"this string".should have(11).characters
|
24
|
+
"this string".should_not have(12).characters
|
25
|
+
end
|
26
|
+
|
27
|
+
it "passes args to target" do
|
28
|
+
target = mock("target")
|
29
|
+
target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
|
30
|
+
target.should have(3).items("arg1","arg2")
|
31
|
+
end
|
32
|
+
it "passes block to target" do
|
33
|
+
target = mock("target")
|
34
|
+
block = lambda { 5 }
|
35
|
+
target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
|
36
|
+
target.should have(3).items("arg1","arg2", block)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# fixtures at end of this file
|
41
|
+
|
42
|
+
context "should have(n).documents" do
|
43
|
+
|
44
|
+
it "pluralizes 'documents'" do
|
45
|
+
@solr_resp_1_doc.should have(1).document
|
46
|
+
end
|
47
|
+
|
48
|
+
it "passes if target response has n documents" do
|
49
|
+
@solr_resp_5_docs.should have(5).documents
|
50
|
+
@solr_resp_no_docs.should have(0).documents
|
51
|
+
end
|
52
|
+
|
53
|
+
it "converts :no to 0" do
|
54
|
+
@solr_resp_no_docs.should have(:no).documents
|
55
|
+
end
|
56
|
+
|
57
|
+
it "converts a String argument to Integer" do
|
58
|
+
@solr_resp_5_docs.should have('5').documents
|
59
|
+
end
|
60
|
+
|
61
|
+
it "fails if target response has < n documents" do
|
62
|
+
lambda {
|
63
|
+
@solr_resp_5_docs.should have(6).documents
|
64
|
+
}.should fail_with("expected 6 documents, got 5")
|
65
|
+
lambda {
|
66
|
+
@solr_resp_no_docs.should have(1).document
|
67
|
+
}.should fail_with("expected 1 document, got 0")
|
68
|
+
end
|
69
|
+
|
70
|
+
it "fails if target response has > n documents" do
|
71
|
+
lambda {
|
72
|
+
@solr_resp_5_docs.should have(4).documents
|
73
|
+
}.should fail_with("expected 4 documents, got 5")
|
74
|
+
lambda {
|
75
|
+
@solr_resp_1_doc.should have(0).documents
|
76
|
+
}.should fail_with("expected 0 documents, got 1")
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
context "should_not have(n).documents" do
|
82
|
+
it "passes if target response has < n documents" do
|
83
|
+
@solr_resp_5_docs.should_not have(6).documents
|
84
|
+
@solr_resp_1_doc.should_not have(2).documents
|
85
|
+
@solr_resp_no_docs.should_not have(1).document
|
86
|
+
end
|
87
|
+
|
88
|
+
it "passes if target response has > n documents" do
|
89
|
+
@solr_resp_5_docs.should_not have(4).documents
|
90
|
+
@solr_resp_1_doc.should_not have(0).documents
|
91
|
+
@solr_resp_no_docs.should_not have(-1).documents
|
92
|
+
end
|
93
|
+
|
94
|
+
it "fails if target response has n documents" do
|
95
|
+
lambda {
|
96
|
+
@solr_resp_5_docs.should_not have(5).documents
|
97
|
+
}.should fail_with("expected target not to have 5 documents, got 5")
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
context "should have_exactly(n).documents" do
|
102
|
+
it "passes if target response has n documents" do
|
103
|
+
@solr_resp_5_docs.should have_exactly(5).documents
|
104
|
+
@solr_resp_no_docs.should have_exactly(0).documents
|
105
|
+
end
|
106
|
+
it "converts :no to 0" do
|
107
|
+
@solr_resp_no_docs.should have_exactly(:no).documents
|
108
|
+
end
|
109
|
+
it "fails if target response has < n documents" do
|
110
|
+
lambda {
|
111
|
+
@solr_resp_5_docs.should have_exactly(6).documents
|
112
|
+
}.should fail_with("expected 6 documents, got 5")
|
113
|
+
lambda {
|
114
|
+
@solr_resp_no_docs.should have_exactly(1).document
|
115
|
+
}.should fail_with("expected 1 document, got 0")
|
116
|
+
end
|
117
|
+
|
118
|
+
it "fails if target response has > n documents" do
|
119
|
+
lambda {
|
120
|
+
@solr_resp_5_docs.should have_exactly(4).documents
|
121
|
+
}.should fail_with("expected 4 documents, got 5")
|
122
|
+
lambda {
|
123
|
+
@solr_resp_1_doc.should have_exactly(0).documents
|
124
|
+
}.should fail_with("expected 0 documents, got 1")
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context "should have_at_least(n).documents" do
|
129
|
+
it "passes if target response has n documents" do
|
130
|
+
@solr_resp_5_docs.should have_at_least(5).documents
|
131
|
+
@solr_resp_1_doc.should have_at_least(1).document
|
132
|
+
@solr_resp_no_docs.should have_at_least(0).documents
|
133
|
+
end
|
134
|
+
|
135
|
+
it "passes if target response has > n documents" do
|
136
|
+
@solr_resp_5_docs.should have_at_least(4).documents
|
137
|
+
@solr_resp_1_doc.should have_at_least(0).documents
|
138
|
+
end
|
139
|
+
|
140
|
+
it "fails if target response has < n documents" do
|
141
|
+
lambda {
|
142
|
+
@solr_resp_5_docs.should have_at_least(6).documents
|
143
|
+
}.should fail_matching("expected at least 6 documents, got 5")
|
144
|
+
lambda {
|
145
|
+
@solr_resp_no_docs.should have_at_least(1).document
|
146
|
+
}.should fail_matching("expected at least 1 document, got 0")
|
147
|
+
end
|
148
|
+
|
149
|
+
it "provides educational negative failure messages" do
|
150
|
+
# given
|
151
|
+
my_matcher = have_at_least(6).documents
|
152
|
+
# when
|
153
|
+
my_matcher.matches?(@solr_resp_5_docs)
|
154
|
+
# then
|
155
|
+
my_matcher.failure_message_for_should_not.should eq <<-EOF
|
156
|
+
Isn't life confusing enough?
|
157
|
+
Instead of having to figure out the meaning of this:
|
158
|
+
should_not have_at_least(6).documents
|
159
|
+
We recommend that you use this instead:
|
160
|
+
should have_at_most(5).documents
|
161
|
+
EOF
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
context "should have_at_most(n).documents" do
|
166
|
+
it "passes if target response has n documents" do
|
167
|
+
@solr_resp_5_docs.should have_at_most(5).documents
|
168
|
+
@solr_resp_1_doc.should have_at_most(1).document
|
169
|
+
@solr_resp_no_docs.should have_at_most(0).documents
|
170
|
+
end
|
171
|
+
|
172
|
+
it "passes if target response has < n documents" do
|
173
|
+
@solr_resp_5_docs.should have_at_most(6).documents
|
174
|
+
@solr_resp_no_docs.should have_at_most(1).document
|
175
|
+
end
|
176
|
+
|
177
|
+
it "fails if target response has > n documents" do
|
178
|
+
lambda {
|
179
|
+
@solr_resp_5_docs.should have_at_most(4).documents
|
180
|
+
}.should fail_matching("expected at most 4 documents, got 5")
|
181
|
+
lambda {
|
182
|
+
@solr_resp_1_doc.should have_at_most(0).documents
|
183
|
+
}.should fail_matching("expected at most 0 documents, got 1")
|
184
|
+
end
|
185
|
+
|
186
|
+
it "provides educational negative failure messages" do
|
187
|
+
# given
|
188
|
+
my_matcher = have_at_most(4).documents
|
189
|
+
# when
|
190
|
+
my_matcher.matches?(@solr_resp_5_docs)
|
191
|
+
# then
|
192
|
+
my_matcher.failure_message_for_should_not.should eq <<-EOF
|
193
|
+
Isn't life confusing enough?
|
194
|
+
Instead of having to figure out the meaning of this:
|
195
|
+
should_not have_at_most(4).documents
|
196
|
+
We recommend that you use this instead:
|
197
|
+
should have_at_least(5).documents
|
198
|
+
EOF
|
199
|
+
end
|
200
|
+
end
|
201
|
+
|
202
|
+
it "should pass according to total Solr docs matching the query, not the number of docs in THIS response" do
|
203
|
+
solr_resp = RSpecSolr::SolrResponseHash.new({ "response" =>
|
204
|
+
{ "numFound" => 3,
|
205
|
+
"start" => 0,
|
206
|
+
"rows" => 1,
|
207
|
+
"docs" => [ {"id"=>"111"} ]
|
208
|
+
}
|
209
|
+
})
|
210
|
+
solr_resp.should have(3).documents
|
211
|
+
solr_resp.should_not have(1).document
|
212
|
+
solr_resp = RSpecSolr::SolrResponseHash.new({ "response" =>
|
213
|
+
{ "numFound" => 3,
|
214
|
+
"start" => 0,
|
215
|
+
"rows" => 0
|
216
|
+
}
|
217
|
+
})
|
218
|
+
solr_resp.should have(3).documents
|
219
|
+
end
|
220
|
+
|
221
|
+
before(:all) do
|
222
|
+
@solr_resp_1_doc = RSpecSolr::SolrResponseHash.new({ "response" =>
|
223
|
+
{ "numFound" => 1,
|
224
|
+
"start" => 0,
|
225
|
+
"docs" => [ {"id"=>"111"} ]
|
226
|
+
}
|
227
|
+
})
|
228
|
+
|
229
|
+
@solr_resp_5_docs = RSpecSolr::SolrResponseHash.new({ "response" =>
|
230
|
+
{ "numFound" => 5,
|
231
|
+
"start" => 0,
|
232
|
+
"docs" =>
|
233
|
+
[ {"id"=>"111"},
|
234
|
+
{"id"=>"222"},
|
235
|
+
{"id"=>"333"},
|
236
|
+
{"id"=>"444"},
|
237
|
+
{"id"=>"555"}
|
238
|
+
]
|
239
|
+
}
|
240
|
+
})
|
241
|
+
|
242
|
+
@solr_resp_no_docs = RSpecSolr::SolrResponseHash.new({ "response" =>
|
243
|
+
{ "numFound" => 0,
|
244
|
+
"start" => 0,
|
245
|
+
"docs" => []
|
246
|
+
}
|
247
|
+
})
|
248
|
+
end
|
249
|
+
|
250
|
+
end
|
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'rspec-solr'
|
3
|
+
|
4
|
+
describe RSpecSolr::SolrResponseHash do
|
5
|
+
|
6
|
+
context "id field" do
|
7
|
+
before(:all) do
|
8
|
+
@srh = RSpecSolr::SolrResponseHash.new({ "response" =>
|
9
|
+
{ "docs" =>
|
10
|
+
[ {"id"=>"111"},
|
11
|
+
{"not_id"=>"222"},
|
12
|
+
{"id"=>"333"},
|
13
|
+
]
|
14
|
+
}
|
15
|
+
})
|
16
|
+
end
|
17
|
+
it "should default to 'id'" do
|
18
|
+
RSpecSolr::SolrResponseHash.new({}).id_field.should == "id"
|
19
|
+
@srh.id_field.should == "id"
|
20
|
+
end
|
21
|
+
it "should be changable to whatever" do
|
22
|
+
@srh.id_field='new'
|
23
|
+
@srh.id_field.should == 'new'
|
24
|
+
@srh.id_field='newer'
|
25
|
+
@srh.id_field.should == 'newer'
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# fixture below
|
30
|
+
|
31
|
+
context "has_document?" do
|
32
|
+
it "should be true when single document meets expectation" do
|
33
|
+
@solr_resp_5_docs.should have_document({"id" => "222"})
|
34
|
+
@solr_resp_5_docs.should have_document({"id" => "111", "fld" => "val"})
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be true when at least one doc meets expectation" do
|
38
|
+
@solr_resp_5_docs.should have_document({"fld" => "val"})
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should be false when no docs meet expectation" do
|
42
|
+
@solr_resp_5_docs.should_not have_document({"id" => "not_there"})
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be false when only part of expectation is met" do
|
46
|
+
@solr_resp_5_docs.should_not have_document({"id" => "222", "fld" => "val"})
|
47
|
+
end
|
48
|
+
|
49
|
+
it "should be true when document is < = expected position in results" do
|
50
|
+
@solr_resp_5_docs.should have_document({"id" => "222"}, 2)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should be false when document is > expected position in results" do
|
54
|
+
@solr_resp_5_docs.should_not have_document({"id" => "222"}, 1)
|
55
|
+
end
|
56
|
+
|
57
|
+
end # has_document?
|
58
|
+
|
59
|
+
context "get_first_doc_index" do
|
60
|
+
it "should get the right document index with an id String argument" do
|
61
|
+
@solr_resp_5_docs.get_first_doc_index("333").should == 2
|
62
|
+
@solr_resp_5_docs.get_first_doc_index("111").should == 0
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should get the first occurrence of a doc meeting a Hash argument" do
|
66
|
+
@solr_resp_5_docs.get_first_doc_index("fld"=>"val").should == 0
|
67
|
+
@solr_resp_5_docs.get_first_doc_index("id"=>"333").should == 2
|
68
|
+
end
|
69
|
+
|
70
|
+
it "should get the index of the doc occurring first in the results from an Array argument" do
|
71
|
+
@solr_resp_5_docs.get_first_doc_index(["fld"=>"val"]).should == 0
|
72
|
+
@solr_resp_5_docs.get_first_doc_index(["222", "444"]).should == 1
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should return nil when the expectation argument isn't met" do
|
76
|
+
@solr_resp_5_docs.get_first_doc_index("666").should be_nil
|
77
|
+
@solr_resp_5_docs.get_first_doc_index("not_there"=>"not_there").should be_nil
|
78
|
+
@solr_resp_5_docs.get_first_doc_index(["222", "666"]).should be_nil
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
context "get_min_index" do
|
84
|
+
it "should return nil when both arguments are nil" do
|
85
|
+
@solr_resp_5_docs.send(:get_min_index, nil, nil).should be_nil
|
86
|
+
end
|
87
|
+
it "should return the first argument when the second argument is nil" do
|
88
|
+
@solr_resp_5_docs.send(:get_min_index, 3, nil).should == 3
|
89
|
+
end
|
90
|
+
it "should return the second argument when the first argument is nil" do
|
91
|
+
@solr_resp_5_docs.send(:get_min_index, nil, 5).should == 5
|
92
|
+
end
|
93
|
+
it "should return the minimum of the two arguments when neither of them is nil" do
|
94
|
+
@solr_resp_5_docs.send(:get_min_index, 2, 5).should == 2
|
95
|
+
@solr_resp_5_docs.send(:get_min_index, 5, 2).should == 2
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
before(:all) do
|
101
|
+
@solr_resp_5_docs = RSpecSolr::SolrResponseHash.new({ "response" =>
|
102
|
+
{ "numFound" => 5,
|
103
|
+
"start" => 0,
|
104
|
+
"docs" =>
|
105
|
+
[ {"id"=>"111", "fld"=>"val"},
|
106
|
+
{"id"=>"222"},
|
107
|
+
{"id"=>"333", "fld"=>"val"},
|
108
|
+
{"id"=>"444"},
|
109
|
+
{"id"=>"555"}
|
110
|
+
]
|
111
|
+
}
|
112
|
+
})
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# for test coverage
|
2
|
+
require 'simplecov'
|
3
|
+
require 'simplecov-rcov'
|
4
|
+
class SimpleCov::Formatter::MergedFormatter
|
5
|
+
def format(result)
|
6
|
+
SimpleCov::Formatter::HTMLFormatter.new.format(result)
|
7
|
+
SimpleCov::Formatter::RcovFormatter.new.format(result)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
|
11
|
+
SimpleCov.start do
|
12
|
+
add_filter "/spec/"
|
13
|
+
end
|
14
|
+
|
15
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
16
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
17
|
+
|
18
|
+
|
19
|
+
Dir['./spec/support/**/*'].each {|f| require f}
|
20
|
+
|
21
|
+
#RSpec.configure do |config|
|
22
|
+
#end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# This file shamelessly copied from rspec-expecations/spec/support/matchers.rb
|
2
|
+
RSpec::Matchers.define :include_method do |expected|
|
3
|
+
match do |actual|
|
4
|
+
actual.map { |m| m.to_s }.include?(expected.to_s)
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
module RSpec
|
9
|
+
module Matchers
|
10
|
+
def fail
|
11
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError)
|
12
|
+
end
|
13
|
+
|
14
|
+
def fail_with(message)
|
15
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError, message)
|
16
|
+
end
|
17
|
+
|
18
|
+
def fail_matching(message)
|
19
|
+
raise_error(RSpec::Expectations::ExpectationNotMetError, /#{Regexp.escape(message)}/)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|