summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJuanjo Rodriguez <jjrodrig@gmail.com>2020-01-04 17:05:44 +0100
committerJan Lehnardt <jan@apache.org>2020-01-04 17:05:44 +0100
commitee47f93a0a847cc4b4c3e4e8f47462050b91c555 (patch)
treeb84e72c15337fb26f04670d55505ac95bec39054
parent571274ee849201074438e2cfbbac3de4f9b2bffb (diff)
downloadcouchdb-ee47f93a0a847cc4b4c3e4e8f47462050b91c555.tar.gz
Port erlang views tests into elixir test suite (#2237)
* Port erlang views tests into elixir test suite * Enable erlang views in elixir testing * Support nil or :null in response
-rw-r--r--Makefile2
-rw-r--r--Makefile.win2
-rw-r--r--test/elixir/README.md2
-rw-r--r--test/elixir/test/erlang_views_test.exs117
4 files changed, 120 insertions, 3 deletions
diff --git a/Makefile b/Makefile
index 9bcd389be..0d4131aec 100644
--- a/Makefile
+++ b/Makefile
@@ -238,7 +238,7 @@ python-black-update: .venv/bin/black
elixir: export MIX_ENV=integration
elixir: export COUCHDB_TEST_ADMIN_PARTY_OVERRIDE=1
elixir: elixir-init elixir-check-formatted elixir-credo devclean
- @dev/run "$(TEST_OPTS)" -a adm:pass -n 1 --no-eval 'mix test --trace --exclude without_quorum_test --exclude with_quorum_test $(EXUNIT_OPTS)'
+ @dev/run "$(TEST_OPTS)" -a adm:pass -n 1 --enable-erlang-views --no-eval 'mix test --trace --exclude without_quorum_test --exclude with_quorum_test $(EXUNIT_OPTS)'
.PHONY: elixir-init
elixir-init: MIX_ENV=test
diff --git a/Makefile.win b/Makefile.win
index c74a54621..7278fec76 100644
--- a/Makefile.win
+++ b/Makefile.win
@@ -189,7 +189,7 @@ python-black-update: .venv/bin/black
.PHONY: elixir
elixir: export COUCHDB_TEST_ADMIN_PARTY_OVERRIDE=1
elixir: elixir-init elixir-check-formatted elixir-credo devclean
- @dev\run -a adm:pass --no-eval 'mix test --trace --exclude without_quorum_test --exclude with_quorum_test $(EXUNIT_OPTS)'
+ @dev\run -a adm:pass --enable-erlang-views --no-eval 'mix test --trace --exclude without_quorum_test --exclude with_quorum_test $(EXUNIT_OPTS)'
.PHONY: elixir-init
elixir-init:
diff --git a/test/elixir/README.md b/test/elixir/README.md
index 7c6b10e5a..7f13d8afc 100644
--- a/test/elixir/README.md
+++ b/test/elixir/README.md
@@ -45,7 +45,7 @@ X means done, - means partially
- [ ] Port design_docs.js
- [ ] Port design_options.js
- [ ] Port design_paths.js
- - [ ] Port erlang_views.js
+ - [X] Port erlang_views.js
- [ ] Port etags_head.js
- [ ] Port etags_views.js
- [ ] Port form_submit.js
diff --git a/test/elixir/test/erlang_views_test.exs b/test/elixir/test/erlang_views_test.exs
new file mode 100644
index 000000000..3346c2274
--- /dev/null
+++ b/test/elixir/test/erlang_views_test.exs
@@ -0,0 +1,117 @@
+defmodule ErlangViewsTest do
+ use CouchTestCase
+
+ @moduletag :erlang_views
+
+ @moduledoc """
+ basic 'smoke tests' of erlang views.
+ This is a port of the erlang_views.js test suite.
+ """
+
+ @doc1 %{:_id => "1", :integer => 1, :string => "str1", :array => [1, 2, 3]}
+
+ @erlang_map_fun """
+ fun({Doc}) ->
+ K = couch_util:get_value(<<"integer">>, Doc, null),
+ V = couch_util:get_value(<<"string">>, Doc, null),
+ Emit(K, V)
+ end.
+ """
+
+ @erlang_reduce_fun """
+ fun (_, Values, false) -> length(Values);
+ (_, Values, true) -> lists:sum(Values)
+ end.
+ """
+
+ @erlang_map_fun_2 """
+ fun({Doc}) ->
+ Words = couch_util:get_value(<<"words">>, Doc),
+ lists:foreach(fun({Word}) ->
+ WordString = couch_util:get_value(<<"word">>, Word),
+ Count = couch_util:get_value(<<"count">>, Word),
+ Emit(WordString , Count)
+ end, Words)
+ end.
+ """
+
+ @erlang_reduce_fun_2 """
+ fun(Keys, Values, RR) -> length(Values) end.
+ """
+
+ @word_list ["foo", "bar", "abc", "def", "baz", "xxyz"]
+
+ @tag :with_db
+ test "Erlang map function", context do
+ db_name = context[:db_name]
+ create_doc(db_name, @doc1)
+
+ results =
+ query(
+ db_name,
+ @erlang_map_fun,
+ nil,
+ nil,
+ nil,
+ "erlang"
+ )
+
+ assert results["total_rows"] == 1
+ assert List.first(results["rows"])["key"] == 1
+ assert List.first(results["rows"])["value"] == "str1"
+ end
+
+ @tag :with_db
+ test "Erlang reduce function", context do
+ db_name = context[:db_name]
+ create_doc(db_name, @doc1)
+ doc2 = @doc1 |> Map.replace!(:_id, "2") |> Map.replace!(:string, "str2")
+ create_doc(db_name, doc2)
+
+ results =
+ query(
+ db_name,
+ @erlang_map_fun,
+ @erlang_reduce_fun,
+ nil,
+ nil,
+ "erlang"
+ )
+
+ assert List.first(results["rows"])["value"] == 2
+ end
+
+ @tag :with_db
+ test "Erlang reduce function larger dataset", context do
+ db_name = context[:db_name]
+ bulk_save(db_name, create_large_dataset(250))
+
+ results =
+ query(
+ db_name,
+ @erlang_map_fun_2,
+ @erlang_reduce_fun_2,
+ nil,
+ nil,
+ "erlang"
+ )
+
+ assert Map.get(List.first(results["rows"]), "key", :null) == :null
+ assert List.first(results["rows"])["value"] > 0
+ end
+
+ defp create_large_dataset(size) do
+ doc_words =
+ for j <- 0..100 do
+ %{word: get_word(j), count: j}
+ end
+
+ template_doc = %{words: doc_words}
+
+ make_docs(0..size, template_doc)
+ end
+
+ defp get_word(idx) do
+ Enum.at(@word_list, rem(idx, length(@word_list)))
+ end
+end