summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuanjo Rodriguez <jjrodrig@gmail.com>2020-03-04 07:49:15 +0100
committerGitHub <noreply@github.com>2020-03-04 07:49:15 +0100
commit8acb4d59c99646786c82ed5a37f8023ec8d78e5a (patch)
tree1656111f8295e25cf9cd1cfe75830beb9325ee9c
parent26e646327f785a9ef8a5adbc03d0c75dbe674636 (diff)
downloadcouchdb-8acb4d59c99646786c82ed5a37f8023ec8d78e5a.tar.gz
Port _design_docs tests, design_options and design_paths from js to elixir (#2596) (#2628)
* Port _design_docs tests from js to elixir * Port design_options and design_paths tests from js to elixir
-rw-r--r--test/elixir/README.md5
-rw-r--r--test/elixir/test/design_docs_query_test.exs273
-rw-r--r--test/elixir/test/design_docs_test.exs108
-rw-r--r--test/elixir/test/design_options_test.exs74
-rw-r--r--test/elixir/test/design_paths_test.exs76
-rw-r--r--test/javascript/tests/design_docs_query.js2
-rw-r--r--test/javascript/tests/design_options.js3
-rw-r--r--test/javascript/tests/design_paths.js1
8 files changed, 431 insertions, 111 deletions
diff --git a/test/elixir/README.md b/test/elixir/README.md
index 5aa70bb9b..6a19c246d 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 000000000..b439a2e02
--- /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 ed0a0dfb5..000000000
--- 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 000000000..95a938e38
--- /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 000000000..b3e10c165
--- /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 07e6577ab..2aefe49b4 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 cc2571f6b..d3f8594d4 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 6e816991a..b85426acf 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"];