From 01773f50d5c839934c2132fe5f8d3d5248f105ac Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 4 Jun 2012 13:11:31 +0400 Subject: [PATCH] Better coverage for storing files in gridfs, include file metadata References #27 --- project.clj | 1 + src/monger/gridfs.clj | 25 ++++++++++++++++++------- test/monger/test/gridfs_test.clj | 28 ++++++++++++++++++---------- 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/project.clj b/project.clj index d9b9a92..6d594d8 100644 --- a/project.clj +++ b/project.clj @@ -13,6 +13,7 @@ :indexing :indexing :external :external :cache :cache + :gridfs :gridfs :performance :performance ;; as in, edge mongodb server :edge-features :edge-features diff --git a/src/monger/gridfs.clj b/src/monger/gridfs.clj index 6fe3847..6a64d9b 100644 --- a/src/monger/gridfs.clj +++ b/src/monger/gridfs.clj @@ -9,9 +9,10 @@ (ns monger.gridfs (:refer-clojure :exclude [remove find]) - (:require [monger.core] + (:require monger.core [clojure.java.io :as io]) - (:use [monger.conversion]) + (:use monger.conversion + [clojurewerkz.support.fn :only [fpartial]]) (:import [com.mongodb DB DBObject] [com.mongodb.gridfs GridFS GridFSInputFile] [java.io InputStream File])) @@ -57,23 +58,33 @@ ([^GridFS fs query] (.getFileList fs query))) +(def ^{:private true} converter + (fpartial from-db-object true)) + +(defn files-as-maps + ([] + (map converter (all-files))) + ([query] + (map converter (all-files query))) + ([^GridFS fs query] + (map converter (all-files fs query)))) (defprotocol GridFSInputFileFactory - (^GridFSInputFile make-input-file [input] "Makes GridFSInputFile out of given input")) + (^GridFSInputFile make-input-file [input] "Makes GridFSInputFile out of the given input")) (extend byte-array-type GridFSInputFileFactory - { :make-input-file (fn [^bytes input] - (.createFile ^GridFS monger.core/*mongodb-gridfs* input)) }) + {:make-input-file (fn [^bytes input] + (.createFile ^GridFS monger.core/*mongodb-gridfs* input))}) (extend-protocol GridFSInputFileFactory String (make-input-file [^String input] - (.createFile ^GridFS monger.core/*mongodb-gridfs* ^InputStream (io/make-input-stream input { :encoding "UTF-8" }))) + (.createFile ^GridFS monger.core/*mongodb-gridfs* ^InputStream (io/make-input-stream input {:encoding "UTF-8"}))) File (make-input-file [^File input] - (.createFile ^GridFS monger.core/*mongodb-gridfs* ^InputStream (io/make-input-stream input { :encoding "UTF-8" }))) + (.createFile ^GridFS monger.core/*mongodb-gridfs* ^InputStream (io/make-input-stream input {:encoding "UTF-8"}))) InputStream (make-input-file [^InputStream input] diff --git a/test/monger/test/gridfs_test.clj b/test/monger/test/gridfs_test.clj index 9710df3..e6cebab 100644 --- a/test/monger/test/gridfs_test.clj +++ b/test/monger/test/gridfs_test.clj @@ -24,7 +24,7 @@ -(deftest test-storing-files-to-gridfs-using-relative-fs-paths +(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-relative-fs-paths (let [input "./test/resources/mongo/js/mapfun1.js"] (is (= 0 (count (gridfs/all-files)))) (store (make-input-file input) @@ -33,7 +33,7 @@ (is (= 1 (count (gridfs/all-files)))))) -(deftest test-storing-files-to-gridfs-using-file-instances +(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-file-instances (let [input (io/as-file "./test/resources/mongo/js/mapfun1.js")] (is (= 0 (count (gridfs/all-files)))) (store (make-input-file input) @@ -41,15 +41,23 @@ (.setContentType "application/octet-stream")) (is (= 1 (count (gridfs/all-files)))))) -(deftest test-storing-bytes-to-gridfs - (let [input (.getBytes "A string")] +(deftest ^{:gridfs true} test-storing-bytes-to-gridfs + (let [input (.getBytes "A string") + md {:format "raw" :source "AwesomeCamera D95"} + filename "monger.test.gridfs.file3" + ct "application/octet-stream"] (is (= 0 (count (gridfs/all-files)))) (store (make-input-file input) - (.setFilename "monger.test.gridfs.file3") + (.setFilename filename) + (.setMetaData (to-db-object md)) (.setContentType "application/octet-stream")) + (let [f (first (gridfs/files-as-maps))] + (is (= ct (:contentType f))) + (is (= filename (:filename f))) + (is (= md (:metadata f)))) (is (= 1 (count (gridfs/all-files)))))) -(deftest test-storing-files-to-gridfs-using-absolute-fs-paths +(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-absolute-fs-paths (let [tmp-file (File/createTempFile "monger.test.gridfs" "test-storing-files-to-gridfs-using-absolute-fs-paths") _ (spit tmp-file "Some content") input (.getAbsolutePath tmp-file)] @@ -59,7 +67,7 @@ (.setContentType "application/octet-stream")) (is (= 1 (count (gridfs/all-files)))))) -(deftest test-storing-files-to-gridfs-using-input-stream +(deftest ^{:gridfs true} test-storing-files-to-gridfs-using-input-stream (let [tmp-file (File/createTempFile "monger.test.gridfs" "test-storing-files-to-gridfs-using-input-stream") _ (spit tmp-file "Some other content")] (is (= 0 (count (gridfs/all-files)))) @@ -70,7 +78,7 @@ -(deftest test-finding-individual-files-on-gridfs +(deftest ^{:gridfs true} test-finding-individual-files-on-gridfs (let [input "./test/resources/mongo/js/mapfun1.js" ct "binary/octet-stream" filename "monger.test.gridfs.file5" @@ -90,7 +98,7 @@ md5 filename md5 (to-db-object { :md5 md5 })))) -(deftest test-finding-multiple-files-on-gridfs +(deftest ^{:gridfs true} test-finding-multiple-files-on-gridfs (let [input "./test/resources/mongo/js/mapfun1.js" ct "binary/octet-stream" md5 "14a09deabb50925a3381315149017bbd" @@ -113,7 +121,7 @@ list4 [stored1 stored2]))) -(deftest test-removing-multiple-files-from-gridfs +(deftest ^{:gridfs true} test-removing-multiple-files-from-gridfs (let [input "./test/resources/mongo/js/mapfun1.js" ct "binary/octet-stream" md5 "14a09deabb50925a3381315149017bbd"