101 lines
5.7 KiB
Clojure
101 lines
5.7 KiB
Clojure
(ns test.auto-ap.graphql
|
|
(:require [auto-ap.graphql :as sut]
|
|
[auto-ap.datomic.migrate :as m]
|
|
[venia.core :as v]
|
|
[clojure.test :as t :refer [deftest is testing use-fixtures]]
|
|
[datomic.api :as d]
|
|
[auto-ap.datomic :refer [uri]]))
|
|
(defn wrap-setup
|
|
[f]
|
|
(d/create-database uri)
|
|
(m/-main false)
|
|
(f)
|
|
(d/release (d/connect uri))
|
|
(d/delete-database uri))
|
|
|
|
(use-fixtures :each wrap-setup)
|
|
(deftest query
|
|
(testing "ledger"
|
|
(testing "it should find ledger entries"
|
|
(let [result (:ledger-page (:data (sut/query nil "{ ledger_page(client_id: null) { count, start, journal_entries { id } }}")))]
|
|
(is (int? (:count result)))
|
|
(is (int? (:start result)))
|
|
(is (seqable? (:journal-entries result))))))
|
|
|
|
(testing "transaction-rules"
|
|
(testing "it should find rules"
|
|
(let [result (-> (sut/query nil "{ transaction_rule_page(client_id: null) { count, start, transaction_rules { id } }}")
|
|
:data
|
|
:transaction-rule-page)]
|
|
(is (int? (:count result)))
|
|
(is (int? (:start result)))
|
|
(is (seqable? (:transaction-rules result)))))
|
|
|
|
(testing "it should add rules"
|
|
(let [q (v/graphql-query {:venia/operation {:operation/type :mutation
|
|
:operation/name "UpsertTransactionRule"}
|
|
:venia/queries [{:query/data (sut/->graphql [:upsert-transaction-rule
|
|
{:transaction-rule {:description "123"}}
|
|
[:id :description]])}]})
|
|
result (-> (sut/query nil q)
|
|
:data
|
|
:upsert-transaction-rule)]
|
|
(is (= "123" (:description result)))
|
|
(is (:id result))))
|
|
|
|
(testing "it should match rules based on description regex"
|
|
(let [matching-transaction @(d/transact (d/connect uri)
|
|
[{:transaction/description-original "matching-desc"
|
|
:transaction/date #inst "2019-01-05"
|
|
:transaction/client {:client/name "1"
|
|
:db/id "client-1"}
|
|
:transaction/bank-account {:db/id "bank-account-1"
|
|
:bank-account/name "1"}
|
|
|
|
:transaction/amount 1.00
|
|
:transaction/id "2019-01-05 matching-desc 1"}
|
|
|
|
{:transaction/description-original "nonmatching-desc"
|
|
:transaction/client {:client/name "2"
|
|
:db/id "client-2"}
|
|
:transaction/bank-account {:db/id "bank-account-2"
|
|
:bank-account/name "2"}
|
|
:transaction/date #inst "2019-01-15"
|
|
:transaction/amount 2.00
|
|
:transaction/id "2019-01-15 nonmatching-desc 2"}])
|
|
{:strs [client-1 client-2 bank-account-1 bank-account-2]} (get-in matching-transaction [:tempids])
|
|
|
|
rule-test (fn [rule]
|
|
(-> (sut/query nil (v/graphql-query {:venia/operation {:operation/type :query
|
|
:operation/name "TestTransactionRule"}
|
|
:venia/queries [{:query/data (sut/->graphql [:test-transaction-rule
|
|
{:transaction-rule rule}
|
|
[:id]])}]}))
|
|
:data
|
|
:test-transaction-rule))]
|
|
(testing "based on date "
|
|
(is (= [{:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:dom-gte 14 :dom-lte 16})))
|
|
(is (= [{:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:dom-gte 14})))
|
|
(is (= [{:id "2019-01-05 matching-desc 1"} {:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:dom-lte 15})))
|
|
#_(is (= [{:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:dom-gte 15})))
|
|
#_(is (= [{:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:dom-gte 15 :dom-lte 15}))))
|
|
|
|
(testing "based on description"
|
|
(is (= [{:id "2019-01-05 matching-desc 1"}] (rule-test {:description "^match"}))))
|
|
|
|
(testing "based on amount"
|
|
(is (= [{:id "2019-01-05 matching-desc 1"}] (rule-test {:amount-gte 1.0 :amount-lte 1.0})))
|
|
(is (= [{:id "2019-01-05 matching-desc 1"} {:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:amount-gte 1.0 })) )
|
|
(is (= [{:id "2019-01-05 matching-desc 1"} {:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:amount-lte 2.0 })) ))
|
|
|
|
(testing "based on client"
|
|
(is (= [{:id "2019-01-05 matching-desc 1"}] (rule-test {:client-id client-1})))
|
|
(is (= [{:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:client-id client-2}))))
|
|
|
|
(testing "based on bank account"
|
|
(is (= [{:id "2019-01-05 matching-desc 1"}] (rule-test {:bank-account-id bank-account-1})))
|
|
(is (= [{:id "2019-01-15 nonmatching-desc 2"}] (rule-test {:bank-account-id bank-account-2}))))))
|
|
|
|
))
|
|
|