4
4
from pydantic import BaseModel
5
5
6
6
REFERENCE_OBJECT_VALID = {"key1" : "value1" , "key2" : 1 }
7
+ REFERENCE_OBJECT_VALID_UPDATED = {"key1" : "value1-updated" , "key2" : 2 }
7
8
REFERENCE_OBJECT_INVALID = {"key1" : "value1" , "key2" : "value2" }
8
9
REFERENCE_OBJECT_VALID_JSON = json .dumps (REFERENCE_OBJECT_VALID )
9
10
REFERENCE_OBJECT_NESTED_VALID = {"nested_key1" : {** REFERENCE_OBJECT_VALID }}
10
- REFERENCE_OBJECT_OVERRIDE_VALID = {"as_class_instance " : {** REFERENCE_OBJECT_VALID }}
11
+ REFERENCE_OBJECT_NESTED_VALID_UPDATED = {"nested_key1 " : {** REFERENCE_OBJECT_VALID_UPDATED }}
11
12
12
13
13
14
class ExampleSchema (BaseModel ):
@@ -19,10 +20,6 @@ class ExampleNestedSchema(BaseModel):
19
20
nested_key1 : ExampleSchema
20
21
21
22
22
- class ExampleOverrideSchema (BaseModel ):
23
- as_class_instance : ExampleSchema
24
-
25
-
26
23
class ExampleClass (ExampleSchema , InMemoryEntityPydantic ):
27
24
pass
28
25
@@ -33,43 +30,53 @@ def nested_key1_instance(self) -> ExampleClass:
33
30
return ExampleClass .create (self .nested_key1 .model_dump ())
34
31
35
32
36
- class ExampleFullClass ( ExampleOverrideSchema , InMemoryEntityPydantic ):
37
- __default_config__ = REFERENCE_OBJECT_OVERRIDE_VALID
33
+ class ExampleNestedKeyAsClassInstanceClass ( ExampleNestedSchema , InMemoryEntityPydantic ):
34
+ __default_config__ = REFERENCE_OBJECT_NESTED_VALID
38
35
39
- # We override the as_class_instance field to be an instance of ExampleClass
40
- as_class_instance : ExampleClass = ExampleClass (** REFERENCE_OBJECT_VALID )
36
+ nested_key1 : ExampleClass = ExampleClass (** REFERENCE_OBJECT_VALID )
41
37
42
38
43
39
def test_create ():
44
- in_memory_entity = ExampleClass .create (REFERENCE_OBJECT_VALID )
45
- assert isinstance (in_memory_entity , ExampleClass )
46
- assert in_memory_entity .key1 == "value1"
47
- assert in_memory_entity .key2 == 1
40
+ entity = ExampleClass .create (REFERENCE_OBJECT_VALID )
41
+ assert isinstance (entity , ExampleClass )
42
+ assert entity .key1 == "value1"
43
+ assert entity .key2 == 1
48
44
49
45
50
46
def test_create_nested ():
51
47
# Test creating an instance with nested valid data
52
- in_memory_entity = ExampleNestedClass .create (REFERENCE_OBJECT_NESTED_VALID )
53
- assert isinstance (in_memory_entity , ExampleNestedClass )
54
- assert isinstance (in_memory_entity .nested_key1 , ExampleSchema )
55
- assert in_memory_entity .nested_key1 .key1 == "value1"
56
- assert in_memory_entity .nested_key1 .key2 == 1
57
- assert isinstance (in_memory_entity .nested_key1_instance , ExampleClass )
58
-
59
-
60
- def test_full_class ():
61
- in_memory_entity = ExampleFullClass .create (REFERENCE_OBJECT_OVERRIDE_VALID )
62
- assert isinstance (in_memory_entity , ExampleFullClass )
63
- assert isinstance (in_memory_entity .as_class_instance , ExampleClass )
64
- assert in_memory_entity .as_class_instance .key1 == "value1"
65
- assert in_memory_entity .as_class_instance .key2 == 1
66
- assert in_memory_entity .get_data_model () == ExampleOverrideSchema
48
+ entity = ExampleNestedClass .create (REFERENCE_OBJECT_NESTED_VALID )
49
+ assert isinstance (entity , ExampleNestedClass )
50
+ assert isinstance (entity .nested_key1 , ExampleSchema )
51
+ assert entity .nested_key1 .key1 == "value1"
52
+ assert entity .nested_key1 .key2 == 1
53
+ assert isinstance (entity .nested_key1_instance , ExampleClass )
54
+
55
+
56
+ def test_create_nested_as_class_instance ():
57
+ entity = ExampleNestedKeyAsClassInstanceClass .create (REFERENCE_OBJECT_NESTED_VALID )
58
+ assert isinstance (entity , ExampleNestedKeyAsClassInstanceClass )
59
+ assert isinstance (entity .nested_key1 , ExampleClass )
60
+ assert entity .nested_key1 .key1 == "value1"
61
+ assert entity .nested_key1 .key2 == 1
62
+ assert entity .get_data_model () == ExampleNestedSchema
63
+
64
+
65
+ def test_update_nested_as_class_instance ():
66
+ entity = ExampleNestedKeyAsClassInstanceClass .create (REFERENCE_OBJECT_NESTED_VALID )
67
+ entity .nested_key1 = ExampleClass (** REFERENCE_OBJECT_VALID_UPDATED )
68
+ assert entity .nested_key1 .key1 == "value1-updated"
69
+ assert entity .nested_key1 .key2 == 2
70
+ entity_json = entity .to_json ()
71
+ reference_json = json .dumps (REFERENCE_OBJECT_NESTED_VALID_UPDATED )
72
+ assert json .loads (entity_json ) == json .loads (reference_json )
73
+ assert isinstance (entity .nested_key1 , ExampleClass )
67
74
68
75
69
76
def test_validate ():
70
77
# Test valid case
71
- in_memory_entity = ExampleClass .create (REFERENCE_OBJECT_VALID )
72
- assert isinstance (in_memory_entity , ExampleClass )
78
+ entity = ExampleClass .create (REFERENCE_OBJECT_VALID )
79
+ assert isinstance (entity , ExampleClass )
73
80
# Test invalid case
74
81
try :
75
82
_ = ExampleClass .create (REFERENCE_OBJECT_INVALID )
@@ -85,10 +92,10 @@ def test_is_valid():
85
92
86
93
def test_from_json ():
87
94
# Test from_json method with valid JSON
88
- in_memory_entity = ExampleClass .from_json (REFERENCE_OBJECT_VALID_JSON )
89
- assert isinstance (in_memory_entity , ExampleClass )
90
- assert in_memory_entity .key1 == "value1"
91
- assert in_memory_entity .key2 == 1
95
+ entity = ExampleClass .from_json (REFERENCE_OBJECT_VALID_JSON )
96
+ assert isinstance (entity , ExampleClass )
97
+ assert entity .key1 == "value1"
98
+ assert entity .key2 == 1
92
99
93
100
# Test from_json with invalid JSON
94
101
try :
0 commit comments