Email handler unit tests

This commit is contained in:
Daniel O'Connell 2019-10-09 13:58:09 +02:00
parent aea45a0609
commit 6a851a6ff1
4 changed files with 37 additions and 7 deletions

View File

@ -30,8 +30,8 @@ optional font info and optional worklogs info, e.g.:
:worklogs [(...)]}
`:font-path` should be the path to a font file, e.g. "/usr/share/fonts/truetype/freefont/FreeSans.ttf"
See `resources/config.edn` for an example configuration.
`:font-path` should be the path to a font file, e.g. `"/usr/share/fonts/truetype/freefont/FreeSans.ttf"`
See [`resources/config.edn`](https://github.com/mruwnik/invoices/blob/master/resources/config.edn) for an example configuration.
### Seller

View File

@ -57,7 +57,7 @@
:default 1
:parse-fn #(Integer/parseInt %)]
["-w" "--when DATE" "The month for which to generate the invoice"
:default (java.time.LocalDate/now)
:default (-> (java.time.LocalDate/now) (.minusMonths 1))
:parse-fn #(java.time.LocalDate/parse %)]
;; A non-idempotent option (:default is applied first)
["-c" "--company NIP" "companies for which to generate invoices. All, if not provided"

View File

@ -56,20 +56,20 @@
(->> content
str/split-lines
(remove str/blank?)
(map #(str/split % #"[\s;]"))))
(map #(str/split % #"[\s;]+"))))
(defn zip-item [headers cell]
(into (sorted-map) (map vector headers cell)))
(defn parse-float [s]
(Float. (re-find #"[\d\.]+" s )))
(defn parse-double [s]
(Double. (re-find #"-?[\d\.]+" s )))
(defn extract-items [headers message]
(->> message
mess/get-content
split-cells
(map (partial zip-item headers))
(map #(update % :worked parse-float))))
(map #(update % :worked parse-double))))
(defn get-worklogs
"Get all worklogs for the given month from the given imap server."

View File

@ -0,0 +1,30 @@
(ns invoices.email-test
(:require [invoices.email :refer :all]
[clojure.test :refer :all]))
(deftest test-split-cells
(let [cells [["row1-col1", "row1-col2", "row1-col3"]
["row2-col1", "row2-col2", "row2-col3"]
["row3-col1", "row3-col2", "row3-col3"]]]
(testing "Check whether cells get split correctly"
(is (= cells (split-cells "row1-col1;row1-col2;row1-col3\nrow2-col1;row2-col2;row2-col3\nrow3-col1;row3-col2;row3-col3\n")))
(is (= cells (split-cells "row1-col1 row1-col2 row1-col3\nrow2-col1 row2-col2 row2-col3\nrow3-col1 row3-col2 row3-col3\n")))
(is (= cells (split-cells "row1-col1 row1-col2 row1-col3\nrow2-col1 row2-col2 row2-col3\nrow3-col1 row3-col2 row3-col3\n")))
(is (= cells (split-cells "row1-col1; row1-col2; row1-col3\nrow2-col1; row2-col2; row2-col3\nrow3-col1; row3-col2; row3-col3\n"))))
(testing "Check whether extra whitespace gets removed"
(is (= cells (split-cells "row1-col1 row1-col2 row1-col3\nrow2-col1 row2-col2 row2-col3\nrow3-col1; \trow3-col2 row3-col3\n"))))))
(deftest test-zip-item
(testing "Check whether items get correctly zipped"
(is (= (zip-item [:coll1 :coll2 :coll3] ["row1-col1", "row1-col2", "row1-col3"])
{:coll1 "row1-col1" :coll2 "row1-col2" :coll3 "row1-col3"}))))
(deftest test-parse-double
(testing "Check whether doubles get correctly parsed"
(is (= (parse-double "123") 123.0))
(is (= (parse-double "123.1234") 123.1234))
(is (= (parse-double "123.654321543") 123.654321543))
(is (= (parse-double "0.00000") 0.0))
(is (= (parse-double "-123") -123.0))
(is (= (parse-double "asdasd123adasd32") 123.0))))