From 0b72761b44ffe1dfd4be4a7c4ba8baebade11d8d Mon Sep 17 00:00:00 2001 From: Michael Klishin Date: Sun, 22 Feb 2015 19:26:27 +0300 Subject: [PATCH] Adapt monger.{core,collection} and tests to recent changes Down to 50 failures and 11 errors in `lein test`. --- src/clojure/monger/collection.clj | 4 +- src/clojure/monger/core.clj | 3 +- test/monger/test/atomic_modifiers_test.clj | 4 +- test/monger/test/command_test.clj | 12 +++--- test/monger/test/core_test.clj | 7 ---- test/monger/test/full_text_search_test.clj | 4 +- test/monger/test/inserting_test.clj | 22 ++++++----- test/monger/test/map_reduce_test.clj | 12 ++---- test/monger/test/result_test.clj | 45 ++++------------------ test/monger/test/stress_test.clj | 2 - test/monger/test/updating_test.clj | 8 ++-- 11 files changed, 41 insertions(+), 82 deletions(-) diff --git a/src/clojure/monger/collection.clj b/src/clojure/monger/collection.clj index 05f7366..a6e597a 100644 --- a/src/clojure/monger/collection.clj +++ b/src/clojure/monger/collection.clj @@ -44,8 +44,8 @@ ;; (defn ^WriteResult insert - "Saves document to collection and returns a write result monger.result/ok? - and similar functions operate on. You can optionally specify a WriteConcern. + "Saves document to collection and returns a write result monger.result/acknowledged? + and related functions operate on. You can optionally specify a WriteConcern. In case you need the exact inserted document returned, with the :_id key generated, use monger.collection/insert-and-return instead." diff --git a/src/clojure/monger/core.clj b/src/clojure/monger/core.clj index 73dfb94..a4074ca 100644 --- a/src/clojure/monger/core.clj +++ b/src/clojure/monger/core.clj @@ -18,8 +18,7 @@ * http://clojuremongodb.info/articles/commands.html * http://clojuremongodb.info/articles/gridfs.html" (:refer-clojure :exclude [count]) - (:require [monger.conversion :refer :all] - [monger.result :refer [ok?]]) + (:require [monger.conversion :refer :all]) (:import [com.mongodb MongoClient MongoClientURI DB WriteConcern DBObject DBCursor Bytes MongoClientOptions MongoClientOptions$Builder ServerAddress MapReduceOutput MongoException] [com.mongodb.gridfs GridFS] [java.util Map ArrayList])) diff --git a/test/monger/test/atomic_modifiers_test.clj b/test/monger/test/atomic_modifiers_test.clj index 93d7d56..0eb6fbb 100644 --- a/test/monger/test/atomic_modifiers_test.clj +++ b/test/monger/test/atomic_modifiers_test.clj @@ -4,7 +4,7 @@ java.util.Date) (:require [monger.core :as mg] [monger.collection :as mc] - [monger.result :refer [ok?]] + [monger.result :refer [acknowledged?]] [clojure.test :refer :all] [monger.operators :refer :all])) @@ -124,7 +124,7 @@ (let [coll "docs" oid (ObjectId.)] (mc/insert db coll {:_id oid :title "Document 1" :published true}) - (is (ok? (mc/update db coll {:_id oid} {$unset {:published 1 :featured true}}))) + (is (acknowledged? (mc/update db coll {:_id oid} {$unset {:published 1 :featured true}}))) (is (= {:_id oid :title "Document 1"} (mc/find-map-by-id db coll oid))))) diff --git a/test/monger/test/command_test.clj b/test/monger/test/command_test.clj index cc257ae..7e3743c 100644 --- a/test/monger/test/command_test.clj +++ b/test/monger/test/command_test.clj @@ -3,7 +3,7 @@ [monger.command :as mcom] [monger.collection :as mc] [clojure.test :refer :all] - [monger.result :refer [ok?]] + [monger.result :refer [acknowledged?]] [monger.conversion :refer [from-db-object]])) (let [conn (mg/connect) @@ -11,24 +11,24 @@ (deftest ^{:command true} test-reindex-collection (let [_ (mc/insert db "test" {:name "Clojure"}) result (mcom/reindex-collection db "test")] - (is (ok? result)) + (is (acknowledged? result)) (is (get result "indexes")))) (deftest ^{:command true} test-server-status (let [status (mcom/server-status db)] - (is (ok? status)) + (is (acknowledged? status)) (is (not-empty status)) (is (get status "serverUsed")))) (deftest ^{:command true} test-top (let [result (mcom/top conn)] - (is (ok? result)) + (is (acknowledged? result)) (is (not-empty result)) (is (get result "serverUsed")))) (deftest ^{:command true} test-running-is-master-as-an-arbitrary-command (let [raw (mg/command db {:isMaster 1}) result (from-db-object raw true)] - (is (ok? result)) - (is (ok? raw)) + (is (acknowledged? result)) + (is (acknowledged? raw)) (is (:ismaster result))))) diff --git a/test/monger/test/core_test.clj b/test/monger/test/core_test.clj index f3965b9..a4d7e9d 100644 --- a/test/monger/test/core_test.clj +++ b/test/monger/test/core_test.clj @@ -55,10 +55,3 @@ dbs (mg/get-db-names conn)] (is (not (empty? dbs))) (is (dbs "monger-test")))) - -(deftest get-last-error - (let [connection (mg/connect) - db (mg/get-db connection "monger-test")] - (is (monger.result/ok? (mg/get-last-error db))) - (is (monger.result/ok? (mg/get-last-error db WriteConcern/NORMAL))) - (is (monger.result/ok? (mg/get-last-error db 1 100 true))))) diff --git a/test/monger/test/full_text_search_test.clj b/test/monger/test/full_text_search_test.clj index 321a5e1..3e69143 100644 --- a/test/monger/test/full_text_search_test.clj +++ b/test/monger/test/full_text_search_test.clj @@ -4,7 +4,7 @@ [monger.search :as ms] [monger.command :as cmd] [clojure.test :refer [deftest is use-fixtures]] - [monger.result :refer [ok?]]) + [monger.result :refer [acknowledged?]]) (:import com.mongodb.BasicDBObjectBuilder)) (let [conn (mg/connect) @@ -18,7 +18,7 @@ (.append "setParameter" 1) (.append "textSearchEnabled" true)) cmd (.get bldr)] - (is (ok? (cmd/raw-admin-command conn cmd)))) + (is (acknowledged? (cmd/raw-admin-command conn cmd)))) (f)) (defn purge-collections diff --git a/test/monger/test/inserting_test.clj b/test/monger/test/inserting_test.clj index e9e1341..a80a154 100644 --- a/test/monger/test/inserting_test.clj +++ b/test/monger/test/inserting_test.clj @@ -36,20 +36,20 @@ (deftest insert-a-basic-document-without-id-and-with-default-write-concern (let [collection "people" doc {:name "Joe" :age 30}] - (is (monger.result/ok? (mc/insert db collection doc))) + (is (mc/insert db collection doc)) (is (= 1 (mc/count db collection))))) (deftest insert-a-basic-document-with-explicitly-passed-database-without-id-and-with-default-write-concern (let [collection "people" doc {:name "Joe" :age 30}] (dotimes [n 5] - (is (monger.result/ok? (mc/insert db collection doc WriteConcern/SAFE)))) + (mc/insert db collection doc WriteConcern/SAFE)) (is (= 5 (mc/count db collection))))) (deftest insert-a-basic-document-without-id-and-with-explicit-write-concern (let [collection "people" doc {:name "Joe" :age 30}] - (is (monger.result/ok? (mc/insert db collection doc WriteConcern/SAFE))) + (is (mc/insert db collection doc WriteConcern/SAFE)) (is (= 1 (mc/count db collection))))) (deftest insert-a-basic-db-object-without-id-and-with-default-write-concern @@ -95,7 +95,9 @@ result (mc/insert db collection doc)] (is (= {:rps 10 :eps 20} (:record (mc/find-map-by-id db collection id)))))) - (deftest test-insert-a-document-with-dbref + ;; TODO: disabled until we figure out how to implement dereferencing of DBRefs + ;; in 3.0 in a compatible way (and if that's possible at all). MK. + #_ (deftest test-insert-a-document-with-dbref (mc/remove db "widgets") (mc/remove db "owners") (let [coll1 "widgets" @@ -153,26 +155,26 @@ (deftest insert-a-batch-of-basic-documents-without-ids-and-with-default-write-concern (let [collection "people" docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]] - (is (monger.result/ok? (mc/insert-batch db collection docs))) + (is (mc/insert-batch db collection docs)) (is (= 2 (mc/count db collection))))) (deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern (let [collection "people" docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]] - (is (monger.result/ok? (mc/insert-batch db collection docs WriteConcern/NORMAL))) + (is (mc/insert-batch db collection docs WriteConcern/FSYNCED)) (is (= 2 (mc/count db collection))))) (deftest insert-a-batch-of-basic-documents-with-explicit-database-without-ids-and-with-explicit-write-concern (let [collection "people" docs [{:name "Joe" :age 30} {:name "Paul" :age 27}]] (dotimes [n 44] - (is (monger.result/ok? (mc/insert-batch db collection docs WriteConcern/NORMAL)))) + (is (mc/insert-batch db collection docs WriteConcern/FSYNCED))) (is (= 88 (mc/count db collection))))) (deftest insert-a-batch-of-basic-documents-from-a-lazy-sequence (let [collection "people" numbers (range 0 1000)] - (is (monger.result/ok? (mc/insert-batch db collection (map (fn [^long l] - {:n l}) - numbers)))) + (is (mc/insert-batch db collection (map (fn [^long l] + {:n l}) + numbers))) (is (= (count numbers) (mc/count db collection)))))) diff --git a/test/monger/test/map_reduce_test.clj b/test/monger/test/map_reduce_test.clj index be3f460..936ce11 100644 --- a/test/monger/test/map_reduce_test.clj +++ b/test/monger/test/map_reduce_test.clj @@ -4,7 +4,6 @@ java.util.Date) (:require [monger.collection :as mc] [monger.core :as mg] - [monger.result :as mgres] [clojurewerkz.support.js :as js] [clojure.test :refer :all] [monger.operators :refer :all] @@ -34,18 +33,16 @@ expected [{:_id "CA", :value 204.9} {:_id "IL", :value 39.5} {:_id "NY", :value 697.0}]] (deftest test-basic-inline-map-reduce-example (mc/remove db collection) - (is (mgres/ok? (mc/insert-batch db collection batch))) + (mc/insert-batch db collection batch) (let [output (mc/map-reduce db collection mapper reducer nil MapReduceCommand$OutputType/INLINE {}) results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)] - (mgres/ok? output) (is (= expected results)))) (deftest test-basic-map-reduce-example-that-replaces-named-collection (mc/remove db collection) - (is (mgres/ok? (mc/insert-batch db collection batch))) + (mc/insert-batch db collection batch) (let [output (mc/map-reduce db collection mapper reducer "mr_outputs" {}) results (from-db-object ^DBObject (.results ^MapReduceOutput output) true)] - (mgres/ok? output) (is (= 3 (mg/count results))) (is (= expected (map #(from-db-object % true) (seq results)))) @@ -55,11 +52,10 @@ (deftest test-basic-map-reduce-example-that-merged-results-into-named-collection (mc/remove db collection) - (is (mgres/ok? (mc/insert-batch db collection batch))) + (mc/insert-batch db collection batch) (mc/map-reduce db collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {}) - (is (mgres/ok? (mc/insert db collection { :state "OR" :price 17.95 :quantity 4 }))) + (mc/insert db collection { :state "OR" :price 17.95 :quantity 4 }) (let [^MapReduceOutput output (mc/map-reduce db collection mapper reducer "merged_mr_outputs" MapReduceCommand$OutputType/MERGE {})] - (mgres/ok? output) (is (= 4 (mg/count output))) (is (= ["CA" "IL" "NY" "OR"] (map :_id (mc/find-maps db "merged_mr_outputs")))) diff --git a/test/monger/test/result_test.clj b/test/monger/test/result_test.clj index ab19f12..8d94238 100644 --- a/test/monger/test/result_test.clj +++ b/test/monger/test/result_test.clj @@ -2,40 +2,10 @@ (:import [com.mongodb BasicDBObject WriteResult WriteConcern] java.util.Date) (:require [monger.core :as mg] [monger.collection :as mc] - monger.result + [monger.result :as mgres] monger.util [clojure.test :refer :all])) -(deftest test-ok? - (let [result-that-is-not-ok-1 (doto (BasicDBObject.) (.put "ok" 0)) - result-that-is-not-ok-2 (doto (BasicDBObject.) (.put "ok" "false")) - result-that-is-ok-1 (doto (BasicDBObject.) (.put "ok" 1)) - result-that-is-ok-2 (doto (BasicDBObject.) (.put "ok" "true")) - result-that-is-ok-3 (doto (BasicDBObject.) (.put "ok" 1.0))] - (is (not (monger.result/ok? result-that-is-not-ok-1))) - (is (not (monger.result/ok? result-that-is-not-ok-2))) - (is (monger.result/ok? result-that-is-ok-1)) - (is (monger.result/ok? result-that-is-ok-2)) - (is (monger.result/ok? result-that-is-ok-3)))) - - -(deftest test-has-error? - (let [result-that-has-no-error1 (doto (BasicDBObject.) (.put "ok" 0)) - result-that-has-no-error2 (doto (BasicDBObject.) (.put "err" "")) - result-that-has-error1 (doto (BasicDBObject.) (.put "err" (BasicDBObject.)))] - (is (not (monger.result/has-error? result-that-has-no-error1))) - (is (not (monger.result/has-error? result-that-has-no-error2))) - (is (monger.result/has-error? result-that-has-error1)))) - - -(deftest test-updated-existing?-with-db-object - (let [input1 (doto (BasicDBObject.) (.put "updatedExisting" true)) - input2 (doto (BasicDBObject.) (.put "updatedExisting" false)) - input3 (BasicDBObject.)] - (is (monger.result/updated-existing? input1)) - (is (not (monger.result/updated-existing? input2))) - (is (not (monger.result/updated-existing? input3))))) - (let [conn (mg/connect) db (mg/get-db conn "monger-test")] (deftest test-updated-existing?-with-write-result @@ -43,9 +13,10 @@ (let [collection "libraries" doc-id (monger.util/random-uuid) date (Date.) - doc { :created-at date, :data-store "MongoDB", :language "Clojure", :_id doc-id } - modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }] - (is (not (monger.result/updated-existing? (mc/update db collection { :language "Clojure" } doc {:upsert true})))) - (is (monger.result/updated-existing? (mc/update db collection { :language "Clojure" } doc {:upsert true}))) - (monger.result/updated-existing? (mc/update db collection { :language "Clojure" } modified-doc {:multi false :upsert true})) - (mc/remove db collection)))) + doc { :created-at date :data-store "MongoDB" :language "Clojure" :_id doc-id } + modified-doc { :created-at date :data-store "MongoDB" :language "Erlang" :_id doc-id }] + (is (not (mgres/updated-existing? (mc/update db collection { :language "Clojure" } doc {:upsert true})))) + (is (mgres/updated-existing? (mc/update db collection { :language "Clojure" } doc {:upsert true}))) + (mgres/updated-existing? (mc/update db collection { :language "Clojure" } modified-doc {:multi false :upsert true})) + (mc/remove db collection) + (mg/disconnect conn)))) diff --git a/test/monger/test/stress_test.clj b/test/monger/test/stress_test.clj index 344f4b2..131f48b 100644 --- a/test/monger/test/stress_test.clj +++ b/test/monger/test/stress_test.clj @@ -21,8 +21,6 @@ (use-fixtures :each purge-things-collection) - (monger.core/set-default-write-concern! WriteConcern/NORMAL) - (deftest ^{:performance true} insert-large-batches-of-documents-without-object-ids (doseq [n [10 100 1000 10000 20000]] (let [collection "things" diff --git a/test/monger/test/updating_test.clj b/test/monger/test/updating_test.clj index e33b5ad..3459d33 100644 --- a/test/monger/test/updating_test.clj +++ b/test/monger/test/updating_test.clj @@ -78,7 +78,7 @@ (deftest ^{:updating true} save-a-new-document (let [collection "people" document {:name "Joe" :age 30}] - (is (mr/ok? (mc/save db "people" document))) + (is (mc/save db "people" document)) (is (= 1 (mc/count db collection))))) (deftest ^{:updating true} save-and-return-a-new-document @@ -103,7 +103,7 @@ (let [collection "people" doc-id "people-1" document { :_id doc-id, :name "Joe", :age 30 }] - (is (mr/ok? (mc/insert db collection document))) + (is (mc/insert db collection document)) (is (= 1 (mc/count db collection))) (mc/save db collection { :_id doc-id, :name "Alan", :age 40 }) (is (= 1 (mc/count db collection { :name "Alan", :age 40 }))))) @@ -122,7 +122,7 @@ (let [collection "people" doc-id (mu/object-id) document { :_id doc-id, :name "Joe", :age 30 }] - (is (mr/ok? (mc/insert db collection document))) + (is (mc/insert db collection document)) (is (= 1 (mc/count db collection))) (is (= 0 (mc/count db collection { :has_kids true }))) (mc/update db collection { :_id doc-id } { $set { :has_kids true } }) @@ -134,7 +134,7 @@ doc-id (mu/object-id) document { :_id doc-id :abc 0 :def 10 }] (mc/remove db collection) - (is (mr/ok? (mc/insert db collection document))) + (is (mc/insert db collection document)) (is (= 1 (mc/count db collection {:abc {$exists true} :def {$exists true}}))) (mc/update db collection {:abc {$exists true} :def {$exists true}} {$inc {:abc 1 :def 0}}) (is (= 1 (mc/count db collection { :abc 1 })))))