Class: CouchPotato::RSpec::MapReduceToMatcher

Inherits:
Object
  • Object
show all
Includes:
RunJS
Defined in:
lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(expected_ruby, input_ruby, options) ⇒ MapReduceToMatcher

Returns a new instance of MapReduceToMatcher



23
24
25
26
27
# File 'lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb', line 23

def initialize(expected_ruby, input_ruby, options)
  @expected_ruby = expected_ruby
  @input_ruby = input_ruby
  @options = options
end

Instance Method Details

#failure_message_for_shouldObject



157
158
159
# File 'lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb', line 157

def failure_message_for_should
  "Expected to map/reduce to #{@expected_ruby.inspect} but got #{@actual_ruby.inspect}."
end

#failure_message_for_should_notObject



161
162
163
# File 'lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb', line 161

def failure_message_for_should_not
  "Expected not to map/reduce to #{@actual_ruby.inspect} but did."
end

#matches?(view_spec) ⇒ Boolean

Returns:

  • (Boolean)


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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/couch_potato/rspec/matchers/map_reduce_to_matcher.rb', line 29

def matches?(view_spec)
  js = "var sum = function(values) {\nreturn values.reduce(function(memo, value) { return memo + value; });\n};\n// Equivalents of couchdb built-in reduce functions whose names can be\n// given as the reduce function in the view_spec:\nvar _sum = function(keys, values, rereduce) {\nreturn sum(values);\n}\nvar _count = function(keys, values, rereduce) {\nif (rereduce) {\nreturn sum(values);\n} else {\nreturn values.length;\n}\n}\nvar _stats = function(keys, values, rereduce) {\nvar result = {sum: 0, count: 0, min: Number.MAX_VALUE, max: Number.MIN_VALUE, sumsqr: 0};\nif (rereduce) {\nfor (var i in values) {\nvar value = values[i];\nresult.sum += value.sum;\nresult.count += value.count;\nresult.min = Math.min(result.min, value.min);\nresult.max = Math.max(result.max, value.max);\nresult.sumsqr += value.sumsqr;\n}\n} else {\nfor (var i in values) {\nvar value = values[i];\nresult.sum += value;\nresult.count += 1;\nresult.min = Math.min(result.min, value);\nresult.max = Math.max(result.max, value);\nresult.sumsqr += Math.pow(value, 2);\n}\n}\nreturn result;\n}\n\nvar docs = \#{@input_ruby.to_json};\nvar options = \#{@options.to_json};\nvar map = \#{view_spec.map_function};\nvar reduce = \#{view_spec.reduce_function};\nvar lib = \#{view_spec.respond_to?(:lib) && view_spec.lib.to_json};\n\n// Map the input docs\nvar require = function(modulePath) {\nvar module = {exports: {}};\nvar exports = module.exports;\nvar pathArray = modulePath.split(\"/\").slice(2);\nvar result = lib;\nfor (var i in pathArray) {\nresult = result[pathArray[i]];\n}\neval(result);\nreturn module.exports;\n}\n\nvar mapResults = [];\nvar emit = function(key, value) {\nmapResults.push({key: key, value: value});\n};\nfor (var i in docs) {\nmap(docs[i]);\n}\n\n// Group the map results, honoring the group and group_level options\nvar grouped = [];\nif (options.group || options.group_level) {\nvar groupLevel = options.group_level;\nif (groupLevel == \"exact\" || options.group == true)\ngroupLevel = 9999;\nvar keysEqual = function(a, b) { return !(a < b || b < a); }\n\nfor (var mr in mapResults) {\nvar mapResult = mapResults[mr];\nvar groupedKey = mapResult.key.slice(0, groupLevel);\nvar groupFound = false;\nfor (var g in grouped) {\nvar group = grouped[g];\nif (keysEqual(groupedKey, group.groupedKey)) {\ngroup.keys.push(mapResult.key);\ngroup.values.push(mapResult.value);\ngroupFound = true;\nbreak;\n}\n}\n\nif (!groupFound)\ngrouped.push({keys: [mapResult.key], groupedKey: groupedKey, values: [mapResult.value]});\n}\n} else {\nvar group = {keys: null, groupedKey: null, values: []};\nfor (var mr in mapResults)\ngroup.values.push(mapResults[mr].value);\ngrouped.push(group);\n}\n\n// Reduce the grouped map results\nvar results = [];\nfor (var g in grouped) {\nvar group = grouped[g], reduced = null;\nif (group.values.length >= 2) {\n// Split the values into two parts, reduce each part, then rereduce those results\nvar mid = parseInt(group.values.length / 2);\nvar keys1 = (group.keys || []).slice(0, mid),\nvalues1 = group.values.slice(0, mid);\nvar reduced1 = reduce(keys1, values1, false);\nvar keys2 = (group.keys || []).slice(mid, group.values.length),\nvalues2 = group.values.slice(mid, group.values.length);\nvar reduced2 = reduce(keys2, values2, false);\nreduced = reduce(null, [reduced1, reduced2], true);\n} else {\n// Not enough values to split, so just reduce, and then rereduce the single result\nreduced = reduce(group.keys, group.values, false);\nreduced = reduce(null, [reduced], true);\n}\nresults.push({key: group.groupedKey, value: reduced});\n}\n\nJSON.stringify(results);\n"
  @actual_ruby = JSON.parse(run_js(js))
  @expected_ruby == @actual_ruby
end