Much progress

This commit is contained in:
Simon Brooke 2018-07-29 00:37:57 +01:00
parent 28e58ea03d
commit 387c15b8a1
6 changed files with 279 additions and 95 deletions

View file

@ -2,28 +2,6 @@
(:require [clojure.test :refer :all]
[adl-support.core :refer :all]))
(deftest query-string-to-map-tests
(testing "conversion of query strings to maps"
(let [expected {}
actual (query-string-to-map nil)]
(is (= expected actual) "Nil arg"))
(let [expected {}
actual (query-string-to-map "")]
(is (= expected actual) "Empty string arg"))
(let [expected {:id 1}
actual (query-string-to-map "id=1")]
(is (= expected actual) "One integer value"))
(let [expected {:name "simon"}
actual (query-string-to-map "name=simon")]
(is (= expected actual) "One string value."))
(let [expected {:name "simon" :id 1}
actual (query-string-to-map "id=1&name=simon")]
(is (= expected actual) "One string value, one integer. Order of pairs might be reversed, and that's OK"))
(let [expected {:address_id_expanded "AIRDS"}
actual (query-string-to-map "id=&address_id_expanded=AIRDS&sub-address=")]
(is (= expected actual) "Yeys with no values should not be included in the map"))
))
(deftest massage-params-tests
(testing "Massaging of params"
(let [expected {:id 67}
@ -49,3 +27,44 @@
:form-params {:id "67" :offset "0" :limit "50"}})]
(is (= expected actual) "Request with form params, params and form params differ"))
))
(deftest compose-exception-reason-tests
(testing "Compose exception reason"
(let [expected "java.lang.Exception: hello"
actual (compose-exception-reason
(Exception. "hello"))]
(is (= expected actual) "Exception with no cause"))
(let [expected "java.lang.Exception: Top-level exception\n\tcaused by: java.lang.Exception: cause"
actual (compose-exception-reason
(Exception.
"Top-level exception"
(Exception. "cause")))]
(is (= expected actual) "Exception with cause"))
(let [expected ""
actual (compose-exception-reason nil)]
(is (= expected actual) "Exception with no cause"))))
(deftest do-or-return-reason-tests
(testing "do-or-return-reason"
(let [expected {:result 1}
actual (do-or-return-reason (/ 1 1))]
(is (= expected actual) "No exception thrown"))
(let [expected {:error "java.lang.ArithmeticException: Divide by zero"}
actual (do-or-return-reason (/ 1 0))]
(is (= expected actual) "Exception thrown"))))
;; These work in REPL, but break in tests. Why?
;; (deftest "do-or-warn-tests"
;; (testing "do-or-warn"
;; (let [expected 1
;; actual (do-or-warn (/ 1 1))]
;; (is (= expected actual) "No exception thrown"))
;; (let [expected nil
;; actual (do-or-warn (/ 1 0))]
;; (is (= expected actual) "Exception thrown"))
;; (let [expected nil
;; actual (do-or-warn (/ 1 0) "hello")]
;; (is (= expected actual) "Exception thrown"))
;; ))