(ns test.auto-ap.yodlee.import (:require [auto-ap.yodlee.import :as sut] [auto-ap.yodlee.core :as c] [datomic.api :as d] [auto-ap.datomic :refer [uri]] [auto-ap.rule-matching :as rm] [auto-ap.datomic.migrate :as m] [clojure.test :as t])) (defn wrap-setup [f] (with-redefs [auto-ap.datomic/uri "datomic:mem://datomic-transactor:4334/invoice"] (d/create-database uri) (m/-main false) (f) (d/release (d/connect uri)) (d/delete-database uri))) (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 :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 "PENDING" :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] :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" :yodlee-merchant #:yodlee-merchant {:yodlee-id "123" :name "456"} :status "PENDING" :id "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" :approval-status :transaction-approval-status/unapproved :description-simple "simple-description"}] result)))) (t/testing "Should not reimport an existing transaction" (let [result (sut/transactions->txs [base-transaction] :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 :bank-account nil)] :bank-account noop-rule #{})] (t/is (= [] 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)] (let [[result] (sut/transactions->txs [(assoc base-transaction :description {:original "CHECK 10001" :simple ""} :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 (= {:db/id payment-id :payment/status :payment-status/cleared} (:transaction/payment 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] (sut/transactions->txs [(assoc base-transaction :description {:original "CHECK 10001" :simple ""} :amount {:amount 30.0} :id 789 :bank-account {:db/id bank-account-id :client/_bank-accounts {:db/id client-id :client/locations ["A"]}})] :bank-account noop-rule #{})] (t/is (= nil (:transaction/payment result))))))) (t/testing "Rules" (t/testing "Should apply rules to imported transaction" (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) [result] (sut/transactions->txs [(assoc base-transaction :description {:original "Hello XXX039" :simple ""} :amount {:amount 31.0} :id 789 :bank-account {:db/id bank-account-id :client/_bank-accounts {:db/id client-id :client/locations ["A"]}})] :bank-account (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 result))))) (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"])))) (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/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"} (apply-rules []) :transaction/matched-rule))) (t/is (= 123 (-> {:transaction/date #inst "2019-01-03T00:00:00.000-08:00"} (apply-rules []) :transaction/matched-rule))) (t/is (= 123 (-> {:transaction/date #inst "2019-01-09T00:00:00.000-08:00"} (apply-rules []) :transaction/matched-rule))) (t/is (nil? (-> {:transaction/date #inst "2019-01-01T00:00:00.000-08:00"} (apply-rules []) :transaction/matched-rule))) (t/is (nil? (-> {:transaction/date #inst "2019-01-10T00:00:00.000-08:00"} (apply-rules []) :transaction/matched-rule))))) (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/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} (apply-rules []) :transaction/matched-rule))) (t/is (nil? (-> {:transaction/client 89} (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} (apply-rules []) :transaction/matched-rule))) (t/is (nil? (-> {:transaction/bank-account 89} (apply-rules []) :transaction/matched-rule))))) (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/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 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)))))))))