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 6cfe6c8
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 4 deletions.
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

0 comments on commit 6cfe6c8

Please sign in to comment.