From 21f4dee7b28966cb593ce7dfb42f590a3dce010d Mon Sep 17 00:00:00 2001 From: Bryce Covert Date: Fri, 24 Mar 2023 15:25:26 -0700 Subject: [PATCH] account tests. --- .../auto_ap/integration/graphql/accounts.clj | 108 ++++++++++++------ test/clj/auto_ap/integration/util.clj | 14 ++- 2 files changed, 81 insertions(+), 41 deletions(-) diff --git a/test/clj/auto_ap/integration/graphql/accounts.clj b/test/clj/auto_ap/integration/graphql/accounts.clj index f567b654..f7187441 100644 --- a/test/clj/auto_ap/integration/graphql/accounts.clj +++ b/test/clj/auto_ap/integration/graphql/accounts.clj @@ -1,44 +1,11 @@ (ns auto-ap.integration.graphql.accounts (:require - [auto-ap.datomic :refer [conn uri]] - [auto-ap.datomic.migrate :as m] + [auto-ap.datomic :refer [conn]] [auto-ap.graphql.accounts :as sut] - [clj-time.core :as time] + [auto-ap.integration.util :refer [admin-token user-token wrap-setup]] [clojure.test :as t :refer [deftest is testing use-fixtures]] [datomic.api :as d])) -(defn wrap-setup - [f] - (with-redefs [auto-ap.datomic/uri "datomic:mem://datomic-transactor:4334/invoice"] - (d/create-database uri) - (with-redefs [auto-ap.datomic/conn (d/connect uri)] - (m/migrate conn) - @(d/transact conn (->> (d/q '[:find ?a - :where [?a :account/name]] - (d/db conn)) - (map (fn [[a]] - {:db/id a - :account/applicability :account-applicability/global})))) - (f) - (d/release conn) - (d/delete-database uri)))) - -#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} -(defn admin-token [] - {:user "TEST ADMIN" - :exp (time/plus (time/now) (time/days 1)) - :user/role "admin" - :user/name "TEST ADMIN"}) - -#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]} -(defn user-token [client-id] - {:user "TEST USER" - :exp (time/plus (time/now) (time/days 1)) - :user/role "user" - :user/name "TEST USER" - :user/clients [{:db/id client-id}]}) - - (use-fixtures :each wrap-setup) (deftest test-account-search @@ -164,4 +131,75 @@ :vendor_id vendor-id} nil)))))))) +(deftest get-graphql + (testing "should retrieve a single account" + (d/transact conn [{:account/numeric-code 1 + :account/default-allowance :allowance/allowed + :account/type :account-type/asset + :account/location "A" + :account/name "Test"}]) + (is (= {:name "Test", + :invoice_allowance nil, + :numeric_code 1, + :vendor_allowance nil, + :location "A", + :applicability nil} + (dissoc (first (:accounts (sut/get-graphql {:id (admin-token)} {} nil))) + :id + :type + :default_allowance))))) + +(deftest upsert-account + (testing "should create a new account" + (let [result (sut/upsert-account {:id (admin-token)} {:account {:client_overrides [] + :numeric_code 123 + :location "A" + :applicability :global + :account-set "global" + :name "Test" + :invoice-allowance :allowed + :vendor-allowance :allowed + :type :asset}} nil)] + (is (= {:search_terms "Test", + :name "Test", + :invoice_allowance :allowed, + :numeric_code 123, + :code "123", + :account_set "global", + :vendor_allowance :allowed, + :location "A", + :applicability :global} + (dissoc result + :id + :type + :default_allowance))) + (testing "Should allow updating account" + (let [edit-result (sut/upsert-account {:id (admin-token)} {:account {:client_overrides [] + :id (:id result) + :numeric_code 890 + :location "B" + :applicability :global + :account-set "global" + :name "Hello" + :invoice-allowance :denied + :vendor-allowance :denied + :type :expense}} nil)] + (is (= {:search_terms "Hello", + :name "Hello", + :invoice_allowance :denied, + :code "123", + :account_set "global", + :vendor_allowance :denied, + :location "B", + :applicability :global} + (dissoc edit-result + :id + :type + :default_allowance + :numeric_code))) + (testing "Should not allow changing numeric code" + + (is (= 123 (:numeric_code edit-result))) + )))))) + diff --git a/test/clj/auto_ap/integration/util.clj b/test/clj/auto_ap/integration/util.clj index 8fa0c091..1274058a 100644 --- a/test/clj/auto_ap/integration/util.clj +++ b/test/clj/auto_ap/integration/util.clj @@ -22,9 +22,11 @@ :user/role "admin" :user/name "TEST ADMIN"}) -(defn user-token [] - {:user "TEST USER" - :exp (time/plus (time/now) (time/days 1)) - :user/role "user" - :user/name "TEST USER" - :user/clients [{:db/id 1}]}) +(defn user-token + ([] (user-token 1)) + ([client-id] + {:user "TEST USER" + :exp (time/plus (time/now) (time/days 1)) + :user/role "user" + :user/name "TEST USER" + :user/clients [{:db/id client-id}]}))