Merge pull request #11 from tobyhede/b51aae652403e98c4979191bfda60a776272836f
monger.db namespace and funcs
This commit is contained in:
commit
1494cc5ad4
3 changed files with 88 additions and 1 deletions
43
src/monger/db.clj
Normal file
43
src/monger/db.clj
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
;; Copyright (c) 2011 Michael S. Klishin
|
||||||
|
;;
|
||||||
|
;; The use and distribution terms for this software are covered by the
|
||||||
|
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
||||||
|
;; which can be found in the file epl-v10.html at the root of this distribution.
|
||||||
|
;; By using this software in any fashion, you are agreeing to be bound by
|
||||||
|
;; the terms of this license.
|
||||||
|
;; You must not remove this notice, or any other, from this software.
|
||||||
|
|
||||||
|
(ns monger.db
|
||||||
|
(:refer-clojure :exclude [find remove count drop distinct empty?])
|
||||||
|
(:import [com.mongodb Mongo DB DBCollection])
|
||||||
|
(:require [monger core]))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(defn add-user
|
||||||
|
"Adds a new user for this db"
|
||||||
|
([^String username, ^chars password]
|
||||||
|
(.addUser ^DB monger.core/*mongodb-database* username password))
|
||||||
|
([^DB database ^String username, ^chars password]
|
||||||
|
(.addUser ^DB database username password)))
|
||||||
|
|
||||||
|
|
||||||
|
(defn drop-db
|
||||||
|
"Drops the currently set database (via core/set-db) or the specified database."
|
||||||
|
([]
|
||||||
|
(.dropDatabase ^DB monger.core/*mongodb-database*))
|
||||||
|
([^DB database]
|
||||||
|
(.dropDatabase ^DB database)))
|
||||||
|
|
||||||
|
|
||||||
|
(defn get-collection-names
|
||||||
|
"Returns a set containing the names of all collections in this database."
|
||||||
|
([]
|
||||||
|
(into #{} (.getCollectionNames ^DB monger.core/*mongodb-database*)))
|
||||||
|
([^DB database]
|
||||||
|
(into #{} (.getCollectionNames ^DB database)))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
(ns monger.test.core
|
(ns monger.test.core
|
||||||
(:require [monger core collection util result]
|
(:require [monger core collection util result]
|
||||||
[monger.test.helper :as helper])
|
[monger.test.helper :as helper]
|
||||||
|
[monger.collection :as mgcol])
|
||||||
(:import (com.mongodb Mongo DB WriteConcern))
|
(:import (com.mongodb Mongo DB WriteConcern))
|
||||||
(:use [clojure.test]))
|
(:use [clojure.test]))
|
||||||
|
|
||||||
|
|
@ -26,6 +27,7 @@
|
||||||
db (monger.core/get-db connection "monger-test")]
|
db (monger.core/get-db connection "monger-test")]
|
||||||
(is (instance? com.mongodb.DB db))))
|
(is (instance? com.mongodb.DB db))))
|
||||||
|
|
||||||
|
|
||||||
;; (deftest get-database-with-valid-credentials
|
;; (deftest get-database-with-valid-credentials
|
||||||
;; (let [connection (monger.core/connect)
|
;; (let [connection (monger.core/connect)
|
||||||
;; db (monger.core/get-db connection "monger-test" "monger" "test_password")]
|
;; db (monger.core/get-db connection "monger-test" "monger" "test_password")]
|
||||||
|
|
|
||||||
42
test/monger/test/db.clj
Normal file
42
test/monger/test/db.clj
Normal file
|
|
@ -0,0 +1,42 @@
|
||||||
|
(ns monger.test.db
|
||||||
|
(:require [monger core db]
|
||||||
|
[monger.test.helper :as helper]
|
||||||
|
[monger.collection :as mgcol])
|
||||||
|
(:import (com.mongodb Mongo DB))
|
||||||
|
(:use [clojure.test]))
|
||||||
|
|
||||||
|
(helper/connect!)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
(deftest add-user
|
||||||
|
(let [username "clojurewerkz/monger!"
|
||||||
|
pwd (.toCharArray "monger!")]
|
||||||
|
(monger.db/add-user username pwd)
|
||||||
|
(is (monger.core/authenticate "monger-test" username pwd))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest drop-database
|
||||||
|
(let [collection "test"
|
||||||
|
_ (mgcol/insert collection { :name "Clojure" })
|
||||||
|
check (mgcol/count collection)
|
||||||
|
_ (monger.db/drop-db)
|
||||||
|
]
|
||||||
|
(is (= 1 check))
|
||||||
|
(is (not (mgcol/exists? collection)))
|
||||||
|
(is (= 0 (mgcol/count collection)))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
(deftest get-collection-names
|
||||||
|
(mgcol/insert "test-1" { :name "Clojure" })
|
||||||
|
(mgcol/insert "test-2" { :name "Clojure" })
|
||||||
|
(let [collections (monger.db/get-collection-names)]
|
||||||
|
(is (.contains collections "test-1"))
|
||||||
|
(is (.contains collections "test-2"))
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
Reference in a new issue