enormous refactor but simplified much!

This commit is contained in:
Bryce Covert
2021-12-22 18:14:49 -08:00
parent a7c9d376bc
commit 7489426ccb
25 changed files with 1188 additions and 1258 deletions

View File

@@ -0,0 +1,60 @@
(ns auto-ap.import.manual-test
(:require [auto-ap.datomic :refer [conn uri]]
[auto-ap.import.manual :as sut]
[auto-ap.time :as time]
[clj-time.coerce :as coerce]
[clojure.test :as t]
[datomic.api :as d]
[auto-ap.datomic.migrate :as m]))
(def raw-tsv "Status Date Original Description High Level Category Category-Subcategory End Here Amount Amount Split Type Note Account Name SLO Acct Name SLO F1 Code Comp ID
posted 8/23/2021 MOUNTAIN MIKES PIZZA - -24.27 -24.27 - - MVSC - BofA Corp Card Sean - 7187 MVSC - BofA Corp Card Sean - 7187 MVSC-6 MVSC
posted 9/1/2021 Prime Video*258CB55D0 - -3.99 -3.99 - - MVSC - BofA Corp Card Sean - 7187 MVSC - BofA Corp Card Sean - 7187 MVSC-6 MVSC
posted 5/21/2021 Prime Video*2L1I62RT2 - -5.99 -5.99 - - MVSC - BofA Corp Card Sean - 7187 MVSC - BofA Corp Card Sean - 7187 MVSC-6 MVSC
posted 9/6/2021 STARBUCKS STORE 06536 - -38.85 -38.85 - - MVSC - BofA Corp Card Sean - 7187 MVSC - BofA Corp Card Sean - 7187 MVSC-6 MVSC")
(def base-transaction {:status "posted"
:raw-date "8/23/2021"
:description-original "MOUNTAIN MIKES PIZZA"
:high-level-category ""
:amount "-24.27"
:bank-account-code "MVSC-6"
:client-code "MVSC"})
(t/deftest tabulate-data
(t/testing "Should tabulate a single row"
(t/is (= base-transaction (first (sut/tabulate-data raw-tsv))))))
(t/deftest manual->transactions
(t/testing "Should transform a single transaction"
(t/is (= #:transaction{:bank-account 1
:client 12
:date #inst "2021-08-23T00:00:00.000-07:00"
:amount -24.27
:description-original "MOUNTAIN MIKES PIZZA"
:status "POSTED"}
(sut/manual->transaction base-transaction {"MVSC-6" 1} {"MVSC-6" 12}))))
(t/testing "Should regard a bad date as an error"
(let [row (sut/manual->transaction (assoc base-transaction :raw-date "124") {"MVSC-6" 1} {"MVSC-6" 12})]
(t/is (=
[{:info "Date 124 must match MM/dd/yyyy"
:details "java.lang.Exception: Date 124 must match MM/dd/yyyy"}]
(:errors row)))))
(t/testing "Should consider an unparseable amount as an error"
(let [row (sut/manual->transaction (assoc base-transaction :amount "212.23,.41") {"MVSC-6" 1} {"MVSC-6" 12})]
(t/is (=
[{:info "Could not parse total from value '212.23,.41'"
:details "java.lang.Exception: Could not parse total from value '212.23,.41'"}]
(:errors row)))))
(t/testing "Should consider a nonexistant bank account as an error"
(let [row (sut/manual->transaction (assoc base-transaction :bank-account-code "DUMMY") {"MVSC-6" 1} {"MVSC-6" 12})]
(t/is (=
[{:info "Cannot find bank account by code DUMMY"
:details "java.lang.Exception: Cannot find bank account by code DUMMY"}
{:info "Cannot find client for bank account code DUMMY"
:details "java.lang.Exception: Cannot find client for bank account code DUMMY"}]
(:errors row))))))

View File

@@ -1,13 +1,10 @@
(ns auto-ap.integration.yodlee.import
(:require [auto-ap.yodlee.import :as sut]
[auto-ap.yodlee.core :as c]
[datomic.api :as d]
[auto-ap.datomic :refer [uri conn]]
[auto-ap.rule-matching :as rm]
(ns auto-ap.import.transactions-test
(:require [auto-ap.datomic :refer [conn uri]]
[auto-ap.datomic.migrate :as m]
[auto-ap.import.transactions :as sut]
[clojure.test :as t]
[clojure.tools.logging :as log]
[clojure.set :as set]))
[datomic.api :as d]
[clj-time.coerce :as coerce]))
(defn wrap-setup
[f]
@@ -24,311 +21,72 @@
(defn noop-rule [transaction locations]
transaction)
(def base-transaction {:postDate "2014-01-04"
:accountId 1234
:date "2014-01-02"
:id 1
:amount {:amount 12.0}
:description {:original "original-description"
:simple "simple-description"}
:merchant {:id "123"
:name "456"}
:baseType "DEBIT"
:status "POSTED"
(def base-transaction #:transaction {:date #inst "2020-01-02T00:00:00-08:00"
:raw-id "1"
:id (digest/sha-256 "1")
:amount 12.0
:description-original "original-description"
:status "POSTED"
:client 123
:bank-account 456})
:bank-account {:db/id 456
:client/_bank-accounts {:db/id 123
:client/locations ["Z" "E"]}}})
(t/deftest categorize-transactions
(let [bank-account {:db/id 456
:client/_bank-accounts {:db/id 123
:client/locations ["MH"]}}]
(t/testing "Should exclude a transaction before start date"
(t/is (= :not-ready
(sut/categorize-transaction (assoc base-transaction :transaction/date #inst "2020-01-01")
(assoc bank-account :bank-account/start-date #inst "2030-01-01")
{})))
(t/is (= :import
(sut/categorize-transaction (assoc base-transaction :transaction/date #inst "2020-01-02")
(assoc bank-account :bank-account/start-date #inst "2020-01-01")
{}))))
(t/testing "Should error a transaction without a client"
(t/is (= :error
(sut/categorize-transaction (dissoc base-transaction :transaction/client)
bank-account
{}))))
(t/deftest add-transactions
(t/testing "Should error a transaction without a bank-account"
(t/is (= :error
(sut/categorize-transaction (dissoc base-transaction :transaction/bank-account)
bank-account
{}))))
(t/testing "Should error a transaction without an id"
(t/is (= :error
(sut/categorize-transaction (dissoc base-transaction :transaction/id)
bank-account
{}))))
(t/testing "Should ignore a transaction that exists"
(t/is (= :extant
(sut/categorize-transaction (assoc base-transaction :transaction/id "123")
bank-account
{"123" :transaction-approval-status/unapproved}))))
(t/testing "Should import a transaction"
(t/is (= :import
(sut/categorize-transaction base-transaction
bank-account
{}))))))
(t/deftest transaction->txs
(t/testing "Should import and code transactions"
(let [[result] (sut/transactions->txs [base-transaction]
:bank-account
noop-rule
#{})]
(t/is (= [#:transaction {:amount -12.0
:date #inst "2014-01-02T08:00:00.000-00:00"
:bank-account 456
:client 123
:post-date #inst "2014-01-04T08:00:00.000-00:00"
:account-id 1234
:description-original "original-description"
:status "POSTED"
:id "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"
:raw-id "1"
:approval-status :transaction-approval-status/unapproved
:description-simple "simple-description"}]
result)))))
(t/testing "Should import one transaction"
(let [{:strs [bank-account-id client-id]} (:tempids @(d/transact conn [{:db/id "bank-account-id"
:bank-account/code "TEST-1"}
{:db/id "client-id"
:client/code "TEST"
:client/locations ["Z" "E"]
:client/bank-accounts ["bank-account-id"]}]))
result (sut/transaction->txs base-transaction
(d/entity (d/db conn) bank-account-id)
noop-rule)]
(t/is (= [(assoc base-transaction :transaction/approval-status :transaction-approval-status/unapproved)]
result))))
(t/deftest do-import
(t/testing "Should import single transaction"
(println "HER")
(let [{:strs [bank-account-id client-id]} (:tempids @(d/transact conn [{:db/id "bank-account-id"
:bank-account/code "TEST-1"}
{:db/id "client-id"
:client/code "TEST"
:client/locations ["Z" "E"]
:client/bank-accounts ["bank-account-id"]}]))
result (sut/transactions->txs [base-transaction]
(d/entity (d/db conn) bank-account-id)
1
noop-rule
#{})]
(t/is (= {:import [[#:transaction {:amount -12.0
:date #inst "2014-01-02T08:00:00.000-00:00"
:bank-account bank-account-id
:client client-id
:post-date #inst "2014-01-04T08:00:00.000-00:00"
:account-id 1234
:description-original "original-description"
:status "POSTED"
:id "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"
:raw-id "1"
:approval-status :transaction-approval-status/unapproved
:description-simple "simple-description"
:import-batch/_entry 1}]]}
result))))
(t/testing "Should exclude a transaction before start date"
(let [{:strs [bank-account-id client-id]} (:tempids @(d/transact conn [{:db/id "bank-account-id"
:bank-account/code "TEST-1"
:bank-account/start-date #inst "2030-01-01"}
{:db/id "client-id"
:client/code "TEST"
:client/locations ["Z" "E"]
:client/bank-accounts ["bank-account-id"]}]))
result (sut/transactions->txs [(assoc base-transaction :date "2020-05-01")]
(d/entity (d/db conn) bank-account-id)
1
noop-rule
#{})]
(t/is (= 1 (count (:not-ready result))))))
(t/testing "Should not reimport an existing transaction"
(let [{:strs [bank-account-id client-id]} (:tempids @(d/transact conn [{:db/id "bank-account-id"
:bank-account/code "TEST-1"
:bank-account/start-date #inst "2030-01-01"}
{:db/id "client-id"
:client/code "TEST"
:client/locations ["Z" "E"]
:client/bank-accounts ["bank-account-id"]}]))
result (sut/transactions->txs [(assoc base-transaction :id "123")]
(d/entity (d/db conn) bank-account-id)
1
noop-rule
{(digest/sha-256 "123") :transaction-approval-status/unapproved})]
(t/is (= 1 (count (:extant result))))))
(t/testing "Should skip transaction if no client is found"
(let [result (sut/transactions->txs [base-transaction]
(d/entity (d/db conn) 1238970123)
1
noop-rule
{})]
(t/is (= 1 (count (:error result))))))
(t/testing "Should match an uncleared check"
(let [{:strs [bank-account-id client-id payment-id]} (->> [#:payment {:status :payment-status/pending
:date #inst "2019-01-01"
:bank-account "bank-account-id"
:client "client-id"
:check-number 10001
:amount 30.0
:db/id "payment-id"}
#:bank-account {:name "Bank account"
:db/id "bank-account-id"}
#:client {:name "Client"
:db/id "client-id"
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)]
(println (sut/transactions->txs [(assoc base-transaction
:description {:original "CHECK 10001"
:simple ""}
:amount {:amount 30.0})]
(d/entity (d/db conn ) bank-account-id)
1
noop-rule
{}))
(let [[[transaction-result]] (:import (sut/transactions->txs [(assoc base-transaction
:description {:original "CHECK 10001"
:simple ""}
:amount {:amount 30.0})]
(d/entity (d/db conn ) bank-account-id)
1
noop-rule
{}))]
(t/is (= {:db/id payment-id
:payment/status :payment-status/cleared}
(:transaction/payment transaction-result))))
(t/testing "Should not match an already matched check"
@(d/transact (d/connect uri) [{:db/id payment-id :payment/status :payment-status/cleared}])
(let [[result] (:import (sut/transactions->txs [(assoc base-transaction
:description {:original "CHECK 10001"
:simple ""}
:amount {:amount 30.0}
:id 789)]
(d/entity (d/db conn) bank-account-id)
1
noop-rule
{}))]
(t/is (= nil
(:transaction/payment result)))))))
(t/testing "Should match expected-deposits"
(let [{:strs [bank-account-id client-id expected-deposit-id]} (->> [#:expected-deposit {:client "client-id"
:date #inst "2021-07-01T00:00:00-08:00"
:total 100.0
:location "MF"
:status :expected-deposit-status/pending
:db/id "expected-deposit-id"}
#:bank-account {:name "Bank account"
:db/id "bank-account-id"}
#:client {:name "Client"
:db/id "client-id"
:locations ["MF"]
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)]
(t/testing "Should match within 10 days"
(let [[[transaction-result]] (:import (sut/transactions->txs [(assoc base-transaction
:date "2021-07-03"
:amount {:amount -100.0})]
(d/entity (d/db conn) bank-account-id)
1
noop-rule
#{}))]
(t/is (= expected-deposit-id
(sut/find-expected-deposit client-id 100.0 (clj-time.coerce/to-date-time #inst "2021-07-03T00:00:00-08:00"))))
(t/is (= {:db/id expected-deposit-id
:expected-deposit/status :expected-deposit-status/cleared}
(:transaction/expected-deposit transaction-result)))))
(t/testing "Should not match old expected deposits"
(let [[[transaction-result]] (:import (sut/transactions->txs [(assoc base-transaction
:date "2021-07-13"
:amount {:amount -100.0})]
(d/entity (d/db conn) bank-account-id)
1
noop-rule
{}))]
(t/is (not (:transaction/expected-deposit transaction-result)))))
(t/testing "Should only match exact."
(let [[[transaction-result]] (:import (sut/transactions->txs [(assoc base-transaction
:date "2021-07-03"
:amount {:amount -100.01})]
(d/entity (d/db conn) bank-account-id)
1
noop-rule
{}))]
(t/is (not (:transaction/expected-deposit transaction-result)))))))
#_(t/testing "Auto-pay Invoices"
(t/testing "Should match paid invoice that doesn't have a payment yet"
(let [{:strs [bank-account-id client-id invoice1-id invoice2-id vendor-id]} (->> [#:invoice {:status :invoice-status/paid
:vendor "vendor-id"
:scheduled-payment #inst "2019-01-04"
:date #inst "2019-01-01"
:client "client-id"
:total 20.0
:db/id "invoice1-id"}
#:invoice {:status :invoice-status/paid
:vendor "vendor-id"
:scheduled-payment #inst "2019-01-04"
:date #inst "2019-01-01"
:client "client-id"
:total 10.0
:db/id "invoice2-id"}
#:vendor {:name "Autopay vendor"
:db/id "vendor-id"}
#:bank-account {:name "Bank account"
:db/id "bank-account-id"}
#:client {:name "Client"
:db/id "client-id"
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)
[[transaction-tx payment-tx invoice-payments1-tx invoice-payments2-tx]] (sut/transactions->txs [(assoc base-transaction
:amount {:amount 30.0}
:bank-account {:db/id bank-account-id
:client/_bank-accounts {:db/id client-id
:client/locations ["A"]}})]
:bank-account
noop-rule
#{})]
(t/is (= :transaction-approval-status/approved
(:transaction/approval-status transaction-tx))
(str "Should have approved transaction " transaction-tx))
(t/is (= #:payment{:status :payment-status/cleared
:type :payment-type/debit
:date (:transaction/date transaction-tx)
:client client-id
:bank-account bank-account-id
:vendor vendor-id
:amount 30.0}
(dissoc payment-tx :db/id))
(str "Should have created payment " payment-tx))
(t/is (= #:invoice-payment{:invoice invoice1-id
:amount 20.0
:payment (:db/id payment-tx)}
(dissoc invoice-payments1-tx :db/id))
(str "Should have paid invoice 1" invoice-payments1-tx))
(t/is (= #:invoice-payment{:invoice invoice2-id
:amount 10.0
:payment (:db/id payment-tx)}
(dissoc invoice-payments2-tx :db/id))
(str "Should have paid invoice 2" invoice-payments2-tx))))
(t/testing "Should not match paid invoice that isn't a scheduled payment"
(let [{:strs [bank-account-id client-id invoice-id]} (->> [#:invoice {:status :invoice-status/paid
:vendor "vendor-id"
:date #inst "2019-01-01"
:client "client-id"
:total 30.0
:db/id "invoice-id"}
#:vendor {:name "Autopay vendor"
:db/id "vendor-id"}
#:bank-account {:name "Bank account"
:db/id "bank-account-id"}
#:client {:name "Client"
:db/id "client-id"
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)
[[transaction-tx payment-tx]] (sut/transactions->txs [(assoc base-transaction
:amount {:amount 30.0}
:bank-account {:db/id bank-account-id
:client/_bank-accounts {:db/id client-id
:client/locations ["A"]}})]
:bank-account
noop-rule
#{})]
(t/is (= :transaction-approval-status/unapproved
(:transaction/approval-status transaction-tx)))
(t/is (nil? (:transaction/payment transaction-tx))))))
(t/testing "Rules"
(t/testing "Should apply rules to imported transaction"
(t/testing "Should match an uncleared check"
(let [{:strs [bank-account-id client-id payment-id]} (->> [#:payment {:status :payment-status/pending
:date #inst "2019-01-01"
:bank-account "bank-account-id"
@@ -343,198 +101,114 @@
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)
[[transaction-tx]] (:import (sut/transactions->txs [(assoc base-transaction
:description {:original "Hello XXX039"
:simple ""}
:amount {:amount 31.0}
:id 789)]
(d/entity (d/db conn) bank-account-id)
1
(rm/rule-applying-fn [{:transaction-rule/description "XXX039"
:transaction-rule/transaction-approval-status :transaction-approval-status/approved}])
#{}))]
(t/is (= :transaction-approval-status/approved
(:transaction/approval-status transaction-tx)))))
:tempids)]
(t/testing "Should apply vendor and approval status"
(let [apply-rules (rm/rule-applying-fn [{:db/id 1
:transaction-rule/description "XXX039"
:transaction-rule/transaction-approval-status :transaction-approval-status/approved
:transaction-rule/vendor {:db/id 123}
:transaction-rule/accounts [{:transaction-rule-account/account {:db/id 9}
:transaction-rule-account/location "Shared"
:transaction-rule-account/percentage 1.0}]}
{:db/id 2
:transaction-rule/description "OtherMatch"
:transaction-rule/transaction-approval-status :transaction-approval-status/requires-feedback
:transaction-rule/vendor {:db/id 456}
:transaction-rule/accounts [{:transaction-rule-account/account {:db/id 9}
:transaction-rule-account/location "Z"
:transaction-rule-account/percentage 1.0}]}])]
(t/is (= {:transaction/description-original "Hello XXX039",
:transaction/vendor 123
:transaction/approval-status :transaction-approval-status/approved
:transaction/accounts [{:transaction-account/account 9
:transaction-account/amount 30.0
:transaction-account/location "Z"}]
:transaction/matched-rule 1
:transaction/amount 30.0}
(-> {:transaction/description-original "Hello XXX039"
:transaction/amount 30.0}
(apply-rules ["Z"]))))
(let [[transaction-result] (sut/transaction->txs (assoc base-transaction
:transaction/description-original "CHECK 10001"
:transaction/amount -30.0)
(d/entity (d/db conn ) bank-account-id)
noop-rule)]
(t/is (= {:db/id payment-id
:payment/status :payment-status/cleared}
(:transaction/payment transaction-result))))
(t/is (= {:transaction/description-original "OtherMatch",
:transaction/approval-status :transaction-approval-status/requires-feedback
:transaction/vendor 456
:transaction/amount 30.0
:transaction/matched-rule 2
:transaction/accounts [{:transaction-account/account 9
:transaction-account/amount 30.0
:transaction-account/location "Z"}]}
(-> {:transaction/description-original "OtherMatch"
:transaction/amount 30.0}
(apply-rules ["Z"]))))
(t/testing "Should not match an already matched check"
@(d/transact (d/connect uri) [{:db/id payment-id :payment/status :payment-status/cleared}])
(let [[result] (sut/transaction->txs (assoc base-transaction
:transaction/description-original "CHECK 10001"
:transaction/amount -30.0)
(d/entity (d/db conn) bank-account-id)
noop-rule)]
(t/is (= nil
(:transaction/payment result)))))))
(t/is (= {:transaction/description-original "Hello Not match"}
(-> {:transaction/description-original "Hello Not match"}
(apply-rules []))))))
(t/testing "Should match if day of month matches"
(let [apply-rules (rm/rule-applying-fn [{:db/id 123
:transaction-rule/dom-gte 3
:transaction-rule/dom-lte 9
:transaction-rule/transaction-approval-status :transaction-approval-status/approved
:transaction-rule/vendor {:db/id 123}}])]
(t/is (= 123
(-> {:transaction/date #inst "2019-01-04T00:00:00.000-08:00"
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/date #inst "2019-01-03T00:00:00.000-08:00"
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/date #inst "2019-01-09T00:00:00.000-08:00"
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/date #inst "2019-01-01T00:00:00.000-08:00"
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/date #inst "2019-01-10T00:00:00.000-08:00"
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should match expected-deposits"
(let [{:strs [bank-account-id client-id expected-deposit-id]} (->> [#:expected-deposit {:client "client-id"
:date #inst "2021-07-01T00:00:00-08:00"
:total 100.0
:location "MF"
:status :expected-deposit-status/pending
:db/id "expected-deposit-id"}
#:bank-account {:name "Bank account"
:db/id "bank-account-id"}
#:client {:name "Client"
:db/id "client-id"
:locations ["MF"]
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)]
(t/testing "Should match if amount matches"
(let [apply-rules (rm/rule-applying-fn [{:db/id 123
:transaction-rule/amount-gte 3.0
:transaction-rule/amount-lte 9.0
:transaction-rule/transaction-approval-status :transaction-approval-status/approved
:transaction-rule/accounts [#:transaction-rule-account {:percentage 1.0
:locatoin "HQ"}]
:transaction-rule/vendor {:db/id 123}}])]
(t/is (= 123
(-> {:transaction/amount 4.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/amount 3.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/amount 9.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/amount 9.01}
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/amount 2.99}
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should match if client matches"
(let [apply-rules (rm/rule-applying-fn [{:db/id 123
:transaction-rule/client {:db/id 456}}])]
(t/is (= 123
(-> {:transaction/client 456
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/client 89
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should match if bank-account matches"
(let [apply-rules (rm/rule-applying-fn [{:db/id 123
:transaction-rule/bank-account {:db/id 456}}])]
(t/is (= 123
(-> {:transaction/bank-account 456
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/bank-account 89
:transaction/amount 1.0}
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should match within 10 days"
(let [[transaction-result] (sut/transaction->txs (assoc base-transaction
:transaction/date #inst "2021-07-03T00:00:00-08:00"
:transaction/amount 100.0)
(d/entity (d/db conn) bank-account-id)
noop-rule)]
(t/is (= expected-deposit-id
(sut/find-expected-deposit client-id 100.0 (clj-time.coerce/to-date-time #inst "2021-07-03T00:00:00-08:00"))))
(t/is (= {:db/id expected-deposit-id
:expected-deposit/status :expected-deposit-status/cleared}
(:transaction/expected-deposit transaction-result)))))
(t/testing "Should prioritize rules"
(let [apply-rules (rm/rule-applying-fn (shuffle [{:db/id 2
:transaction-rule/description "Hello"
:transaction-rule/amount-gte 5.0}
{:db/id 1
:transaction-rule/description "Hello"}
{:db/id 0
:transaction-rule/description "Hello"}
{:db/id 3
:transaction-rule/description "Hello"
:transaction-rule/client {:db/id 789}}
{:db/id 4
:transaction-rule/description "Hello"
:transaction-rule/client {:db/id 789}
:transaction-rule/bank-account {:db/id 456}}]))]
(t/testing "Should not match old expected deposits"
(let [[transaction-result] (sut/transaction->txs (assoc base-transaction
:transaction/date #inst "2021-07-13"
:transaction/amount 100.0)
(d/entity (d/db conn) bank-account-id)
noop-rule)]
(t/is (not (:transaction/expected-deposit transaction-result)))))
(t/is (= 4
(-> {:transaction/bank-account 456
:transaction/client 789
:transaction/description-original "Hello"
:transaction/amount 6.0}
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 3
(-> {:transaction/bank-account 457
:transaction/client 789
:transaction/amount 6.0
:transaction/description-original "Hello"}
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 3
(-> {:transaction/bank-account 457
:transaction/client 789
:transaction/amount 6.0
:transaction/description-original "Hello"}
(apply-rules [])
:transaction/matched-rule)))
(t/testing "Should only match exact."
(let [[transaction-result] (sut/transaction->txs (assoc base-transaction
:transaction/date "2021-07-03"
:transaction/amount 100.01)
(d/entity (d/db conn) bank-account-id)
noop-rule)]
(t/is (not (:transaction/expected-deposit transaction-result)))))))))
(t/testing "Should only apply if there is a single rule at that specificity level"
(t/is (nil?
(-> {:transaction/bank-account 3
:transaction/client 1
:transaction/description-original "Hello"
:transaction/amount 0.0}
(apply-rules [])
:transaction/matched-rule))))))))
(t/deftest apply-synthetic-ids
(t/testing "Should increment index for duplicate transactions"
(t/is (= ["2020-01-02T00:00:00.000-08:00-456-original-description-12.0-0-123"
"2020-01-02T00:00:00.000-08:00-456-original-description-12.0-1-123"]
(map :transaction/raw-id (sut/apply-synthetic-ids [base-transaction base-transaction])))))
(t/testing "Should use unique ids if a parameter is different"
(t/is (= ["2020-01-02T00:00:00.000-08:00-456-original-description-12.0-0-123"
"2020-01-02T00:00:00.000-08:00-456-original-description-13.0-0-123"]
(map :transaction/raw-id (sut/apply-synthetic-ids [base-transaction (assoc base-transaction :transaction/amount 13.0)])))))
(t/testing "Should increment index if an unimportant field is set"
(t/is (= ["2020-01-02T00:00:00.000-08:00-456-original-description-12.0-0-123"
"2020-01-02T00:00:00.000-08:00-456-original-description-12.0-1-123"]
(map :transaction/raw-id (sut/apply-synthetic-ids [base-transaction (assoc base-transaction :transaction/other-random-value 10.0)])))))
(t/testing "Should be forgiving if dates come in other formats"
(t/is (= "2020-01-02T00:00:00.000-08:00-456-original-description-12.0-0-123"
(->> (sut/apply-synthetic-ids [(assoc base-transaction
:transaction/date #inst "2020-01-02T00:00:00-08:00")])
first
:transaction/raw-id)))
(t/is (= "2020-01-02T00:00:00.000-08:00-456-original-description-12.0-0-123"
(->> (sut/apply-synthetic-ids [(assoc base-transaction
:transaction/date #inst "2020-01-02T08:00:00-00:00")])
first
:transaction/raw-id)))
(t/is (= "2020-01-02T00:00:00.000-08:00-456-original-description-12.0-0-123"
(->> (sut/apply-synthetic-ids [(assoc base-transaction
:transaction/date (coerce/to-date-time #inst "2020-01-02T08:00:00-00:00"))])
first
:transaction/raw-id)))
(t/is (= "2020-01-02T00:00:00.000-08:00-456-original-description-12.0-0-123"
(->> (sut/apply-synthetic-ids [(assoc base-transaction
:transaction/date (coerce/to-date-time #inst "2020-01-02T00:00:00-08:00"))])
first
:transaction/raw-id)))))
(t/deftest match-transaction-to-single-unfulfilled-payments
@@ -580,7 +254,7 @@
(t/testing "Should not match unpaid invoice"
(let [{:strs [client-id invoice-id]} (->> [#:invoice {:status :invoice-status/unpaid
:scheduled-payment #inst "2019-01-04"
:vendor vendor1-id
:vendor vendor1-id
:date #inst "2019-01-01"
:client "client-id"
:total 30.0
@@ -609,7 +283,7 @@
deref
:tempids)
invoices-matches (sut/match-transaction-to-single-unfulfilled-autopayments -30.0
client-id)]
client-id)]
(t/is (= [] invoices-matches))))
(t/testing "Should match multiple invoices for same vendor that total to transaction amount"
(let [{:strs [client-id invoice1-id invoice2-id]} (->> [#:invoice {:status :invoice-status/paid
@@ -709,3 +383,98 @@
(str "Expected to match with the chronologically adjacent invoice-1 and invoice-3."))
(t/is (= [] (sut/match-transaction-to-single-unfulfilled-autopayments -31.0 client-id))
(str "Expected to not match, because there is invoice-3 is between invoice-1 and invoice-2.")))))))
#_(t/testing "Auto-pay Invoices"
(t/testing "Should match paid invoice that doesn't have a payment yet"
(let [{:strs [bank-account-id client-id invoice1-id invoice2-id vendor-id]} (->> [#:invoice {:status :invoice-status/paid
:vendor "vendor-id"
:scheduled-payment #inst "2019-01-04"
:date #inst "2019-01-01"
:client "client-id"
:total 20.0
:db/id "invoice1-id"}
#:invoice {:status :invoice-status/paid
:vendor "vendor-id"
:scheduled-payment #inst "2019-01-04"
:date #inst "2019-01-01"
:client "client-id"
:total 10.0
:db/id "invoice2-id"}
#:vendor {:name "Autopay vendor"
:db/id "vendor-id"}
#:bank-account {:name "Bank account"
:db/id "bank-account-id"}
#:client {:name "Client"
:db/id "client-id"
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)
[[transaction-tx payment-tx invoice-payments1-tx invoice-payments2-tx]] (sut/yodlees->transactions [(assoc base-yodlee-transaction
:amount {:amount 30.0}
:bank-account {:db/id bank-account-id
:client/_bank-accounts {:db/id client-id
:client/locations ["A"]}})]
:bank-account
noop-rule
#{})]
(t/is (= :transaction-approval-status/approved
(:transaction/approval-status transaction-tx))
(str "Should have approved transaction " transaction-tx))
(t/is (= #:payment{:status :payment-status/cleared
:type :payment-type/debit
:date (:transaction/date transaction-tx)
:client client-id
:bank-account bank-account-id
:vendor vendor-id
:amount 30.0}
(dissoc payment-tx :db/id))
(str "Should have created payment " payment-tx))
(t/is (= #:invoice-payment{:invoice invoice1-id
:amount 20.0
:payment (:db/id payment-tx)}
(dissoc invoice-payments1-tx :db/id))
(str "Should have paid invoice 1" invoice-payments1-tx))
(t/is (= #:invoice-payment{:invoice invoice2-id
:amount 10.0
:payment (:db/id payment-tx)}
(dissoc invoice-payments2-tx :db/id))
(str "Should have paid invoice 2" invoice-payments2-tx))))
(t/testing "Should not match paid invoice that isn't a scheduled payment"
(let [{:strs [bank-account-id client-id invoice-id]} (->> [#:invoice {:status :invoice-status/paid
:vendor "vendor-id"
:date #inst "2019-01-01"
:client "client-id"
:total 30.0
:db/id "invoice-id"}
#:vendor {:name "Autopay vendor"
:db/id "vendor-id"}
#:bank-account {:name "Bank account"
:db/id "bank-account-id"}
#:client {:name "Client"
:db/id "client-id"
:bank-accounts ["bank-account-id"]}]
(d/transact (d/connect uri))
deref
:tempids)
[[transaction-tx payment-tx]] (sut/yodlees->transactions [(assoc base-yodlee-transaction
:amount {:amount 30.0}
:bank-account {:db/id bank-account-id
:client/_bank-accounts {:db/id client-id
:client/locations ["A"]}})]
:bank-account
noop-rule
#{})]
(t/is (= :transaction-approval-status/unapproved
(:transaction/approval-status transaction-tx)))
(t/is (nil? (:transaction/payment transaction-tx))))))

View File

@@ -0,0 +1,29 @@
(ns auto-ap.import.yodlee-test
(:require [auto-ap.import.yodlee :as sut]
[clojure.test :as t]))
(def base-transaction {:postDate "2014-01-04"
:accountId 1234
:date "2014-01-02"
:id 1
:amount {:amount 12.0}
:description {:original "original-description"
:simple "simple-description"}
:merchant {:id "123"
:name "456"}
:baseType "DEBIT"
:status "POSTED"})
(t/deftest yodlee->transaction
(t/testing "Should parse dates"
(t/is (= #inst "2021-01-01T00:00:00-08:00" (:transaction/date (sut/yodlee->transaction (assoc base-transaction :date "2021-01-01")))))
(t/is (= #inst "2021-06-01T00:00:00-07:00" (:transaction/date (sut/yodlee->transaction (assoc base-transaction :date "2021-06-01"))))))
(t/testing "Should invert amount for debits"
(t/is (= -12.0 (:transaction/amount (sut/yodlee->transaction (assoc base-transaction
:amount {:amount 12.0}
:baseType "DEBIT")))))
(t/is (= 12.0 (:transaction/amount (sut/yodlee->transaction (assoc base-transaction
:amount {:amount 12.0}
:baseType "CREDIT")))))))