From 66e5e929ad202241c97ad0c211e8e285d326c5f5 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Tue, 14 Feb 2012 21:48:59 +1100 Subject: [PATCH 1/5] add get-collections-names to core --- src/monger/core.clj | 8 ++++++++ test/monger/test/core.clj | 12 +++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/monger/core.clj b/src/monger/core.clj index 90fe581..e538bc9 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -170,6 +170,14 @@ [^Map cmd] (.command ^DB *mongodb-database* ^DBObject (to-db-object cmd))) +(defn ^Set get-collection-names + "Returns a set containing the names of all collections in this database.." + ([] + (.getCollectionNames ^DB *mongodb-database*)) + ([^DB database] + (.getCollectionNames ^DB database)) +) + (defprotocol Countable (count [this] "Returns size of the object")) diff --git a/test/monger/test/core.clj b/test/monger/test/core.clj index 3f14d0a..c43bd1b 100644 --- a/test/monger/test/core.clj +++ b/test/monger/test/core.clj @@ -1,6 +1,7 @@ (ns monger.test.core (: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)) (:use [clojure.test])) @@ -26,6 +27,15 @@ db (monger.core/get-db connection "monger-test")] (is (instance? com.mongodb.DB db)))) + +(deftest get-collection-names + (mgcol/insert "test-1" { :name "Clojure" }) + (mgcol/insert "test-2" { :name "Clojure" }) + (let [collections (monger.core/get-collection-names)] + (is (.contains collections "test-1")) + (is (.contains collections "test-2")) + )) + ;; (deftest get-database-with-valid-credentials ;; (let [connection (monger.core/connect) ;; db (monger.core/get-db connection "monger-test" "monger" "test_password")] From 300220da15a2a8879c0765d86427816761077c48 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Wed, 15 Feb 2012 21:00:01 +1100 Subject: [PATCH 2/5] moved get-collection-names to new db ns --- src/monger/core.clj | 8 -------- src/monger/db.clj | 26 ++++++++++++++++++++++++++ test/monger/test/core.clj | 8 -------- test/monger/test/db.clj | 19 +++++++++++++++++++ 4 files changed, 45 insertions(+), 16 deletions(-) create mode 100644 src/monger/db.clj create mode 100644 test/monger/test/db.clj diff --git a/src/monger/core.clj b/src/monger/core.clj index e538bc9..90fe581 100644 --- a/src/monger/core.clj +++ b/src/monger/core.clj @@ -170,14 +170,6 @@ [^Map cmd] (.command ^DB *mongodb-database* ^DBObject (to-db-object cmd))) -(defn ^Set get-collection-names - "Returns a set containing the names of all collections in this database.." - ([] - (.getCollectionNames ^DB *mongodb-database*)) - ([^DB database] - (.getCollectionNames ^DB database)) -) - (defprotocol Countable (count [this] "Returns size of the object")) diff --git a/src/monger/db.clj b/src/monger/db.clj new file mode 100644 index 0000000..345e6fb --- /dev/null +++ b/src/monger/db.clj @@ -0,0 +1,26 @@ +;; 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 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))) +) + + + diff --git a/test/monger/test/core.clj b/test/monger/test/core.clj index c43bd1b..62b7513 100644 --- a/test/monger/test/core.clj +++ b/test/monger/test/core.clj @@ -28,14 +28,6 @@ (is (instance? com.mongodb.DB db)))) -(deftest get-collection-names - (mgcol/insert "test-1" { :name "Clojure" }) - (mgcol/insert "test-2" { :name "Clojure" }) - (let [collections (monger.core/get-collection-names)] - (is (.contains collections "test-1")) - (is (.contains collections "test-2")) - )) - ;; (deftest get-database-with-valid-credentials ;; (let [connection (monger.core/connect) ;; db (monger.core/get-db connection "monger-test" "monger" "test_password")] diff --git a/test/monger/test/db.clj b/test/monger/test/db.clj new file mode 100644 index 0000000..300bf4c --- /dev/null +++ b/test/monger/test/db.clj @@ -0,0 +1,19 @@ +(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 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")) + )) + + From 62e0fb945edb1fe58f0f9fdbffe023c464d8d269 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Wed, 15 Feb 2012 21:32:45 +1100 Subject: [PATCH 3/5] test for drop database --- src/monger/db.clj | 10 +++++++++- test/monger/test/db.clj | 13 +++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/monger/db.clj b/src/monger/db.clj index 345e6fb..330d500 100644 --- a/src/monger/db.clj +++ b/src/monger/db.clj @@ -13,9 +13,16 @@ (:require [monger core])) +(defn drop-db + "Drops 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.." + "Returns a set containing the names of all collections in this database." ([] (into #{} (.getCollectionNames ^DB monger.core/*mongodb-database*))) ([^DB database] @@ -24,3 +31,4 @@ + diff --git a/test/monger/test/db.clj b/test/monger/test/db.clj index 300bf4c..b8f2406 100644 --- a/test/monger/test/db.clj +++ b/test/monger/test/db.clj @@ -17,3 +17,16 @@ )) +(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))) + ) +) + + From 3151587d671b82b202924c01f8dacc617b3cc033 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Fri, 17 Feb 2012 19:12:16 +1100 Subject: [PATCH 4/5] add-user and test, db ns --- src/monger/db.clj | 9 +++++++++ test/monger/test/db.clj | 24 +++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/monger/db.clj b/src/monger/db.clj index 330d500..80d34e0 100644 --- a/src/monger/db.clj +++ b/src/monger/db.clj @@ -13,6 +13,15 @@ (: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 specified database." ([] diff --git a/test/monger/test/db.clj b/test/monger/test/db.clj index b8f2406..b360e56 100644 --- a/test/monger/test/db.clj +++ b/test/monger/test/db.clj @@ -8,13 +8,12 @@ (helper/connect!) -(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")) - )) + +(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 @@ -30,3 +29,14 @@ ) +(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")) + )) + + + + From b51aae652403e98c4979191bfda60a776272836f Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Sat, 18 Feb 2012 12:18:03 +1100 Subject: [PATCH 5/5] clarified documentation for drop-database --- src/monger/db.clj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/monger/db.clj b/src/monger/db.clj index 80d34e0..fc9a781 100644 --- a/src/monger/db.clj +++ b/src/monger/db.clj @@ -23,7 +23,7 @@ (defn drop-db - "Drops the specified database." + "Drops the currently set database (via core/set-db) or the specified database." ([] (.dropDatabase ^DB monger.core/*mongodb-database*)) ([^DB database]