summaryrefslogtreecommitdiff
path: root/test/elixir/test/local_docs_test.exs
blob: d7ed137c8715c5a382b5563edb1f81932a19c544 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
defmodule LocalDocsTest do
  use CouchTestCase

  @moduletag :local_docs
  @moduletag kind: :single_node

  @moduledoc """
  Test CouchDB _local_docs
  """

  setup_all do
    db_name = random_db_name()
    {:ok, _} = create_db(db_name)
    on_exit(fn -> delete_db(db_name) end)

    resp1 = Couch.put(
      "/#{db_name}/_local/foo",
      body: %{
        _id: "foo",
        bar: "baz"
      }
    )
    assert resp1.status_code == 201

    resp2 = Couch.put(
      "/#{db_name}/_local/foo2",
      body: %{
        _id: "foo",
        bar: "baz2"
      }
    )
    assert resp2.status_code == 201

    {:ok, [db_name: db_name]}
  end

  test "GET with no parameters", context do
    resp = Couch.get(
      "/#{context[:db_name]}/_local_docs"
    )

    assert resp.status_code == 200
    assert length(Map.get(resp, :body)["rows"]) == 2
  end

  test "GET with multiple keys", context do
    resp = Couch.get(
      "/#{context[:db_name]}/_local_docs",
      query: %{
        :keys => "[\"_local/foo\", \"_local/foo2\"]",
      }
    )

    assert resp.status_code == 200
    assert length(Map.get(resp, :body)["rows"]) == 2
  end

  test "POST with empty body", context do
    resp = Couch.post(
      "/#{context[:db_name]}/_local_docs",
      body: %{}
    )

    assert resp.status_code == 200
    assert length(Map.get(resp, :body)["rows"]) == 2
  end

  test "POST with keys and limit", context do
    resp = Couch.post(
      "/#{context[:db_name]}/_local_docs",
      body: %{
        :keys => ["_local/foo", "_local/foo2"],
        :limit => 1
      }
    )

    assert resp.status_code == 200
    assert length(Map.get(resp, :body)["rows"]) == 1
  end

  test "POST with query parameter and JSON body", context do
    resp = Couch.post(
      "/#{context[:db_name]}/_local_docs",
      query: %{
        :limit => 1
      },
      body: %{
        :keys => ["_local/foo", "_local/foo2"]
      }
    )

    assert resp.status_code == 200
    assert length(Map.get(resp, :body)["rows"]) == 1
  end

  test "POST edge case with colliding parameters - query takes precedence", context do
    resp = Couch.post(
      "/#{context[:db_name]}/_local_docs",
      query: %{
        :limit => 0
      },
      body: %{
        :keys => ["_local/foo", "_local/foo2"],
        :limit => 2
      }
    )

    assert resp.status_code == 200
    assert Enum.empty?(Map.get(resp, :body)["rows"])
  end
end