jiangphcn commented on a change in pull request #1953: Jenkins add attachment 
test
URL: https://github.com/apache/couchdb/pull/1953#discussion_r262409739
 
 

 ##########
 File path: test/elixir/test/attachments.exs
 ##########
 @@ -0,0 +1,471 @@
+defmodule AttachmentsTest do
+  use CouchTestCase
+
+  @moduletag :attachments
+
+  #  MD5 Digests of compressible attachments and therefore Etags
+  #  will vary depending on platform gzip implementation.
+  #  These MIME types are defined in [attachments] compressible_types
+  @bin_att_doc %{
+    _id: "bin_doc",
+    _attachments: %{
+      "foo.txt": %{
+        content_type: "application/octet-stream",
+        data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
+      }
+    }
+  }
+
+  @moduledoc """
+  Test CouchDB attachments
+  This is a port of the attachments.js suite
+  """
+
+  @tag :with_db
+  test "saves attachment successfully", context do
+    db_name = context[:db_name]
+
+    resp = Couch.put("/#{db_name}/bin_doc", body: @bin_att_doc)
+    assert resp.status_code in [201, 202]
+    assert resp.body["ok"]
+  end
+
+  @tag :with_db
+  test "errors for bad attachment", context do
+    db_name = context[:db_name]
+
+    bad_att_doc = %{
+      _id: "bad_doc",
+      _attachments: %{
+        "foo.txt": %{
+          content_type: "text/plain",
+          data: "notBase64Encoded="
+        }
+      }
+    }
+
+    resp = Couch.put("/#{db_name}/bad_doc", body: bad_att_doc)
+    assert resp.status_code == 400
+  end
+
+  @tag :with_db
+  test "reads attachment successfully", context do
+    db_name = context[:db_name]
+
+    resp = Couch.put("/#{db_name}/bin_doc", body: @bin_att_doc)
+    assert resp.status_code in [201, 202]
+
+    resp = Couch.get("/#{db_name}/bin_doc/foo.txt", body: @bin_att_doc)
+
+    assert resp.body == "This is a base64 encoded text"
+    assert resp.headers["Content-Type"] == "application/octet-stream"
+    assert resp.headers["Etag"] == "\"aEI7pOYCRBLTRQvvqYrrJQ==\""
+  end
+
+  @tag :with_db
+  test "update attachment", context do
+    db_name = context[:db_name]
+
+    bin_att_doc2 = %{
+      _id: "bin_doc2",
+      _attachments: %{
+        "foo.txt": %{
+          content_type: "text/plain",
+          data: ""
+        }
+      }
+    }
+
+    resp = Couch.put("/#{db_name}/bin_doc2", body: bin_att_doc2)
+    assert resp.status_code in [201, 202]
+    rev = resp.body["rev"]
+
+    resp = Couch.get("/#{db_name}/bin_doc2/foo.txt")
+
+    assert resp.headers["Content-Type"] == "text/plain"
+    assert resp.body == ""
+
+    resp =
+      Couch.put(
+        "/#{db_name}/bin_doc2/foo2.txt",
+        query: %{rev: rev},
+        body: "This is no base64 encoded text",
+        headers: ["Content-Type": "text/plain;charset=utf-8"]
+      )
+
+    assert resp.status_code in [201, 202]
+    assert Regex.match?(~r/bin_doc2\/foo2.txt/, resp.headers["location"])
+  end
+
+  @tag :with_db
+  test "delete attachment", context do
+    db_name = context[:db_name]
+
+    resp = Couch.put("/#{db_name}/bin_doc", body: @bin_att_doc)
+    assert resp.status_code in [201, 202]
+    rev = resp.body["rev"]
+
+    resp = Couch.delete("/#{db_name}/bin_doc/foo.txt")
+
+    assert resp.status_code == 409
+
+    resp = Couch.delete("/#{db_name}/bin_doc/foo.txt", query: %{rev: rev})
+    assert resp.status_code == 200
+    assert resp.headers["location"] == nil
+  end
+
+  @tag :with_db
+  test "saves binary", context do
+    db_name = context[:db_name]
+
+    bin_data = "JHAPDO*AU£PN ){(3u[d 93DQ9¡€])}    
ææøo'∂ƒæ≤çæππ•¥∫¶®#†π¶®¥π€ª®˙π8np"
+
+    resp =
+      Couch.put(
+        "/#{db_name}/bin_doc3/attachment.txt",
+        body: bin_data,
+        headers: ["Content-Type": "text/plain;charset=utf-8"]
+      )
+
+    assert resp.status_code in [201, 202]
+    assert resp.body["ok"]
+
+    rev = resp.body["rev"]
+
+    resp = Couch.get("/#{db_name}/bin_doc3/attachment.txt")
+    assert resp.body == bin_data
+
+    resp = Couch.put("/#{db_name}/bin_doc3/attachment.txt", body: bin_data)
+    assert resp.status_code == 409
 
 Review comment:
   found once
   ```
     1) test saves binary (AttachmentsTest)
        test/attachments.exs:118
        Assertion with == failed
        code:  assert resp.status_code() == 409
        left:  202
        right: 409
        stacktrace:
          test/attachments.exs:139: (test)
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to