Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make to_ymd return a compliant ymd when no elapsed days #78

Merged
merged 4 commits into from
May 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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