couch_potato 0.2.13 → 0.2.14
Sign up to get free protection for your applications and to get access to all the features.
data/README.md
CHANGED
@@ -293,9 +293,11 @@ Couch Potato provides custom RSpec matchers for testing the map and reduce funct
|
|
293
293
|
end
|
294
294
|
|
295
295
|
#RSpec
|
296
|
+
require 'couch_potato/rspec/matchers'
|
297
|
+
|
296
298
|
describe User, 'by_name' do
|
297
299
|
it "should map users to their name" do
|
298
|
-
User.by_name.should map({:name => 'bill', :age => 23}).to([
|
300
|
+
User.by_name.should map({:name => 'bill', :age => 23}).to(['bill', null])
|
299
301
|
end
|
300
302
|
|
301
303
|
it "should reduce the users to the sum of their age" do
|
@@ -307,7 +309,7 @@ Couch Potato provides custom RSpec matchers for testing the map and reduce funct
|
|
307
309
|
end
|
308
310
|
end
|
309
311
|
|
310
|
-
This will actually run your map/reduce functions in a JavaScript interpreter, passing the arguments as JSON and converting the results back to Ruby.
|
312
|
+
This will actually run your map/reduce functions in a JavaScript interpreter, passing the arguments as JSON and converting the results back to Ruby. For more examples see the [spec](http://github.com/langalex/couch_potato/blob/master/spec/unit/rspec_matchers_spec.rb).
|
311
313
|
|
312
314
|
In order for this to work you must have the `js` executable in your PATH. This is usually part of the _spidermonkey_ package/port. (On MacPorts that's _spidemonkey_, on Linux it could be one of _libjs_, _libmozjs_ or _libspidermonkey_). When you installed CouchDB via your packet manager Spidermonkey should already be there.
|
313
315
|
|
data/VERSION.yml
CHANGED
@@ -1,41 +1,14 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
-
module Spec
|
4
|
-
module Matchers
|
5
|
-
def map(document)
|
6
|
-
CouchPotato::RSpec::MapToProxy.new(document)
|
7
|
-
end
|
8
|
-
|
9
|
-
def reduce(docs, keys)
|
10
|
-
CouchPotato::RSpec::ReduceToProxy.new(docs, keys)
|
11
|
-
end
|
12
|
-
|
13
|
-
def rereduce(docs, keys)
|
14
|
-
CouchPotato::RSpec::ReduceToProxy.new(docs, keys, true)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
3
|
|
19
4
|
module CouchPotato
|
20
5
|
module RSpec
|
21
|
-
|
22
|
-
module RunJS
|
23
|
-
private
|
24
|
-
|
25
|
-
def run_js(js)
|
26
|
-
path = 'couch_potato_js_runner.js'
|
27
|
-
File.open(path, 'w') {|f| f << js}
|
28
|
-
`js #{path}`
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
|
33
6
|
class MapToProxy
|
34
7
|
def initialize(input_ruby)
|
35
8
|
@input_ruby = input_ruby
|
36
9
|
end
|
37
10
|
|
38
|
-
def to(expected_ruby)
|
11
|
+
def to(*expected_ruby)
|
39
12
|
MapToMatcher.new(expected_ruby, @input_ruby)
|
40
13
|
end
|
41
14
|
end
|
@@ -72,54 +45,5 @@ module CouchPotato
|
|
72
45
|
"Expected not to map to #{@actual_ruby.inspect} but did."
|
73
46
|
end
|
74
47
|
end
|
75
|
-
|
76
|
-
class ReduceToProxy
|
77
|
-
def initialize(docs, keys, rereduce = false)
|
78
|
-
@docs, @keys, @rereduce = docs, keys, rereduce
|
79
|
-
end
|
80
|
-
|
81
|
-
def to(expected_ruby)
|
82
|
-
ReduceToMatcher.new(expected_ruby, @docs, @keys, @rereduce)
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
class ReduceToMatcher
|
87
|
-
include RunJS
|
88
|
-
|
89
|
-
def initialize(expected_ruby, docs, keys, rereduce = false)
|
90
|
-
@expected_ruby, @docs, @keys, @rereduce = expected_ruby, docs, keys, rereduce
|
91
|
-
end
|
92
|
-
|
93
|
-
def matches?(view_spec)
|
94
|
-
js = <<-JS
|
95
|
-
#{File.read(File.dirname(__FILE__) + '/print_r.js')}
|
96
|
-
|
97
|
-
sum = function(values) {
|
98
|
-
var rv = 0;
|
99
|
-
for (var i in values) {
|
100
|
-
rv += values[i];
|
101
|
-
}
|
102
|
-
return rv;
|
103
|
-
};
|
104
|
-
|
105
|
-
var docs = #{@docs.to_json};
|
106
|
-
var keys = #{@keys.to_json};
|
107
|
-
var reduce = #{view_spec.reduce_function};
|
108
|
-
print(print_r({result: reduce(docs, keys, #{@rereduce})}));
|
109
|
-
JS
|
110
|
-
@actual_ruby = JSON.parse(run_js(js))['result']
|
111
|
-
@expected_ruby == @actual_ruby
|
112
|
-
end
|
113
|
-
|
114
|
-
def failure_message_for_should
|
115
|
-
"Expected to reduce to #{@expected_ruby.inspect} but got #{@actual_ruby.inspect}."
|
116
|
-
end
|
117
|
-
|
118
|
-
def failure_message_for_should_not
|
119
|
-
"Expected not to reduce to #{@actual_ruby.inspect} but did."
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
48
|
end
|
125
49
|
end
|
@@ -1,6 +1,50 @@
|
|
1
1
|
module CouchPotato
|
2
2
|
module RSpec
|
3
|
+
class ReduceToProxy
|
4
|
+
def initialize(docs, keys, rereduce = false)
|
5
|
+
@docs, @keys, @rereduce = docs, keys, rereduce
|
6
|
+
end
|
7
|
+
|
8
|
+
def to(expected_ruby)
|
9
|
+
ReduceToMatcher.new(expected_ruby, @docs, @keys, @rereduce)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
3
13
|
class ReduceToMatcher
|
14
|
+
include RunJS
|
15
|
+
|
16
|
+
def initialize(expected_ruby, docs, keys, rereduce = false)
|
17
|
+
@expected_ruby, @docs, @keys, @rereduce = expected_ruby, docs, keys, rereduce
|
18
|
+
end
|
19
|
+
|
20
|
+
def matches?(view_spec)
|
21
|
+
js = <<-JS
|
22
|
+
#{File.read(File.dirname(__FILE__) + '/print_r.js')}
|
23
|
+
|
24
|
+
sum = function(values) {
|
25
|
+
var rv = 0;
|
26
|
+
for (var i in values) {
|
27
|
+
rv += values[i];
|
28
|
+
}
|
29
|
+
return rv;
|
30
|
+
};
|
31
|
+
|
32
|
+
var docs = #{@docs.to_json};
|
33
|
+
var keys = #{@keys.to_json};
|
34
|
+
var reduce = #{view_spec.reduce_function};
|
35
|
+
print(print_r({result: reduce(docs, keys, #{@rereduce})}));
|
36
|
+
JS
|
37
|
+
@actual_ruby = JSON.parse(run_js(js))['result']
|
38
|
+
@expected_ruby == @actual_ruby
|
39
|
+
end
|
40
|
+
|
41
|
+
def failure_message_for_should
|
42
|
+
"Expected to reduce to #{@expected_ruby.inspect} but got #{@actual_ruby.inspect}."
|
43
|
+
end
|
44
|
+
|
45
|
+
def failure_message_for_should_not
|
46
|
+
"Expected not to reduce to #{@actual_ruby.inspect} but did."
|
47
|
+
end
|
4
48
|
end
|
5
49
|
end
|
6
50
|
end
|
@@ -1,2 +1,34 @@
|
|
1
|
+
module CouchPotato
|
2
|
+
module RSpec
|
3
|
+
module RunJS
|
4
|
+
private
|
5
|
+
|
6
|
+
def run_js(js)
|
7
|
+
path = 'couch_potato_js_runner.js'
|
8
|
+
File.open(path, 'w') {|f| f << js}
|
9
|
+
`js #{path}`
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
|
1
16
|
require File.dirname(__FILE__) + '/matchers/map_to_matcher'
|
2
|
-
require File.dirname(__FILE__) + '/matchers/reduce_to_matcher'
|
17
|
+
require File.dirname(__FILE__) + '/matchers/reduce_to_matcher'
|
18
|
+
|
19
|
+
module Spec
|
20
|
+
module Matchers
|
21
|
+
def map(document)
|
22
|
+
CouchPotato::RSpec::MapToProxy.new(document)
|
23
|
+
end
|
24
|
+
|
25
|
+
def reduce(docs, keys)
|
26
|
+
CouchPotato::RSpec::ReduceToProxy.new(docs, keys)
|
27
|
+
end
|
28
|
+
|
29
|
+
def rereduce(docs, keys)
|
30
|
+
CouchPotato::RSpec::ReduceToProxy.new(docs, keys, true)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -10,11 +10,11 @@ describe CouchPotato::RSpec::MapToMatcher do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
it "should pass if the given function emits the expected javascript" do
|
13
|
-
@view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to([
|
13
|
+
@view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2])
|
14
14
|
end
|
15
15
|
|
16
16
|
it "should not pass if the given function emits different javascript" do
|
17
|
-
@view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to([
|
17
|
+
@view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 3])
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
@@ -24,11 +24,11 @@ describe CouchPotato::RSpec::MapToMatcher do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "should pass if the given function emits the expected javascript" do
|
27
|
-
@view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to([
|
27
|
+
@view_spec.should map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2], ['person', 'male'])
|
28
28
|
end
|
29
29
|
|
30
30
|
it "should return false if the given function emits different javascript" do
|
31
|
-
@view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to([
|
31
|
+
@view_spec.should_not map({:name => 'horst', :tags => ['person', 'male']}).to(['horst', 2], ['male', 'person'])
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
@@ -39,13 +39,13 @@ describe CouchPotato::RSpec::MapToMatcher do
|
|
39
39
|
|
40
40
|
it "should have a nice error message for failing should" do
|
41
41
|
lambda {
|
42
|
-
@view_spec.should map({:name => 'bill'}).to([
|
42
|
+
@view_spec.should map({:name => 'bill'}).to(['linus', nil])
|
43
43
|
}.should raise_error('Expected to map to [["linus", nil]] but got [["bill", nil]].')
|
44
44
|
end
|
45
45
|
|
46
46
|
it "should have a nice error message for failing should not" do
|
47
47
|
lambda {
|
48
|
-
@view_spec.should_not map({:name => 'bill'}).to([
|
48
|
+
@view_spec.should_not map({:name => 'bill'}).to(['bill', nil])
|
49
49
|
}.should raise_error('Expected not to map to [["bill", nil]] but did.')
|
50
50
|
end
|
51
51
|
end
|