Skip to content

Commit d553a0a

Browse files
committed
test: add more cases
1 parent fd0b170 commit d553a0a

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

python/cocoindex/tests/test_convert.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,37 @@ class MixedStruct:
11601160
validate_full_roundtrip(instance, MixedStruct)
11611161

11621162

1163+
def test_roundtrip_simple_struct_to_dict_binding() -> None:
1164+
"""Test struct -> dict binding with Any annotation."""
1165+
1166+
@dataclass
1167+
class SimpleStruct:
1168+
first_name: str
1169+
last_name: str
1170+
1171+
instance = SimpleStruct("John", "Doe")
1172+
expected_dict = {"first_name": "John", "last_name": "Doe"}
1173+
1174+
# Test Any annotation
1175+
validate_full_roundtrip(
1176+
instance,
1177+
SimpleStruct,
1178+
(expected_dict, Any),
1179+
(expected_dict, dict),
1180+
(expected_dict, dict[Any, Any]),
1181+
(expected_dict, dict[str, Any]),
1182+
# For simple struct, all fields have the same type, so we can directly use the type as the dict value type.
1183+
(expected_dict, dict[Any, str]),
1184+
(expected_dict, dict[str, str]),
1185+
)
1186+
1187+
with pytest.raises(ValueError):
1188+
validate_full_roundtrip(instance, SimpleStruct, (expected_dict, dict[str, int]))
1189+
1190+
with pytest.raises(ValueError):
1191+
validate_full_roundtrip(instance, SimpleStruct, (expected_dict, dict[int, Any]))
1192+
1193+
11631194
def test_roundtrip_struct_to_dict_binding() -> None:
11641195
"""Test struct -> dict binding with Any annotation."""
11651196

@@ -1173,7 +1204,20 @@ class SimpleStruct:
11731204
expected_dict = {"name": "test", "value": 42, "price": 3.14}
11741205

11751206
# Test Any annotation
1176-
validate_full_roundtrip(instance, SimpleStruct, (expected_dict, Any))
1207+
validate_full_roundtrip(
1208+
instance,
1209+
SimpleStruct,
1210+
(expected_dict, Any),
1211+
(expected_dict, dict),
1212+
(expected_dict, dict[Any, Any]),
1213+
(expected_dict, dict[str, Any]),
1214+
)
1215+
1216+
with pytest.raises(ValueError):
1217+
validate_full_roundtrip(instance, SimpleStruct, (expected_dict, dict[str, str]))
1218+
1219+
with pytest.raises(ValueError):
1220+
validate_full_roundtrip(instance, SimpleStruct, (expected_dict, dict[int, Any]))
11771221

11781222

11791223
def test_roundtrip_struct_to_dict_explicit() -> None:

0 commit comments

Comments
 (0)