Skip to content

Commit 741a23e

Browse files
committed
autogenerate module
1 parent bde7b0b commit 741a23e

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

backend/tests/utils/generic_utils.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from decimal import Decimal
77
from typing import Literal, Any
88
from jsonpath_ng.ext import parse
9+
from datetime import datetime, date
10+
from typing import Union, List
911

1012

1113
def load_json_data(filename: str):
@@ -91,3 +93,21 @@ def update_contained_resource_field(
9193
{field_to_update: update_value}
9294
)
9395
return json_data
96+
97+
def format_future_dates(dates: List[Union[date, datetime]], mode: str = "auto") -> List[str]:
98+
"""
99+
Accepts a list of date or datetime objects and returns them as strings:
100+
- datetime → ISO 8601 string with timezone if present
101+
- date → 'YYYY-MM-DD'
102+
"""
103+
formatted = []
104+
105+
for future_date in dates:
106+
if mode == "datetime":
107+
formatted.append(future_date.isoformat()) # full datetime with timezone
108+
elif mode == "date":
109+
formatted.append(future_date.strftime('%Y-%m-%d')) # just date
110+
else:
111+
raise TypeError(f"Unsupported type {type(future_date)}; expected date or datetime.")
112+
113+
return formatted

backend/tests/utils/values_for_tests.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
from dataclasses import dataclass
44
from decimal import Decimal
5+
from .generic_utils import format_future_dates
56
from datetime import datetime, timedelta
6-
import random
77

88

99
# Lists of data types for 'invalid data type' testing
@@ -294,15 +294,20 @@ class InvalidValues:
294294
"2000-02-30", # Invalid combination of month and day
295295
]
296296

297-
for_future_dates = [
298-
(datetime.now() + timedelta(days=random.randint(1, 30))).strftime("%Y-%m-%d")
299-
for i in range(3)
297+
now = datetime.now()
298+
sample_inputs = [
299+
now + timedelta(days=1),
300+
now + timedelta(days=365),
301+
now + timedelta(days=730)
300302
]
301303

304+
for_future_dates = format_future_dates(sample_inputs, mode = "date")
305+
302306
# Strings which are not in acceptable date time format
303307
for_date_time_string_formats_for_relaxed_timezone = [
304308
"", # Empty string
305309
"invalid", # Invalid format
310+
*format_future_dates(sample_inputs, mode = "datetime"),
306311
"20000101", # Date digits only (i.e. without hypens)
307312
"20000101000000", # Date and time digits only
308313
"200001010000000000", # Date, time and timezone digits only
@@ -394,4 +399,3 @@ class InvalidValues:
394399
]
395400

396401
invalid_dose_quantity = {"value": 2, "unit": "ml", "code": "258773002"}
397-

0 commit comments

Comments
 (0)