From f0d190cff7ed8819d46138d49442517607d73011 Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Mon, 2 Apr 2012 12:14:24 +0400 Subject: [PATCH] Support conversion of Clojure ratios to MongoDB data types We go with doubles because it is the only realistic solution that is interoperable with all other technologies. Plus, Clojure ratios are just lazily evaluated doubles anyway. --- src/monger/conversion.clj | 6 +++++- test/monger/test/conversion.clj | 5 +++++ test/monger/test/inserting.clj | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/monger/conversion.clj b/src/monger/conversion.clj index 23ca31b..76f41bb 100644 --- a/src/monger/conversion.clj +++ b/src/monger/conversion.clj @@ -23,7 +23,7 @@ (ns monger.conversion (:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor] - [clojure.lang IPersistentMap Keyword] + [clojure.lang IPersistentMap Keyword Ratio] [java.util List Map Date] [org.bson.types ObjectId])) @@ -39,6 +39,10 @@ (to-db-object [input] input) + Ratio + (to-db-object [^Ratio input] + (double input)) + Keyword (to-db-object [^Keyword input] (.getName input)) diff --git a/test/monger/test/conversion.clj b/test/monger/test/conversion.clj index 2d15643..dfa960e 100644 --- a/test/monger/test/conversion.clj +++ b/test/monger/test/conversion.clj @@ -26,6 +26,11 @@ output (cnv/to-db-object input)] (is (= input output)))) +(deftest convert-rationale-to-dbobject + (let [input 11/2 + output (cnv/to-db-object input)] + (is (= 5.5 output)))) + (deftest convert-string-to-dbobject (let [input "MongoDB" output (cnv/to-db-object input)] diff --git a/test/monger/test/inserting.clj b/test/monger/test/inserting.clj index 366e97e..7924451 100644 --- a/test/monger/test/inserting.clj +++ b/test/monger/test/inserting.clj @@ -55,6 +55,13 @@ result (mgcol/insert "people" doc)] (is (= id (monger.util/get-id doc))))) +(deftest insert-a-document-with-clojure-ratio-in-it + (let [collection "widgets" + id (ObjectId.) + doc { :ratio 11/2 "_id" id } + result (mgcol/insert "widgets" doc)] + (is (= 5.5 (:ratio (mgcol/find-map-by-id collection id)))))) + ;;