Adding monger.utils/get-id protocol extension for DBObject.
This commit is contained in:
parent
25a24a5231
commit
5a94424402
3 changed files with 30 additions and 7 deletions
|
|
@ -8,7 +8,7 @@
|
||||||
;; You must not remove this notice, or any other, from this software.
|
;; You must not remove this notice, or any other, from this software.
|
||||||
|
|
||||||
(ns monger.util
|
(ns monger.util
|
||||||
(:import (java.security SecureRandom) (java.math BigInteger) (org.bson.types ObjectId)))
|
(:import (java.security SecureRandom) (java.math BigInteger) (org.bson.types ObjectId) (com.mongodb DBObject)))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
;; API
|
;; API
|
||||||
|
|
@ -35,3 +35,12 @@
|
||||||
[ns & body]
|
[ns & body]
|
||||||
`(binding [*ns* (the-ns ~ns)]
|
`(binding [*ns* (the-ns ~ns)]
|
||||||
~@(map (fn [form] `(eval '~form)) body)))
|
~@(map (fn [form] `(eval '~form)) body)))
|
||||||
|
|
||||||
|
(defprotocol GetDBObjectId
|
||||||
|
(get-id [input] "Returns object id"))
|
||||||
|
|
||||||
|
(extend-protocol GetDBObjectId
|
||||||
|
DBObject
|
||||||
|
(get-id
|
||||||
|
[^DBObject object]
|
||||||
|
(.get ^DBObject object "_id")))
|
||||||
|
|
@ -60,9 +60,9 @@
|
||||||
(deftest insert-a-basic-db-object-without-id-and-with-default-write-concern
|
(deftest insert-a-basic-db-object-without-id-and-with-default-write-concern
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
doc (monger.convertion/to-db-object { :name "Joe", :age 30 })]
|
doc (monger.convertion/to-db-object { :name "Joe", :age 30 })]
|
||||||
(is (nil? (.get ^DBObject doc "_id")))
|
(is (nil? (monger.util/get-id doc)))
|
||||||
(monger.collection/insert "people" doc)
|
(monger.collection/insert "people" doc)
|
||||||
(is (not (nil? (.get ^DBObject doc "_id"))))))
|
(is (not (nil? (monger.util/get-id doc))))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -154,7 +154,7 @@
|
||||||
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }]
|
doc { :data-store "MongoDB", :language "Clojure", :_id doc-id }]
|
||||||
(monger.collection/insert collection doc)
|
(monger.collection/insert collection doc)
|
||||||
(def #^DBObject found-one (monger.collection/find-one collection { :language "Clojure" }))
|
(def #^DBObject found-one (monger.collection/find-one collection { :language "Clojure" }))
|
||||||
(is (= (:_id doc) (.get ^DBObject found-one "_id")))
|
(is (= (:_id doc) (monger.util/get-id found-one)))
|
||||||
(is (= (monger.convertion/from-db-object found-one true) doc))
|
(is (= (monger.convertion/from-db-object found-one true) doc))
|
||||||
(is (= (monger.convertion/to-db-object doc) found-one))))
|
(is (= (monger.convertion/to-db-object doc) found-one))))
|
||||||
|
|
||||||
|
|
@ -176,7 +176,7 @@
|
||||||
(monger.collection/insert collection doc)
|
(monger.collection/insert collection doc)
|
||||||
(def #^DBObject loaded (monger.collection/find-one collection { :language "Clojure" } fields))
|
(def #^DBObject loaded (monger.collection/find-one collection { :language "Clojure" } fields))
|
||||||
(is (nil? (.get #^DBObject loaded "data-store")))
|
(is (nil? (.get #^DBObject loaded "data-store")))
|
||||||
(is (= doc-id (.get #^DBObject loaded "_id")))
|
(is (= doc-id (monger.util/get-id loaded)))
|
||||||
(is (= "Clojure" (.get #^DBObject loaded "language")))))
|
(is (= "Clojure" (.get #^DBObject loaded "language")))))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -329,9 +329,9 @@
|
||||||
(deftest save-a-new-basic-db-object
|
(deftest save-a-new-basic-db-object
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
doc (monger.convertion/to-db-object { :name "Joe", :age 30 })]
|
doc (monger.convertion/to-db-object { :name "Joe", :age 30 })]
|
||||||
(is (nil? (.get ^DBObject doc "_id")))
|
(is (nil? (monger.util/get-id doc)))
|
||||||
(monger.collection/save "people" doc)
|
(monger.collection/save "people" doc)
|
||||||
(is (not (nil? (.get ^DBObject doc "_id"))))))
|
(is (not (nil? (monger.util/get-id doc))))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
14
test/monger/test/util.clj
Normal file
14
test/monger/test/util.clj
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
(ns monger.test.util
|
||||||
|
(:import (com.mongodb DBObject))
|
||||||
|
(:require [monger util convertion])
|
||||||
|
(:use [clojure.test]))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest get-object-id
|
||||||
|
(let [id #^ObjectId (monger.util/object-id)
|
||||||
|
input #^DBObject (monger.convertion/to-db-object { :_id id })
|
||||||
|
output (monger.util/get-id input)]
|
||||||
|
|
||||||
|
(is (not (nil? output)))))
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in a new issue