Index tests
This commit is contained in:
parent
1a9ef79536
commit
1816cab2ba
2 changed files with 107 additions and 29 deletions
|
|
@ -605,17 +605,31 @@
|
||||||
(let [opts' (->CreateCollectionOptions opts)]
|
(let [opts' (->CreateCollectionOptions opts)]
|
||||||
(.createCollection db coll opts'))))
|
(.createCollection db coll opts'))))
|
||||||
|
|
||||||
|
(defn ->RenameCollectionOptions
|
||||||
|
"Coerce options map into RenameCollectionOptions. See `rename` usage."
|
||||||
|
[{:keys [rename-collection-options drop-target?]}]
|
||||||
|
(let [opts (or rename-collection-options (RenameCollectionOptions.))]
|
||||||
|
(when (some? drop-target?) (.dropTarget opts drop-target?))
|
||||||
|
|
||||||
|
opts))
|
||||||
|
|
||||||
(defn rename
|
(defn rename
|
||||||
"Renames `coll` to `new-coll` in the same DB."
|
"Renames `coll` to `new-coll` in the same DB.
|
||||||
[^MongoDatabase db coll new-coll opts]
|
|
||||||
(let [{:keys [drop-target?]} opts
|
|
||||||
opts' (RenameCollectionOptions.)]
|
|
||||||
|
|
||||||
(when drop-target? (.dropTarget opts' true))
|
Takes an options map:
|
||||||
|
-- query options
|
||||||
|
:drop-target? Boolean drop tne target collection if it exists. Default: false
|
||||||
|
|
||||||
(.renameCollection (collection db coll opts)
|
:rename-collection-options A RenameCollectionOptions for configuring directly. If specified,
|
||||||
(MongoNamespace. (.getName db) new-coll)
|
any other query options will be applied to it"
|
||||||
opts')))
|
([^MongoDatabase db coll new-coll]
|
||||||
|
(rename db coll new-coll {}))
|
||||||
|
([^MongoDatabase db coll new-coll opts]
|
||||||
|
(let [opts' (->RenameCollectionOptions opts)]
|
||||||
|
|
||||||
|
(.renameCollection (collection db coll opts)
|
||||||
|
(MongoNamespace. (.getName db) new-coll)
|
||||||
|
opts'))))
|
||||||
|
|
||||||
(defn drop
|
(defn drop
|
||||||
"Drops a collection from a database."
|
"Drops a collection from a database."
|
||||||
|
|
@ -666,30 +680,24 @@
|
||||||
|
|
||||||
-- optional
|
-- optional
|
||||||
any attributes available in `->IndexOptions`"
|
any attributes available in `->IndexOptions`"
|
||||||
[^MongoDatabase db coll indexes]
|
([^MongoDatabase db coll indexes]
|
||||||
(->> indexes
|
(create-indexes db coll indexes {}))
|
||||||
(map (fn [x] (IndexModel. (document (:keys x)) (->IndexOptions x))))
|
([^MongoDatabase db coll indexes opts]
|
||||||
(.createIndexes (collection db coll))))
|
(->> indexes
|
||||||
|
(map (fn [x] (IndexModel. (document (:keys x)) (->IndexOptions x))))
|
||||||
|
(.createIndexes (collection db coll opts)))))
|
||||||
|
|
||||||
(defn list-indexes
|
(defn list-indexes
|
||||||
"Lists indexes."
|
"Lists indexes."
|
||||||
[^MongoDatabase db coll]
|
([^MongoDatabase db coll]
|
||||||
(->> (.listIndexes (collection db coll))
|
(list-indexes db coll {}))
|
||||||
(map #(from-document % true))))
|
([^MongoDatabase db coll opts]
|
||||||
|
(->> (.listIndexes (collection db coll opts))
|
||||||
|
(map #(from-document % true)))))
|
||||||
|
|
||||||
;;; Utility functions
|
;;; Utility functions
|
||||||
|
|
||||||
(defn empty?
|
(defn- with-transaction
|
||||||
"Returns true if a collection is empty"
|
|
||||||
[^MongoDatabase db coll]
|
|
||||||
(zero? (count-documents db coll)))
|
|
||||||
|
|
||||||
(defn exists?
|
|
||||||
"Returns true if collection exists"
|
|
||||||
[^MongoDatabase db coll]
|
|
||||||
(some #(= coll %) (.listCollectionNames db)))
|
|
||||||
|
|
||||||
(defn with-transaction
|
|
||||||
"Executes `body` in a transaction.
|
"Executes `body` in a transaction.
|
||||||
|
|
||||||
`body` should be a fn with one or more mongo operations in it.
|
`body` should be a fn with one or more mongo operations in it.
|
||||||
|
|
|
||||||
|
|
@ -4,9 +4,10 @@
|
||||||
[mongo-driver-3.collection :as mc])
|
[mongo-driver-3.collection :as mc])
|
||||||
(:import (com.mongodb ReadConcern ReadPreference WriteConcern)
|
(:import (com.mongodb ReadConcern ReadPreference WriteConcern)
|
||||||
(java.util.concurrent TimeUnit)
|
(java.util.concurrent TimeUnit)
|
||||||
(com.mongodb.client.model InsertOneOptions InsertManyOptions DeleteOptions FindOneAndUpdateOptions ReturnDocument FindOneAndReplaceOptions CountOptions UpdateOptions ReplaceOptions IndexOptions CreateCollectionOptions)
|
(com.mongodb.client.model InsertOneOptions InsertManyOptions DeleteOptions FindOneAndUpdateOptions ReturnDocument FindOneAndReplaceOptions CountOptions UpdateOptions ReplaceOptions IndexOptions CreateCollectionOptions RenameCollectionOptions)
|
||||||
(java.time ZoneId LocalDate LocalTime LocalDateTime)
|
(java.time ZoneId LocalDate LocalTime LocalDateTime)
|
||||||
(java.util Date UUID)))
|
(java.util Date UUID)
|
||||||
|
(com.mongodb.client MongoDatabase)))
|
||||||
|
|
||||||
;;; Unit
|
;;; Unit
|
||||||
|
|
||||||
|
|
@ -97,6 +98,16 @@
|
||||||
(let [opts (DeleteOptions.)]
|
(let [opts (DeleteOptions.)]
|
||||||
(is (= opts (mc/->DeleteOptions {:delete-options opts})) "configure directly")))
|
(is (= opts (mc/->DeleteOptions {:delete-options opts})) "configure directly")))
|
||||||
|
|
||||||
|
(deftest test->RenameCollectionOptions
|
||||||
|
(is (instance? RenameCollectionOptions (mc/->RenameCollectionOptions {})))
|
||||||
|
(are [expected arg]
|
||||||
|
(= expected (.isDropTarget (mc/->RenameCollectionOptions {:drop-target? arg})))
|
||||||
|
true true
|
||||||
|
false false
|
||||||
|
false nil)
|
||||||
|
(let [opts (RenameCollectionOptions.)]
|
||||||
|
(is (= opts (mc/->RenameCollectionOptions {:rename-collection-options opts})) "configure directly")))
|
||||||
|
|
||||||
(deftest test->FindOneAndUpdateOptions
|
(deftest test->FindOneAndUpdateOptions
|
||||||
(is (instance? FindOneAndUpdateOptions (mc/->FindOneAndUpdateOptions {})))
|
(is (instance? FindOneAndUpdateOptions (mc/->FindOneAndUpdateOptions {})))
|
||||||
(let [opts (FindOneAndUpdateOptions.)]
|
(let [opts (FindOneAndUpdateOptions.)]
|
||||||
|
|
@ -459,4 +470,63 @@
|
||||||
(let [res (mc/update-many db "test" {:id 1} {:$set {:r 1}} {:upsert? true})]
|
(let [res (mc/update-many db "test" {:id 1} {:$set {:r 1}} {:upsert? true})]
|
||||||
(is (= 0 (.getMatchedCount res)))
|
(is (= 0 (.getMatchedCount res)))
|
||||||
(is (= 0 (.getModifiedCount res)))
|
(is (= 0 (.getModifiedCount res)))
|
||||||
(is (some? (.getUpsertedId res)))))))
|
(is (some? (.getUpsertedId res)))))))
|
||||||
|
|
||||||
|
(defn- coll-exists?
|
||||||
|
"Returns true if collection exists"
|
||||||
|
[db coll]
|
||||||
|
(some #(= coll %) (.listCollectionNames db)))
|
||||||
|
|
||||||
|
(deftest ^:integration test-create
|
||||||
|
(testing "not existing"
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create db "my-coll")]
|
||||||
|
(is (true? (coll-exists? db "my-coll")))))
|
||||||
|
|
||||||
|
(testing "existing"
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create db "my-coll")]
|
||||||
|
(is (thrown? Exception (mc/create db "my-coll"))))))
|
||||||
|
|
||||||
|
(deftest ^:integration test-rename
|
||||||
|
(testing "not existing"
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create db "old")
|
||||||
|
_ (mc/rename db "old" "new")]
|
||||||
|
(is (not (coll-exists? db "old")))
|
||||||
|
(is (true? (coll-exists? db "new")))))
|
||||||
|
|
||||||
|
(testing "existing"
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create db "old")
|
||||||
|
_ (mc/create db "new")]
|
||||||
|
(is (thrown? Exception (mc/rename db "old" "new")))
|
||||||
|
(mc/rename db "old" "new" {:drop-target? true})
|
||||||
|
(is (not (coll-exists? db "old")))
|
||||||
|
(is (true? (coll-exists? db "new"))))))
|
||||||
|
|
||||||
|
(deftest ^:integration test-drop
|
||||||
|
(testing "existing"
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create db "my-coll")
|
||||||
|
_ (mc/drop db "my-coll")]
|
||||||
|
(is (not (coll-exists? db "my-coll")))))
|
||||||
|
|
||||||
|
(testing "not existing"
|
||||||
|
(let [db (new-db @client)]
|
||||||
|
(is (nil? (mc/drop db "my-coll"))))))
|
||||||
|
|
||||||
|
(deftest ^:integration test-list-indexes
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create db "test")]
|
||||||
|
(is (= 1 (count (mc/list-indexes db "test"))) "has default index")))
|
||||||
|
|
||||||
|
(deftest ^:integration test-create-index
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create-index db "test" {:a 1})]
|
||||||
|
(is (= [{:_id 1} {:a 1}] (map :key (mc/list-indexes db "test"))))))
|
||||||
|
|
||||||
|
(deftest ^:integration test-create-indexes
|
||||||
|
(let [db (new-db @client)
|
||||||
|
_ (mc/create-indexes db "test" [{:keys {:a 1}} {:keys {:b 1}}])]
|
||||||
|
(is (= [{:_id 1} {:a 1} {:b 1}] (map :key (mc/list-indexes db "test"))))))
|
||||||
Loading…
Reference in a new issue