From 2a703cde7a4ed9ef72b35b849c00b152822d6381 Mon Sep 17 00:00:00 2001 From: Vlad Iftime Date: Sat, 4 Jan 2025 15:54:01 +0200 Subject: [PATCH 1/3] Fixed unit test gen to generate only if the tests do not already exists --- .gitignore | 1 + .../gen_unit_test_template.py | 42 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 14a680a..8a082ed 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ venv *venv* .tox/ dist/ +build/ diff --git a/development_utilities/gen_unit_test_template.py b/development_utilities/gen_unit_test_template.py index 93e2b03..4456254 100644 --- a/development_utilities/gen_unit_test_template.py +++ b/development_utilities/gen_unit_test_template.py @@ -1,5 +1,6 @@ import datetime import json +import os from enum import Enum import inspect import pprint @@ -17,6 +18,7 @@ import uuid import pydantic +from pydantic.types import AwareDatetime from s2python import frbc from s2python.common import Duration, PowerRange, NumberRange @@ -64,7 +66,7 @@ def get_list_arg(field_type): def is_enum(field_type): - return issubclass(field_type, Enum) + return inspect.isclass(field_type) and issubclass(field_type, Enum) def snake_case(camelcased: str) -> str: @@ -111,6 +113,17 @@ def generate_json_test_data_for_field(field_type: Type): value = bool(random.randint(0, 1)) elif field_type is float: value = random.random() * 9000.0 + elif field_type is AwareDatetime: + # Generate a timezone-aware datetime + value = datetime.datetime( + year=random.randint(2020, 2023), + month=random.randint(1, 12), + day=random.randint(1, 28), + hour=random.randint(0, 23), + minute=random.randint(0, 59), + second=random.randint(0, 59), + tzinfo=datetime.timezone(datetime.timedelta(hours=random.randint(-12, 14))), + ) elif field_type is datetime.datetime: value = datetime.datetime( year=random.randint(2020, 2023), @@ -167,13 +180,21 @@ def dump_test_data_as_constructor_field_for(test_data, field_type: Type) -> str: value = str(test_data) elif field_type is float: value = str(test_data) - elif field_type is datetime.datetime: + elif field_type is AwareDatetime or field_type is datetime.datetime: test_data: datetime.datetime offset: datetime.timedelta = test_data.tzinfo.utcoffset(None) - value = f"datetime(year={test_data.year}, month={test_data.month}, day={test_data.day}, hour={test_data.hour}, minute={test_data.minute}, second={test_data.second}, tzinfo=offset(offset=timedelta(seconds={offset.total_seconds()})))" + value = ( + f"datetime(" + f"year={test_data.year}, month={test_data.month}, day={test_data.day}, " + f"hour={test_data.hour}, minute={test_data.minute}, second={test_data.second}, " + f"tzinfo=offset(offset=timedelta(seconds={offset.total_seconds()})))" + ) elif field_type is uuid.UUID: value = f'uuid.UUID("{test_data}")' + elif type(field_type).__name__ == "_LiteralGenericAlias": + value = field_type.__args__[0] else: + breakpoint() raise RuntimeError( f"Please implement dump test data for field type {field_type}" ) @@ -217,11 +238,13 @@ def dump_test_data_as_json_field_for(test_data, field_type: Type): value = test_data elif field_type is float: value = test_data - elif field_type is datetime.datetime: + elif field_type is AwareDatetime or field_type is datetime.datetime: test_data: datetime.datetime value = test_data.isoformat() elif field_type is uuid.UUID: value = str(test_data) + elif type(field_type).__name__ == "_LiteralGenericAlias": + value = test_data else: raise RuntimeError( f"Please implement dump test data to json for field type {field_type}" @@ -294,7 +317,10 @@ def test__to_json__happy_path_full(self): print() print() - with open( - f"tests/unit/frbc/{snake_case(class_name)}_test.py", "w+" - ) as unit_test_file: - unit_test_file.write(template) + # Check if the file already exists + if not os.path.exists(f"tests/unit/frbc/{snake_case(class_name)}_test.py"): + with open( + f"tests/unit/frbc/{snake_case(class_name)}_test.py", "w+" + ) as unit_test_file: + unit_test_file.write(template) + print(f"Created tests/unit/frbc/{snake_case(class_name)}_test.py") From 251e7d019858a67eea928170d4e3174b6b5fd6b5 Mon Sep 17 00:00:00 2001 From: "F.N. Claessen" Date: Thu, 16 Jan 2025 10:27:05 +0100 Subject: [PATCH 2/3] fix: remove leftover breakpoint Signed-off-by: F.N. Claessen --- development_utilities/gen_unit_test_template.py | 1 - 1 file changed, 1 deletion(-) diff --git a/development_utilities/gen_unit_test_template.py b/development_utilities/gen_unit_test_template.py index 4456254..532a934 100644 --- a/development_utilities/gen_unit_test_template.py +++ b/development_utilities/gen_unit_test_template.py @@ -194,7 +194,6 @@ def dump_test_data_as_constructor_field_for(test_data, field_type: Type) -> str: elif type(field_type).__name__ == "_LiteralGenericAlias": value = field_type.__args__[0] else: - breakpoint() raise RuntimeError( f"Please implement dump test data for field type {field_type}" ) From 0c0d939f975e526bd381c8743b4208dff9772b86 Mon Sep 17 00:00:00 2001 From: "F.N. Claessen" Date: Thu, 16 Jan 2025 10:28:22 +0100 Subject: [PATCH 3/3] refactor: simplify if statements Signed-off-by: F.N. Claessen --- development_utilities/gen_unit_test_template.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/development_utilities/gen_unit_test_template.py b/development_utilities/gen_unit_test_template.py index 532a934..87bddc9 100644 --- a/development_utilities/gen_unit_test_template.py +++ b/development_utilities/gen_unit_test_template.py @@ -113,7 +113,7 @@ def generate_json_test_data_for_field(field_type: Type): value = bool(random.randint(0, 1)) elif field_type is float: value = random.random() * 9000.0 - elif field_type is AwareDatetime: + elif field_type in (AwareDatetime, datetime.datetime): # Generate a timezone-aware datetime value = datetime.datetime( year=random.randint(2020, 2023), @@ -124,18 +124,6 @@ def generate_json_test_data_for_field(field_type: Type): second=random.randint(0, 59), tzinfo=datetime.timezone(datetime.timedelta(hours=random.randint(-12, 14))), ) - elif field_type is datetime.datetime: - value = datetime.datetime( - year=random.randint(2020, 2023), - month=random.randint(1, 12), - day=random.randint(1, 28), - hour=random.randint(0, 23), - minute=random.randint(0, 59), - second=random.randint(0, 59), - tzinfo=datetime.timezone( - offset=datetime.timedelta(hours=random.randint(0, 2)) - ), - ) elif field_type is uuid.UUID: value = uuid.uuid4() else: @@ -237,7 +225,7 @@ def dump_test_data_as_json_field_for(test_data, field_type: Type): value = test_data elif field_type is float: value = test_data - elif field_type is AwareDatetime or field_type is datetime.datetime: + elif field_type in (AwareDatetime, datetime.datetime): test_data: datetime.datetime value = test_data.isoformat() elif field_type is uuid.UUID: