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
|
defmodule ViewTest do
use CouchTestCase
@moduletag :view
@moduledoc """
Test CouchDB /{db}/_design/{ddoc}/_view/{view}
"""
setup_all do
db_name = random_db_name()
{:ok, _} = create_db(db_name)
on_exit(fn -> delete_db(db_name) end)
{:ok, _} = create_doc(
db_name,
%{
_id: "foo",
bar: "baz"
}
)
{:ok, _} = create_doc(
db_name,
%{
_id: "foo2",
bar: "baz2"
}
)
map_fun = """
function(doc) {
emit(doc._id, doc.bar);
}
"""
body = %{
:docs => [
%{
_id: "_design/map",
views: %{
some: %{
map: map_fun
}
}
}
]
}
resp = Couch.post("/#{db_name}/_bulk_docs", body: body)
Enum.each(resp.body, &assert(&1["ok"]))
{:ok, [db_name: db_name]}
end
test "GET with no parameters", context do
resp = Couch.get(
"/#{context[:db_name]}/_design/map/_view/some"
)
assert resp.status_code == 200
assert length(Map.get(resp, :body)["rows"]) == 2
end
test "GET with one key", context do
resp = Couch.get(
"/#{context[:db_name]}/_design/map/_view/some",
query: %{
:key => "\"foo\"",
}
)
assert resp.status_code == 200
assert length(Map.get(resp, :body)["rows"]) == 1
end
test "GET with multiple keys", context do
resp = Couch.get(
"/#{context[:db_name]}/_design/map/_view/some",
query: %{
:keys => "[\"foo\", \"foo2\"]",
}
)
assert resp.status_code == 200
assert length(Map.get(resp, :body)["rows"]) == 2
end
test "POST with empty body", context do
resp = Couch.post(
"/#{context[:db_name]}/_design/map/_view/some",
body: %{}
)
assert resp.status_code == 200
assert length(Map.get(resp, :body)["rows"]) == 2
end
test "POST with keys and limit", context do
resp = Couch.post(
"/#{context[:db_name]}/_design/map/_view/some",
body: %{
:keys => ["foo", "foo2"],
:limit => 1
}
)
assert resp.status_code == 200
assert length(Map.get(resp, :body)["rows"]) == 1
end
test "POST with query parameter and JSON body", context do
resp = Couch.post(
"/#{context[:db_name]}/_design/map/_view/some",
query: %{
:limit => 1
},
body: %{
:keys => ["foo", "foo2"]
}
)
assert resp.status_code == 200
assert length(Map.get(resp, :body)["rows"]) == 1
end
test "POST edge case with colliding parameters - query takes precedence", context do
resp = Couch.post(
"/#{context[:db_name]}/_design/map/_view/some",
query: %{
:limit => 1
},
body: %{
:keys => ["foo", "foo2"],
:limit => 2
}
)
assert resp.status_code == 200
assert length(Map.get(resp, :body)["rows"]) == 1
end
test "POST with boolean parameter", context do
resp = Couch.post(
"/#{context[:db_name]}/_design/map/_view/some",
body: %{
:stable => true,
:update => true
}
)
assert resp.status_code == 200
end
end
|