Skip to content

Commit

Permalink
with-db macro to provide local binding of current db, with test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Walker committed Feb 23, 2012
1 parent 105fddb commit d90649e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/somnium/congomongo.clj
Expand Up @@ -130,6 +130,15 @@ object may be passed as the last argument."
(binding [*mongo-config* c#]
~@body))))

(defmacro with-db
"Make title the active database in the enclosing scope.
When with-db and set-database! interact, last one wins."
[title & body]
`(let [db# (.getDB (:mongo *mongo-config*) (name ~title))]
(binding [*mongo-config* (assoc *mongo-config* :db db#)]
~@body)))

(defn set-connection!
"Makes the connection active. Takes a connection created by make-connection.
Expand Down
10 changes: 10 additions & 0 deletions test/somnium/test/congomongo.clj
Expand Up @@ -79,6 +79,16 @@
;; check a default option as well
(is (not (.slaveOk opts))))))

(deftest with-mongo-database
(with-test-mongo
(let [a (make-connection "congomongotest-db-a" :host test-db-host :port test-db-port)]
(with-mongo a
(with-db "congomongotest-db-b"
(testing "with-mongo uses new database"
(is (= "congomongotest-db-b" (.getName (*mongo-config* :db))))))
(testing "with-mongo uses connection db "
(is (= "congomongotest-db-a" (.getName (*mongo-config* :db)))))))))

(deftest with-mongo-interactions
(with-test-mongo
(let [a (make-connection "congomongotest-db-a" :host test-db-host :port test-db-port)
Expand Down

0 comments on commit d90649e

Please sign in to comment.