Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 318 lines (271 sloc) 11.728 kb
d5bc8f1 @langalex just require spec_helper instead of giving relative path
authored
1 require 'spec_helper'
6123da5 @langalex added rspec helpers for stubbing out views
authored
2 require 'couch_potato/rspec'
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
3
4 describe CouchPotato::RSpec::MapToMatcher do
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
5
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
6 describe "basic map function" do
7 before(:each) do
e383ce2 @langalex support for testing list functions
authored
8 @view_spec = stub(:map_function => "function(doc) {emit(doc.name, doc.tags.length);}")
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
9 end
10
11 it "should pass if the given function emits the expected javascript" do
085d3a9 @langalex improved syntax of map function specs by splatting results
authored
12 @view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2])
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
13 end
14
15 it "should not pass if the given function emits different javascript" do
085d3a9 @langalex improved syntax of map function specs by splatting results
authored
16 @view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 3])
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
17 end
18 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
19
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
20 describe "functions emitting multiple times" do
21 before(:each) do
e383ce2 @langalex support for testing list functions
authored
22 @view_spec = stub(:map_function => "function(doc) {emit(doc.name, doc.tags.length); emit(doc.tags[0], doc.tags[1])};")
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
23 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
24
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
25 it "should pass if the given function emits the expected javascript" do
085d3a9 @langalex improved syntax of map function specs by splatting results
authored
26 @view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2], ['person', 'male'])
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
27 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
28
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
29 it "should return false if the given function emits different javascript" do
085d3a9 @langalex improved syntax of map function specs by splatting results
authored
30 @view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2], ['male', 'person'])
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
31 end
32 end
b60c41c @andymorris Replace print_r with JSON.stringify to handle dates in JS runtime specs
andymorris authored
33
34 it "should work with date emit values" do
35 spec = stub(:map_function => "function(doc) { emit(null, new Date(1368802800000)); }")
36 spec.should map({}).to([nil, "2013-05-17T15:00:00.000Z"])
37 end
c672918 @erichaberkorn added specs for the map/reduce matchers with commonJS and made a few var...
erichaberkorn authored
38
ca73408 @andymorris Make specs handle CommonJS modules via module.exports as well as exports
andymorris authored
39 it "should work with commonJS modules that use 'exports'" do
c672918 @erichaberkorn added specs for the map/reduce matchers with commonJS and made a few var...
erichaberkorn authored
40 spec = stub(
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
41 :map_function => "function(doc) { var test = require('views/lib/test'); emit(null, test.test); }",
c672918 @erichaberkorn added specs for the map/reduce matchers with commonJS and made a few var...
erichaberkorn authored
42 :lib => {:test => "exports.test = 'test';"}
43 )
44 spec.should map({}).to([nil, "test"])
45 end
46
ca73408 @andymorris Make specs handle CommonJS modules via module.exports as well as exports
andymorris authored
47 it "should work with commonJS modules that use 'module.exports'" do
48 spec = stub(
49 :map_function => "function(doc) { var test = require('views/lib/test'); emit(null, test.test); }",
50 :lib => {:test => "module.exports.test = 'test';"}
51 )
52 spec.should map({}).to([nil, "test"])
53 end
54
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
55 describe "failing specs" do
56 before(:each) do
e383ce2 @langalex support for testing list functions
authored
57 @view_spec = stub(:map_function => "function(doc) {emit(doc.name, null)}")
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
58 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
59
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
60 it "should have a nice error message for failing should" do
61 lambda {
085d3a9 @langalex improved syntax of map function specs by splatting results
authored
62 @view_spec.should map({:name => 'bill'}).to(['linus', nil])
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
63 }.should raise_error('Expected to map to [["linus", nil]] but got [["bill", nil]].')
64 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
65
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
66 it "should have a nice error message for failing should not" do
67 lambda {
085d3a9 @langalex improved syntax of map function specs by splatting results
authored
68 @view_spec.should_not map({:name => 'bill'}).to(['bill', nil])
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
69 }.should raise_error('Expected not to map to [["bill", nil]] but did.')
70 end
71 end
72 end
73
74 describe CouchPotato::RSpec::ReduceToMatcher do
75 before(:each) do
f0b178a @andymorris Rename docs/keys to keys/values to match couchdb documentation and conve...
andymorris authored
76 @view_spec = stub(:reduce_function => "function(keys, values, rereduce) {
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
77 if(rereduce) {
f0b178a @andymorris Rename docs/keys to keys/values to match couchdb documentation and conve...
andymorris authored
78 return(sum(values) * 2);
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
79 } else {
f0b178a @andymorris Rename docs/keys to keys/values to match couchdb documentation and conve...
andymorris authored
80 return(sum(values));
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
81 };
82 }")
83 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
84
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
85 it "should pass if the given function return the expected javascript" do
86 @view_spec.should reduce([], [1, 2, 3]).to(6)
87 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
88
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
89 it "should not pass if the given function returns different javascript" do
90 @view_spec.should_not reduce([], [1, 2, 3]).to(7)
91 end
b60c41c @andymorris Replace print_r with JSON.stringify to handle dates in JS runtime specs
andymorris authored
92
93 it "should work with date return values" do
94 spec = stub(:reduce_function => "function() { return new Date(1368802800000); }")
95 spec.should reduce([], []).to("2013-05-17T15:00:00.000Z")
96 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
97
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
98 describe "rereduce" do
99 it "should pass if the given function return the expected javascript" do
100 @view_spec.should rereduce([], [1, 2, 3]).to(12)
101 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
102
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
103 it "should not pass if the given function returns different javascript" do
104 @view_spec.should_not rereduce([], [1, 2, 3]).to(13)
105 end
106 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
107
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
108 describe 'failing specs' do
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
109
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
110 it "should have a nice error message for failing should" do
111 lambda {
112 @view_spec.should reduce([], [1, 2, 3]).to(7)
113 }.should raise_error('Expected to reduce to 7 but got 6.')
114 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
115
d0fd1ef @langalex added support for unit testing map/reduce functions in views
authored
116 it "should have a nice error message for failing should not" do
117 lambda {
118 @view_spec.should_not reduce([], [1, 2, 3]).to(6)
119 }.should raise_error('Expected not to reduce to 6 but did.')
120 end
121 end
e383ce2 @langalex support for testing list functions
authored
122 end
123
36b7d43 @andymorris Add MapReduceToMatcher to support ViewSpec specs covering the combinatio...
andymorris authored
124 describe CouchPotato::RSpec::MapReduceToMatcher do
125 before(:each) do
126 @view_spec = stub(
127 :map_function => "function(doc) {
128 for (var i in doc.numbers)
129 emit([doc.age, doc.name], doc.numbers[i]);
130 }",
131 :reduce_function => "function (keys, values, rereduce) {
132 return Math.max.apply(this, values);
c672918 @erichaberkorn added specs for the map/reduce matchers with commonJS and made a few var...
erichaberkorn authored
133 }"
134 )
36b7d43 @andymorris Add MapReduceToMatcher to support ViewSpec specs covering the combinatio...
andymorris authored
135 @docs = [
136 {:name => "a", :age => 25, :numbers => [1, 2]},
137 {:name => "b", :age => 25, :numbers => [3, 4]},
138 {:name => "c", :age => 26, :numbers => [5, 6]},
139 {:name => "d", :age => 27, :numbers => [7, 8]}]
140 end
141
d3a0f8e @andymorris Correctly handle Javascript Dates in MapReduceToMatcher
andymorris authored
142 it "should handle date return values" do
143 spec = stub(:map_function => "function() { emit(null, null); }",
144 :reduce_function => "function() { return new Date(1368802800000); }")
145 spec.should map_reduce({}).to({"key" => nil, "value" => "2013-05-17T15:00:00.000Z"})
146 end
147
ca73408 @andymorris Make specs handle CommonJS modules via module.exports as well as exports
andymorris authored
148 it "should handle CommonJS requires for modules that use 'exports'" do
c672918 @erichaberkorn added specs for the map/reduce matchers with commonJS and made a few var...
erichaberkorn authored
149 spec = stub(
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
150 :map_function => "function() { var test = require('views/lib/test'); emit(null, test.test); }",
c672918 @erichaberkorn added specs for the map/reduce matchers with commonJS and made a few var...
erichaberkorn authored
151 :reduce_function => "function(keys, values) { return 'test' }",
152 :lib => {:test => "exports.test = 'test'"})
153 spec.should map_reduce({}).to({"key" => nil, "value" => "test"})
154 end
155
ca73408 @andymorris Make specs handle CommonJS modules via module.exports as well as exports
andymorris authored
156 it "should handle CommonJS requires for modules that use 'module.exports'" do
157 spec = stub(
158 :map_function => "function() { var test = require('views/lib/test'); emit(null, test.test); }",
159 :reduce_function => "function(keys, values) { return 'test' }",
160 :lib => {:test => "module.exports.test = 'test'"})
161 spec.should map_reduce({}).to({"key" => nil, "value" => "test"})
162 end
163
4729a0a @annismckenzie Added a test for the missing `sum` function.
annismckenzie authored
164 it "should handle sum function" do
165 spec = stub(
166 :map_function => "function(doc) { emit(null, doc.age); }",
167 :reduce_function => "function(keys, values) { return sum(values); }")
168 spec.should map_reduce(@docs).to({"key" => nil, "value" => 103})
169 end
170
36b7d43 @andymorris Add MapReduceToMatcher to support ViewSpec specs covering the combinatio...
andymorris authored
171 context "without grouping" do
172 it "should not group by key by default" do
173 @view_spec.should map_reduce(@docs).to({"key" => nil, "value" => 8})
174 end
175
176 it "should group by key with :group => false" do
177 @view_spec.should map_reduce(@docs).with_options(:group => false).to({"key" => nil, "value" => 8})
178 end
179 end
180
181 context "with grouping" do
182 [true, "exact"].each do |group_value|
183 it "should group by the full key with option :group => #{group_value}" do
184 @view_spec.should map_reduce(@docs).with_options(:group => group_value).to(
185 {"key" => [25, "a"], "value" => 2},
186 {"key" => [25, "b"], "value" => 4},
187 {"key" => [26, "c"], "value" => 6},
188 {"key" => [27, "d"], "value" => 8})
189 end
190 end
191
192 it "should group by parts of the keys based on the :group_level option" do
193 @view_spec.should map_reduce(@docs).with_options(:group_level => 1).to(
194 {"key" => [25], "value" => 4},
195 {"key" => [26], "value" => 6},
196 {"key" => [27], "value" => 8})
197 end
198 end
199
200 describe "rereducing" do
201 before :each do
202 @view_spec = stub(:map_function => "function(doc) {
203 emit(doc.name, doc.number);
204 }",
205 :reduce_function => "function (keys, values, rereduce) {
206 if (rereduce) {
207 var result = {rereduce_values: []};
208 for (var v in values) {
209 result.rereduce_values.push(values[v].reduce_values);
210 }
211 return result;
212 }
213 return {reduce_values: values};
214 }")
215 end
216
217 it "should reduce and rereduce for a single emit" do
218 @view_spec.should map_reduce({:name => "a", :number => 1}).to({"key" => nil, "value" => {"rereduce_values" => [[1]]}})
219 end
220
221 it "should split and reduce each half of emitted values separately and rereduce the results" do
222 docs = [
223 {:name => "a", :number => 1},
224 {:name => "a", :number => 2},
225 {:name => "a", :number => 3},
226 {:name => "a", :number => 4}]
227 @view_spec.should map_reduce(docs).to({"key" => nil, "value" => {"rereduce_values" => [[1, 2], [3, 4]]}})
228 end
229
230 it "should correctly split and rereduce with an odd number of emits" do
231 docs = [
232 {:name => "a", :number => 1},
233 {:name => "a", :number => 2},
234 {:name => "a", :number => 3}]
235 @view_spec.should map_reduce(docs).to({"key" => nil, "value" => {"rereduce_values" => [[1], [2, 3]]}})
236 end
237 end
238
239 describe "failing specs" do
240 it "should have a nice error message for failing should" do
241 lambda {
242 @view_spec.should map_reduce(@docs).with_options(:group => false).to({"key" => nil, "value" => 9})
243 }.should raise_error('Expected to map/reduce to [{"key"=>nil, "value"=>9}] but got [{"key"=>nil, "value"=>8}].')
244 end
245
246 it "should have a nice error message for failing should not" do
247 lambda {
248 @view_spec.should_not map_reduce(@docs).with_options(:group => false).to({"key" => nil, "value" => 8})
249 }.should raise_error('Expected not to map/reduce to [{"key"=>nil, "value"=>8}] but did.')
250 end
251 end
694af21 @andymorris Add support for built-in couchdb reduce functions in map reduce specs
andymorris authored
252
253 describe "couchdb built-in reduce functions" do
254 describe "_sum" do
255 it "should return the sum of emitted values" do
256 spec = stub(:map_function => @view_spec.map_function, :reduce_function => "_sum")
257 spec.should map_reduce(@docs).to({"key" => nil, "value" => 36})
258 end
259 end
260
261 describe "_count" do
262 it "should return the count of emitted values" do
263 spec = stub(:map_function => @view_spec.map_function, :reduce_function => "_count")
264 spec.should map_reduce(@docs).to({"key" => nil, "value" => 8})
265 end
266 end
267
268 describe "_stats" do
269 it "should return the numerical statistics of emitted values" do
270 spec = stub(:map_function => @view_spec.map_function, :reduce_function => "_stats")
271 spec.should map_reduce(@docs).to({
272 "key" => nil,
273 "value" => {
274 "sum" => 36,
275 "count" => 8,
276 "min" => 1,
277 "max" => 8,
278 "sumsqr" => 204
279 }
280 })
281 end
282 end
283 end
36b7d43 @andymorris Add MapReduceToMatcher to support ViewSpec specs covering the combinatio...
andymorris authored
284 end
285
e383ce2 @langalex support for testing list functions
authored
286 describe CouchPotato::RSpec::ListAsMatcher do
287 before(:each) do
288 @view_spec = stub(:list_function => "function() {var row = getRow(); send(JSON.stringify([{text: row.text + ' world'}]));}")
289 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
290
e383ce2 @langalex support for testing list functions
authored
291 it "should pass if the function return the expected json" do
292 @view_spec.should list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello world'}])
293 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
294
e383ce2 @langalex support for testing list functions
authored
295 it "should not pass if the function does not return the expected json" do
296 @view_spec.should_not list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello there'}])
297 end
b60c41c @andymorris Replace print_r with JSON.stringify to handle dates in JS runtime specs
andymorris authored
298
299 it "should work with date values" do
300 spec = stub(:list_function => "function() { send(JSON.stringify([{date: new Date(1368802800000)}])); }")
301 spec.should list({"rows" => [{}]}).as([{"date" => "2013-05-17T15:00:00.000Z"}])
302 end
303
e383ce2 @langalex support for testing list functions
authored
304 describe "failing specs" do
305 it "should have a nice error message for failing should" do
306 lambda {
307 @view_spec.should list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello there'}])
308 }.should raise_error('Expected to list as [{"text"=>"hello there"}] but got [{"text"=>"hello world"}].')
309 end
e977809 @erichaberkorn made commonJS work for CouchPotato::Config.split_design_documents_per_vi...
erichaberkorn authored
310
e383ce2 @langalex support for testing list functions
authored
311 it "should have a nice error message for failing should not" do
312 lambda {
313 @view_spec.should_not list({'rows' => [{:text => 'hello'}]}).as([{'text' => 'hello world'}])
314 }.should raise_error('Expected to not list as [{"text"=>"hello world"}] but did.')
315 end
316 end
b60c41c @andymorris Replace print_r with JSON.stringify to handle dates in JS runtime specs
andymorris authored
317 end
Something went wrong with that request. Please try again.