Skip to content

Commit

Permalink
Move History DB into Util
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkiel committed Aug 8, 2019
1 parent dc9aca9 commit 1046317
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 30 deletions.
11 changes: 11 additions & 0 deletions src/blaze/handler/fhir/history/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
[clojure.spec.alpha :as s]
[datomic.api :as d]
[datomic-spec.core :as ds]
[manifold.deferred :as md]
[reitit.core :as reitit])
(:import
[java.time Instant]
Expand Down Expand Up @@ -115,6 +116,16 @@
(assoc :resource (pull/pull-resource* db type resource)))))


(s/fdef db
:args (s/cat :conn ::ds/conn :t (s/nilable nat-int?))
:ret md/deferred?)

(defn db [conn t]
(if t
(-> (d/sync conn t) (md/chain #(d/as-of % t)))
(d/db conn)))


(s/fdef tx-db
:args (s/cat :db ::ds/db :since-t (s/nilable nat-int?) :page-t (s/nilable nat-int?))
:ret ::ds/db)
Expand Down
8 changes: 1 addition & 7 deletions src/blaze/handler/fhir/history_instance.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,10 @@
:fhir/issue "not-found"})))


(defn- db [conn t]
(if t
(-> (d/sync conn t) (md/chain #(d/as-of % t)))
(d/db conn)))


(defn- handler-intern [conn]
(fn [{::reitit/keys [router match] :keys [query-params]
{:keys [type id]} :path-params}]
(-> (db conn (fhir-util/t query-params))
(-> (history-util/db conn (fhir-util/t query-params))
(md/chain' #(handle router match query-params % type id)))))


Expand Down
8 changes: 1 addition & 7 deletions src/blaze/handler/fhir/history_system.clj
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,9 @@
(build-response router match query-params db since-t transactions)))


(defn- db [conn t]
(if t
(-> (d/sync conn t) (md/chain #(d/as-of % t)))
(d/db conn)))


(defn- handler-intern [conn]
(fn [{::reitit/keys [router match] :keys [query-params]}]
(-> (db conn (fhir-util/t query-params))
(-> (history-util/db conn (fhir-util/t query-params))
(md/chain' #(handle router match query-params %)))))


Expand Down
8 changes: 1 addition & 7 deletions src/blaze/handler/fhir/history_type.clj
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,10 @@
(build-response router match query-params db since-t type transactions)))


(defn- db [conn t]
(if t
(-> (d/sync conn t) (md/chain #(d/as-of % t)))
(d/db conn)))


(defn- handler-intern [conn]
(fn [{::reitit/keys [router match] :keys [query-params]
{:keys [type]} :path-params}]
(-> (db conn (fhir-util/t query-params))
(-> (history-util/db conn (fhir-util/t query-params))
(md/chain' #(handle router match query-params % type)))))


Expand Down
12 changes: 12 additions & 0 deletions test/blaze/handler/fhir/history/test_util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@
#{`util/since-t}}))


(defn stub-db [conn t-spec db]
(st/instrument
[`util/db]
{:spec
{`util/db
(s/fspec
:args (s/cat :conn #{conn} :t t-spec)
:ret #{db})}
:stub
#{`util/db}}))


(defn stub-tx-db [db since-t-spec page-t-spec tx-db]
(st/instrument
[`util/tx-db]
Expand Down
8 changes: 5 additions & 3 deletions test/blaze/handler/fhir/history_instance_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@

(deftest handler-test-0
(testing "Returns Not Found on Non-Existing Resource"
(datomic-test-util/stub-db ::conn ::db)
(fhir-test-util/stub-t ::query-params nil?)
(history-test-util/stub-db ::conn nil? ::db)
(datomic-test-util/stub-resource ::db #{"Patient"} #{"0"} nil?)

(let [{:keys [status body]}
@((handler ::conn)
{:path-params {:type "Patient" :id "0"}})]
{:query-params ::query-params
:path-params {:type "Patient" :id "0"}})]

(is (= 404 status))

Expand All @@ -55,7 +57,7 @@
(testing "Returns History with one Patient"
(let [patient {:instance/version ::foo}]
(fhir-test-util/stub-t ::query-params nil?)
(datomic-test-util/stub-db ::conn ::db)
(history-test-util/stub-db ::conn nil? ::db)
(datomic-test-util/stub-resource ::db #{"Patient"} #{"0"} #{{:db/id 0}})
(history-test-util/stub-page-t ::query-params nil?)
(history-test-util/stub-since-t ::db ::query-params nil?)
Expand Down
10 changes: 5 additions & 5 deletions test/blaze/handler/fhir/history_system_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
(deftest handler-test-0
(testing "Returns empty History"
(fhir-test-util/stub-t ::query-params nil?)
(datomic-test-util/stub-db ::conn ::db)
(history-test-util/stub-db ::conn nil? ::db)
(history-test-util/stub-page-t ::query-params nil?)
(history-test-util/stub-since-t ::db ::query-params nil?)
(history-test-util/stub-tx-db ::db nil? nil? ::db)
Expand Down Expand Up @@ -68,7 +68,7 @@
(testing "Returns History with one Patient"
(let [tx {:db/id ::tx-eid}]
(fhir-test-util/stub-t ::query-params nil?)
(datomic-test-util/stub-db ::conn ::db)
(history-test-util/stub-db ::conn nil? ::db)
(history-test-util/stub-page-t ::query-params nil?)
(history-test-util/stub-since-t ::db ::query-params nil?)
(history-test-util/stub-tx-db ::db nil? nil? ::db)
Expand Down Expand Up @@ -115,7 +115,7 @@
(testing "Returns History with two Patients in one Transaction"
(let [tx {:db/id ::tx-eid}]
(fhir-test-util/stub-t ::query-params nil?)
(datomic-test-util/stub-db ::conn ::db)
(history-test-util/stub-db ::conn nil? ::db)
(history-test-util/stub-page-t ::query-params nil?)
(history-test-util/stub-since-t ::db ::query-params nil?)
(history-test-util/stub-tx-db ::db nil? nil? ::db)
Expand Down Expand Up @@ -167,7 +167,7 @@
(let [tx-1 {:db/id ::tx-1-eid}
tx-2 {:db/id ::tx-2-eid}]
(fhir-test-util/stub-t ::query-params nil?)
(datomic-test-util/stub-db ::conn ::db)
(history-test-util/stub-db ::conn nil? ::db)
(history-test-util/stub-page-t ::query-params nil?)
(history-test-util/stub-since-t ::db ::query-params nil?)
(history-test-util/stub-tx-db ::db nil? nil? ::db)
Expand Down Expand Up @@ -221,7 +221,7 @@
(testing "Returns History with next Link"
(let [tx {:db/id ::tx-eid}]
(fhir-test-util/stub-t ::query-params nil?)
(datomic-test-util/stub-db ::conn ::db)
(history-test-util/stub-db ::conn nil? ::db)
(history-test-util/stub-page-t ::query-params nil?)
(history-test-util/stub-since-t ::db ::query-params nil?)
(history-test-util/stub-tx-db ::db nil? nil? ::db)
Expand Down
2 changes: 1 addition & 1 deletion test/blaze/handler/fhir/history_type_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
(testing "Returns History with one Patient"
(let [tx {:db/id ::tx-eid}]
(fhir-test-util/stub-t ::query-params nil?)
(datomic-test-util/stub-db ::conn ::db)
(history-test-util/stub-db ::conn nil? ::db)
(history-test-util/stub-page-t ::query-params nil?)
(history-test-util/stub-since-t ::db ::query-params nil?)
(history-test-util/stub-tx-db ::db nil? nil? ::db)
Expand Down

0 comments on commit 1046317

Please sign in to comment.