added validation
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
[auto-ap.datomic.transactions :as t]
|
||||
[datomic.api :as d]
|
||||
[auto-ap.datomic :refer [remove-nils uri merge-query]]
|
||||
[auto-ap.utils :refer [dollars=]]
|
||||
[auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin result->page]]
|
||||
[clj-time.coerce :as c]
|
||||
[clojure.set :as set])
|
||||
@@ -33,6 +34,10 @@
|
||||
#_(assert-admin (:id context))
|
||||
(let [existing-transaction (tr/get-by-id id)
|
||||
deleted (deleted-accounts existing-transaction accounts)
|
||||
account-total (reduce + 0 (map (fn [x] (:percentage x)) accounts))
|
||||
_ (when-not (dollars= 1.0 account-total)
|
||||
(let [error (str "Account total (" account-total ") does not reach 100%")]
|
||||
(throw (ex-info error {:validation-error error}))))
|
||||
transaction [(remove-nils #:transaction-rule {:db/id (if id
|
||||
id
|
||||
"transaction-rule")
|
||||
|
||||
@@ -32,54 +32,64 @@
|
||||
(is (seqable? (:transaction-rules result))))))
|
||||
|
||||
(deftest upsert-transaction-rule
|
||||
(testing "it should add rules"
|
||||
(let [{:strs [vendor-id account-id]} (-> (d/connect uri)
|
||||
(d/transact
|
||||
[{:vendor/name "Bryce's Meat Co"
|
||||
:db/id "vendor-id"}
|
||||
{:account/name "hello"
|
||||
:db/id "account-id"}])
|
||||
deref
|
||||
:tempids)
|
||||
q (v/graphql-query {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertTransactionRule"}
|
||||
:venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule
|
||||
{:transaction-rule {:description "123"
|
||||
:vendor-id vendor-id
|
||||
:accounts [{:account-id account-id
|
||||
:percentage "0.5"
|
||||
:location "B"}
|
||||
{:account-id account-id
|
||||
:percentage "0.5"
|
||||
:location "A"}]}}
|
||||
[:id :description
|
||||
[:vendor [:name]]
|
||||
[:accounts [:id :percentage [:account [:name]]]]]])}]})
|
||||
result (-> (sut/query nil q)
|
||||
:data
|
||||
:upsert-transaction-rule)]
|
||||
|
||||
(is (= "123" (:description result)))
|
||||
(is (= "Bryce's Meat Co" (-> result :vendor :name)))
|
||||
(is (= "hello" (-> result :accounts (get 0) :account :name )))
|
||||
(is (:id result))
|
||||
(testing "it should delete removed rules"
|
||||
(let [q (v/graphql-query {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertTransactionRule"}
|
||||
:venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule
|
||||
{:transaction-rule {:id (:id result)
|
||||
:description "123"
|
||||
:vendor-id vendor-id
|
||||
:accounts [{:id (-> result :accounts (get 0) :id)
|
||||
:account-id account-id
|
||||
:percentage "1.0"
|
||||
:location "B"}]}}
|
||||
[[:accounts [:id :percentage [:account [:name]]]]]])}]})
|
||||
result (-> (sut/query nil q)
|
||||
:data
|
||||
:upsert-transaction-rule)]
|
||||
(let [{:strs [vendor-id account-id]} (-> (d/connect uri)
|
||||
(d/transact
|
||||
[{:vendor/name "Bryce's Meat Co"
|
||||
:db/id "vendor-id"}
|
||||
{:account/name "hello"
|
||||
:db/id "account-id"}])
|
||||
deref
|
||||
:tempids)]
|
||||
(testing "it should reject rules that don't add up to 100%"
|
||||
(let [q (v/graphql-query {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertTransactionRule"}
|
||||
:venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule
|
||||
{:transaction-rule {:accounts [{:account-id account-id
|
||||
:percentage "0.25"
|
||||
:location "B"}]}}
|
||||
[:id ]])}]})
|
||||
]
|
||||
(is (thrown? clojure.lang.ExceptionInfo (sut/query nil q)))))
|
||||
(testing "it should add rules"
|
||||
(let [q (v/graphql-query {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertTransactionRule"}
|
||||
:venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule
|
||||
{:transaction-rule {:description "123"
|
||||
:vendor-id vendor-id
|
||||
:accounts [{:account-id account-id
|
||||
:percentage "0.5"
|
||||
:location "B"}
|
||||
{:account-id account-id
|
||||
:percentage "0.5"
|
||||
:location "A"}]}}
|
||||
[:id :description
|
||||
[:vendor [:name]]
|
||||
[:accounts [:id :percentage [:account [:name]]]]]])}]})
|
||||
result (-> (sut/query nil q)
|
||||
:data
|
||||
:upsert-transaction-rule)]
|
||||
|
||||
(is (= "123" (:description result)))
|
||||
(is (= "Bryce's Meat Co" (-> result :vendor :name)))
|
||||
(is (= "hello" (-> result :accounts (get 0) :account :name )))
|
||||
(is (:id result))
|
||||
(testing "it should delete removed rules"
|
||||
(let [q (v/graphql-query {:venia/operation {:operation/type :mutation
|
||||
:operation/name "UpsertTransactionRule"}
|
||||
:venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule
|
||||
{:transaction-rule {:id (:id result)
|
||||
:description "123"
|
||||
:vendor-id vendor-id
|
||||
:accounts [{:id (-> result :accounts (get 0) :id)
|
||||
:account-id account-id
|
||||
:percentage "1.0"
|
||||
:location "B"}]}}
|
||||
[[:accounts [:id :percentage [:account [:name]]]]]])}]})
|
||||
result (-> (sut/query nil q)
|
||||
:data
|
||||
:upsert-transaction-rule)]
|
||||
|
||||
(is (= 1 (count (:accounts result)))))))))
|
||||
(is (= 1 (count (:accounts result))))))))))
|
||||
|
||||
(deftest test-transaction-rule
|
||||
(testing "it should match rules"
|
||||
|
||||
Reference in New Issue
Block a user