monger.errors => monger.result, extend MongoCommandResult protocol with has-error?
This commit is contained in:
parent
7f2e3bc46e
commit
8fdfe32ccb
3 changed files with 45 additions and 28 deletions
|
|
@ -7,8 +7,8 @@
|
||||||
;; the terms of this license.
|
;; the terms of this license.
|
||||||
;; You must not remove this notice, or any other, from this software.
|
;; You must not remove this notice, or any other, from this software.
|
||||||
|
|
||||||
(ns monger.errors
|
(ns monger.result
|
||||||
(:import (com.mongodb DBObject WriteResult)
|
(:import (com.mongodb DBObject BasicDBObject WriteResult CommandResult)
|
||||||
(clojure.lang IPersistentMap))
|
(clojure.lang IPersistentMap))
|
||||||
(:require [monger convertion]))
|
(:require [monger convertion]))
|
||||||
|
|
||||||
|
|
@ -18,16 +18,25 @@
|
||||||
;;
|
;;
|
||||||
|
|
||||||
(defprotocol MongoCommandResult
|
(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
|
(extend-protocol MongoCommandResult
|
||||||
DBObject
|
DBObject
|
||||||
(ok?
|
(ok?
|
||||||
[^DBObject result]
|
[^DBObject result]
|
||||||
(.contains [true "true" 1 1.0] (.get result "ok")))
|
(.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
|
WriteResult
|
||||||
(ok?
|
(ok?
|
||||||
[^WriteResult result]
|
[^WriteResult result]
|
||||||
(ok? (.getLastError result))))
|
(and (not (nil? result)) (ok? (.getLastError result))))
|
||||||
|
(has-error?
|
||||||
|
[^WriteResult result]
|
||||||
|
(has-error? (.getLastError result))))
|
||||||
|
|
@ -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))))
|
|
||||||
|
|
||||||
31
test/monger/test/result.clj
Normal file
31
test/monger/test/result.clj
Normal file
|
|
@ -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))))
|
||||||
Loading…
Reference in a new issue