1- from datetime import date
1+ """
2+ Test backend utility functions
3+ """
4+
5+ # pylint: disable=C0115,C0116
6+
7+ from datetime import date , datetime
8+ from decimal import Decimal
29from typing import Any
310
411import pytest
512from pydantic import BaseModel
613
7- from dve .core_engine .backends .utilities import is_field_complex
14+ from dve .core_engine .backends .utilities import is_field_complex , stringify_model
15+ from dve .metadata_parser .domain_types import FormattedDatetime
16+
17+
18+ def model_to_schema (mdl : BaseModel ) -> dict [str , Any ]:
19+ schema = {}
20+ for field_name , field_md in mdl .model_fields .items ():
21+ if issubclass (field_md .annotation , BaseModel ):
22+ schema [field_name ] = model_to_schema (field_md .annotation )
23+ else :
24+ schema [field_name ] = field_md .annotation
25+
26+ return schema
827
928
1029class AnotherTestModel (BaseModel ):
@@ -21,6 +40,23 @@ class TestModel(BaseModel):
2140 simple_tuple : tuple [int , str ]
2241 another_model : AnotherTestModel
2342 date_example : date
43+ test_fdatetime : FormattedDatetime
44+
45+
46+ class ChildMultiTypeModel (BaseModel ):
47+ child_test_int : int
48+
49+
50+ class MultiTypeModel (BaseModel ):
51+ test_str : str
52+ test_int : int
53+ test_dict : dict [str , int ]
54+ test_list : list [int ]
55+ test_decimal : Decimal
56+ test_date : date
57+ test_datetime : datetime
58+ test_fdatetime : FormattedDatetime
59+ test_child_model : ChildMultiTypeModel
2460
2561
2662TEST_MODEL = TestModel (
@@ -32,7 +68,8 @@ class TestModel(BaseModel):
3268 simple_set = {"a" , "b" , "c" },
3369 simple_tuple = (1 , "wow" ),
3470 another_model = AnotherTestModel (test_field = "lemon" ),
35- date_example = date (2026 ,1 ,1 )
71+ date_example = date (2026 ,1 ,1 ),
72+ test_fdatetime = "2026-01-01T12:30:10" ,
3673)
3774
3875
@@ -47,9 +84,29 @@ class TestModel(BaseModel):
4784 ("simple_set" , True ),
4885 ("simple_tuple" , True ),
4986 ("another_model" , True ),
87+ ("test_fdatetime" , False ),
5088 ]
5189)
5290def test_is_field_complex (field_name : str , expected : bool ):
5391 _field = TEST_MODEL .model_fields [field_name ]
5492 _res = is_field_complex (_field )
5593 assert _res == expected , f"Expected { expected } . Got { _res } ."
94+
95+
96+ def test_stringify_model ():
97+ expected_dict = {
98+ "test_str" : str ,
99+ "test_int" : str ,
100+ "test_dict" : dict [str , str ],
101+ "test_list" : list [str ],
102+ "test_decimal" : str ,
103+ "test_date" : str ,
104+ "test_datetime" : str ,
105+ "test_fdatetime" : str ,
106+ "test_child_model" : {
107+ "child_test_int" : str
108+ },
109+ }
110+ serialised_model_result = model_to_schema (stringify_model (MultiTypeModel ))
111+
112+ assert expected_dict == serialised_model_result
0 commit comments