-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.clj
66 lines (55 loc) · 1.82 KB
/
test_helper.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(ns minimal-api-lein.test-helper
(:require [clj-http.client :as client]
[clojure.java.io :as io]
[integrant.core :as ig]))
(defn test-config []
(-> (io/resource "config.edn")
slurp
ig/read-string
(assoc-in [:minimal-api-lein.server/server :options :port] 3001)
(doto ig/load-namespaces)))
(defn test-system []
(ig/prep (test-config)))
(def test-url-prefix "http://localhost:3001")
;;; macros for testing context
(defmacro with-system [[bound-sym binding-expr] & body]
`(let [~bound-sym (ig/init ~binding-expr)]
(try
~@body
(finally (ig/halt! ~bound-sym)))))
(defmacro with-db-data [[system db-data-map] & body]
`(let [db# (:minimal-api-lein.boundary.db.core/db ~system)
old-val# @(:data db#)]
(try
(reset! (:data db#) ~db-data-map)
~@body
(finally (reset! (:data db#) old-val#)))))
;;; HTTP client
(defn http-get [path]
(client/get (str test-url-prefix path)
{:accept :json
:as :json
:coerce :always
:throw-exceptions? false}))
(defn http-post [path body]
(client/post (str test-url-prefix path)
{:form-params body
:content-type :json
:accept :json
:as :json
:coerce :always
:throw-exceptions? false}))
(defn http-put [path body]
(client/put (str test-url-prefix path)
{:form-params body
:content-type :json
:accept :json
:as :json
:coerce :always
:throw-exceptions? false}))
(defn http-delete [path]
(client/delete (str test-url-prefix path)
{:accept :json
:as :json
:coerce :always
:throw-exceptions? false}))