Implement monger.result.MongoCommandResult/updated-existing?
This commit is contained in:
parent
8fdfe32ccb
commit
2ff340ab08
4 changed files with 28 additions and 12 deletions
|
|
@ -9,7 +9,7 @@
|
||||||
|
|
||||||
(ns monger.collection
|
(ns monger.collection
|
||||||
(:import (com.mongodb Mongo DB DBCollection WriteResult DBObject WriteConcern DBCursor) (java.util List Map))
|
(: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]))
|
(:use [monger.convertion]))
|
||||||
|
|
||||||
;;
|
;;
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
;; 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.result
|
(ns monger.result
|
||||||
(:import (com.mongodb DBObject BasicDBObject WriteResult CommandResult)
|
(:import (com.mongodb DBObject WriteResult)
|
||||||
(clojure.lang IPersistentMap))
|
(clojure.lang IPersistentMap))
|
||||||
(:require [monger convertion]))
|
(:require [monger convertion]))
|
||||||
|
|
||||||
|
|
@ -31,6 +31,10 @@
|
||||||
[^DBObject result]
|
[^DBObject result]
|
||||||
;; yes, this is exactly the logic MongoDB Java driver uses.
|
;; yes, this is exactly the logic MongoDB Java driver uses.
|
||||||
(> (count (str (.get result "err"))) 0))
|
(> (count (str (.get result "err"))) 0))
|
||||||
|
(updated-existing?
|
||||||
|
[^DBObject result]
|
||||||
|
(let [v (.get result "updatedExisting")]
|
||||||
|
(and v (Boolean/valueOf v))))
|
||||||
|
|
||||||
|
|
||||||
WriteResult
|
WriteResult
|
||||||
|
|
@ -39,4 +43,7 @@
|
||||||
(and (not (nil? result)) (ok? (.getLastError result))))
|
(and (not (nil? result)) (ok? (.getLastError result))))
|
||||||
(has-error?
|
(has-error?
|
||||||
[^WriteResult result]
|
[^WriteResult result]
|
||||||
(has-error? (.getLastError result))))
|
(has-error? (.getLastError result)))
|
||||||
|
(updated-existing?
|
||||||
|
[^WriteResult result]
|
||||||
|
(updated-existing? (.getLastError result))))
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
(ns monger.test.collection
|
(ns monger.test.collection
|
||||||
(:import [com.mongodb WriteResult WriteConcern DBCursor DBObject] [java.util Date])
|
(: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]))
|
(:use [clojure.test]))
|
||||||
|
|
||||||
(monger.util/with-ns 'monger.core
|
(monger.util/with-ns 'monger.core
|
||||||
|
|
@ -19,7 +19,7 @@
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
doc { :name "Joe", :age 30 }]
|
doc { :name "Joe", :age 30 }]
|
||||||
(monger.collection/remove collection)
|
(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)))))
|
(is (= 1 (monger.collection/count collection)))))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
doc { :name "Joe", :age 30 }]
|
doc { :name "Joe", :age 30 }]
|
||||||
(monger.collection/remove collection)
|
(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)))))
|
(is (= 1 (monger.collection/count collection)))))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -40,14 +40,14 @@
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]]
|
docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]]
|
||||||
(monger.collection/remove collection)
|
(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)))))
|
(is (= 2 (monger.collection/count collection)))))
|
||||||
|
|
||||||
(deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern
|
(deftest insert-a-batch-of-basic-documents-without-ids-and-with-explicit-write-concern
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]]
|
docs [{ :name "Joe", :age 30 }, { :name "Paul", :age 27 }]]
|
||||||
(monger.collection/remove collection)
|
(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)))))
|
(is (= 2 (monger.collection/count collection)))))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -243,7 +243,7 @@
|
||||||
(let [collection "people"
|
(let [collection "people"
|
||||||
document { :name "Joe", :age 30 }]
|
document { :name "Joe", :age 30 }]
|
||||||
(monger.collection/remove collection)
|
(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)))))
|
(is (= 1 (monger.collection/count collection)))))
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -252,7 +252,7 @@
|
||||||
doc-id "people-1"
|
doc-id "people-1"
|
||||||
document { :_id doc-id, :name "Joe", :age 30 }]
|
document { :_id doc-id, :name "Joe", :age 30 }]
|
||||||
(monger.collection/remove collection)
|
(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)))
|
(is (= 1 (monger.collection/count collection)))
|
||||||
(monger.collection/save collection { :_id doc-id, :name "Alan", :age 40 })
|
(monger.collection/save collection { :_id doc-id, :name "Alan", :age 40 })
|
||||||
(is (= 1 (monger.collection/count collection { :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 }
|
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 }]
|
modified-doc { :created-at date, :data-store "MongoDB", :language "Erlang", :_id doc-id }]
|
||||||
(monger.collection/remove collection)
|
(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)))
|
(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 (= 1 (monger.collection/count collection)))
|
||||||
(is (= (modified-doc (monger.collection/find-by-id collection doc-id))))))
|
(is (= (modified-doc (monger.collection/find-by-id collection doc-id))))))
|
||||||
|
|
|
||||||
|
|
@ -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-error1)))
|
||||||
(is (not (monger.result/has-error? result-that-has-no-error2)))
|
(is (not (monger.result/has-error? result-that-has-no-error2)))
|
||||||
(is (monger.result/has-error? result-that-has-error1))))
|
(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)))))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue