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.
This commit is contained in:
parent
59b32e119c
commit
f0d190cff7
3 changed files with 17 additions and 1 deletions
|
|
@ -23,7 +23,7 @@
|
||||||
|
|
||||||
(ns monger.conversion
|
(ns monger.conversion
|
||||||
(:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor]
|
(:import [com.mongodb DBObject BasicDBObject BasicDBList DBCursor]
|
||||||
[clojure.lang IPersistentMap Keyword]
|
[clojure.lang IPersistentMap Keyword Ratio]
|
||||||
[java.util List Map Date]
|
[java.util List Map Date]
|
||||||
[org.bson.types ObjectId]))
|
[org.bson.types ObjectId]))
|
||||||
|
|
||||||
|
|
@ -39,6 +39,10 @@
|
||||||
(to-db-object [input]
|
(to-db-object [input]
|
||||||
input)
|
input)
|
||||||
|
|
||||||
|
Ratio
|
||||||
|
(to-db-object [^Ratio input]
|
||||||
|
(double input))
|
||||||
|
|
||||||
Keyword
|
Keyword
|
||||||
(to-db-object [^Keyword input] (.getName input))
|
(to-db-object [^Keyword input] (.getName input))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,11 @@
|
||||||
output (cnv/to-db-object input)]
|
output (cnv/to-db-object input)]
|
||||||
(is (= input output))))
|
(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
|
(deftest convert-string-to-dbobject
|
||||||
(let [input "MongoDB"
|
(let [input "MongoDB"
|
||||||
output (cnv/to-db-object input)]
|
output (cnv/to-db-object input)]
|
||||||
|
|
|
||||||
|
|
@ -55,6 +55,13 @@
|
||||||
result (mgcol/insert "people" doc)]
|
result (mgcol/insert "people" doc)]
|
||||||
(is (= id (monger.util/get-id 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))))))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;;
|
;;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue