(ns auto-ap.ssr.pos.tenders-test (:require [auto-ap.datomic :as datomic] [auto-ap.ssr.pos.tenders :as tenders] [auto-ap.integration.util :refer [admin-token setup-test-data wrap-setup]] [clojure.test :refer [deftest is testing use-fixtures]] [datomic.api :as dc])) (use-fixtures :each wrap-setup) ;; ============================================================================ ;; Helpers ;; ============================================================================ (defn- make-request ([client-id query-params] {:query-params query-params :clients [{:db/id client-id}] :trimmed-clients #{client-id}}) ([client-id query-params extra] (merge (make-request client-id query-params) extra))) (defn- create-charge [client-id {:keys [date total tip processor location] :or {date #inst "2024-01-15" total 50.0 tip 5.0 processor :ccp-processor/square location "DT"}}] (let [result @(dc/transact datomic/conn [{:db/id "ch" :charge/client client-id :charge/date date :charge/total total :charge/tip tip :charge/processor processor :charge/location location}])] (get-in result [:tempids "ch"]))) ;; ============================================================================ ;; 9.2: Client column hide? when single client ;; ============================================================================ (deftest test-client-column-visibility (testing "Behavior 9.2: Client column hidden when single client, shown when multiple" (let [client-header (first (:headers tenders/grid-page))] (is (fn? (:hide? client-header))) (is ((:hide? client-header) {:clients [{:db/id 1}]})) (is (not ((:hide? client-header) {:clients [{:db/id 1} {:db/id 2}]})))))) ;; ============================================================================ ;; 10.x Filtering ;; ============================================================================ (deftest test-filter-date-range (testing "Behavior 10.1: Date range filtering" (let [{:strs [test-client-id]} (setup-test-data [])] (create-charge test-client-id {:date #inst "2024-01-10" :total 100.0}) (create-charge test-client-id {:date #inst "2024-01-20" :total 200.0}) (create-charge test-client-id {:date #inst "2024-02-01" :total 300.0}) (let [request (make-request test-client-id {:start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) {:keys [ids count]} (tenders/fetch-ids (dc/db datomic/conn) request)] (is (= 2 count)))))) (deftest test-filter-processor (testing "Behavior 10.2: Processor filtering" (let [{:strs [test-client-id]} (setup-test-data [])] (create-charge test-client-id {:date #inst "2024-01-15" :total 100.0 :processor :ccp-processor/square}) (create-charge test-client-id {:date #inst "2024-01-15" :total 200.0 :processor :ccp-processor/toast}) (let [request (make-request test-client-id {:start-date #inst "2024-01-01" :end-date #inst "2024-01-31" :processor :ccp-processor/square}) {:keys [count]} (tenders/fetch-ids (dc/db datomic/conn) request)] (is (= 1 count)))))) (deftest test-filter-total-range (testing "Behavior 10.3: Total range filtering" (let [{:strs [test-client-id]} (setup-test-data [])] (create-charge test-client-id {:date #inst "2024-01-15" :total 50.0}) (create-charge test-client-id {:date #inst "2024-01-15" :total 100.0}) (create-charge test-client-id {:date #inst "2024-01-15" :total 200.0}) (let [request (make-request test-client-id {:start-date #inst "2024-01-01" :end-date #inst "2024-01-31" :total-gte 75.0 :total-lte 150.0}) {:keys [count]} (tenders/fetch-ids (dc/db datomic/conn) request)] (is (= 1 count)))))) (deftest test-filter-combined (testing "Behavior 10.4: Combined filters" (let [{:strs [test-client-id]} (setup-test-data [])] (create-charge test-client-id {:date #inst "2024-01-10" :total 50.0 :processor :ccp-processor/square}) (create-charge test-client-id {:date #inst "2024-01-20" :total 150.0 :processor :ccp-processor/square}) (create-charge test-client-id {:date #inst "2024-01-20" :total 250.0 :processor :ccp-processor/toast}) (let [request (make-request test-client-id {:start-date #inst "2024-01-15" :end-date #inst "2024-01-31" :total-gte 100.0 :processor :ccp-processor/square}) {:keys [count]} (tenders/fetch-ids (dc/db datomic/conn) request)] (is (= 1 count)) (let [[results _] (tenders/fetch-page request)] (is (= 150.0 (:charge/total (first results))))))))) ;; ============================================================================ ;; 11.x Sorting ;; ============================================================================ (deftest test-sort-by-fields (testing "Behaviors 11.1-11.6: Sort by various fields and toggle direction" (let [{:strs [test-client-id]} (setup-test-data [])] (doall (for [i (range 3)] (create-charge test-client-id {:date (case i 0 #inst "2024-01-10" 1 #inst "2024-01-20" 2 #inst "2024-01-15") :total (case i 0 50.0 1 150.0 2 100.0) :tip (case i 0 2.0 1 8.0 2 5.0) :processor (case i 0 :ccp-processor/square 1 :ccp-processor/toast 2 :ccp-processor/square)}))) ;; Sort by date ascending (let [request (make-request test-client-id {:sort [{:sort-key "date" :asc true}] :start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results _] (tenders/fetch-page request)] (is (= 3 (count results))) (is (= 50.0 (:charge/total (first results))))) ;; Sort by date descending (let [request (make-request test-client-id {:sort [{:sort-key "date" :asc false}] :start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results _] (tenders/fetch-page request)] (is (= 150.0 (:charge/total (first results))))) ;; Sort by total ascending (let [request (make-request test-client-id {:sort [{:sort-key "total" :asc true}] :start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results _] (tenders/fetch-page request)] (is (= 50.0 (:charge/total (first results))))) ;; Sort by tip ascending (let [request (make-request test-client-id {:sort [{:sort-key "tip" :asc true}] :start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results _] (tenders/fetch-page request)] (is (= 2.0 (:charge/tip (first results))))) ;; Sort by processor ascending (let [request (make-request test-client-id {:sort [{:sort-key "processor" :asc true}] :start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results _] (tenders/fetch-page request)] (is (= 3 (count results))))))) ;; ============================================================================ ;; 12.x Pagination ;; ============================================================================ (deftest test-default-pagination (testing "Behavior 12.1: Default 25 per page" (let [{:strs [test-client-id]} (setup-test-data [])] (dotimes [_ 30] (create-charge test-client-id {:date #inst "2024-01-15"})) (let [request (make-request test-client-id {:start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results total] (tenders/fetch-page request)] (is (= 25 (count results))) (is (= 30 total)))))) (deftest test-change-per-page (testing "Behavior 12.2: Change per-page size" (let [{:strs [test-client-id]} (setup-test-data [])] (dotimes [_ 30] (create-charge test-client-id {:date #inst "2024-01-15"})) (let [request (make-request test-client-id {:per-page 10 :start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results total] (tenders/fetch-page request)] (is (= 10 (count results))) (is (= 30 total))) (let [request (make-request test-client-id {:per-page 50 :start-date #inst "2024-01-01" :end-date #inst "2024-01-31"}) [results total] (tenders/fetch-page request)] (is (= 30 (count results))) (is (= 30 total))))))