From 5a94424402dc5c6fdb1ff76d1d99911ae0d46df8 Mon Sep 17 00:00:00 2001 From: Oleksandr Petrov Date: Sun, 11 Sep 2011 16:13:14 +0200 Subject: [PATCH] Adding monger.utils/get-id protocol extension for DBObject. --- src/monger/util.clj | 11 ++++++++++- test/monger/test/collection.clj | 12 ++++++------ test/monger/test/util.clj | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 7 deletions(-) create mode 100644 test/monger/test/util.clj diff --git a/src/monger/util.clj b/src/monger/util.clj index 4d5cf18..2947b01 100644 --- a/src/monger/util.clj +++ b/src/monger/util.clj @@ -8,7 +8,7 @@ ;; You must not remove this notice, or any other, from this software. (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 @@ -35,3 +35,12 @@ [ns & body] `(binding [*ns* (the-ns ~ns)] ~@(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"))) \ No newline at end of file diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index ec15775..00776d8 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -60,9 +60,9 @@ (deftest insert-a-basic-db-object-without-id-and-with-default-write-concern (let [collection "people" 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) - (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 }] (monger.collection/insert collection doc) (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/to-db-object doc) found-one)))) @@ -176,7 +176,7 @@ (monger.collection/insert collection doc) (def #^DBObject loaded (monger.collection/find-one collection { :language "Clojure" } fields)) (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"))))) @@ -329,9 +329,9 @@ (deftest save-a-new-basic-db-object (let [collection "people" 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) - (is (not (nil? (.get ^DBObject doc "_id")))))) + (is (not (nil? (monger.util/get-id doc)))))) diff --git a/test/monger/test/util.clj b/test/monger/test/util.clj new file mode 100644 index 0000000..4317b1f --- /dev/null +++ b/test/monger/test/util.clj @@ -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))))) + +