Skip to content

Commit

Permalink
Fixed unit test gen (#62)
Browse files Browse the repository at this point in the history
* Fixed unit test gen to generate only if the tests do not already exists

* fix: remove leftover breakpoint

Signed-off-by: F.N. Claessen <[email protected]>

* refactor: simplify if statements

Signed-off-by: F.N. Claessen <[email protected]>

---------

Signed-off-by: F.N. Claessen <[email protected]>
Co-authored-by: F.N. Claessen <[email protected]>
  • Loading branch information
VladIftime and Flix6x authored Jan 16, 2025
1 parent ad6f255 commit c35b7e4
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions development_utilities/gen_unit_test_template.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import datetime
import json
import os
from enum import Enum
import inspect
import pprint
Expand All @@ -17,6 +18,7 @@
import uuid

import pydantic
from pydantic.types import AwareDatetime

from s2python import frbc
from s2python.common import Duration, PowerRange, NumberRange
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -111,17 +113,16 @@ 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 datetime.datetime:
elif field_type in (AwareDatetime, datetime.datetime):
# 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(
offset=datetime.timedelta(hours=random.randint(0, 2))
),
tzinfo=datetime.timezone(datetime.timedelta(hours=random.randint(-12, 14))),
)
elif field_type is uuid.UUID:
value = uuid.uuid4()
Expand Down Expand Up @@ -167,12 +168,19 @@ 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:
raise RuntimeError(
f"Please implement dump test data for field type {field_type}"
Expand Down Expand Up @@ -217,11 +225,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 in (AwareDatetime, 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}"
Expand Down Expand Up @@ -294,7 +304,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")

0 comments on commit c35b7e4

Please sign in to comment.