Skip to content

Commit

Permalink
Merge pull request #78 from senaite/ymd-compliant-when-empty
Browse files Browse the repository at this point in the history
Make `to_ymd` return a compliant ymd when no elapsed days
  • Loading branch information
ramonski authored May 19, 2023
2 parents 8145a4b + 007b338 commit 1b45355
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ Changelog
1.4.0 (unreleased)
------------------

- #76 Fix tracbeack on DateOfBirth update while validation fails for others
- #78 Make to_ymd return a compliant ymd when no elapsed days
- #77 Use system's default timezone for TZ-naive birth dates
- #76 Fix tracbeack on DateOfBirth update while validation fails for others
- #74 Migrate sample's DateOfBirth field to AgeDateOfBirthField
- #72 Move Patient ID to identifiers
- #70 Ensure Require MRN setting is honoured
Expand Down
5 changes: 4 additions & 1 deletion src/senaite/patient/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,10 @@ def to_ymd(period, default=_marker):
# Return in ymd format, with zeros omitted
ymd_values = map(str, ymd_values)
ymd = filter(lambda it: int(it[0]), zip(ymd_values, "ymd"))
return " ".join(map("".join, ymd))
ymd = " ".join(map("".join, ymd))

# return a compliant ymd when no elapsed days
return ymd or "0d"


def is_ymd(ymd):
Expand Down
24 changes: 20 additions & 4 deletions src/senaite/patient/tests/doctests/API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ We can convert a relativedelta to ymd format:
>>> api.to_ymd(period)
'6m 2d'

>>> period = relativedelta()
>>> api.to_ymd(period)
''

Or can transform an already existing ymd to its standard format:

>>> api.to_ymd("1y2m3d")
Expand All @@ -62,6 +58,11 @@ Returns a TypeError if the value is not of the expected type:

Returns a ValueError if the value has the rihgt type, but format is wrong:

>>> api.to_ymd("")
Traceback (most recent call last):
[...]
ValueError: Not a valid ymd: ''

>>> api.to_ymd("123")
Traceback (most recent call last):
[...]
Expand All @@ -72,6 +73,18 @@ Returns a ValueError if the value has the rihgt type, but format is wrong:
[...]
ValueError: Not a valid ymd: 'y123d'

And returns a ymd-compliant result when current date or no period is set:

>>> period = relativedelta()
>>> api.to_ymd(period)
'0d'

>>> api.to_ymd("0y")
'0d'

>>> api.to_ymd("0y0m0d")
'0d'

Function is even aware of monthly and yearly shifts:

>>> period = relativedelta(years=1235, months=23, days=10)
Expand Down Expand Up @@ -102,6 +115,9 @@ Returns true for ymd-like strings:
>>> api.is_ymd("0y0m0d")
True

>>> api.is_ymd("0d")
True

But returns false if the format or type is not valid:

>>> api.is_ymd("y3d")
Expand Down

0 comments on commit 1b45355

Please sign in to comment.