Initial monger.convertion.* protocols work
This commit is contained in:
parent
98d292fcc0
commit
6c0eb8f1e0
2 changed files with 53 additions and 0 deletions
23
src/monger/convertion.clj
Normal file
23
src/monger/convertion.clj
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
(ns monger.convertion
|
||||
(:import (com.mongodb DBObject BasicDBObject)
|
||||
(java.util Map List)))
|
||||
|
||||
(defprotocol ConvertToDBObject
|
||||
(to-db-object [input] "Converts given piece of Clojure data to BasicDBObject MongoDB Java driver uses"))
|
||||
|
||||
(extend-protocol ConvertToDBObject
|
||||
nil
|
||||
(to-db-object [input]
|
||||
input)
|
||||
Object
|
||||
(to-db-object [input]
|
||||
input)
|
||||
Map
|
||||
(to-db-object [input]
|
||||
{}))
|
||||
|
||||
|
||||
|
||||
|
||||
(defprotocol ConvertFromDBObject
|
||||
(from-db-object [input] "Converts given DBObject instance to a piece of Clojure data"))
|
||||
30
test/monger/test/convertion.clj
Normal file
30
test/monger/test/convertion.clj
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
(ns monger.test.convertion
|
||||
(:require [monger core collection convertion])
|
||||
(:use [clojure.test]))
|
||||
|
||||
(deftest convert-nil-to-dbobject
|
||||
(let [input nil
|
||||
output (monger.convertion/to-db-object input)]
|
||||
(is (nil? output))))
|
||||
|
||||
(deftest convert-integer-to-dbobject
|
||||
(let [input 1
|
||||
output (monger.convertion/to-db-object input)]
|
||||
(is (= input output))))
|
||||
|
||||
(deftest convert-float-to-dbobject
|
||||
(let [input 11.12
|
||||
output (monger.convertion/to-db-object input)]
|
||||
(is (= input output))))
|
||||
|
||||
(deftest convert-string-to-dbobject
|
||||
(let [input "MongoDB"
|
||||
output (monger.convertion/to-db-object input)]
|
||||
(is (= input output))))
|
||||
|
||||
|
||||
(deftest convert-map-to-dbobject
|
||||
(let [input { :int 1, :string "Mongo", :float 22.23 }
|
||||
output (monger.convertion/to-db-object input)]
|
||||
(is (= 1 (.get output "int")))
|
||||
))
|
||||
Loading…
Reference in a new issue