Skip to content

Commit

Permalink
Use Integrant to Build Systems under Test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkiel committed Jul 31, 2021
1 parent 02b360e commit f5d3185
Show file tree
Hide file tree
Showing 106 changed files with 6,341 additions and 6,408 deletions.
28 changes: 26 additions & 2 deletions modules/anomaly/src/blaze/anomaly.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
(ns blaze.anomaly
(:refer-clojure :exclude [map])
(:refer-clojure :exclude [map map-indexed])
(:require
[cognitect.anomalies :as anom]))
[cognitect.anomalies :as anom])
(:import
[java.util.concurrent CompletableFuture]))


(defn anomaly? [x]
(some? (::anom/category x)))


(defn incorrect [msg & kvs]
(apply assoc {::anom/category ::anom/incorrect ::anom/message msg} kvs))


(defn unsupported [msg & kvs]
(apply assoc {::anom/category ::anom/unsupported ::anom/message msg} kvs))


(defn ex-anom
Expand All @@ -11,6 +25,12 @@
(ex-info (::anom/message anom "") anom))


(defn completion-stage [x]
(if (anomaly? x)
(CompletableFuture/failedFuture (ex-anom x))
(CompletableFuture/completedFuture x)))


(defn throw-anom
"Throws an ExceptionInfo build with `ex-anom`."
[category message & {:as kvs}]
Expand Down Expand Up @@ -68,3 +88,7 @@

(defn exceptionally [x f]
(if (::anom/category x) (f x) x))


(defn map-indexed [f]
(comp (clojure.core/map-indexed f) (halt-when anomaly?)))
2 changes: 2 additions & 0 deletions modules/cql/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{:lint-as
{blaze.anomaly/when-ok clojure.core/let
blaze.db.api-stub/with-system-data clojure.core/with-open
blaze.test-util/with-system clojure.core/with-open
blaze.elm.compiler.macros/defunop clojure.core/defn
blaze.elm.compiler.macros/defbinop clojure.core/defn
blaze.elm.compiler.macros/defternop clojure.core/defn
Expand Down
12 changes: 6 additions & 6 deletions modules/cql/deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
{blaze/db-stub
{:local/root "../db-stub"}

blaze/test-util
{:local/root "../test-util"}

lambdaisland/kaocha
{:mvn/version "1.0.861"}

org.clojars.akiel/iota
{:mvn/version "0.1"}

org.clojure/data.xml
{:mvn/version "0.2.0-alpha6"}

Expand All @@ -57,12 +57,12 @@
{blaze/db-stub
{:local/root "../db-stub"}

blaze/test-util
{:local/root "../test-util"}

cloverage/cloverage
{:mvn/version "1.2.2"}

org.clojars.akiel/iota
{:mvn/version "0.1"}

org.clojure/data.xml
{:mvn/version "0.2.0-alpha6"}

Expand Down
126 changes: 66 additions & 60 deletions modules/cql/test/blaze/elm/compiler/external_data_test.clj
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
(ns blaze.elm.compiler.external-data-test
(:require
[blaze.db.api :as d]
[blaze.db.api-stub :refer [mem-node-with]]
[blaze.db.api-stub :refer [mem-node-system with-system-data]]
[blaze.elm.compiler :as c]
[blaze.elm.compiler.core :as core]
[blaze.elm.compiler.external-data]
[blaze.elm.compiler.test-util :as tu]
[blaze.elm.literal :as elm]
[blaze.fhir.spec :as fhir-spec]
[blaze.fhir.spec.type]
[blaze.test-util :refer [with-system]]
[clojure.spec.test.alpha :as st]
[clojure.test :as test :refer [deftest is testing]]
[cognitect.anomalies :as anom]
Expand Down Expand Up @@ -59,8 +60,9 @@
(deftest compile-retrieve-test
(testing "Patient context"
(testing "Patient"
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Patient :id "0"}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]]]

(let [context
{:node node
:eval-context "Patient"
Expand All @@ -74,11 +76,12 @@
[0 :id] := "0"))))

(testing "Observation"
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "1"
:subject
#fhir/Reference{:reference "Patient/0"}}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "1"
:subject
#fhir/Reference{:reference "Patient/0"}}]]]

(let [context
{:node node
:eval-context "Patient"
Expand All @@ -92,20 +95,21 @@
[0 :id] := "1")))

(testing "with one code"
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]
[:put {:fhir/type :fhir/Observation :id "1"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-192253"
:code #fhir/code"code-192300"}]}
:subject
#fhir/Reference{:reference "Patient/0"}}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]
[:put {:fhir/type :fhir/Observation :id "1"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-192253"
:code #fhir/code"code-192300"}]}
:subject
#fhir/Reference{:reference "Patient/0"}}]]]

(let [context
{:node node
:eval-context "Patient"
Expand All @@ -128,29 +132,30 @@
[0 :id] := "1"))))

(testing "with two codes"
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]
[:put {:fhir/type :fhir/Observation :id "1"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-192253"
:code #fhir/code"code-192300"}]}
:subject
#fhir/Reference{:reference "Patient/0"}}]
[:put {:fhir/type :fhir/Observation :id "2"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-192253"
:code #fhir/code"code-140541"}]}
:subject
#fhir/Reference{:reference "Patient/0"}}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]
[:put {:fhir/type :fhir/Observation :id "1"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-192253"
:code #fhir/code"code-192300"}]}
:subject
#fhir/Reference{:reference "Patient/0"}}]
[:put {:fhir/type :fhir/Observation :id "2"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-192253"
:code #fhir/code"code-140541"}]}
:subject
#fhir/Reference{:reference "Patient/0"}}]]]

(let [context
{:node node
:eval-context "Patient"
Expand All @@ -177,11 +182,12 @@

(testing "Specimen context"
(testing "Patient"
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Specimen :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Specimen :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]]]

(let [context
{:node node
:eval-context "Specimen"
Expand All @@ -196,14 +202,15 @@

(testing "Unfiltered context"
(testing "Medication"
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Medication :id "0"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-225806"
:code #fhir/code"code-225809"}]}}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Medication :id "0"
:code
#fhir/CodeableConcept
{:coding
[#fhir/Coding
{:system #fhir/uri"system-225806"
:code #fhir/code"code-225809"}]}}]]]

(let [context
{:node node
:eval-context "Unfiltered"
Expand All @@ -226,7 +233,7 @@

(testing "with related context"
(testing "with pre-compiled database query"
(with-open [node (mem-node-with [])]
(with-system [{:blaze.db/keys [node]} mem-node-system]
(let [library {:codeSystems
{:def [{:name "sys-def-174848" :id "system-174915"}]}
:statements
Expand All @@ -244,4 +251,3 @@
(testing "with unsupported type namespace"
(let [elm {:type "Retrieve" :dataType "{foo}Bar"}]
(is (thrown-anom? ::anom/unsupported (c/compile {} elm))))))

11 changes: 6 additions & 5 deletions modules/cql/test/blaze/elm/compiler/library_test.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
(ns blaze.elm.compiler.library-test
(:require
[blaze.cql-translator :as t]
[blaze.db.api-stub :refer [mem-node]]
[blaze.db.api-stub :refer [mem-node-system]]
[blaze.elm.compiler.library :as library]
[blaze.elm.compiler.library-spec]
[blaze.test-util :refer [with-system]]
[clojure.spec.test.alpha :as st]
[clojure.test :as test :refer [deftest testing]]
[cognitect.anomalies :as anom]
Expand All @@ -25,13 +26,13 @@
(deftest compile-library-test
(testing "empty library"
(let [library (t/translate "library Test")]
(with-open [node (mem-node)]
(with-system [{:blaze.db/keys [node]} mem-node-system]
(given (library/compile-library node library {})
:life/compiled-expression-defs := {}))))

(testing "one static expression"
(let [library (t/translate "library Test define Foo: true")]
(with-open [node (mem-node)]
(with-system [{:blaze.db/keys [node]} mem-node-system]
(given (library/compile-library node library {})
:life/compiled-expression-defs := {"Foo" true}))))

Expand All @@ -40,15 +41,15 @@
using FHIR version '4.0.0'
context Patient
define Gender: Patient.gender")]
(with-open [node (mem-node)]
(with-system [{:blaze.db/keys [node]} mem-node-system]
(given (library/compile-library node library {})
[:life/compiled-expression-defs "Gender" :key] := :gender
[:life/compiled-expression-defs "Gender" :source :name] := "Patient"))))

(testing "one static expression"
(let [library (t/translate "library Test
define Error: singleton from {1, 2}")]
(with-open [node (mem-node)]
(with-system [{:blaze.db/keys [node]} mem-node-system]
(given (library/compile-library node library {})
::anom/category := ::anom/conflict
::anom/message := "More than one element in `SingletonFrom` expression.")))))
28 changes: 16 additions & 12 deletions modules/cql/test/blaze/elm/compiler/queries_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns blaze.elm.compiler.queries-test
(:require
[blaze.db.api :as d]
[blaze.db.api-stub :refer [mem-node-with]]
[blaze.db.api-stub :refer [mem-node-system with-system-data]]
[blaze.elm.code :as code]
[blaze.elm.code-spec]
[blaze.elm.compiler :as c]
Expand Down Expand Up @@ -113,7 +113,9 @@
(is (not (instance? IPersistentCollection res))))))

(testing "Retrieve queries"
(with-open [node (mem-node-with [[[:put {:fhir/type :fhir/Patient :id "0"}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]]]

(let [db (d/db node)
retrieve {:type "Retrieve" :dataType "{http://hl7.org/fhir}Patient"}
where {:type "Equal"
Expand Down Expand Up @@ -171,11 +173,12 @@
;; 10.12. With
(deftest compile-with-clause-test
(testing "Equiv With with two Observations comparing there subjects."
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]]]

(let [elm {:alias "O1"
:type "WithEquiv"
:expression
Expand Down Expand Up @@ -204,11 +207,12 @@
(is (= [lhs-entity] (into [] xform [lhs-entity]))))))

(testing "Equiv With with one Patient and one Observation comparing the patient with the operation subject."
(with-open [node (mem-node-with
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]]])]
(with-system-data [{:blaze.db/keys [node]} mem-node-system]
[[[:put {:fhir/type :fhir/Patient :id "0"}]
[:put {:fhir/type :fhir/Observation :id "0"
:subject
#fhir/Reference{:reference "Patient/0"}}]]]

(let [elm {:alias "O"
:type "WithEquiv"
:expression
Expand Down
1 change: 1 addition & 0 deletions modules/db-resource-store-cassandra/.clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{:lint-as
{blaze.anomaly/when-ok clojure.core/let
blaze.test-util/with-system clojure.core/with-open
prometheus.alpha/defhistogram clojure.core/def}

:linters
Expand Down
Loading

0 comments on commit f5d3185

Please sign in to comment.