Skip to content

Commit

Permalink
✨ Resolved issue #96 as FHIR compatible datetime representer has been…
Browse files Browse the repository at this point in the history
… created.

* tests coverages are added.
* changes log updated.
  • Loading branch information
nazrulworld committed Apr 2, 2022
1 parent a6e4763 commit 8f47c06
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ History
6.2.2 (unreleased)
------------------

- Nothing changed yet.
- Issue `#96 https://github.com/nazrulworld/fhir.resources/issues/96`_, fixes datetime's ISO format representation for YAML. [nazrulworld]


6.2.1 (2022-01-14)
Expand Down
7 changes: 7 additions & 0 deletions fhir/resources/utils/yaml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# _*_ coding: utf-8 _*_
import datetime
from collections import OrderedDict
from decimal import Decimal

Expand All @@ -25,11 +26,17 @@ def represent_decimal(self, data):
data = float(data)
return self.represent_float(data)

def represent_datetime(self, data):
"""Issue #96, we are using T separator for ISO format"""
value = data.isoformat(sep="T")
return self.represent_scalar("tag:yaml.org,2002:timestamp", value)


BaseRepresenter.add_representer(Decimal, Representer.represent_decimal)
# important! we don't want explicit tag of OrderedDict
# (tag:yaml.org,2002:python/object/apply:collections.OrderedDict)
BaseRepresenter.add_representer(OrderedDict, SafeRepresenter.represent_dict)
BaseRepresenter.add_representer(datetime.datetime, Representer.represent_datetime)


def yaml_loads(stream, loader=None):
Expand Down
20 changes: 19 additions & 1 deletion tests/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pydantic import ValidationError

from fhir.resources.patient import Patient
from fhir.resources.period import Period

__author__ = "Md Nazrul Islam<[email protected]>"

Expand All @@ -14,7 +15,7 @@ def test_issue_74():


def test_issue_64():
"""Allow empty string """
"""Allow empty string"""
try:
Patient(
active=True,
Expand Down Expand Up @@ -57,3 +58,20 @@ def test_issue_64():
# should not raise validation error
assert 1 == 2, "Code should not come here, we allow empty string"
String.configure_empty_str(allow=False)


def test_issue_96():
"""YAML datetime serialization"""
from datetime import datetime

pd = Period(
end=datetime(2022, 3, 29, 11, 48, 37, 482248),
start=datetime(2022, 3, 28, 23, 48, 37, 481725),
)
assert "2022-03-28T23:48:37.481725" in pd.yaml()
try:
"2022-03-28T23:48:37.481725" in Period.parse_raw(
pd.yaml(), content_type="text/yaml"
).json()
except ValidationError:
raise AssertionError("Code should not come here")

0 comments on commit 8f47c06

Please sign in to comment.