Implement monger.core/connect and monger.core/get-db

This commit is contained in:
Michael S. Klishin 2011-08-04 17:44:31 +04:00
parent c70074a3ed
commit 1299aecb3a
2 changed files with 61 additions and 4 deletions

View file

@ -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))

View file

@ -1,6 +1,29 @@
(ns monger.test.core
(:use [monger.core])
(:require [monger.core])
(:import (com.mongodb Mongo DB))
(:use [clojure.test]))
(deftest replace-me ;; FIXME: write
(is false "No tests have been written."))
(deftest connect-to-mongo-with-default-host-and-port
(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))))