summaryrefslogtreecommitdiff
path: root/test/elixir/test/design_paths_test.exs
blob: f90172a0800baa3bbc40d422600ac6a1f689e86c (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
77
defmodule DesignPathTest do
  use CouchTestCase

  @moduletag :design_docs
  @moduletag kind: :single_node

  @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