summaryrefslogtreecommitdiff
path: root/test/elixir/test/view_multi_key_all_docs_test.exs
blob: 6426eb2c2e62656bfdbc8a0e88fb67cb0525e0fd (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
defmodule ViewMultiKeyAllDocsTest do
  use CouchTestCase

  @moduletag kind: :single_node

  @keys ["10", "15", "30", "37", "50"]

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

    bulk_save(db_name, make_docs(0..99))

    {:ok, [db_name: db_name]}
  end

  test "keys in POST body", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, nil, @keys)
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == length(@keys)

    rows_id = Enum.map(rows, & &1["id"])
    assert rows_id == @keys
  end

  test "keys in GET parameters", context do
    db_name = context[:db_name]
    resp = all_docs(db_name, keys: :jiffy.encode(@keys))
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == length(@keys)
    rows_id = Enum.map(rows, & &1["id"])
    assert rows_id == @keys
  end

  test "keys in POST body (limit)", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, [limit: 1], @keys)
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 1
    assert Enum.at(rows, 0)["id"] == Enum.at(@keys, 0)
  end

  test "keys in GET parameters (limit)", context do
    db_name = context[:db_name]
    resp = all_docs(db_name, limit: 1, keys: :jiffy.encode(@keys))
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 1
    assert Enum.at(rows, 0)["id"] == Enum.at(@keys, 0)
  end

  test "keys in POST body (skip)", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, [skip: 2], @keys)
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 3

    rows_id = Enum.map(rows, & &1["id"])
    assert rows_id == Enum.drop(@keys, 2)
  end

  test "keys in GET parameters (skip)", context do
    db_name = context[:db_name]
    resp = all_docs(db_name, skip: 2, keys: :jiffy.encode(@keys))
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 3
    rows_id = Enum.map(rows, & &1["id"])
    assert rows_id == Enum.drop(@keys, 2)
  end

  test "keys in POST body (descending)", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, [descending: true], @keys)
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == length(@keys)

    rows_id = Enum.map(rows, & &1["id"])
    assert rows_id == Enum.reverse(@keys)
  end

  test "keys in GET parameters (descending)", context do
    db_name = context[:db_name]
    resp = all_docs(db_name, descending: true, keys: :jiffy.encode(@keys))
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == length(@keys)
    rows_id = Enum.map(rows, & &1["id"])
    assert rows_id == Enum.reverse(@keys)
  end

  test "keys in POST body (descending, skip, limit)", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, [descending: "true", skip: 3, limit: 1], @keys)
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 1

    key =
      @keys
      |> Enum.reverse()
      |> Enum.drop(3)
      |> Enum.at(0)

    assert Enum.at(rows, 0)["id"] == key
  end

  test "keys in GET parameters (descending, skip, limit)", context do
    db_name = context[:db_name]

    resp =
      all_docs(db_name, descending: "true", skip: 3, limit: 1, keys: :jiffy.encode(@keys))

    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 1

    key =
      @keys
      |> Enum.reverse()
      |> Enum.drop(3)
      |> Enum.at(0)

    assert Enum.at(rows, 0)["id"] == key
  end

  test "POST - get invalid rows when the key doesn't exist", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, nil, ["1211", "i_dont_exist", "0"])
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 3
    assert Enum.at(rows, 0)["error"] == "not_found"
    assert not Map.has_key?(Enum.at(rows, 0), "id")
    assert Enum.at(rows, 1)["error"] == "not_found"
    assert not Map.has_key?(Enum.at(rows, 1), "id")
    assert Enum.at(rows, 2)["id"] == Enum.at(rows, 2)["key"]
    assert Enum.at(rows, 2)["key"] == "0"
  end

  test "GET - get invalid rows when the key doesn't exist", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, keys: :jiffy.encode(["1211", "i_dont_exist", "0"]))
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert length(rows) == 3
    assert Enum.at(rows, 0)["error"] == "not_found"
    assert not Map.has_key?(Enum.at(rows, 0), "id")
    assert Enum.at(rows, 1)["error"] == "not_found"
    assert not Map.has_key?(Enum.at(rows, 1), "id")
    assert Enum.at(rows, 2)["id"] == Enum.at(rows, 2)["key"]
    assert Enum.at(rows, 2)["key"] == "0"
  end

  test "empty keys", context do
    db_name = context[:db_name]

    resp = all_docs(db_name, keys: :jiffy.encode([]))
    assert resp.status_code == 200
    rows = resp.body["rows"]
    assert Enum.empty?(rows)
  end

  defp all_docs(db_name, options, keys \\ nil) do
    resp =
      case keys do
        nil ->
          Couch.get("/#{db_name}/_all_docs", query: options)

        _ ->
          Couch.post("/#{db_name}/_all_docs",
            query: options,
            body: %{"keys" => keys}
          )
      end

    resp
  end
end