More test coverage, also found a bug where bulk voided invoices were not unvoidable
This commit is contained in:
@@ -319,7 +319,8 @@
|
|||||||
}]
|
}]
|
||||||
(map
|
(map
|
||||||
(fn [iea]
|
(fn [iea]
|
||||||
[:db/retract (:db/id i) :invoice/expense-accounts (:db/id iea)])
|
{:db/id (:db/id iea)
|
||||||
|
:invoice-expense-account/amount 0.0})
|
||||||
(:invoice/expense-accounts i))))))
|
(:invoice/expense-accounts i))))))
|
||||||
(:id context))
|
(:id context))
|
||||||
{:message (str "Succesfully voided " (count all-ids))}))
|
{:message (str "Succesfully voided " (count all-ids))}))
|
||||||
@@ -339,6 +340,8 @@
|
|||||||
:in ['$ '?e]}
|
:in ['$ '?e]}
|
||||||
:args [history id]})
|
:args [history id]})
|
||||||
[last-transaction] (->> txs (sort-by first) (last))]
|
[last-transaction] (->> txs (sort-by first) (last))]
|
||||||
|
(mu/log ::here
|
||||||
|
:txes txs)
|
||||||
(transact-with-ledger [(->> txs
|
(transact-with-ledger [(->> txs
|
||||||
(filter (fn [[tx]] (= tx last-transaction)))
|
(filter (fn [[tx]] (= tx last-transaction)))
|
||||||
(reduce (fn [new-transaction [_ entity original-status original-outstanding total expense-account expense-account-amount]]
|
(reduce (fn [new-transaction [_ entity original-status original-outstanding total expense-account expense-account-amount]]
|
||||||
|
|||||||
@@ -1,101 +1,210 @@
|
|||||||
(ns auto-ap.integration.graphql.invoices
|
(ns auto-ap.integration.graphql.invoices
|
||||||
(:require
|
(:require
|
||||||
[auto-ap.datomic :refer [conn uri]]
|
[auto-ap.datomic :refer [conn]]
|
||||||
[auto-ap.datomic.migrate :as m]
|
|
||||||
[auto-ap.time-reader]
|
|
||||||
[auto-ap.graphql.invoices :as sut]
|
[auto-ap.graphql.invoices :as sut]
|
||||||
[clj-time.core :as time]
|
[auto-ap.integration.util
|
||||||
|
:refer [admin-token
|
||||||
|
setup-test-data
|
||||||
|
test-account
|
||||||
|
test-invoice
|
||||||
|
test-vendor
|
||||||
|
wrap-setup]]
|
||||||
[clojure.test :as t :refer [deftest is testing use-fixtures]]
|
[clojure.test :as t :refer [deftest is testing use-fixtures]]
|
||||||
[datomic.api :as d]))
|
[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)
|
|
||||||
|
|
||||||
(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)
|
(use-fixtures :each wrap-setup)
|
||||||
|
|
||||||
(deftest test-add-invoice
|
(deftest test-add-invoice
|
||||||
(testing "It should Add an invoice"
|
(testing "It should Add an invoice"
|
||||||
(let [{:strs [vendor-id client-id account-id]}
|
(let [{:strs [test-vendor-id test-client-id test-account-id
|
||||||
(:tempids @(d/transact conn [{:client/code "ABC"
|
denied-account-id vendor-account-id vendor-with-special-account]}
|
||||||
:db/id "client-id"
|
(setup-test-data [(test-account :db/id "denied-account-id"
|
||||||
:client/locations ["DT"]}
|
:account/invoice-allowance :allowance/denied)
|
||||||
{:vendor/name "Vendy"
|
(test-account :db/id "vendor-account-id"
|
||||||
:db/id "vendor-id"
|
:account/invoice-allowance :allowance/denied
|
||||||
:vendor/default-account "account-id"}
|
:account/vendor-allowance :allowance/allowed)
|
||||||
{:account/name "Account"
|
(test-vendor :db/id "vendor-with-special-account"
|
||||||
:account/numeric-code 123
|
:vendor/default-account "vendor-account-id")])]
|
||||||
:account/invoice-allowance :allowance/allowed
|
|
||||||
:db/id "account-id"}]))]
|
|
||||||
(is (some? (sut/add-invoice {:id (admin-token)}
|
(is (some? (sut/add-invoice {:id (admin-token)}
|
||||||
{:invoice {:client_id client-id
|
{:invoice {:client_id test-client-id
|
||||||
:vendor_id vendor-id
|
:vendor_id test-vendor-id
|
||||||
:invoice_number "123"
|
:invoice_number "123"
|
||||||
:date #clj-time/date-time "2022-01-01"
|
:date #clj-time/date-time "2022-01-01"
|
||||||
:total 10.00
|
:total 10.00
|
||||||
:expense_accounts [{:amount 10.0
|
:expense_accounts [{:amount 10.0
|
||||||
:location "DT"
|
:location "DT"
|
||||||
:account_id account-id}]}}
|
:account_id test-account-id}]}}
|
||||||
nil)))
|
nil)))
|
||||||
(testing "It should prevent an expense account that isn't allowed"
|
(testing "It should prevent an expense account that isn't allowed"
|
||||||
(let [{:strs [denied-account-id]}
|
(is (thrown? Exception (sut/add-invoice {:id (admin-token)}
|
||||||
(:tempids @(d/transact conn [
|
{:invoice {:client_id test-client-id
|
||||||
{:account/name "Account"
|
:vendor_id test-vendor-id
|
||||||
:account/numeric-code 123
|
:invoice_number "789"
|
||||||
:account/invoice-allowance :allowance/denied
|
:date #clj-time/date-time "2022-01-01"
|
||||||
:db/id "denied-account-id"}]))]
|
:total 10.00
|
||||||
(is (thrown? Exception (sut/add-invoice {:id (admin-token)}
|
:expense_accounts [{:amount 10.0
|
||||||
{:invoice {:client_id client-id
|
:location "DT"
|
||||||
:vendor_id vendor-id
|
:account_id denied-account-id}]}}
|
||||||
:invoice_number "789"
|
nil))))
|
||||||
:date #clj-time/date-time "2022-01-01"
|
|
||||||
:total 10.00
|
|
||||||
:expense_accounts [{:amount 10.0
|
|
||||||
:location "DT"
|
|
||||||
:account_id denied-account-id}]}}
|
|
||||||
nil)))))
|
|
||||||
|
|
||||||
(testing "It should allow an expense account that is valid for the vendor"
|
(testing "It should allow an expense account that is valid for the vendor"
|
||||||
(let [{:strs [vendor-account-id vendor-2]}
|
(is (some? (sut/add-invoice {:id (admin-token)}
|
||||||
(:tempids @(d/transact conn [
|
{:invoice {:client_id test-client-id
|
||||||
{:account/name "Account"
|
:vendor_id vendor-with-special-account
|
||||||
:account/numeric-code 123
|
:invoice_number "456"
|
||||||
:account/invoice-allowance :allowance/denied
|
:date #clj-time/date-time "2022-01-01"
|
||||||
:account/vendor-allowance :allowance/allowed
|
:total 10.00
|
||||||
:db/id "vendor-account-id"}
|
:expense_accounts [{:amount 10.0
|
||||||
{:vendor/name "Testy"
|
:location "DT"
|
||||||
:vendor/default-account "vendor-account-id"
|
:account_id vendor-account-id}]}}
|
||||||
:db/id "vendor-2"}]))]
|
nil)))))))
|
||||||
(is (some? (sut/add-invoice {:id (admin-token)}
|
|
||||||
{:invoice {:client_id client-id
|
(deftest edit-invoice
|
||||||
:vendor_id vendor-2
|
(testing "It should edit invoices"
|
||||||
:invoice_number "456"
|
(let [{:strs [invoice-id
|
||||||
:date #clj-time/date-time "2022-01-01"
|
new-vendor-id
|
||||||
:total 10.00
|
new-account-id]}
|
||||||
:expense_accounts [{:amount 10.0
|
(setup-test-data [(test-invoice :db/id "invoice-id")
|
||||||
:location "DT"
|
(test-vendor :db/id "new-vendor-id")
|
||||||
:account_id vendor-account-id}]}}
|
(test-account :db/id "new-account-id")])]
|
||||||
nil))))))))
|
(is (some? (sut/edit-invoice {:id (admin-token)}
|
||||||
|
{:invoice {:id invoice-id
|
||||||
|
:vendor_id new-vendor-id
|
||||||
|
:invoice_number "890213"
|
||||||
|
:date #clj-time/date-time "2023-01-01"
|
||||||
|
:total 100.00
|
||||||
|
:outstanding_balance 100.00
|
||||||
|
:expense_accounts [{:amount 100.0
|
||||||
|
:location "DT"
|
||||||
|
:account_id new-account-id}]}}
|
||||||
|
nil)))
|
||||||
|
(is (= #:invoice{:invoice-number "890213"
|
||||||
|
:date #inst "2023-01-01T00:00:00.000-00:00"
|
||||||
|
:total 100.0
|
||||||
|
:expense-accounts
|
||||||
|
[#:invoice-expense-account{:amount 100.0
|
||||||
|
:location "DT"
|
||||||
|
:account {:db/id new-account-id}}]}
|
||||||
|
(d/pull (d/db conn) [:invoice/invoice-number
|
||||||
|
:invoice/date
|
||||||
|
:invoice/total
|
||||||
|
{:invoice/expense-accounts [:invoice-expense-account/amount
|
||||||
|
:invoice-expense-account/location
|
||||||
|
:invoice-expense-account/account]}]
|
||||||
|
invoice-id)))
|
||||||
|
|
||||||
|
(testing "Should not be able to change vendor"
|
||||||
|
(is (not= new-vendor-id (:db/id (:invoice/vendor (d/pull (d/db conn) [:invoice/vendor] invoice-id))))))))
|
||||||
|
|
||||||
|
(testing "Should disallow adding a conflicting invoice"
|
||||||
|
(let [{:strs [invoice-id]}
|
||||||
|
(setup-test-data [(test-invoice :db/id "invoice-id"
|
||||||
|
:invoice/invoice-number "original-invoice"
|
||||||
|
:invoice/vendor "test-vendor-id"
|
||||||
|
:invoice/client "test-client-id")
|
||||||
|
(test-invoice :db/id "extant-invoice-id"
|
||||||
|
:invoice/invoice-number "already taken"
|
||||||
|
:invoice/vendor "test-vendor-id"
|
||||||
|
:invoice/client "test-client-id")])]
|
||||||
|
(is (thrown? Exception (sut/edit-invoice {:id (admin-token)}
|
||||||
|
{:invoice {:id invoice-id
|
||||||
|
:invoice_number "already taken"}}
|
||||||
|
nil))))))
|
||||||
|
|
||||||
|
(deftest edit-expense-accounts
|
||||||
|
(testing "It should edit expense accounts"
|
||||||
|
(let [{:strs [invoice-id
|
||||||
|
new-account-id]}
|
||||||
|
(setup-test-data [(test-invoice :db/id "invoice-id")
|
||||||
|
(test-account :db/id "new-account-id")])]
|
||||||
|
(is (some? (sut/edit-expense-accounts {:id (admin-token)}
|
||||||
|
{:invoice_id invoice-id
|
||||||
|
:expense_accounts [{:amount 100.0
|
||||||
|
:account_id new-account-id
|
||||||
|
:location "DT"}]}
|
||||||
|
nil)))
|
||||||
|
(is (= [#:invoice-expense-account{:amount 100.0
|
||||||
|
:location "DT"
|
||||||
|
:account {:db/id new-account-id}}]
|
||||||
|
(-> (d/pull (d/db conn) [{:invoice/expense-accounts [:invoice-expense-account/amount
|
||||||
|
:invoice-expense-account/location
|
||||||
|
:invoice-expense-account/account]}]
|
||||||
|
invoice-id)
|
||||||
|
:invoice/expense-accounts))))))
|
||||||
|
|
||||||
|
(deftest bulk-change-invoices
|
||||||
|
(testing "It should change expense accounts in bulk"
|
||||||
|
(let [{:strs [invoice-id
|
||||||
|
new-account-id
|
||||||
|
test-client-id]}
|
||||||
|
(setup-test-data [(test-invoice :db/id "invoice-id")
|
||||||
|
(test-account :db/id "new-account-id")])]
|
||||||
|
(is (some? (sut/bulk-change-invoices {:id (admin-token)}
|
||||||
|
{:client_id test-client-id
|
||||||
|
:filters {:client_id test-client-id}
|
||||||
|
:accounts [{:percentage 1.0
|
||||||
|
:account_id new-account-id
|
||||||
|
:location "Shared"}]}
|
||||||
|
nil)))
|
||||||
|
(is (= [#:invoice-expense-account{:amount 100.0
|
||||||
|
:location "DT"
|
||||||
|
:account {:db/id new-account-id}}]
|
||||||
|
(-> (d/pull (d/db conn) [{:invoice/expense-accounts [:invoice-expense-account/amount
|
||||||
|
:invoice-expense-account/location
|
||||||
|
:invoice-expense-account/account]}]
|
||||||
|
invoice-id)
|
||||||
|
:invoice/expense-accounts))))))
|
||||||
|
|
||||||
|
(deftest void-invoices
|
||||||
|
(testing "It should voide invoices in bulk"
|
||||||
|
(let [{:strs [invoice-id test-client-id]}
|
||||||
|
(setup-test-data [(test-invoice :db/id "invoice-id"
|
||||||
|
:invoice/status :invoice-status/unpaid)
|
||||||
|
(test-account :db/id "new-account-id")])]
|
||||||
|
|
||||||
|
(is (some? (sut/void-invoices {:id (admin-token)}
|
||||||
|
{:filters {:client_id test-client-id}}
|
||||||
|
nil)))
|
||||||
|
(is (= :invoice-status/voided
|
||||||
|
(-> (d/pull (d/db conn) [{:invoice/status [:db/ident]}]
|
||||||
|
invoice-id)
|
||||||
|
:invoice/status
|
||||||
|
:db/ident)))
|
||||||
|
|
||||||
|
(testing "Should unvoid invoice"
|
||||||
|
(is (some? (sut/unvoid-invoice {:id (admin-token)}
|
||||||
|
{:invoice_id invoice-id}
|
||||||
|
nil)))
|
||||||
|
(is (= :invoice-status/unpaid
|
||||||
|
(-> (d/pull (d/db conn) [{:invoice/status [:db/ident]}]
|
||||||
|
invoice-id)
|
||||||
|
:invoice/status
|
||||||
|
:db/ident)))))))
|
||||||
|
|
||||||
|
|
||||||
|
(deftest void-invoice
|
||||||
|
(testing "It should voide invoices in bulk"
|
||||||
|
(let [{:strs [invoice-id]}
|
||||||
|
(setup-test-data [(test-invoice :db/id "invoice-id"
|
||||||
|
:invoice/status :invoice-status/unpaid)
|
||||||
|
(test-account :db/id "new-account-id")])]
|
||||||
|
|
||||||
|
|
||||||
|
(is (some? (sut/void-invoice {:id (admin-token)}
|
||||||
|
{:invoice_id invoice-id}
|
||||||
|
nil)))
|
||||||
|
(is (= :invoice-status/voided
|
||||||
|
(-> (d/pull (d/db conn) [{:invoice/status [:db/ident]}]
|
||||||
|
invoice-id)
|
||||||
|
:invoice/status
|
||||||
|
:db/ident)))
|
||||||
|
|
||||||
|
(testing "Should unvoid invoice"
|
||||||
|
(is (some? (sut/unvoid-invoice {:id (admin-token)}
|
||||||
|
{:invoice_id invoice-id}
|
||||||
|
nil)))
|
||||||
|
(is (= :invoice-status/unpaid
|
||||||
|
(-> (d/pull (d/db conn) [{:invoice/status [:db/ident]}]
|
||||||
|
invoice-id)
|
||||||
|
:invoice/status
|
||||||
|
:db/ident)))))))
|
||||||
|
|||||||
@@ -43,7 +43,8 @@
|
|||||||
|
|
||||||
(defn test-vendor [& kwargs]
|
(defn test-vendor [& kwargs]
|
||||||
(apply assoc {:db/id "vendor-id"
|
(apply assoc {:db/id "vendor-id"
|
||||||
:vendor/name "Vendorson"}
|
:vendor/name "Vendorson"
|
||||||
|
:vendor/default-account "test-account-id"}
|
||||||
kwargs))
|
kwargs))
|
||||||
|
|
||||||
(defn test-bank-account [& kwargs]
|
(defn test-bank-account [& kwargs]
|
||||||
@@ -75,7 +76,10 @@
|
|||||||
(apply assoc {:db/id "test-invoice-id"
|
(apply assoc {:db/id "test-invoice-id"
|
||||||
:invoice/date #inst "2022-01-01"
|
:invoice/date #inst "2022-01-01"
|
||||||
:invoice/client "test-client-id"
|
:invoice/client "test-client-id"
|
||||||
|
:invoice/status :invoice-status/unpaid
|
||||||
|
:invoice/import-status :import-status/imported
|
||||||
:invoice/total 100.0
|
:invoice/total 100.0
|
||||||
|
:invoice/outstanding-balance 100.00
|
||||||
:invoice/vendor "test-vendor-id"
|
:invoice/vendor "test-vendor-id"
|
||||||
:invoice/invoice-number (str "INVOICE " (rand-int 1000000))
|
:invoice/invoice-number (str "INVOICE " (rand-int 1000000))
|
||||||
:invoice/expense-accounts [{:invoice-expense-account/account "test-account-id"
|
:invoice/expense-accounts [{:invoice-expense-account/account "test-account-id"
|
||||||
|
|||||||
Reference in New Issue
Block a user