You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ju...@apache.org on 2020/06/30 11:17:43 UTC

[couchdb] branch master updated: Port view_update_seq.js into elixir

This is an automated email from the ASF dual-hosted git repository.

juanjo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb.git


The following commit(s) were added to refs/heads/master by this push:
     new eaf6e74  Port view_update_seq.js into elixir
eaf6e74 is described below

commit eaf6e744bf286cdca8b07ea63303dd3920bcff2a
Author: Juanjo Rodriguez <ju...@apache.org>
AuthorDate: Mon Jun 29 18:02:39 2020 +0200

    Port view_update_seq.js into elixir
---
 test/elixir/lib/couch/db_test.ex          |   2 +-
 test/elixir/test/view_update_seq_test.exs | 142 ++++++++++++++++++++++++++++++
 test/javascript/tests/view_update_seq.js  |   1 +
 3 files changed, 144 insertions(+), 1 deletion(-)

diff --git a/test/elixir/lib/couch/db_test.ex b/test/elixir/lib/couch/db_test.ex
index a61db14..23f1093 100644
--- a/test/elixir/lib/couch/db_test.ex
+++ b/test/elixir/lib/couch/db_test.ex
@@ -341,7 +341,7 @@ defmodule Couch.DBTest do
           Couch.get("/#{db_name}/_design/#{view_root}/_view/#{view_name}", query: options)
 
         _ ->
-          Couch.post("/#{db_name}/_design/#{view_root}/_view/#{view_name}",
+          Couch.post("/#{db_name}/_design/#{view_root}/_view/#{view_name}", query: options,
             body: %{"keys" => keys}
           )
       end
diff --git a/test/elixir/test/view_update_seq_test.exs b/test/elixir/test/view_update_seq_test.exs
new file mode 100644
index 0000000..38b42c7
--- /dev/null
+++ b/test/elixir/test/view_update_seq_test.exs
@@ -0,0 +1,142 @@
+defmodule ViewUpdateSeqTest do
+  use CouchTestCase
+
+  @moduletag :view_update_seq
+
+  @moduledoc """
+  This is a port of the view_update_seq.js test suite.
+  """
+
+  @design_doc %{
+    _id: "_design/test",
+    language: "javascript",
+    autoupdate: false,
+    views: %{
+      all_docs: %{
+        map: "function(doc) { emit(doc.integer, doc.string) }"
+      },
+      summate: %{
+        map:
+          "function (doc) { if (typeof doc.integer === 'number') { emit(doc.integer, doc.integer)}; }",
+        reduce: "function (keys, values) { return sum(values); };"
+      }
+    }
+  }
+
+  defp seq_int(seq) do
+    {int, _} =
+      seq
+      |> String.split("-")
+      |> Enum.at(0)
+      |> Integer.parse()
+
+    int
+  end
+
+  @tag :with_db
+  test "db info update seq", context do
+    db_name = context[:db_name]
+
+    info = info(db_name)
+    assert seq_int(info["update_seq"]) == 0
+
+    create_doc(db_name, @design_doc)
+
+    info = info(db_name)
+    assert seq_int(info["update_seq"]) == 1
+  end
+
+  @tag :with_db
+  test "_all_docs update seq", context do
+    db_name = context[:db_name]
+
+    resp = Couch.get("/#{db_name}/_all_docs", query: %{:update_seq => true})
+    assert seq_int(resp.body["update_seq"]) == 0
+
+    create_doc(db_name, @design_doc)
+
+    resp = Couch.get("/#{db_name}/_all_docs", query: %{:update_seq => true})
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 1
+
+    docs = make_docs(0..99)
+    bulk_save(db_name, docs)
+
+    resp = Couch.get("/#{db_name}/_all_docs", query: %{:limit => 1})
+    assert length(resp.body["rows"]) == 1
+    assert Map.has_key?(resp.body, "update_seq") == false
+
+    resp = Couch.get("/#{db_name}/_all_docs", query: %{:limit => 1, :update_seq => true})
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 101
+  end
+
+  @tag :with_db
+  test "view update seq", context do
+    db_name = context[:db_name]
+
+    create_doc(db_name, @design_doc)
+    docs = make_docs(0..99)
+    bulk_save(db_name, docs)
+
+    resp = view(db_name, "test/all_docs", %{:limit => 1, :update_seq => true})
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 101
+
+    resp = view(db_name, "test/all_docs", %{:limit => 1, :update_seq => false})
+    assert length(resp.body["rows"]) == 1
+    assert Map.has_key?(resp.body, "update_seq") == false
+
+    resp = view(db_name, "test/summate", %{:update_seq => true})
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 101
+
+    save(db_name, %{"_id" => "A", "integer" => 1})
+
+    resp =
+      view(db_name, "test/all_docs", %{:limit => 1, :stale => "ok", :update_seq => true})
+
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 101
+
+    save(db_name, %{"_id" => "AA", "integer" => 2})
+
+    resp =
+      view(db_name, "test/all_docs", %{
+        :limit => 1,
+        :stale => "update_after",
+        :update_seq => true
+      })
+
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 101
+
+    retry_until(fn ->
+      resp =
+        view(db_name, "test/all_docs", %{:limit => 1, :stale => "ok", :update_seq => true})
+
+      assert length(resp.body["rows"]) == 1
+      seq_int(resp.body["update_seq"]) == 103
+    end)
+
+    resp =
+      view(db_name, "test/all_docs", %{:limit => 1, :stale => "ok", :update_seq => true})
+
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 103
+
+    resp = view(db_name, "test/all_docs", %{:limit => 1, :update_seq => true})
+
+    assert length(resp.body["rows"]) == 1
+    assert seq_int(resp.body["update_seq"]) == 103
+
+    resp = view(db_name, "test/all_docs", %{:update_seq => true}, ["0", "1"])
+    assert seq_int(resp.body["update_seq"]) == 103
+
+    resp = view(db_name, "test/all_docs", %{:update_seq => true}, ["0", "1"])
+    assert seq_int(resp.body["update_seq"]) == 103
+
+    resp = view(db_name, "test/summate", %{:group => true, :update_seq => true}, [0, 1])
+    assert seq_int(resp.body["update_seq"]) == 103
+  end
+end
diff --git a/test/javascript/tests/view_update_seq.js b/test/javascript/tests/view_update_seq.js
index c14453f..8b3a3fb 100644
--- a/test/javascript/tests/view_update_seq.js
+++ b/test/javascript/tests/view_update_seq.js
@@ -10,6 +10,7 @@
 // License for the specific language governing permissions and limitations under
 // the License.
 
+couchTests.elixir = true;
 couchTests.view_update_seq = function(debug) {
   var db_name = get_random_db_name();
   var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});