summaryrefslogtreecommitdiff
path: root/test/elixir/test/reduce_false_test.exs
blob: 675c11dbd83e2aae208a621f0fabd486f68caddc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
defmodule ReduceFalseTest do
  use CouchTestCase

  @moduletag :views

  @moduledoc """
  Test CouchDB view without reduces
  This is a port of the reduce_false.js suite
  """

  def summate(n) do
    (n + 1) * n / 2
  end

  @tag :with_db
  test "Basic reduce functions", context do
    db_name = context[:db_name]
    view_url = "/#{db_name}/_design/foo/_view/summate"
    num_docs = 5

    map = ~s"""
    function (doc) {
      emit(doc.integer, doc.integer);
    };
    """

    reduce = "function (keys, values) { return sum(values); };"
    red_doc = %{:views => %{:summate => %{:map => map, :reduce => reduce}}}
    assert Couch.put("/#{db_name}/_design/foo", body: red_doc).body["ok"]

    docs = make_docs(1..num_docs)
    resp = Couch.post("/#{db_name}/_bulk_docs", body: %{:docs => docs}, query: %{w: 3})
    assert resp.status_code in [201, 202]

    # Test that the reduce works
    rows = Couch.get(view_url).body["rows"]
    assert length(rows) == 1
    assert hd(rows)["value"] == summate(num_docs)

    # Test that we got our docs back
    rows = Couch.get(view_url, query: %{reduce: false}).body["rows"]
    assert length(rows) == 5

    rows
    |> Enum.with_index(1)
    |> Enum.each(fn {row, i} ->
      assert i == row["value"]
    end)
  end
end