From 51691fa55386bb515f538961956eacee9fb5bace Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Sun, 12 May 2019 09:27:03 -0700 Subject: [PATCH] added validation --- src/clj/auto_ap/graphql/transaction_rules.clj | 5 + test/clj/auto_ap/graphql.clj | 104 ++++++++++-------- 2 files changed, 62 insertions(+), 47 deletions(-) diff --git a/src/clj/auto_ap/graphql/transaction_rules.clj b/src/clj/auto_ap/graphql/transaction_rules.clj index c250455d..efc799a6 100644 --- a/src/clj/auto_ap/graphql/transaction_rules.clj +++ b/src/clj/auto_ap/graphql/transaction_rules.clj @@ -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") diff --git a/test/clj/auto_ap/graphql.clj b/test/clj/auto_ap/graphql.clj index bf3aeade..3b5881d3 100644 --- a/test/clj/auto_ap/graphql.clj +++ b/test/clj/auto_ap/graphql.clj @@ -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"