From 2ff340ab08af86e2774b3af98ffa25f6b5f21d1f Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Thu, 1 Sep 2011 22:29:46 +0400 Subject: [PATCH] Implement monger.result.MongoCommandResult/updated-existing? --- src/monger/collection.clj | 2 +- src/monger/result.clj | 11 +++++++++-- test/monger/test/collection.clj | 18 +++++++++--------- test/monger/test/result.clj | 9 +++++++++ 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/monger/collection.clj b/src/monger/collection.clj index 0bd9e57..3d6d44f 100644 --- a/src/monger/collection.clj +++ b/src/monger/collection.clj @@ -9,7 +9,7 @@ (ns monger.collection (:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map)) - (:require [monger core errors]) + (:require [monger core result]) (:use [monger.convertion])) ;; diff --git a/src/monger/result.clj b/src/monger/result.clj index 83e3a5d..768787a 100644 --- a/src/monger/result.clj +++ b/src/monger/result.clj @@ -8,7 +8,7 @@ ;; You must not remove this notice, or any other, from this software. (ns monger.result - (:import (com.mongodb DBObject BasicDBObject WriteResult CommandResult) + (:import (com.mongodb DBObject WriteResult) (clojure.lang IPersistentMap)) (:require [monger convertion])) @@ -31,6 +31,10 @@ [^DBObject result] ;; yes, this is exactly the logic MongoDB Java driver uses. (> (count (str (.get result "err"))) 0)) + (updated-existing? + [^DBObject result] + (let [v (.get result "updatedExisting")] + (and v (Boolean/valueOf v)))) WriteResult @@ -39,4 +43,7 @@ (and (not (nil? result)) (ok? (.getLastError result)))) (has-error? [^WriteResult result] - (has-error? (.getLastError result)))) \ No newline at end of file + (has-error? (.getLastError result))) + (updated-existing? + [^WriteResult result] + (updated-existing? (.getLastError result)))) diff --git a/test/monger/test/collection.clj b/test/monger/test/collection.clj index ddd7702..6522b73 100644 --- a/test/monger/test/collection.clj +++ b/test/monger/test/collection.clj @@ -2,7 +2,7 @@ (ns monger.test.collection (:import [com.mongodb WriteResult WriteConcern DBCursor DBObject] [java.util Date]) - (:require [monger core collection errors util] [clojure stacktrace]) + (:require [monger core collection result util] [clojure stacktrace]) (:use [clojure.test])) (monger.util/with-ns 'monger.core @@ -19,7 +19,7 @@ (let [collection "people" doc { :name "Joe", :age 30 }] (monger.collection/remove collection) - (is (monger.errors/ok? (monger.collection/insert "people" doc))) + (is (monger.result/ok? (monger.collection/insert "people" doc))) (is (= 1 (monger.collection/count collection))))) @@ -27,7 +27,7 @@ (let [collection "people" doc { :name "Joe", :age 30 }] (monger.collection/remove collection) - (is (monger.errors/ok? (monger.collection/insert "people" doc WriteConcern/SAFE))) + (is (monger.result/ok? (monger.collection/insert "people" doc WriteConcern/SAFE))) (is (= 1 (monger.collection/count collection))))) @@ -40,14 +40,14 @@ (let [collection "people" docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]] (monger.collection/remove collection) - (is (monger.errors/ok? (monger.collection/insert-batch "people" docs))) + (is (monger.result/ok? (monger.collection/insert-batch "people" docs))) (is (= 2 (monger.collection/count 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 }]] (monger.collection/remove collection) - (is (monger.errors/ok? (monger.collection/insert-batch "people" docs WriteConcern/NORMAL))) + (is (monger.result/ok? (monger.collection/insert-batch "people" docs WriteConcern/NORMAL))) (is (= 2 (monger.collection/count collection))))) @@ -243,7 +243,7 @@ (let [collection "people" document { :name "Joe", :age 30 }] (monger.collection/remove collection) - (is (monger.errors/ok? (monger.collection/save "people" document))) + (is (monger.result/ok? (monger.collection/save "people" document))) (is (= 1 (monger.collection/count collection))))) @@ -252,7 +252,7 @@ doc-id "people-1" document { :_id doc-id, :name "Joe", :age 30 }] (monger.collection/remove collection) - (is (monger.errors/ok? (monger.collection/insert "people" document))) + (is (monger.result/ok? (monger.collection/insert "people" document))) (is (= 1 (monger.collection/count collection))) (monger.collection/save collection { :_id doc-id, :name "Alan", :age 40 }) (is (= 1 (monger.collection/count collection { :name "Alan", :age 40 }))))) @@ -265,8 +265,8 @@ 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 }] (monger.collection/remove collection) - (monger.collection/update collection { :language "Clojure" } doc :upsert true) + (is (not (monger.result/updated-existing? (monger.collection/update collection { :language "Clojure" } doc :upsert true)))) (is (= 1 (monger.collection/count collection))) - (monger.collection/update collection { :language "Clojure" } modified-doc :multi false :upsert true) + (is (monger.result/updated-existing? (monger.collection/update collection { :language "Clojure" } modified-doc :multi false :upsert true))) (is (= 1 (monger.collection/count collection))) (is (= (modified-doc (monger.collection/find-by-id collection doc-id)))))) diff --git a/test/monger/test/result.clj b/test/monger/test/result.clj index 6225deb..6068a24 100644 --- a/test/monger/test/result.clj +++ b/test/monger/test/result.clj @@ -29,3 +29,12 @@ (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? + (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)))))