From 6cfe6c8d7fbfbda99c622eb57afc31462edf3cb2 Mon Sep 17 00:00:00 2001 From: Alexander Kiel Date: Mon, 24 Jun 2024 07:01:13 +0200 Subject: [PATCH] Add Record for Complex Type Ratio Needed in: #1051 --- .../src/blaze/fhir/spec/impl.clj | 4 + .../src/blaze/fhir/spec/type.clj | 5 ++ .../src/blaze/fhir/spec/type/system_spec.clj | 4 + .../blaze/fhir/spec/type_test_mem.clj | 2 + .../test/blaze/fhir/spec/generators.clj | 10 ++- .../test/blaze/fhir/spec/type/system_test.clj | 28 +++++++ .../test/blaze/fhir/spec/type_test.clj | 72 ++++++++++++++++- .../test/blaze/fhir/spec_test.clj | 81 ++++++++++++++++++- 8 files changed, 202 insertions(+), 4 deletions(-) diff --git a/modules/fhir-structure/src/blaze/fhir/spec/impl.clj b/modules/fhir-structure/src/blaze/fhir/spec/impl.clj index 0fd7010ce..6d54377d5 100644 --- a/modules/fhir-structure/src/blaze/fhir/spec/impl.clj +++ b/modules/fhir-structure/src/blaze/fhir/spec/impl.clj @@ -311,6 +311,7 @@ :fhir/Coding :fhir/CodeableConcept :fhir/Quantity + :fhir/Ratio :fhir/Period :fhir/Identifier :fhir/HumanName @@ -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 @@ -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 @@ -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 diff --git a/modules/fhir-structure/src/blaze/fhir/spec/type.clj b/modules/fhir-structure/src/blaze/fhir/spec/type.clj index 77de41e01..0e94f40ed 100644 --- a/modules/fhir-structure/src/blaze/fhir/spec/type.clj +++ b/modules/fhir-structure/src/blaze/fhir/spec/type.clj @@ -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] diff --git a/modules/fhir-structure/src/blaze/fhir/spec/type/system_spec.clj b/modules/fhir-structure/src/blaze/fhir/spec/type/system_spec.clj index 2e9b3eeff..5284df41f 100644 --- a/modules/fhir-structure/src/blaze/fhir/spec/type/system_spec.clj +++ b/modules/fhir-structure/src/blaze/fhir/spec/type/system_spec.clj @@ -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? diff --git a/modules/fhir-structure/test-perf/blaze/fhir/spec/type_test_mem.clj b/modules/fhir-structure/test-perf/blaze/fhir/spec/type_test_mem.clj index cd94ee05b..dc9dce381 100644 --- a/modules/fhir-structure/test-perf/blaze/fhir/spec/type_test_mem.clj +++ b/modules/fhir-structure/test-perf/blaze/fhir/spec/type_test_mem.clj @@ -83,6 +83,8 @@ #fhir/Quantity{} 56 + #fhir/Ratio{} 48 + #fhir/Period{} 48 #fhir/Identifier{} 64 diff --git a/modules/fhir-structure/test/blaze/fhir/spec/generators.clj b/modules/fhir-structure/test/blaze/fhir/spec/generators.clj index 15ebeaf94..92e4e7cfd 100644 --- a/modules/fhir-structure/test/blaze/fhir/spec/generators.clj +++ b/modules/fhir-structure/test/blaze/fhir/spec/generators.clj @@ -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 diff --git a/modules/fhir-structure/test/blaze/fhir/spec/type/system_test.clj b/modules/fhir-structure/test/blaze/fhir/spec/type/system_test.clj index 72f9a50ee..40f0353c5 100644 --- a/modules/fhir-structure/test/blaze/fhir/spec/type/system_test.clj +++ b/modules/fhir-structure/test/blaze/fhir/spec/type/system_test.clj @@ -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" diff --git a/modules/fhir-structure/test/blaze/fhir/spec/type_test.clj b/modules/fhir-structure/test/blaze/fhir/spec/type_test.clj index 0039fe286..56dd2b435 100644 --- a/modules/fhir-structure/test/blaze/fhir/spec/type_test.clj +++ b/modules/fhir-structure/test/blaze/fhir/spec/type_test.clj @@ -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"} @@ -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{} diff --git a/modules/fhir-structure/test/blaze/fhir/spec_test.clj b/modules/fhir-structure/test/blaze/fhir/spec_test.clj index a4083d827..3e73d10e6 100644 --- a/modules/fhir-structure/test/blaze/fhir/spec_test.clj +++ b/modules/fhir-structure/test/blaze/fhir/spec_test.clj @@ -3532,7 +3532,7 @@ #fhir/Quantity{} {:value 1M} - #fhir/Quantity{:value 1M} + #fhir/Quantity{:value #fhir/decimal 1M} {:value "1"} ::s2/invalid))) @@ -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"} @@ -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"