summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuanjo Rodriguez <juanjo@apache.org>2020-03-23 00:14:30 +0100
committerJuanjo Rodriguez <jjrodrig@gmail.com>2020-03-25 08:51:59 +0100
commit49dbb6af0305d0dc10cb0abec3732b2aa0b29993 (patch)
treea0ad5cf6318daf51f6c92e8691e162d4f8920b6a
parent2b95500ac57d831ed4ac7abfc9250c739179b230 (diff)
downloadcouchdb-49dbb6af0305d0dc10cb0abec3732b2aa0b29993.tar.gz
Port purge.js into elixir test suite
-rw-r--r--test/elixir/README.md2
-rw-r--r--test/elixir/test/purge_test.exs168
-rw-r--r--test/javascript/tests/purge.js2
3 files changed, 170 insertions, 2 deletions
diff --git a/test/elixir/README.md b/test/elixir/README.md
index 4a6e52a8e..4c81dd59e 100644
--- a/test/elixir/README.md
+++ b/test/elixir/README.md
@@ -61,7 +61,7 @@ X means done, - means partially
- [X] Port method_override.js
- [X] Port multiple_rows.js
- [X] Port proxyauth.js
- - [ ] Port purge.js
+ - [X] Port purge.js
- [ ] Port reader_acl.js
- [ ] Port recreate_doc.js
- [X] Port reduce_builtin.js
diff --git a/test/elixir/test/purge_test.exs b/test/elixir/test/purge_test.exs
new file mode 100644
index 000000000..3920b3f26
--- /dev/null
+++ b/test/elixir/test/purge_test.exs
@@ -0,0 +1,168 @@
+defmodule PurgeTest do
+ use CouchTestCase
+
+ @moduletag :purge
+
+ @tag :with_db
+ test "purge documents", context do
+ db_name = context[:db_name]
+
+ design_doc = %{
+ _id: "_design/test",
+ language: "javascript",
+ views: %{
+ all_docs_twice: %{
+ map: "function(doc) { emit(doc.integer, null); emit(doc.integer, null) }"
+ },
+ single_doc: %{
+ map: "function(doc) { if (doc._id == \"1\") { emit(1, null) }}"
+ }
+ }
+ }
+
+ {:ok, _} = create_doc(db_name, design_doc)
+
+ num_docs = 10
+ bulk_save(db_name, make_docs(1..(num_docs + 1)))
+
+ test_all_docs_twice(db_name, num_docs, 1)
+
+ info = info(db_name)
+
+ doc1 = open_doc(db_name, 1)
+ doc2 = open_doc(db_name, 2)
+
+ resp =
+ Couch.post("/#{db_name}/_purge",
+ body: %{"1": [doc1["_rev"]], "2": [doc2["_rev"]]}
+ )
+
+ assert resp.status_code == 201
+ result = resp.body
+
+ assert Enum.at(result["purged"]["1"], 0) == doc1["_rev"]
+ assert Enum.at(result["purged"]["2"], 0) == doc2["_rev"]
+
+ open_doc(db_name, 1, 404)
+ open_doc(db_name, 2, 404)
+
+ purged_info = info(db_name)
+
+ assert purged_info["purge_seq"] != info["purge_seq"]
+
+ test_all_docs_twice(db_name, num_docs, 0, 2)
+
+ # purge sequences are preserved after compaction (COUCHDB-1021)
+ resp = Couch.post("/#{db_name}/_compact")
+ assert resp.status_code == 202
+
+ retry_until(fn ->
+ info(db_name)["compact_running"] == false
+ end)
+
+ compacted_info = info(db_name)
+ assert compacted_info["purge_seq"] == purged_info["purge_seq"]
+
+ # purge documents twice in a row without loading views
+ # (causes full view rebuilds)
+
+ doc3 = open_doc(db_name, 3)
+ doc4 = open_doc(db_name, 4)
+
+ resp =
+ Couch.post("/#{db_name}/_purge",
+ body: %{"3": [doc3["_rev"]]}
+ )
+
+ assert resp.status_code == 201
+
+ resp =
+ Couch.post("/#{db_name}/_purge",
+ body: %{"4": [doc4["_rev"]]}
+ )
+
+ assert resp.status_code == 201
+
+ test_all_docs_twice(db_name, num_docs, 0, 4)
+ end
+
+ @tag :with_db
+ test "COUCHDB-1065", context do
+ db_name_a = context[:db_name]
+ db_name_b = random_db_name()
+ {:ok, _} = create_db(db_name_b)
+
+ {:ok, doc_a_resp} = create_doc(db_name_a, %{_id: "test", a: 1})
+ {:ok, doc_b_resp} = create_doc(db_name_b, %{_id: "test", a: 2})
+ replicate(db_name_a, db_name_b)
+
+ open_rev(db_name_b, "test", doc_a_resp.body["rev"], 200)
+ open_rev(db_name_b, "test", doc_b_resp.body["rev"], 200)
+
+ resp =
+ Couch.post("/#{db_name_b}/_purge",
+ body: %{test: [doc_a_resp.body["rev"]]}
+ )
+
+ assert resp.status_code == 201
+
+ open_rev(db_name_b, "test", doc_a_resp.body["rev"], 404)
+
+ resp =
+ Couch.post("/#{db_name_b}/_purge",
+ body: %{test: [doc_b_resp.body["rev"]]}
+ )
+
+ assert resp.status_code == 201
+
+ open_rev(db_name_b, "test", doc_b_resp.body["rev"], 404)
+
+ resp =
+ Couch.post("/#{db_name_b}/_purge",
+ body: %{test: [doc_a_resp.body["rev"], doc_b_resp.body["rev"]]}
+ )
+
+ assert resp.status_code == 201
+
+ delete_db(db_name_b)
+ end
+
+ def replicate(src, tgt, options \\ []) do
+ defaults = [headers: [], body: %{}, timeout: 30_000]
+ options = defaults |> Keyword.merge(options) |> Enum.into(%{})
+
+ %{body: body} = options
+ body = [source: src, target: tgt] |> Enum.into(body)
+ options = Map.put(options, :body, body)
+
+ resp = Couch.post("/_replicate", Enum.to_list(options))
+ assert HTTPotion.Response.success?(resp), "#{inspect(resp)}"
+ resp.body
+ end
+
+ defp open_doc(db_name, id, expect \\ 200) do
+ resp = Couch.get("/#{db_name}/#{id}")
+ assert resp.status_code == expect
+ resp.body
+ end
+
+ defp open_rev(db_name, id, rev, expect) do
+ resp = Couch.get("/#{db_name}/#{id}?rev=#{rev}")
+ assert resp.status_code == expect
+ resp.body
+ end
+
+ defp test_all_docs_twice(db_name, num_docs, sigle_doc_expect, offset \\ 0) do
+ resp = Couch.get("/#{db_name}/_design/test/_view/all_docs_twice")
+ assert resp.status_code == 200
+ rows = resp.body["rows"]
+
+ for x <- 0..(num_docs - offset) do
+ assert Map.get(Enum.at(rows, 2 * x), "key") == x + offset + 1
+ assert Map.get(Enum.at(rows, 2 * x + 1), "key") == x + offset + 1
+ end
+
+ resp = Couch.get("/#{db_name}/_design/test/_view/single_doc")
+ assert resp.body["total_rows"] == sigle_doc_expect
+ end
+end
diff --git a/test/javascript/tests/purge.js b/test/javascript/tests/purge.js
index 0c11d9ad8..15fd63710 100644
--- a/test/javascript/tests/purge.js
+++ b/test/javascript/tests/purge.js
@@ -9,7 +9,7 @@
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
-
+couchTests.elixir = true;
couchTests.purge = function(debug) {
var db_name = get_random_db_name();
var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});