From f6a80fa40a7e07f1d61a52637a6af365c230b614 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 13 Feb 2012 22:52:03 +0400 Subject: [PATCH] Make sure monger.collection/find-by-id and /find-map-by-id fail fast when id is nil --- src/monger/collection.clj | 31 ++++++++++++++++++---------- test/monger/test/regular_finders.clj | 11 ++++++++++ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index ddce83a..b12b308 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -15,6 +15,20 @@ (:require [monger core result]) (:use [monger.conversion])) +;; +;; Implementation +;; + +(defn- fields-to-db-object + [^List fields] + (zipmap fields (repeat 1))) + +(definline check-not-nil! + [ref message] + `(when (nil? ~ref) + (throw (IllegalArgumentException. ~message)))) + + ;; ;; API ;; @@ -66,7 +80,6 @@ ;; ;; monger.collection/find ;; -(declare fields-to-db-object) (defn ^DBCursor find "Queries for objects in this collection. @@ -178,19 +191,25 @@ (mgcol/find-one-by-id collection (ObjectId. \"4ef45ab4744e9fd632640e2d\") [:language]) " ([^String collection id] + (check-not-nil! id "id must not be nil") (find-one collection { :_id id })) ([^String collection id ^List fields] + (check-not-nil! id "id must not be nil") (find-one collection { :_id id } fields)) ([^DB db ^String collection id ^List fields] + (check-not-nil! id "id must not be nil") (find-one db collection { :_id id } fields))) (defn ^IPersistentMap find-map-by-id "Returns a single object, converted to map with matching _id field." ([^String collection id] + (check-not-nil! id "id must not be nil") (from-db-object ^DBObject (find-one-as-map collection { :_id id }) true)) ([^String collection id keywordize] + (check-not-nil! id "id must not be nil") (from-db-object ^DBObject (find-one-as-map collection { :_id id }) keywordize)) ([^String collection id ^List fields keywordize] + (check-not-nil! id "id must not be nil") (from-db-object ^DBObject (find-one-as-map collection { :_id id } fields) keywordize))) @@ -503,13 +522,3 @@ ([^DB db ^String collection ^String key ^Map query] (let [^DBCollection coll (.getCollection db collection)] (.distinct coll ^String (to-db-object key) ^DBObject (to-db-object query))))) - - - -;; -;; Implementation -;; - -(defn- fields-to-db-object - [^List fields] - (zipmap fields (repeat 1))) diff --git a/test/monger/test/regular_finders.clj b/test/monger/test/regular_finders.clj index bad3f0a..fa636dd 100644 --- a/test/monger/test/regular_finders.clj +++ b/test/monger/test/regular_finders.clj @@ -101,6 +101,11 @@ doc-id (monger.util/random-uuid)] (is (nil? (mgcol/find-by-id collection doc-id))))) +(deftest find-full-document-by-string-id-when-id-is-nil + (let [collection "libraries" + doc-id nil] + (is (thrown? IllegalArgumentException (mgcol/find-by-id collection doc-id))))) + (deftest find-full-document-by-object-id-when-that-document-does-not-exist (let [collection "libraries" doc-id (ObjectId.)] @@ -111,6 +116,12 @@ doc-id (monger.util/random-uuid)] (is (nil? (mgcol/find-map-by-id collection doc-id))))) +(deftest find-full-document-by-id-as-map-when-id-is-nil + (let [collection "libraries" + doc-id nil] + (is (thrown? IllegalArgumentException + (mgcol/find-map-by-id collection doc-id))))) + (deftest find-full-document-by-string-id-when-document-does-exist (let [collection "libraries"