Skip to content
This repository has been archived by the owner on Jun 26, 2018. It is now read-only.

Commit

Permalink
Fix interaction of test atom and service atom
Browse files Browse the repository at this point in the history
  • Loading branch information
Relevance Pair committed Sep 10, 2014
1 parent 58f37ac commit 1d08f6b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Project related
my-new-payload.edn
data/
/datomic/
/resources/public/ratings/

# Java related
pom.xml
Expand Down
4 changes: 2 additions & 2 deletions src/cr_ocs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
(defn maybe-enable-http-upsert
[master-routes routes]
(if-let [new-verbs (when (config :http-upsert)
`{:post [:cr-ocs/append-api (fn [req#] (append-api req#))]})]
`{:post [:cr-ocs/append-api append-api]})]
(update-api-roots master-routes
(fn [root _]
(let [verbs-index (keep-indexed #(when (map? %2) %1) root)]
Expand All @@ -137,7 +137,7 @@
interceptor/attach-request-id
;; In the future, html-body should be json-body
bootstrap/html-body]
^:cr-ocs/api-root ["/api" {:get [:cr-ocs/show-routes `(fn [req#] (cr-ocs/show-routes req#))]}
^:cr-ocs/api-root ["/api" {:get [:cr-ocs/show-routes show-routes]}
^:interceptors [bootstrap/json-body
interceptor/json-error-ring-response]]]
descriptor (util/edn-resource (get config :initial-descriptor "sample_descriptor.edn"))
Expand Down
19 changes: 15 additions & 4 deletions test/cr_ocs/service_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
(deftest about-page-test
(is (.contains
(:body (helper/GET "/about"))
"Clojure 1.6"))
"Clojure 1.7"))
(is (=
(selected-headers helper/GET "/about" ["Content-Type"])
{"Content-Type" "text/html;charset=UTF-8"})))
Expand Down Expand Up @@ -85,6 +85,17 @@
[:response :payload])
"Another Hello World Route"))))

(deftest bash-descriptor-test
(let [serv-map (helper/refresh-service-map)
serv (helper/service-fn serv-map)
routes-atom (:routes-atom serv-map)
descriptor (:descriptor (meta service/routes))
pre-condition (response-for serv :get "/api/example/v2/hello")
_ (cr-ocs/bash-from-descriptor! routes-atom descriptor :example :v2)
post-condition (response-for serv :get "/api/example/v2/hello")]
(is (= (:status pre-condition) 404))
(is (= (:status post-condition) 200))))

;; TODO This needs to use a stateful service
(deftest bash-http-descriptor-test
(let [serv-map (helper/refresh-service-map)
Expand All @@ -94,12 +105,12 @@
(= (:body (response-for serv :get "/api?f=v2"))
""))
(let [ ;; Add V2
pre-modified-routes @(:routes-atom serv-map)
pre-modified-routes (:body (response-for serv :get "/api"))
post-response (response-for serv :post "/api" :body (slurp "config/sample_payload.edn") :headers {"Content-Type" "application/edn"})
post-modified-routes @(:routes-atom serv-map)
post-modified-routes (:body (response-for serv :get "/api"))
new-api-resp (response-for serv :get "/api/example/v2/hello")]
(is (= (:body post-response) "{:added [:v2]}"))
(is (< (count pre-modified-routes) (count post-modified-routes)))
(is (not= pre-modified-routes post-modified-routes))
(is (= (select-keys (:headers new-api-resp) ["Content-Type"])
{"Content-Type" "application/json;charset=UTF-8"}))
(is (= (select-keys (util/read-json (:body new-api-resp)) [:response :errors])
Expand Down
19 changes: 17 additions & 2 deletions test/cr_ocs/test_helper.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [io.pedestal.test :refer [response-for]]
[io.pedestal.http :as bootstrap]
[io.pedestal.log :as log]
[cr-ocs.interceptor :as interceptor]
[cr-ocs.service :as cserv]
[cr-ocs]
[cr-ocs.util :as util]
Expand All @@ -11,6 +12,18 @@

(def base-service-map cserv/service)

(defn make-master-routes
[routes-atom]
`["/" {:get cserv/health-check} ^:interceptors [interceptor/attach-received-time
interceptor/attach-request-id
;; In the future, html-body should be json-body
bootstrap/html-body
~(interceptor/bind-routes routes-atom)]
["/about" {:get cserv/clj-ver}]
^:cr-ocs/api-root ["/api" {:get cr-ocs/show-routes}
^:interceptors [bootstrap/json-body
interceptor/json-error-ring-response]]])

(defn test-with-fresh-db
[f]
(let [uri cdb/uri
Expand All @@ -24,8 +37,10 @@
([]
(refresh-service-map base-service-map))
([serv-map]
(let [routes-atom (cr-ocs/init-descriptor-routes!
:master-routes cserv/master-routes)]
(let [routes-atom (atom nil)
routes-atom (cr-ocs/init-descriptor-routes!
:master-routes (make-master-routes routes-atom)
:routes-atom routes-atom)]
(assoc serv-map
:io.pedestal.http/routes
;(if (config :enable-upsert) #(deref routes-atom) @routes-atom)
Expand Down

0 comments on commit 1d08f6b

Please sign in to comment.