1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
defmodule ViewPartitionTest do
use CouchTestCase
import PartitionHelpers
@moduledoc """
Test Partition functionality for views
"""
setup_all do
db_name = random_db_name()
{:ok, _} = create_db(db_name, query: %{partitioned: true, q: 1})
on_exit(fn -> delete_db(db_name) end)
create_partition_docs(db_name)
map_fun1 = """
function(doc) {
if (doc.some) {
emit(doc.value, doc.some);
}
}
"""
map_fun2 = """
function(doc) {
if (doc.group) {
emit([doc.some, doc.group], 1);
}
}
"""
query = %{:w => 3}
body = %{
:docs => [
%{
_id: "_design/map",
views: %{some: %{map: map_fun1}}
},
%{
_id: "_design/map_some",
views: %{some: %{map: map_fun2}}
},
%{
_id: "_design/partitioned_true",
views: %{some: %{map: map_fun1}},
options: %{partitioned: true}
},
%{
_id: "_design/partitioned_false",
views: %{some: %{map: map_fun1}},
options: %{partitioned: false}
},
%{
_id: "_design/reduce",
views: %{some: %{map: map_fun2, reduce: "_count"}}
},
%{
_id: "_design/include_ddocs",
views: %{some: %{map: map_fun1}},
options: %{include_design: true}
}
]
}
resp = Couch.post("/#{db_name}/_bulk_docs", query: query, body: body)
Enum.each(resp.body, &assert(&1["ok"]))
{:ok, [db_name: db_name]}
end
def get_reduce_result(resp) do
%{:body => %{"rows" => rows}} = resp
rows
end
test "query with partitioned:true returns partitioned fields", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/partitioned_true/_view/some"
resp = Couch.get(url)
assert resp.status_code == 200
partitions = get_partitions(resp)
assert Enum.dedup(partitions) == ["foo"]
url = "/#{db_name}/_partition/bar/_design/partitioned_true/_view/some"
resp = Couch.get(url)
assert resp.status_code == 200
partitions = get_partitions(resp)
assert Enum.dedup(partitions) == ["bar"]
end
test "default view query returns partitioned fields", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.get(url)
assert resp.status_code == 200
partitions = get_partitions(resp)
assert Enum.dedup(partitions) == ["foo"]
url = "/#{db_name}/_partition/bar/_design/map/_view/some"
resp = Couch.get(url)
assert resp.status_code == 200
partitions = get_partitions(resp)
assert Enum.dedup(partitions) == ["bar"]
end
test "query will return zero results for wrong inputs", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.get(url, query: %{start_key: "\"foo:12\""})
assert resp.status_code == 200
assert Map.get(resp, :body)["rows"] == []
end
test "partitioned ddoc cannot be used in global query", context do
db_name = context[:db_name]
url = "/#{db_name}/_design/map/_view/some"
resp = Couch.get(url)
%{:body => %{"reason" => reason}} = resp
assert resp.status_code == 400
assert Regex.match?(~r/mandatory for queries to this view./, reason)
end
test "partitioned query cannot be used with global ddoc", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/partitioned_false/_view/some"
resp = Couch.get(url)
%{:body => %{"reason" => reason}} = resp
assert resp.status_code == 400
assert Regex.match?(~r/is not supported in this design doc/, reason)
end
test "view query returns all docs for global query", context do
db_name = context[:db_name]
url = "/#{db_name}/_design/partitioned_false/_view/some"
resp = Couch.get(url)
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 100
end
test "partition query errors with incorrect partition supplied", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/_bar/_design/map/_view/some"
resp = Couch.get(url)
assert resp.status_code == 400
url = "/#{db_name}/_partition//_design/map/_view/some"
resp = Couch.get(url)
assert resp.status_code == 400
end
test "partitioned query works with startkey, endkey range", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.get(url, query: %{start_key: 12, end_key: 20})
assert resp.status_code == 200
partitions = get_partitions(resp)
assert length(partitions) == 5
assert Enum.dedup(partitions) == ["foo"]
end
test "partitioned query works with keys", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.post(url, body: %{keys: [2, 4, 6]})
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 3
assert ids == ["foo:2", "foo:4", "foo:6"]
end
test "global query works with keys", context do
db_name = context[:db_name]
url = "/#{db_name}/_design/partitioned_false/_view/some"
resp = Couch.post(url, body: %{keys: [2, 4, 6]})
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 3
assert ids == ["foo:2", "foo:4", "foo:6"]
end
test "partition query works with limit", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.get(url, query: %{limit: 5})
assert resp.status_code == 200
partitions = get_partitions(resp)
assert length(partitions) == 5
assert Enum.dedup(partitions) == ["foo"]
end
test "partition query with descending", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.get(url, query: %{descending: true, limit: 5})
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 5
assert ids == ["foo:100", "foo:98", "foo:96", "foo:94", "foo:92"]
resp = Couch.get(url, query: %{descending: false, limit: 5})
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 5
assert ids == ["foo:2", "foo:4", "foo:6", "foo:8", "foo:10"]
end
test "partition query with skip", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.get(url, query: %{skip: 5, limit: 5})
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 5
assert ids == ["foo:12", "foo:14", "foo:16", "foo:18", "foo:20"]
end
test "partition query with key", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map/_view/some"
resp = Couch.get(url, query: %{key: 22})
assert resp.status_code == 200
ids = get_ids(resp)
assert length(ids) == 1
assert ids == ["foo:22"]
end
test "partition query with startkey_docid and endkey_docid", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/map_some/_view/some"
resp =
Couch.get(url,
query: %{
startkey: "[\"field\",\"one\"]",
endkey: "[\"field\",\"one\"]",
startkey_docid: "foo:12",
endkey_docid: "foo:30"
}
)
assert resp.status_code == 200
ids = get_ids(resp)
assert ids == ["foo:12", "foo:18", "foo:24", "foo:30"]
end
test "query with reduce works", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/reduce/_view/some"
resp = Couch.get(url, query: %{reduce: true, group_level: 1})
assert resp.status_code == 200
results = get_reduce_result(resp)
assert results == [%{"key" => ["field"], "value" => 50}]
resp = Couch.get(url, query: %{reduce: true, group_level: 2})
results = get_reduce_result(resp)
assert results == [
%{"key" => ["field", "one"], "value" => 16},
%{"key" => ["field", "two"], "value" => 34}
]
resp = Couch.get(url, query: %{reduce: true, group: true})
results = get_reduce_result(resp)
assert results == [
%{"key" => ["field", "one"], "value" => 16},
%{"key" => ["field", "two"], "value" => 34}
]
end
test "include_design works correctly", context do
db_name = context[:db_name]
url = "/#{db_name}/_partition/foo/_design/include_ddocs/_view/some"
resp = Couch.get(url)
assert resp.status_code == 200
partitions = get_partitions(resp)
assert length(partitions) == 50
assert Enum.dedup(partitions) == ["foo"]
end
end
|