diff --git a/src/monger/errors.clj b/src/monger/result.clj similarity index 54% rename from src/monger/errors.clj rename to src/monger/result.clj index 250a881..83e3a5d 100644 --- a/src/monger/errors.clj +++ b/src/monger/result.clj @@ -7,8 +7,8 @@ ;; the terms of this license. ;; You must not remove this notice, or any other, from this software. -(ns monger.errors - (:import (com.mongodb DBObject WriteResult) +(ns monger.result + (:import (com.mongodb DBObject BasicDBObject WriteResult CommandResult) (clojure.lang IPersistentMap)) (:require [monger convertion])) @@ -18,16 +18,25 @@ ;; (defprotocol MongoCommandResult - (ok? [input] "Returns true if command result is a success")) + (ok? [input] "Returns true if command result is a success") + (has-error? [input] "Returns true if command result indicates an error") + (updated-existing? [input] "Returns true if command result has `updatedExisting` field set to true")) (extend-protocol MongoCommandResult DBObject (ok? [^DBObject result] (.contains [true "true" 1 1.0] (.get result "ok"))) + (has-error? + [^DBObject result] + ;; yes, this is exactly the logic MongoDB Java driver uses. + (> (count (str (.get result "err"))) 0)) + WriteResult (ok? [^WriteResult result] - (ok? (.getLastError result)))) - + (and (not (nil? result)) (ok? (.getLastError result)))) + (has-error? + [^WriteResult result] + (has-error? (.getLastError result)))) \ No newline at end of file diff --git a/test/monger/test/errors.clj b/test/monger/test/errors.clj deleted file mode 100644 index ed8d7c2..0000000 --- a/test/monger/test/errors.clj +++ /dev/null @@ -1,23 +0,0 @@ -(ns monger.test.errors - (:import (com.mongodb BasicDBObject WriteResult WriteConcern)) - (:require [monger core collection convertion]) - (:use [clojure.test])) - - -;; -;; MongoCommandResult -;; - - -(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.errors/ok? result-that-is-not-ok-1))) - (is (not (monger.errors/ok? result-that-is-not-ok-2))) - (is (monger.errors/ok? result-that-is-ok-1)) - (is (monger.errors/ok? result-that-is-ok-2)) - (is (monger.errors/ok? result-that-is-ok-3)))) - diff --git a/test/monger/test/result.clj b/test/monger/test/result.clj new file mode 100644 index 0000000..6225deb --- /dev/null +++ b/test/monger/test/result.clj @@ -0,0 +1,31 @@ +(ns monger.test.result + (:import (com.mongodb BasicDBObject WriteResult WriteConcern)) + (:require [monger core collection convertion]) + (:use [clojure.test])) + + +;; +;; MongoCommandResult +;; + + +(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))))