summaryrefslogtreecommitdiff
path: root/test/elixir/test/partition_ddoc_test.exs
blob: 85f66c45cfe71816c0dde5f81bcb8b1e0cf4e90f (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
defmodule PartitionDDocTest do
  use CouchTestCase

  @moduledoc """
  Test partition design doc interactions
  """

  setup do
    db_name = random_db_name()
    {:ok, _} = create_db(db_name, query: %{partitioned: true, q: 1})
    on_exit(fn -> delete_db(db_name) end)

    {:ok, [db_name: db_name]}
  end

  test "PUT /dbname/_design/foo", context do
    db_name = context[:db_name]
    resp = Couch.put("/#{db_name}/_design/foo", body: %{stuff: "here"})
    assert resp.status_code == 201
  end

  test "PUT /dbname/_design/foo to update", context do
    db_name = context[:db_name]
    ddoc_id = "_design/foo"

    ddoc = %{
      _id: ddoc_id,
      stuff: "here"
    }

    resp = Couch.put("/#{db_name}/#{ddoc_id}", body: ddoc)
    assert resp.status_code == 201
    %{body: body} = resp

    ddoc = Map.put(ddoc, :_rev, body["rev"])
    ddoc = Map.put(ddoc, :other, "attribute")
    resp = Couch.put("/#{db_name}/#{ddoc_id}", body: ddoc)
    assert resp.status_code == 201
  end

  test "PUT /dbname/_design/foo/readme.txt", context do
    db_name = context[:db_name]
    ddoc_id = "_design/foo"

    ddoc = %{
      _id: ddoc_id,
      stuff: "here"
    }

    resp = Couch.put("/#{db_name}/#{ddoc_id}", body: ddoc)
    assert resp.status_code == 201
    %{body: body} = resp

    att = "This is a readme.txt"

    opts = [
      headers: [{:"Content-Type", "text/plain"}],
      query: [rev: body["rev"]],
      body: att
    ]

    resp = Couch.put("/#{db_name}/#{ddoc_id}/readme.txt", opts)
    assert resp.status_code == 201
  end

  test "DELETE /dbname/_design/foo", context do
    db_name = context[:db_name]
    ddoc_id = "_design/foo"

    ddoc = %{
      _id: ddoc_id,
      stuff: "here"
    }

    resp = Couch.put("/#{db_name}/#{ddoc_id}", body: ddoc)
    assert resp.status_code == 201
    %{body: body} = resp

    resp = Couch.delete("/#{db_name}/#{ddoc_id}", query: [rev: body["rev"]])
    assert resp.status_code == 200
  end

  test "POST /dbname with design doc", context do
    db_name = context[:db_name]
    body = %{_id: "_design/foo", stuff: "here"}
    resp = Couch.post("/#{db_name}", body: body)
    assert resp.status_code == 201
  end

  test "POST /dbname/_bulk_docs with design doc", context do
    db_name = context[:db_name]
    body = %{:docs => [%{_id: "_design/foo", stuff: "here"}]}
    resp = Couch.post("/#{db_name}/_bulk_docs", body: body)
    assert resp.status_code == 201
  end

  test "GET /dbname/_design/foo", context do
    db_name = context[:db_name]
    resp = Couch.put("/#{db_name}/_design/foo", body: %{stuff: "here"})
    assert resp.status_code == 201

    resp = Couch.get("/#{db_name}/_design/foo")
    assert resp.status_code == 200
  end

  test "GET /dbname/_design/foo?rev=$rev", context do
    db_name = context[:db_name]
    resp = Couch.put("/#{db_name}/_design/foo", body: %{stuff: "here"})
    assert resp.status_code == 201
    %{body: body} = resp

    resp = Couch.get("/#{db_name}/_design/foo", query: [rev: body["rev"]])
    assert resp.status_code == 200
  end

  test "GET /dbname/_bulk_get", context do
    db_name = context[:db_name]
    resp = Couch.put("/#{db_name}/_design/foo", body: %{stuff: "here"})
    assert resp.status_code == 201

    body = %{docs: [%{id: "_design/foo"}]}
    resp = Couch.post("/#{db_name}/_bulk_get", body: body)
    assert resp.status_code == 200
    %{body: body} = resp

    assert length(body["results"]) == 1

    %{"results" => [%{"id" => "_design/foo", "docs" => [%{"ok" => _}]}]} = body
  end

  test "GET /dbname/_bulk_get with rev", context do
    db_name = context[:db_name]
    resp = Couch.put("/#{db_name}/_design/foo", body: %{stuff: "here"})
    assert resp.status_code == 201
    %{body: body} = resp

    body = %{docs: [%{id: "_design/foo", rev: body["rev"]}]}
    resp = Couch.post("/#{db_name}/_bulk_get", body: body)
    assert resp.status_code == 200
    %{body: body} = resp

    assert length(body["results"]) == 1
    %{"results" => [%{"id" => "_design/foo", "docs" => [%{"ok" => _}]}]} = body
  end

  test "GET /dbname/_all_docs?key=$ddoc_id", context do
    db_name = context[:db_name]
    resp = Couch.put("/#{db_name}/_design/foo", body: %{stuff: "here"}, query: [w: 3])
    assert resp.status_code == 201

    resp = Couch.get("/#{db_name}/_all_docs", query: [key: "\"_design/foo\""])
    assert resp.status_code == 200
    %{body: body} = resp

    assert length(body["rows"]) == 1
    assert %{"rows" => [%{"id" => "_design/foo"}]} = body
  end

  @tag :skip_on_jenkins
  test "GET /dbname/_design_docs", context do
    db_name = context[:db_name]

    retry_until(fn ->
      resp = Couch.put("/#{db_name}/_design/foo", body: %{stuff: "here"})
      assert resp.status_code == 201

      resp = Couch.get("/#{db_name}/_design_docs")
      assert resp.status_code == 200
      %{body: body} = resp

      assert length(body["rows"]) == 1
      %{"rows" => [%{"id" => "_design/foo"}]} = body
    end)
  end
end