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