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/03/02 21:35:52 UTC

[couchdb] branch master updated: Port _design_docs tests, design_options and design_paths from js to elixir (#2596)

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 86ec2f3  Port _design_docs tests, design_options and design_paths from js to elixir (#2596)
86ec2f3 is described below

commit 86ec2f3be092f6b6c1a4aa5757c87ea32da28d9c
Author: Juanjo Rodriguez <jj...@gmail.com>
AuthorDate: Mon Mar 2 22:35:46 2020 +0100

    Port _design_docs tests, design_options and design_paths from js to elixir (#2596)
    
    * Port _design_docs tests from js to elixir
    * Port design_options and design_paths tests from js to elixir
---
 test/elixir/README.md                       |   5 +-
 test/elixir/test/design_docs_query_test.exs | 273 ++++++++++++++++++++++++++++
 test/elixir/test/design_docs_test.exs       | 108 -----------
 test/elixir/test/design_options_test.exs    |  74 ++++++++
 test/elixir/test/design_paths_test.exs      |  76 ++++++++
 test/javascript/tests/design_docs_query.js  |   2 +
 test/javascript/tests/design_options.js     |   3 +-
 test/javascript/tests/design_paths.js       |   1 +
 8 files changed, 431 insertions(+), 111 deletions(-)

diff --git a/test/elixir/README.md b/test/elixir/README.md
index ee087c0..1fc0ce6 100644
--- a/test/elixir/README.md
+++ b/test/elixir/README.md
@@ -43,8 +43,9 @@ X means done, - means partially
   - [X] Port cookie_auth.js
   - [X] Port copy_doc.js
   - [ ] Port design_docs.js
-  - [ ] Port design_options.js
-  - [ ] Port design_paths.js
+  - [X] Port design_docs_query.js
+  - [X] Port design_options.js
+  - [X] Port design_paths.js
   - [X] Port erlang_views.js
   - [X] Port etags_head.js
   - [ ] ~~Port etags_views.js~~ (skipped in js test suite)
diff --git a/test/elixir/test/design_docs_query_test.exs b/test/elixir/test/design_docs_query_test.exs
new file mode 100644
index 0000000..b439a2e
--- /dev/null
+++ b/test/elixir/test/design_docs_query_test.exs
@@ -0,0 +1,273 @@
+defmodule DesignDocsQueryTest do
+  use CouchTestCase
+
+  @moduletag :design_docs
+
+  @moduledoc """
+  Test CouchDB /{db}/_design_docs
+  """
+
+  setup_all do
+    db_name = random_db_name()
+    {:ok, _} = create_db(db_name)
+    on_exit(fn -> delete_db(db_name) end)
+
+    bulk_save(db_name, make_docs(1..5))
+
+    Enum.each(1..5, fn x -> create_ddoc(db_name, x) end)
+
+    {:ok, [db_name: db_name]}
+  end
+
+  defp create_ddoc(db_name, idx) do
+    ddoc = %{
+      _id: "_design/ddoc0#{idx}",
+      views: %{
+        testing: %{
+          map: "function(){emit(1,1)}"
+        }
+      }
+    }
+
+    create_doc(db_name, ddoc)
+  end
+
+  test "query _design_docs (GET with no parameters)", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs")
+    assert resp.status_code == 200, "standard get should be 200"
+    assert resp.body["total_rows"] == 5, "total_rows mismatch"
+    assert length(resp.body["rows"]) == 5, "amount of rows mismatch"
+  end
+
+  test "query _design_docs with single key", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs?key=\"_design/ddoc03\"")
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 1, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 0)["key"] == "_design/ddoc03"
+  end
+
+  test "query _design_docs with multiple key", context do
+    resp =
+      Couch.get(
+        "/#{context[:db_name]}/_design_docs",
+        query: %{
+          :keys => "[\"_design/ddoc02\", \"_design/ddoc03\"]"
+        }
+      )
+
+    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_docs",
+        body: %{}
+      )
+
+    assert resp.status_code == 200
+    assert length(Map.get(resp, :body)["rows"]) == 5
+  end
+
+  test "POST with keys and limit", context do
+    resp =
+      Couch.post(
+        "/#{context[:db_name]}/_design_docs",
+        body: %{
+          :keys => ["_design/ddoc02", "_design/ddoc03"],
+          :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_docs",
+        query: %{
+          :limit => 1
+        },
+        body: %{
+          :keys => ["_design/ddoc02", "_design/ddoc03"]
+        }
+      )
+
+    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_docs",
+        query: %{
+          :limit => 0
+        },
+        body: %{
+          :keys => ["_design/ddoc02", "_design/ddoc03"],
+          :limit => 2
+        }
+      )
+
+    assert resp.status_code == 200
+    assert Enum.empty?(Map.get(resp, :body)["rows"])
+  end
+
+  test "query _design_docs descending=true", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs?descending=true")
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 5, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 0)["key"] == "_design/ddoc05"
+  end
+
+  test "query _design_docs descending=false", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs?descending=false")
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 5, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 0)["key"] == "_design/ddoc01"
+  end
+
+  test "query _design_docs end_key", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs?end_key=\"_design/ddoc03\"")
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 3, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 2)["key"] == "_design/ddoc03"
+  end
+
+  test "query _design_docs endkey", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs?endkey=\"_design/ddoc03\"")
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 3, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 2)["key"] == "_design/ddoc03"
+  end
+
+  test "query _design_docs start_key", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs?start_key=\"_design/ddoc03\"")
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 3, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 0)["key"] == "_design/ddoc03"
+  end
+
+  test "query _design_docs startkey", context do
+    db_name = context[:db_name]
+    resp = Couch.get("/#{db_name}/_design_docs?startkey=\"_design/ddoc03\"")
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 3, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 0)["key"] == "_design/ddoc03"
+  end
+
+  test "query _design_docs end_key inclusive_end=true", context do
+    db_name = context[:db_name]
+
+    resp =
+      Couch.get("/#{db_name}/_design_docs",
+        query: [end_key: "\"_design/ddoc03\"", inclusive_end: true]
+      )
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 3, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 2)["key"] == "_design/ddoc03"
+  end
+
+  test "query _design_docs end_key inclusive_end=false", context do
+    db_name = context[:db_name]
+
+    resp =
+      Couch.get("/#{db_name}/_design_docs",
+        query: [end_key: "\"_design/ddoc03\"", inclusive_end: false]
+      )
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 2, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 1)["key"] == "_design/ddoc02"
+  end
+
+  test "query _design_docs end_key inclusive_end=false descending", context do
+    db_name = context[:db_name]
+
+    resp =
+      Couch.get("/#{db_name}/_design_docs",
+        query: [end_key: "\"_design/ddoc03\"", inclusive_end: false, descending: true]
+      )
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 2, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 1)["key"] == "_design/ddoc04"
+  end
+
+  test "query _design_docs end_key limit", context do
+    db_name = context[:db_name]
+
+    resp =
+      Couch.get("/#{db_name}/_design_docs",
+        query: [end_key: "\"_design/ddoc05\"", limit: 2]
+      )
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 2, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 1)["key"] == "_design/ddoc02"
+  end
+
+  test "query _design_docs end_key skip", context do
+    db_name = context[:db_name]
+
+    resp =
+      Couch.get("/#{db_name}/_design_docs",
+        query: [end_key: "\"_design/ddoc05\"", skip: 2]
+      )
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 3, "amount of rows mismatch"
+    assert Enum.at(resp.body["rows"], 0)["key"] == "_design/ddoc03"
+    assert Enum.at(resp.body["rows"], 2)["key"] == "_design/ddoc05"
+  end
+
+  test "query _design_docs update_seq", context do
+    db_name = context[:db_name]
+
+    resp =
+      Couch.get("/#{db_name}/_design_docs",
+        query: [end_key: "\"_design/ddoc05\"", update_seq: true]
+      )
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert Map.has_key?(resp.body, "update_seq")
+  end
+
+  test "query _design_docs post with keys", context do
+    db_name = context[:db_name]
+
+    resp =
+      Couch.post("/#{db_name}/_design_docs",
+        headers: ["Content-Type": "application/json"],
+        body: %{keys: ["_design/ddoc02", "_design/ddoc03"]}
+      )
+
+    keys =
+      resp.body["rows"]
+      |> Enum.map(fn p -> p["key"] end)
+
+    assert resp.status_code == 200, "standard get should be 200"
+    assert length(resp.body["rows"]) == 2, "amount of rows mismatch"
+    assert Enum.member?(keys, "_design/ddoc03")
+    assert Enum.member?(keys, "_design/ddoc02")
+  end
+end
diff --git a/test/elixir/test/design_docs_test.exs b/test/elixir/test/design_docs_test.exs
deleted file mode 100644
index ed0a0df..0000000
--- a/test/elixir/test/design_docs_test.exs
+++ /dev/null
@@ -1,108 +0,0 @@
-defmodule DesignDocsTest do
-  use CouchTestCase
-
-  @moduletag :design_docs
-
-  @moduledoc """
-  Test CouchDB /{db}/_design_docs
-  """
-
-  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: "_design/foo",
-        bar: "baz"
-      }
-    )
-
-    {:ok, _} = create_doc(
-      db_name,
-      %{
-        _id: "_design/foo2",
-        bar: "baz2"
-      }
-    )
-
-    {:ok, [db_name: db_name]}
-  end
-
-  test "GET with no parameters", context do
-    resp = Couch.get(
-      "/#{context[:db_name]}/_design_docs"
-    )
-
-    assert resp.status_code == 200
-    assert length(Map.get(resp, :body)["rows"]) == 2
-  end
-
-  test "GET with multiple keys", context do
-    resp = Couch.get(
-      "/#{context[:db_name]}/_design_docs",
-      query: %{
-        :keys => "[\"_design/foo\", \"_design/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_docs",
-      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_docs",
-      body: %{
-        :keys => ["_design/foo", "_design/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_docs",
-      query: %{
-        :limit => 1
-      },
-      body: %{
-        :keys => ["_design/foo", "_design/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_docs",
-      query: %{
-        :limit => 0
-      },
-      body: %{
-        :keys => ["_design/foo", "_design/foo2"],
-        :limit => 2
-      }
-    )
-
-    assert resp.status_code == 200
-    assert Enum.empty?(Map.get(resp, :body)["rows"])
-  end
-end
diff --git a/test/elixir/test/design_options_test.exs b/test/elixir/test/design_options_test.exs
new file mode 100644
index 0000000..95a938e
--- /dev/null
+++ b/test/elixir/test/design_options_test.exs
@@ -0,0 +1,74 @@
+defmodule DesignOptionsTest do
+  use CouchTestCase
+
+  @moduletag :design_docs
+
+  @moduledoc """
+  Test CouchDB design documents options include_design and local_seq
+  """
+  @tag :with_db
+  test "design doc options - include_desing=true", context do
+    db_name = context[:db_name]
+
+    create_test_view(db_name, "_design/fu", %{include_design: true})
+
+    resp = Couch.get("/#{db_name}/_design/fu/_view/data")
+    assert resp.status_code == 200
+    assert length(Map.get(resp, :body)["rows"]) == 1
+    assert Enum.at(resp.body["rows"], 0)["value"] == "_design/fu"
+  end
+
+  @tag :with_db
+  test "design doc options - include_desing=false", context do
+    db_name = context[:db_name]
+
+    create_test_view(db_name, "_design/bingo", %{include_design: false})
+
+    resp = Couch.get("/#{db_name}/_design/bingo/_view/data")
+    assert resp.status_code == 200
+    assert Enum.empty?(Map.get(resp, :body)["rows"])
+  end
+
+  @tag :with_db
+  test "design doc options - include_design default value", context do
+    db_name = context[:db_name]
+
+    create_test_view(db_name, "_design/bango", %{})
+
+    resp = Couch.get("/#{db_name}/_design/bango/_view/data")
+    assert resp.status_code == 200
+    assert Enum.empty?(Map.get(resp, :body)["rows"])
+  end
+
+  @tag :with_db
+  test "design doc options - local_seq=true", context do
+    db_name = context[:db_name]
+
+    create_test_view(db_name, "_design/fu", %{include_design: true, local_seq: true})
+    create_doc(db_name, %{})
+    resp = Couch.get("/#{db_name}/_design/fu/_view/with_seq")
+
+    row_with_key =
+      resp.body["rows"]
+      |> Enum.filter(fn p -> p["key"] != :null end)
+
+    assert length(row_with_key) == 2
+  end
+
+  defp create_test_view(db_name, id, options) do
+    map = "function (doc) {emit(null, doc._id);}"
+    withseq = "function(doc) {emit(doc._local_seq, null)}"
+
+    design_doc = %{
+      _id: id,
+      language: "javascript",
+      options: options,
+      views: %{
+        data: %{map: map},
+        with_seq: %{map: withseq}
+      }
+    }
+
+    create_doc(db_name, design_doc)
+  end
+end
diff --git a/test/elixir/test/design_paths_test.exs b/test/elixir/test/design_paths_test.exs
new file mode 100644
index 0000000..b3e10c1
--- /dev/null
+++ b/test/elixir/test/design_paths_test.exs
@@ -0,0 +1,76 @@
+defmodule DesignPathTest do
+  use CouchTestCase
+
+  @moduletag :design_docs
+
+  @moduledoc """
+  Test CouchDB design documents path
+  """
+  @tag :with_db
+  test "design doc path", context do
+    db_name = context[:db_name]
+    ddoc_path_test(db_name)
+  end
+
+  @tag :with_db_name
+  test "design doc path with slash in db name", context do
+    db_name = URI.encode_www_form(context[:db_name] <> "/with_slashes")
+    create_db(db_name)
+    ddoc_path_test(db_name)
+  end
+
+  defp ddoc_path_test(db_name) do
+    create_test_view(db_name, "_design/test")
+
+    resp = Couch.get("/#{db_name}/_design/test")
+    assert resp.body["_id"] == "_design/test"
+
+    resp =
+      Couch.get(Couch.process_url("/#{db_name}/_design%2Ftest"),
+        follow_redirects: true
+      )
+
+    assert resp.body["_id"] == "_design/test"
+
+    resp = Couch.get("/#{db_name}/_design/test/_view/testing")
+    assert Enum.empty?(Map.get(resp, :body)["rows"])
+
+    design_doc2 = %{
+      _id: "_design/test2",
+      views: %{
+        testing: %{
+          map: "function(){emit(1,1)}"
+        }
+      }
+    }
+
+    resp = Couch.put("/#{db_name}/_design/test2", body: design_doc2)
+    assert resp.status_code == 201
+
+    resp = Couch.get("/#{db_name}/_design/test2")
+    assert resp.body["_id"] == "_design/test2"
+
+    resp =
+      Couch.get(Couch.process_url("/#{db_name}/_design%2Ftest2"),
+        follow_redirects: true
+      )
+
+    assert resp.body["_id"] == "_design/test2"
+
+    resp = Couch.get("/#{db_name}/_design/test2/_view/testing")
+    assert Enum.empty?(Map.get(resp, :body)["rows"])
+  end
+
+  defp create_test_view(db_name, id) do
+    design_doc = %{
+      _id: id,
+      views: %{
+        testing: %{
+          map: "function(){emit(1,1)}"
+        }
+      }
+    }
+
+    create_doc(db_name, design_doc)
+  end
+end
diff --git a/test/javascript/tests/design_docs_query.js b/test/javascript/tests/design_docs_query.js
index 07e6577..2aefe49 100644
--- a/test/javascript/tests/design_docs_query.js
+++ b/test/javascript/tests/design_docs_query.js
@@ -11,6 +11,8 @@
 // the License.
 
 couchTests.design_docs_query = function(debug) {
+  return console.log('done in test/elixir/test/design_docs_query_test.exs');
+
   var db_name = get_random_db_name();
   var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});
   db.createDb();
diff --git a/test/javascript/tests/design_options.js b/test/javascript/tests/design_options.js
index cc2571f..d3f8594 100644
--- a/test/javascript/tests/design_options.js
+++ b/test/javascript/tests/design_options.js
@@ -11,6 +11,7 @@
 // the License.
 
 couchTests.design_options = function(debug) {
+  return console.log('done in test/elixir/test/design_options.exs');
   var db_name = get_random_db_name();
   var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"false"});
   db.createDb();
@@ -36,7 +37,7 @@ couchTests.design_options = function(debug) {
   T(db.save(designDoc).ok);
 
   // should work for temp views
-  // no more there on cluster - pointless test 
+  // no more there on cluster - pointless test
   //var rows = db.query(map, null, {options:{include_design: true}}).rows;
   //T(rows.length == 1);
   //T(rows[0].value == "_design/fu");
diff --git a/test/javascript/tests/design_paths.js b/test/javascript/tests/design_paths.js
index 6e81699..b85426a 100644
--- a/test/javascript/tests/design_paths.js
+++ b/test/javascript/tests/design_paths.js
@@ -11,6 +11,7 @@
 // the License.
 
 couchTests.design_paths = function(debug) {
+  return console.log('done in test/elixir/test/design_paths.exs');
   if (debug) debugger;
   var db_name = get_random_db_name()
   var dbNames = [db_name, db_name + "/with_slashes"];