much cleaner matching solution... won't rematch

This commit is contained in:
Bryce Covert
2019-05-16 20:16:12 -07:00
parent dde757e8fc
commit 93ef07f4b1
2 changed files with 105 additions and 104 deletions

View File

@@ -17,6 +17,9 @@
(t/use-fixtures :each wrap-setup)
(defn noop-rule [transaction locations]
transaction)
(t/deftest do-import
(let [base-transaction {:postDate "2014-01-04"
:accountId 1234
@@ -30,13 +33,13 @@
:baseType "DEBIT"
:status "PENDING"
:client-id 123
:bank-account-id 456}]
:bank-account {:db/id 456
:client/_bank-accounts {:db/id 123
:client/locations ["Z" "E"]}}}]
(t/testing "Should import single transaction"
(let [result (sut/transactions->txs [base-transaction]
:client-id
:bank-account-id
identity
:bank-account
noop-rule
#{})]
(t/is (= [#:transaction {:amount -12.0
:date #inst "2014-01-02T08:00:00.000-00:00"
@@ -55,19 +58,17 @@
(t/testing "Should not reimport an existing transaction"
(let [result (sut/transactions->txs [base-transaction]
:client-id
:bank-account-id
identity
:bank-account
noop-rule
#{"6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b"})]
(t/is (= []
result))))
(t/testing "Should skip transaction if no client is found"
(let [result (sut/transactions->txs [(assoc base-transaction :client-id nil)]
:client-id
:bank-account-id
identity
(let [result (sut/transactions->txs [(assoc base-transaction :bank-account nil)]
:bank-account
noop-rule
#{})]
(t/is (= [] result))))
@@ -93,11 +94,11 @@
:description {:original "CHECK 10001"
:simple ""}
:amount {:amount 30.0}
:client-id client-id
:bank-account-id bank-account-id)]
:client-id
:bank-account-id
identity
:bank-account {:db/id bank-account-id
:client/_bank-accounts {:db/id client-id
:client/locations ["A"]}})]
:bank-account
noop-rule
#{})]
(t/is (= {:db/id payment-id
@@ -111,11 +112,11 @@
:simple ""}
:amount {:amount 30.0}
:id 789
:client-id client-id
:bank-account-id bank-account-id)]
:client-id
:bank-account-id
identity
:bank-account {:db/id bank-account-id
:client/_bank-accounts {:db/id client-id
:client/locations ["A"]}})]
:bank-account
noop-rule
#{})]
(t/is (= nil
@@ -144,10 +145,10 @@
:simple ""}
:amount {:amount 30.0}
:id 789
:client-id client-id
:bank-account-id bank-account-id)]
:client-id
:bank-account-id
:bank-account {:db/id bank-account-id
:client/_bank-accounts {:db/id client-id
:client/locations ["A"]}})]
:bank-account
(sut/rule-applying-fn [{:transaction-rule/description "XXX039"
:transaction-rule/transaction-approval-status :transaction-approval-status/approved}])
#{})]
@@ -161,7 +162,7 @@
: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 "Z"
:transaction-rule-account/location "Shared"
:transaction-rule-account/percentage 1.0}]}
{:db/id 2
:transaction-rule/description "OtherMatch"
@@ -180,7 +181,7 @@
:transaction/amount 30.0}
(-> {:transaction/description-original "Hello XXX039"
:transaction/amount 30.0}
apply-rules)))
(apply-rules ["Z"]))))
(t/is (= {:transaction/description-original "OtherMatch",
:transaction/approval-status :transaction-approval-status/requires-feedback
@@ -192,11 +193,11 @@
:transaction-account/location "Z"}]}
(-> {:transaction/description-original "OtherMatch"
:transaction/amount 30.0}
apply-rules)))
(apply-rules ["Z"]))))
(t/is (= {:transaction/description-original "Hello Not match"}
(-> {:transaction/description-original "Hello Not match"}
apply-rules)))))
(apply-rules []))))))
(t/testing "Should match if day of month matches"
(let [apply-rules (sut/rule-applying-fn [{:db/id 123
@@ -206,23 +207,23 @@
:transaction-rule/vendor {:db/id 123}}])]
(t/is (= 123
(-> {:transaction/date #inst "2019-01-04T00:00:00.000-08:00"}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/date #inst "2019-01-03T00:00:00.000-08:00"}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/date #inst "2019-01-09T00:00:00.000-08:00"}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/date #inst "2019-01-01T00:00:00.000-08:00"}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/date #inst "2019-01-10T00:00:00.000-08:00"}
apply-rules
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should match if amount matches"
@@ -233,23 +234,23 @@
:transaction-rule/vendor {:db/id 123}}])]
(t/is (= 123
(-> {:transaction/amount 4.0}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/amount 3.0}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (= 123
(-> {:transaction/amount 9.0}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/amount 9.01}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/amount 2.99}
apply-rules
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should match if client matches"
@@ -257,22 +258,22 @@
:transaction-rule/client {:db/id 456}}])]
(t/is (= 123
(-> {:transaction/client 456}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/client 89}
apply-rules
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should match if bank-account matches"
(let [apply-rules (sut/rule-applying-fn [{:db/id 123
:transaction-rule/bank-account {:db/id 456}}])]
(t/is (= 123
(-> {:transaction/bank-account 456}
apply-rules
(apply-rules [])
:transaction/matched-rule)))
(t/is (nil?
(-> {:transaction/bank-account 89}
apply-rules
(apply-rules [])
:transaction/matched-rule)))))
(t/testing "Should prioritize rules"
@@ -296,21 +297,21 @@
:transaction/client 789
:transaction/description-original "Hello"
:transaction/amount 6.0}
apply-rules
(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
(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
(apply-rules [])
:transaction/matched-rule)))
(t/testing "Should only apply if there is a single rule at that specificity level"
@@ -319,9 +320,5 @@
:transaction/client 1
:transaction/description-original "Hello"
:transaction/amount 0.0}
apply-rules
:transaction/matched-rule))))
#_(t/is (nil?
(-> {:transaction/bank-account 89}
apply-rules
:transaction/matched-rule))))))))
(apply-rules [])
:transaction/matched-rule)))))))))