Skip to content

Commit

Permalink
Add Record for Complex Type Ratio
Browse files Browse the repository at this point in the history
Needed in: #1051
  • Loading branch information
alexanderkiel committed Jun 24, 2024
1 parent 82022a6 commit 1c597c3
Show file tree
Hide file tree
Showing 24 changed files with 261 additions and 63 deletions.
2 changes: 1 addition & 1 deletion modules/admin-api/test/blaze/admin_api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@
[#fhir/Coding
{:system #fhir/uri"system-192253"
:code #fhir/code"code-192300"}]}
:subject (type/map->Reference {:reference (str "Patient/" pat-id)})}]))
:subject (type/reference {:reference (str "Patient/" pat-id)})}]))
(range 120)))
(mapv (range 100)))

Expand Down
16 changes: 8 additions & 8 deletions modules/db/test/blaze/db/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@
(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Patient :id "0"
:active true
:name [(type/map->HumanName {:family (apply str (repeat 1000 "a"))})]}]]]
:name [(type/human-name {:family (apply str (repeat 1000 "a"))})]}]]]

(testing "as first clause"
(given (pull-type-query node "Patient" [["family" (apply str (repeat 1000 "a"))]])
Expand All @@ -1419,7 +1419,7 @@
(with-system-data [{:blaze.db/keys [node]} config]
[[[:put {:fhir/type :fhir/Patient :id "0"
:active true
:name [(type/map->HumanName {:family name})]}]]]
:name [(type/human-name {:family name})]}]]]

(testing "as first clause"
(given (pull-type-query node "Patient" [["family" name]])
Expand Down Expand Up @@ -4563,7 +4563,7 @@

(defn- patient-w-identifier [i]
{:fhir/type :fhir/Patient :id (str i)
:identifier [(type/map->Identifier {:value (str i)})]})
:identifier [(type/identifier {:value (str i)})]})

(deftest type-query-identifier-non-matching-test
(st/unstrument)
Expand All @@ -4586,15 +4586,15 @@
[[[:put {:fhir/type :fhir/Patient :id "0"
:active true
:identifier
[(type/map->Identifier {:system system :value "0"})]}]
[(type/identifier {:system system :value "0"})]}]
[:put {:fhir/type :fhir/Patient :id "1"
:active true
:identifier
[(type/map->Identifier {:system system :value "0"})]}]
[(type/identifier {:system system :value "0"})]}]
[:put {:fhir/type :fhir/Patient :id "2"
:active true
:identifier
[(type/map->Identifier {:system system :value "0"})]}]]]
[(type/identifier {:system system :value "0"})]}]]]

(doseq [value (if system ["0" "foo|0"] ["0" "|0"])]
(given (pull-type-query node "Patient" [["identifier" value]])
Expand Down Expand Up @@ -6356,9 +6356,9 @@
(defn- observation-create-op [id]
[:create {:fhir/type :fhir/Observation :id (format "%05d" id)
:category
[(type/map->CodeableConcept
[(type/codeable-concept
{:coding
[(type/map->Coding
[(type/coding
{:system #fhir/uri"system-141902"
:code (type/code (format "%05d" id))})]})]}])

Expand Down
4 changes: 4 additions & 0 deletions modules/fhir-structure/src/blaze/fhir/spec/impl.clj
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
:fhir/Coding
:fhir/CodeableConcept
:fhir/Quantity
:fhir/Ratio
:fhir/Period
:fhir/Identifier
:fhir/HumanName
Expand Down Expand Up @@ -425,6 +426,7 @@
:fhir.json/Meta
:fhir.json/Attachment
:fhir.json/Quantity
:fhir.json/Ratio
:fhir.json/Period
:fhir.json/Identifier
:fhir.json/HumanName
Expand Down Expand Up @@ -549,6 +551,7 @@
:fhir.xml/Coding
:fhir.xml/CodeableConcept
:fhir.xml/Quantity
:fhir.xml/Ratio
:fhir.xml/Period
:fhir.xml/Identifier
:fhir.xml/HumanName
Expand Down Expand Up @@ -590,6 +593,7 @@
:fhir.cbor/Meta
:fhir.cbor/Attachment
:fhir.cbor/Quantity
:fhir.cbor/Ratio
:fhir.cbor/Period
:fhir.cbor/Identifier
:fhir.cbor/HumanName
Expand Down
5 changes: 5 additions & 0 deletions modules/fhir-structure/src/blaze/fhir/spec/type.clj
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,11 @@
:hash-num 40
:interned (and (nil? id) (p/-interned extension) (nil? value)))

(declare ratio)

(def-complex-type Ratio [^String id extension numerator denominator]
:hash-num 48)

(declare period)

(def-complex-type Period [^String id extension ^:primitive start ^:primitive end]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
:args (s/cat :x any?)
:ret boolean?)

(s/fdef system/date
:args (s/cat :year int? :month (s/? int?) :day (s/? int?))
:ret system/date?)

;; ---- System.DateTime -------------------------------------------------------

(s/fdef system/date-time?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@

#fhir/Quantity{} 56

#fhir/Ratio{} 48

#fhir/Period{} 48

#fhir/Identifier{} 64
Expand Down
10 changes: 9 additions & 1 deletion modules/fhir-structure/test/blaze/fhir/spec/generators.clj
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,15 @@

;; TODO: Range

;; TODO: Ratio
(defn ratio
[& {:keys [id extension numerator denominator]
:or {id (gen/return nil)
extension (extensions)
numerator (nilable (quantity))
denominator (nilable (quantity))}}]
(->> (gen/tuple id extension numerator denominator)
(to-map [:id :extension :numerator :denominator])
(gen/fmap type/ratio)))

;; TODO: RatioRange

Expand Down
28 changes: 28 additions & 0 deletions modules/fhir-structure/test/blaze/fhir/spec/type/system_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,34 @@
nil
#system/date-time"2020"))

(testing "date"
(testing "year"
(are [year date] (= date (system/date year))
1000 #system/date"1000"
2024 #system/date"2024"
9999 #system/date"9999")

(given-thrown (system/date -1)
:message := "Invalid value for Year (valid values 1 - 9999): -1"))

(testing "year-month"
(are [year month date] (= date (system/date year month))
1000 1 #system/date"1000-01"
2024 6 #system/date"2024-06"
9999 12 #system/date"9999-12")

(given-thrown (system/date 2024 0)
:message := "Invalid value for MonthOfYear (valid values 1 - 12): 0"))

(testing "year-month-day"
(are [year month day date] (= date (system/date year month day))
1000 1 1 #system/date"1000-01-01"
2024 6 15 #system/date"2024-06-15"
9999 12 31 #system/date"9999-12-31")

(given-thrown (system/date 2023 2 29)
:message := "Invalid date 'February 29' as '2023' is not a leap year")))

(testing "system equals"
(testing "same precision"
(testing "within date"
Expand Down
72 changes: 71 additions & 1 deletion modules/fhir-structure/test/blaze/fhir/spec/type_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2246,7 +2246,7 @@
#fhir/Quantity{:extension [#fhir/Extension{}]}
"4f5028ac"

#fhir/Quantity{:value 1M}
#fhir/Quantity{:value #fhir/decimal 1M}
"4adf97ab"

#fhir/Quantity{:comparator #fhir/code"comparator-153342"}
Expand All @@ -2271,10 +2271,80 @@
#fhir/Quantity{} "#fhir/Quantity{}"
#fhir/Quantity{:id "212329"} "#fhir/Quantity{:id \"212329\"}")))

(deftest ratio-test
(testing "type"
(is (= :fhir/Ratio (type/type #fhir/Ratio{}))))

(testing "interned"
(are [x y] (not-interned? x y)
#fhir/Ratio{:id "foo"}
#fhir/Ratio{:id "foo"}

#fhir/Ratio{:extension [#fhir/Extension{:url "foo" :value "bar"}]}
#fhir/Ratio{:extension [#fhir/Extension{:url "foo" :value "bar"}]}

#fhir/Ratio{:numerator #fhir/Quantity{:value #fhir/decimal 1M}}
#fhir/Ratio{:numerator #fhir/Quantity{:value #fhir/decimal 1M}}

#fhir/Ratio{:denominator #fhir/Quantity{:value #fhir/decimal 1M}}
#fhir/Ratio{:denominator #fhir/Quantity{:value #fhir/decimal 1M}})

(are [x y] (interned? x y)
#fhir/Ratio{:extension [#fhir/Extension{:url "foo" :value #fhir/code"bar"}]}
#fhir/Ratio{:extension [#fhir/Extension{:url "foo" :value #fhir/code"bar"}]}

#fhir/Ratio{:numerator #fhir/Quantity{:code #fhir/code"foo"}}
#fhir/Ratio{:numerator #fhir/Quantity{:code #fhir/code"foo"}}

#fhir/Ratio{:denominator #fhir/Quantity{:code #fhir/code"foo"}}
#fhir/Ratio{:denominator #fhir/Quantity{:code #fhir/code"foo"}}))

(testing "hash-into"
(are [x hex] (= hex (murmur3 x))
#fhir/Ratio{}
"d271c07f"

#fhir/Ratio{:id "id-130710"}
"e3c0ee3c"

#fhir/Ratio{:extension [#fhir/Extension{}]}
"23473d24"

#fhir/Ratio{:numerator #fhir/Quantity{:value #fhir/decimal 1M}}
"fbf83a67"

#fhir/Ratio{:denominator #fhir/Quantity{:value #fhir/decimal 1M}}
"7f2075fb"))

(testing "references"
(are [x refs] (= refs (type/references x))
#fhir/Ratio{}
[]))

(testing "print"
(are [v s] (= s (pr-str v))
#fhir/Ratio{} "#fhir/Ratio{}"
#fhir/Ratio{:id "212329"} "#fhir/Ratio{:id \"212329\"}")))

(deftest period-test
(testing "type"
(is (= :fhir/Period (type/type #fhir/Period{}))))

(testing "interned"
(are [x y] (not-interned? x y)
#fhir/Period{:id "foo"}
#fhir/Period{:id "foo"}

#fhir/Period{:extension [#fhir/Extension{:url "foo" :value "bar"}]}
#fhir/Period{:extension [#fhir/Extension{:url "foo" :value "bar"}]}

#fhir/Period{:start #fhir/dateTime"2020"}
#fhir/Period{:start #fhir/dateTime"2020"})

(are [x y] (interned? x y)
#fhir/Period{:extension [#fhir/Extension{:url "foo" :value #fhir/code"bar"}]}
#fhir/Period{:extension [#fhir/Extension{:url "foo" :value #fhir/code"bar"}]}))

(testing "hash-into"
(are [x hex] (= hex (murmur3 x))
#fhir/Period{}
Expand Down
81 changes: 79 additions & 2 deletions modules/fhir-structure/test/blaze/fhir/spec_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3532,7 +3532,7 @@
#fhir/Quantity{}

{:value 1M}
#fhir/Quantity{:value 1M}
#fhir/Quantity{:value #fhir/decimal 1M}

{:value "1"}
::s2/invalid)))
Expand All @@ -3552,7 +3552,7 @@
#fhir/Quantity{:extension [#fhir/Extension{} #fhir/Extension{}]}
{:extension [{} {}]}

#fhir/Quantity{:value 1M}
#fhir/Quantity{:value #fhir/decimal 1M}
{:value 1}

#fhir/Quantity{:comparator #fhir/code"code-153342"}
Expand All @@ -3567,6 +3567,83 @@
#fhir/Quantity{:code #fhir/code"code-153427"}
{:code "code-153427"}))))

(deftest ratio-test
(testing "FHIR spec"
(testing "valid"
(satisfies-prop 1000
(prop/for-all [x (fg/ratio)]
(s2/valid? :fhir/Ratio x))))

(testing "invalid"
(are [x] (not (s2/valid? :fhir/Ratio x))
#fhir/Ratio{:numerator "1"})))

(testing "transforming"
(testing "JSON"
(satisfies-prop 1000
(prop/for-all [x (fg/ratio)]
(= (->> x
fhir-spec/unform-json
fhir-spec/parse-json
(s2/conform :fhir.json/Ratio))
x))))

(testing "XML"
(satisfies-prop 1000
(prop/for-all [x (fg/ratio)]
(= (->> x
fhir-spec/unform-xml
(s2/conform :fhir.xml/Ratio))
x))))

(testing "CBOR"
(satisfies-prop 1000
(prop/for-all [x (fg/ratio)]
(= (->> x
fhir-spec/unform-cbor
fhir-spec/parse-cbor
(s2/conform :fhir.cbor/Ratio))
x)))))

(testing "conforming"
(testing "JSON"
(are [json fhir] (= fhir (s2/conform :fhir.json/Ratio json))
{}
#fhir/Ratio{}

{:id "id-151304"}
#fhir/Ratio{:id "id-151304"}

{:extension [{}]}
#fhir/Ratio{:extension [#fhir/Extension{}]}

{:numerator {:value 1M}}
#fhir/Ratio{:numerator #fhir/Quantity{:value #fhir/decimal 1M}}

{:numerator "foo"}
::s2/invalid)))

(testing "unforming"
(testing "JSON"
(are [fhir json] (= json (fhir-spec/parse-json (fhir-spec/unform-json fhir)))
#fhir/Ratio{}
{}

#fhir/Ratio{:id "id-134428"}
{:id "id-134428"}

#fhir/Ratio{:extension [#fhir/Extension{}]}
{:extension [{}]}

#fhir/Ratio{:extension [#fhir/Extension{} #fhir/Extension{}]}
{:extension [{} {}]}

#fhir/Ratio{:numerator #fhir/Quantity{:value #fhir/decimal 1M}}
{:numerator {:value 1}}

#fhir/Ratio{:denominator #fhir/Quantity{:value #fhir/decimal 1M}}
{:denominator {:value 1}}))))

(deftest period-test
(testing "FHIR spec"
(testing "valid"
Expand Down
2 changes: 1 addition & 1 deletion modules/interaction/src/blaze/interaction/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
(let [meta (into {} (keep (fn [[k v]] (when (and v (not (#{:versionId :lastUpdated} k))) [k v]))) meta)]
(if (empty? meta)
(dissoc resource :meta)
(assoc resource :meta (type/map->Meta meta)))))
(assoc resource :meta (type/meta meta)))))

(defn keep?
"Determines whether `tx-op` is a keep operator."
Expand Down
Loading

0 comments on commit 1c597c3

Please sign in to comment.