Skip to content

Commit 7fd9ab0

Browse files
committed
Port purge.js into elixir test suite
1 parent e90bb1d commit 7fd9ab0

File tree

3 files changed

+170
-2
lines changed

3 files changed

+170
-2
lines changed

test/elixir/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ X means done, - means partially
6161
- [X] Port method_override.js
6262
- [X] Port multiple_rows.js
6363
- [X] Port proxyauth.js
64-
- [ ] Port purge.js
64+
- [X] Port purge.js
6565
- [ ] Port reader_acl.js
6666
- [ ] Port recreate_doc.js
6767
- [ ] Port reduce_builtin.js

test/elixir/test/purge_test.exs

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
defmodule PurgeTest do
2+
use CouchTestCase
3+
4+
@moduletag :purge
5+
6+
@tag :with_db
7+
test "purge documents", context do
8+
db_name = context[:db_name]
9+
10+
design_doc = %{
11+
_id: "_design/test",
12+
language: "javascript",
13+
views: %{
14+
all_docs_twice: %{
15+
map: "function(doc) { emit(doc.integer, null); emit(doc.integer, null) }"
16+
},
17+
single_doc: %{
18+
map: "function(doc) { if (doc._id == \"1\") { emit(1, null) }}"
19+
}
20+
}
21+
}
22+
23+
{:ok, _} = create_doc(db_name, design_doc)
24+
25+
num_docs = 10
26+
bulk_save(db_name, make_docs(1..(num_docs + 1)))
27+
28+
test_all_docs_twice(db_name, num_docs, 1)
29+
30+
info = info(db_name)
31+
32+
doc1 = open_doc(db_name, 1)
33+
doc2 = open_doc(db_name, 2)
34+
35+
resp =
36+
Couch.post("/#{db_name}/_purge",
37+
body: %{"1": [doc1["_rev"]], "2": [doc2["_rev"]]}
38+
)
39+
40+
assert resp.status_code == 201
41+
result = resp.body
42+
43+
assert Enum.at(result["purged"]["1"], 0) == doc1["_rev"]
44+
assert Enum.at(result["purged"]["2"], 0) == doc2["_rev"]
45+
46+
open_doc(db_name, 1, 404)
47+
open_doc(db_name, 2, 404)
48+
49+
purged_info = info(db_name)
50+
51+
assert purged_info["purge_seq"] != info["purge_seq"]
52+
53+
test_all_docs_twice(db_name, num_docs, 0, 2)
54+
55+
# purge sequences are preserved after compaction (COUCHDB-1021)
56+
resp = Couch.post("/#{db_name}/_compact")
57+
assert resp.status_code == 202
58+
59+
retry_until(fn ->
60+
info(db_name)["compact_running"] == false
61+
end)
62+
63+
compacted_info = info(db_name)
64+
assert compacted_info["purge_seq"] == purged_info["purge_seq"]
65+
66+
# purge documents twice in a row without loading views
67+
# (causes full view rebuilds)
68+
69+
doc3 = open_doc(db_name, 3)
70+
doc4 = open_doc(db_name, 4)
71+
72+
resp =
73+
Couch.post("/#{db_name}/_purge",
74+
body: %{"3": [doc3["_rev"]]}
75+
)
76+
77+
assert resp.status_code == 201
78+
79+
resp =
80+
Couch.post("/#{db_name}/_purge",
81+
body: %{"4": [doc4["_rev"]]}
82+
)
83+
84+
assert resp.status_code == 201
85+
86+
test_all_docs_twice(db_name, num_docs, 0, 4)
87+
end
88+
89+
@tag :with_db
90+
test "COUCHDB-1065", context do
91+
db_name_a = context[:db_name]
92+
db_name_b = random_db_name()
93+
{:ok, _} = create_db(db_name_b)
94+
95+
{:ok, doc_a_resp} = create_doc(db_name_a, %{_id: "test", a: 1})
96+
{:ok, doc_b_resp} = create_doc(db_name_b, %{_id: "test", a: 2})
97+
replicate(db_name_a, db_name_b)
98+
99+
open_rev(db_name_b, "test", doc_a_resp.body["rev"], 200)
100+
open_rev(db_name_b, "test", doc_b_resp.body["rev"], 200)
101+
102+
resp =
103+
Couch.post("/#{db_name_b}/_purge",
104+
body: %{test: [doc_a_resp.body["rev"]]}
105+
)
106+
107+
assert resp.status_code == 201
108+
109+
open_rev(db_name_b, "test", doc_a_resp.body["rev"], 404)
110+
111+
resp =
112+
Couch.post("/#{db_name_b}/_purge",
113+
body: %{test: [doc_b_resp.body["rev"]]}
114+
)
115+
116+
assert resp.status_code == 201
117+
118+
open_rev(db_name_b, "test", doc_b_resp.body["rev"], 404)
119+
120+
resp =
121+
Couch.post("/#{db_name_b}/_purge",
122+
body: %{test: [doc_a_resp.body["rev"], doc_b_resp.body["rev"]]}
123+
)
124+
125+
assert resp.status_code == 201
126+
127+
delete_db(db_name_b)
128+
end
129+
130+
def replicate(src, tgt, options \\ []) do
131+
defaults = [headers: [], body: %{}, timeout: 30_000]
132+
options = defaults |> Keyword.merge(options) |> Enum.into(%{})
133+
134+
%{body: body} = options
135+
body = [source: src, target: tgt] |> Enum.into(body)
136+
options = Map.put(options, :body, body)
137+
138+
resp = Couch.post("/_replicate", Enum.to_list(options))
139+
assert HTTPotion.Response.success?(resp), "#{inspect(resp)}"
140+
resp.body
141+
end
142+
143+
defp open_doc(db_name, id, expect \\ 200) do
144+
resp = Couch.get("/#{db_name}/#{id}")
145+
assert resp.status_code == expect
146+
resp.body
147+
end
148+
149+
defp open_rev(db_name, id, rev, expect) do
150+
resp = Couch.get("/#{db_name}/#{id}?rev=#{rev}")
151+
assert resp.status_code == expect
152+
resp.body
153+
end
154+
155+
defp test_all_docs_twice(db_name, num_docs, sigle_doc_expect, offset \\ 0) do
156+
resp = Couch.get("/#{db_name}/_design/test/_view/all_docs_twice")
157+
assert resp.status_code == 200
158+
rows = resp.body["rows"]
159+
160+
for x <- 0..(num_docs - offset) do
161+
assert Map.get(Enum.at(rows, 2 * x), "key") == x + offset + 1
162+
assert Map.get(Enum.at(rows, 2 * x + 1), "key") == x + offset + 1
163+
end
164+
165+
resp = Couch.get("/#{db_name}/_design/test/_view/single_doc")
166+
assert resp.body["total_rows"] == sigle_doc_expect
167+
end
168+
end

test/javascript/tests/purge.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
1010
// License for the specific language governing permissions and limitations under
1111
// the License.
12-
12+
couchTests.elixir = true;
1313
couchTests.purge = function(debug) {
1414
var db_name = get_random_db_name();
1515
var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});

0 commit comments

Comments
 (0)