You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@couchdb.apache.org by GitBox <gi...@apache.org> on 2020/02/12 10:43:23 UTC

[GitHub] [couchdb] leo-pires commented on a change in pull request #2541: Port reduce_false.js and reduce_builtin.js to Elixir

leo-pires commented on a change in pull request #2541: Port reduce_false.js and reduce_builtin.js to Elixir
URL: https://github.com/apache/couchdb/pull/2541#discussion_r378171202
 
 

 ##########
 File path: test/elixir/test/reduce_builtin_test.exs
 ##########
 @@ -0,0 +1,259 @@
+defmodule ReduceBultinTest do
+  use CouchTestCase
+
+  @moduletag :views
+
+  @moduledoc """
+  Test CouchDB view builtin reduce functions
+  This is a port of the reduce_builtin.js suite
+  """
+
+  def random_ddoc(db_name) do
+    "/#{db_name}/_design/#{:erlang.monotonic_time()}"
+  end
+
+  def summate(n) do
+    (n + 1) * n / 2
+  end
+
+  def sumsqr(n) do
+    1..n |> Enum.reduce(0, fn i, acc -> acc + i * i end)
+  end
+
+  def check_approx_distinct(expected, estimated) do
+    # see https://en.wikipedia.org/wiki/HyperLogLog
+    err = 1.04 / :math.sqrt(:math.pow(2, 11 - 1))
+    abs(expected - estimated) < expected * err
+  end
+
+  def query_rows(ddoc_url, builtin_fun, query \\ nil) do
+    http_opts = if query, do: [query: query], else: []
+    Couch.get("#{ddoc_url}/_view/builtin#{builtin_fun}", http_opts).body["rows"]
+  end
+  def query_value(ddoc_url, builtin_fun, query \\ nil) do
+    hd(query_rows(ddoc_url, builtin_fun, query))["value"]
+  end
+
+  @tag :with_db
+  test "Builtin reduce functions", context do
+    db_name = context[:db_name]
+    num_docs = 500
+
+    docs = make_docs(1..num_docs)
+    assert Couch.post("/#{db_name}/_bulk_docs", body: %{:docs => docs}, query: %{w: 3}).status_code in [
+             201,
+             202
+           ]
+
+    ddoc_url = random_ddoc(db_name)
+    map = ~s"""
+    function (doc) {
+      emit(doc.integer, doc.integer);
+      emit(doc.integer, doc.integer);
+    };
+    """
+    design_doc = %{:views => %{
+      :builtin_sum => %{:map => map, :reduce => "_sum"},
+      :builtin_count => %{:map => map, :reduce => "_count"},
+      :builtin_stats => %{:map => map, :reduce => "_stats"},
+      :builtin_approx_count_distinct => %{:map => map, :reduce => "_approx_count_distinct"}
+    }}
+    assert Couch.put(ddoc_url, body: design_doc).body["ok"]
+
+    value = ddoc_url |> query_value("_sum")
 
 Review comment:
   It's just idiomatics, being clearer that we're looking for a `_sum` here.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services