Skip to content

Commit

Permalink
Fixed bug with binary attachment get. Updated test cases accordingly.
Browse files Browse the repository at this point in the history
  • Loading branch information
joubertnel committed Oct 23, 2010
1 parent be3b97f commit c1f4e1d
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 51 deletions.
14 changes: 9 additions & 5 deletions src/couchdb/client.clj
Original file line number Diff line number Diff line change
Expand Up @@ -442,16 +442,20 @@ http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API"
id))

(defn attachment-get
"returns the attachment as a seq of strings in :body"
"Returns the attachment in :body
If the data is text/plain, :body is a seq of string, otherwise it is a byte array"
[server database document id]
(when-let [database (validate-dbname database)]
(let [document (do-get-doc database document)
response (couch-request {:url (str (normalize-url server) database "/"
(url-encode (as-str document)) "/"
(url-encode (as-str id)))})]

{:body (:body response)
:content-type (get (:headers response) "content-type")})))
(url-encode (as-str id)))
:as :byte-array})
content-type (.toLowerCase (get (:headers response) "content-type"))]
{:body (if (.contains content-type "text/plain")
(String. (:body response))
(:body response))
:content-type content-type})))


(defn attachment-delete
Expand Down
118 changes: 72 additions & 46 deletions test/couchdb/client/tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,49 +105,75 @@


(deftest attachments
;; list
(is (= (couchdb/attachment-list +test-server+ +test-db+ "foobar") {}))
;; create
(is (= (couchdb/attachment-create +test-server+ +test-db+
"foobar" "my-attachment #1"
"ATTACHMENT DATA" "text/plain")
"my-attachment #1"))
;; get
(is (= (couchdb/attachment-get +test-server+ +test-db+
"foobar" "my-attachment #1")
{:body "ATTACHMENT DATA"
:content-type "text/plain; charset=UTF-8"}))

;; re-check the list
(let [atts (couchdb/attachment-list +test-server+ +test-db+ "foobar")
att1 (get atts "my-attachment #1")]
(is (= (count atts) 1))
(is (not (nil? att1)))
(is (= (select-keys att1 [:length :content_type :stub])
{:length 15
:content_type "text/plain; charset=UTF-8"
:stub true})))
;; delete
(is (= (couchdb/attachment-delete +test-server+ +test-db+
"foobar" "my-attachment #1") true))
;; re-check the list again
(is (= (couchdb/attachment-list +test-server+ +test-db+ "foobar") {}))
(let [binary-attachment (byte-array [(byte 0x12)
(byte 0x23)
(byte 0x44)])]
;; list
(is (= (couchdb/attachment-list +test-server+ +test-db+ "foobar") {}))
;; create textual
(is (= (couchdb/attachment-create +test-server+ +test-db+
"foobar" "my-attachment #1"
"ATTACHMENT DATA" "text/plain")
"my-attachment #1"))
;; create binary
(is (= (couchdb/attachment-create +test-server+ +test-db+
"foobar" "attachment-2"
binary-attachment "application/binary"))
"attachment-2")

;; get textual
(is (= (couchdb/attachment-get +test-server+ +test-db+
"foobar" "my-attachment #1")
{:body "ATTACHMENT DATA"
:content-type "text/plain; charset=utf-8"}))

;; get binary attachment and ensure it is what we uploaded
(is (let [attachment (:body (couchdb/attachment-get +test-server+ +test-db+
"foobar" "attachment-2"))
max_index (dec (count attachment))]
(loop [index 0]
(if (> index max_index)
true
(let [attachment_byte (nth attachment index)
data_byte (nth binary-attachment index)]
(if (not (= attachment_byte data_byte))
'false
(recur (inc index))))))))


;; re-check the list
(let [atts (couchdb/attachment-list +test-server+ +test-db+ "foobar")
att1 (get atts "my-attachment #1")]
(is (= (count atts) 2))
(is (not (nil? att1)))
(is (= (select-keys att1 [:length :content_type :stub])
{:length 15
:content_type "text/plain; charset=UTF-8"
:stub true})))
;; delete textual
(is (= (couchdb/attachment-delete +test-server+ +test-db+
"foobar" "my-attachment #1") true))
;; delete binary
(is (= (couchdb/attachment-delete +test-server+ +test-db+
"foobar" "attachment-2") true))
;; re-check the list again
(is (= (couchdb/attachment-list +test-server+ +test-db+ "foobar") {}))

;; create with InputStream
(if-not (.exists (file *file*))
(println "File " *file* "not found. Skipping InputStream-Test")
(do
(let [istream (FileInputStream. *file*)]
(is (= (couchdb/attachment-create +test-server+ +test-db+
"foobar" "my-attachment #2"
istream "text/clojure")
"my-attachment #2")))
;; get back the attachment we just created
(let [istream (FileInputStream. *file*)]
(is (= (couchdb/attachment-get +test-server+ +test-db+
"foobar" "my-attachment #2")
{:body (line-seq (reader istream))
:content-type "text/clojure"}))))))
;; create with InputStream
(if-not (.exists (file *file*))
(println "File " *file* "not found. Skipping InputStream-Test")
(do
(let [istream (FileInputStream. *file*)]
(is (= (couchdb/attachment-create +test-server+ +test-db+
"foobar" "my-attachment #2"
istream "text/clojure")
"my-attachment #2")))
;; get back the attachment we just created
(let [istream (FileInputStream. *file*)]
(is (= (couchdb/attachment-get +test-server+ +test-db+
"foobar" "my-attachment #2")
{:body (line-seq (reader istream))
:content-type "text/clojure"})))))))

(deftest views
(let [design-doc "viewdocs"]
Expand Down Expand Up @@ -238,10 +264,10 @@
;; creating
(let [regdoc-return (couchdb/attachment-create +test-server+ +test-db+
"regdoc" "att1"
"payload" "content/type")
"payload" "text/plain")
mapdoc-return (couchdb/attachment-create +test-server+ +test-db+
mapdoc "att1" "payload"
"content/type")]
"text/plain")]
(is (= regdoc-return mapdoc-return)))
;; listing
(let [regdoc-return (couchdb/attachment-list +test-server+ +test-db+
Expand All @@ -263,10 +289,10 @@
;; are both return values true?
(is (= regdoc-return mapdoc-return true))
;; make sure fetching both attachments gives a DocumentNotFound error
(is (raised? couchdb/AttachmentNotFound
(is (raised? couchdb/DocumentNotFound
(couchdb/attachment-get +test-server+ +test-db+
"regdoc" "att1")))
(is (raised? couchdb/AttachmentNotFound
(is (raised? couchdb/DocumentNotFound
(couchdb/attachment-get +test-server+ +test-db+
mapdoc "att1"))))))

Expand Down

0 comments on commit c1f4e1d

Please sign in to comment.