summaryrefslogtreecommitdiff
path: root/test/elixir/test/design_options_test.exs
blob: a68b794cfd47631c7c3e8d65681499e0d931a7f6 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
defmodule DesignOptionsTest do
  use CouchTestCase

  @moduletag :design_docs
  @moduletag kind: :single_node

  @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 :pending # Design doc returned
  @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