summaryrefslogtreecommitdiff
path: root/test/elixir/test/cluster_without_quorum_test.exs
blob: 63371f1a60bb87b41f1bd4fee71a64d817c85497 (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
defmodule WithoutQuorumTest do
  use CouchTestCase

  @moduletag :without_quorum_test
  @moduletag kind: :degraded_cluster

  @moduledoc """
  Test CouchDB API in a cluster without quorum.
  """
  @tag :with_db_name
  test "Creating/Deleting DB should return 202-Acepted", context do
    db_name = context[:db_name]
    resp = Couch.put("/#{db_name}")
    msg = "Should return 202-Acepted"
    assert resp.status_code == 202, msg
    resp = Couch.delete("/#{db_name}")
    assert resp.status_code == 202, msg
  end

  @tag :with_db_name
  test "Creating/Updating/Deleting doc should return 202-Acepted", context do
    db_name = context[:db_name]
    Couch.put("/#{db_name}")

    resp = Couch.post("/#{context[:db_name]}", body: %{:_id => "0", :a => 1})
    msg = "Should return 202-Acepted"
    assert resp.status_code == 202, msg

    resp = Couch.get("/#{context[:db_name]}/0")
    rev = resp.body["_rev"]

    resp =
      Couch.put("/#{context[:db_name]}/0", body: %{:_id => "0", :_rev => rev, :a => 2})

    msg = "Should return 202-Acepted"
    assert resp.status_code == 202, msg

    resp = Couch.get("/#{context[:db_name]}/0")
    rev = resp.body["_rev"]
    resp = Couch.delete("/#{context[:db_name]}/0", query: %{:rev => rev})
    msg = "Should return 202-Acepted"
    assert resp.status_code == 202, msg

    Couch.delete("/#{db_name}")
  end

  @tag :with_db_name
  test "Creating-Updating/Deleting doc with overriden quorum should return 201-Created/200-OK",
       context do
    db_name = context[:db_name]
    Couch.put("/#{db_name}")

    resp =
      Couch.post(
        "/#{context[:db_name]}",
        query: %{:w => 1},
        body: %{:_id => "0", :a => 1}
      )

    msg = "Should return 201-Created"
    assert resp.status_code in [201, 202], msg

    resp = Couch.get("/#{context[:db_name]}/0")
    rev = resp.body["_rev"]

    resp =
      Couch.put(
        "/#{context[:db_name]}/0",
        query: %{:w => 1},
        body: %{:_id => "0", :_rev => rev, :a => 2}
      )

    msg = "Should return 201-Created"
    assert resp.status_code in [201, 202], msg

    resp = Couch.get("/#{context[:db_name]}/0")
    rev = resp.body["_rev"]
    resp = Couch.delete("/#{context[:db_name]}/0", query: %{:w => 1, :rev => rev})
    msg = "Should return 200-Ok"
    assert resp.status_code == 200, msg

    Couch.delete("/#{db_name}")
  end

  @tag :with_db_name
  test "Copy doc should return 202-Acepted", context do
    db_name = context[:db_name]
    Couch.put("/#{db_name}")

    Couch.post(
      "/#{context[:db_name]}",
      body: %{:_id => "0", :a => 1}
    )

    headers = [Destination: "1"]
    resp = Couch.request(:copy, "/#{context[:db_name]}/0", headers: headers)
    msg = "Should return 202-Acepted"
    assert resp.status_code == 202, msg
    Couch.delete("/#{db_name}")
  end

  @doc_range 1..5

  @tag :with_db_name
  test "Bulk docs should return 202-Acepted", context do
    db_name = context[:db_name]
    Couch.put("/#{db_name}")
    docs = create_docs(@doc_range)
    resp = Couch.post("/#{db_name}/_bulk_docs", body: %{docs: docs})
    msg = "Should return 202-Acepted"
    assert resp.status_code == 202, msg

    Couch.delete("/#{db_name}")
  end

  @tag :with_db_name
  test "Bulk docs overriden quorum should return 201-Created", context do
    db_name = context[:db_name]
    Couch.put("/#{db_name}")
    docs = create_docs(@doc_range)
    resp = Couch.post("/#{db_name}/_bulk_docs", query: %{:w => 1}, body: %{docs: docs})
    msg = "Should return 201-Created"
    assert resp.status_code in [201, 202], msg

    Couch.delete("/#{db_name}")
  end

  @tag :with_db_name
  test "Attachments should return 202-Acepted", context do
    db_name = context[:db_name]
    Couch.put("/#{db_name}")
    resp = Couch.post("/#{context[:db_name]}", body: %{:_id => "0"})
    rev = resp.body["rev"]

    resp =
      Couch.put(
        "/#{context[:db_name]}/0/foo.txt",
        query: %{:rev => rev},
        body: "This is a no bas64 encoded text",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    msg = "Should return 202-Acepted"
    assert resp.status_code == 202, msg

    rev = resp.body["rev"]
    resp = Couch.delete("/#{context[:db_name]}/0/foo.txt", query: %{:rev => rev})
    msg = "Should return 200-Ok"
    assert resp.status_code == 200, msg

    Couch.delete("/#{db_name}")
  end

  @tag :with_db_name
  test "Attachments overriden quorum should return 201-Created", context do
    db_name = context[:db_name]
    Couch.put("/#{db_name}")
    resp = Couch.post("/#{context[:db_name]}", body: %{:_id => "0"})
    rev = resp.body["rev"]

    resp =
      Couch.put(
        "/#{context[:db_name]}/0/foo.txt",
        query: %{:rev => rev, :w => 1},
        body: "This is a no bas64 encoded text",
        headers: ["Content-Type": "text/plain;charset=utf-8"]
      )

    msg = "Should return 201-Created"
    assert resp.status_code in [201, 202], msg

    rev = resp.body["rev"]

    resp =
      Couch.delete(
        "/#{context[:db_name]}/0/foo.txt",
        query: %{:rev => rev, :w => 1}
      )

    msg = "Should return 200-Ok"
    assert resp.status_code == 200, msg

    Couch.delete("/#{db_name}")
  end
end