Implement monger.core/connect and monger.core/get-db
This commit is contained in:
parent
c70074a3ed
commit
1299aecb3a
2 changed files with 61 additions and 4 deletions
|
|
@ -1 +1,35 @@
|
||||||
(ns monger.core)
|
(ns monger.core
|
||||||
|
(:import (com.mongodb Mongo DB))
|
||||||
|
)
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Defaults
|
||||||
|
;;
|
||||||
|
|
||||||
|
(def ^:dynamic *default-host* "localhost")
|
||||||
|
(def ^:dynamic *default-port* 27017)
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; Protocols
|
||||||
|
;;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
;;
|
||||||
|
;; API
|
||||||
|
;;
|
||||||
|
|
||||||
|
(defn ^Mongo connect
|
||||||
|
"Connects to MongoDB"
|
||||||
|
([]
|
||||||
|
(Mongo.))
|
||||||
|
([{ :keys [host port] :or { host *default-host*, port *default-port* }}]
|
||||||
|
(Mongo. host port)))
|
||||||
|
|
||||||
|
(defn ^DB get-db
|
||||||
|
"Get database reference by name"
|
||||||
|
[^Mongo connection, ^String name]
|
||||||
|
(.getDB connection name))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,29 @@
|
||||||
(ns monger.test.core
|
(ns monger.test.core
|
||||||
(:use [monger.core])
|
(:require [monger.core])
|
||||||
|
(:import (com.mongodb Mongo DB))
|
||||||
(:use [clojure.test]))
|
(:use [clojure.test]))
|
||||||
|
|
||||||
(deftest replace-me ;; FIXME: write
|
(deftest connect-to-mongo-with-default-host-and-port
|
||||||
(is false "No tests have been written."))
|
(let [connection (monger.core/connect)]
|
||||||
|
(is (instance? com.mongodb.Mongo connection))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest connect-to-mongo-with-default-host-and-explicit-port
|
||||||
|
(let [connection (monger.core/connect { :port 27017 })]
|
||||||
|
(is (instance? com.mongodb.Mongo connection))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest connect-to-mongo-with-default-port-and-explicit-host
|
||||||
|
(let [connection (monger.core/connect { :host "127.0.0.1" })]
|
||||||
|
(is (instance? com.mongodb.Mongo connection))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest get-database
|
||||||
|
(let [connection (monger.core/connect)
|
||||||
|
db (monger.core/get-db connection "monger-test")]
|
||||||
|
(is (instance? com.mongodb.DB db))))
|
||||||
|
|
||||||
|
;; (deftest get-database-with-valid-credentials
|
||||||
|
;; (let [connection (monger.core/connect)
|
||||||
|
;; db (monger.core/get-db connection "monger-test" "monger" "test_password")]
|
||||||
|
;; (is (instance? com.mongodb.DB db))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue