Skip to content
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
8 changes: 5 additions & 3 deletions src/dve/core_engine/backends/utilities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Necessary, otherwise uncategorised backend functionality."""

import inspect
import sys
from dataclasses import is_dataclass
from datetime import date, datetime, time
Expand Down Expand Up @@ -41,10 +42,11 @@

def is_type_complex(type_: Any) -> bool:
"""Check whether a type is a complex type or not."""
if type_ in (str, int, float, bool, bytes):
return False
_simple_types = (str, int, float, bool, bytes, date, datetime, time, Decimal)

if type_ in (date, datetime, time, Decimal):
if type_ in _simple_types or (
inspect.isclass(type_) and any(issubclass(type_, st) for st in _simple_types)
):
return False

return True
Expand Down
2 changes: 1 addition & 1 deletion src/dve/metadata_parser/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def get_type_and_validators(
elif hasattr(possible_python_type, "get_type_and_validators"):
possible_python_type: "FieldSpecification" # type: ignore
nested_vals = possible_python_type.get_type_and_validators( # type: ignore
field_name, *type_mappings, schemas=schemas, is_mandatory=False
field_name, *type_mappings, schemas=schemas, is_mandatory=is_mandatory
)
python_type, nested_default, nested_validators = nested_vals # type: ignore

Expand Down
63 changes: 60 additions & 3 deletions tests/test_core_engine/test_backends/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
from datetime import date
"""
Test backend utility functions
"""

# pylint: disable=C0115,C0116

from datetime import date, datetime
from decimal import Decimal
from typing import Any

import pytest
from pydantic import BaseModel

from dve.core_engine.backends.utilities import is_field_complex
from dve.core_engine.backends.utilities import is_field_complex, stringify_model
from dve.metadata_parser.domain_types import FormattedDatetime


def model_to_schema(mdl: BaseModel) -> dict[str, Any]:
schema = {}
for field_name, field_md in mdl.model_fields.items():
if issubclass(field_md.annotation, BaseModel):
schema[field_name] = model_to_schema(field_md.annotation)
else:
schema[field_name] = field_md.annotation

return schema


class AnotherTestModel(BaseModel):
Expand All @@ -21,6 +40,23 @@ class TestModel(BaseModel):
simple_tuple: tuple[int, str]
another_model: AnotherTestModel
date_example: date
test_fdatetime: FormattedDatetime


class ChildMultiTypeModel(BaseModel):
child_test_int: int


class MultiTypeModel(BaseModel):
test_str: str
test_int: int
test_dict: dict[str, int]
test_list: list[int]
test_decimal: Decimal
test_date: date
test_datetime: datetime
test_fdatetime: FormattedDatetime
test_child_model: ChildMultiTypeModel


TEST_MODEL = TestModel(
Expand All @@ -32,7 +68,8 @@ class TestModel(BaseModel):
simple_set={"a", "b", "c"},
simple_tuple=(1, "wow"),
another_model=AnotherTestModel(test_field="lemon"),
date_example=date(2026,1,1)
date_example=date(2026,1,1),
test_fdatetime="2026-01-01T12:30:10",
)


Expand All @@ -47,9 +84,29 @@ class TestModel(BaseModel):
("simple_set", True),
("simple_tuple", True),
("another_model", True),
("test_fdatetime", False),
]
)
def test_is_field_complex(field_name: str, expected: bool):
_field = TEST_MODEL.model_fields[field_name]
_res = is_field_complex(_field)
assert _res == expected, f"Expected {expected}. Got {_res}."


def test_stringify_model():
expected_dict = {
"test_str": str,
"test_int": str,
"test_dict": dict[str, str],
"test_list": list[str],
"test_decimal": str,
"test_date": str,
"test_datetime": str,
"test_fdatetime": str,
"test_child_model": {
"child_test_int": str
},
}
serialised_model_result = model_to_schema(stringify_model(MultiTypeModel))

assert expected_dict == serialised_model_result
Loading