Skip to content

Commit 71a869e

Browse files
committed
test: add tests for enum behavior with msgspec structures
1 parent 88750e2 commit 71a869e

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

tests/test_enum_casting.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Test enum usage behavior in Schema.org models.
4+
"""
5+
6+
import msgspec
7+
8+
from msgspec_schemaorg.models.event import DeliveryEvent
9+
from msgspec_schemaorg.enums.intangible.DeliveryMethod import DeliveryMethod
10+
11+
12+
def test_enum_usage():
13+
"""Test basic enum behavior with msgspec models."""
14+
# 1. Test passing an enum value directly
15+
event1 = DeliveryEvent(hasDeliveryMethod=DeliveryMethod.LockerDelivery)
16+
assert isinstance(event1.hasDeliveryMethod, DeliveryMethod)
17+
assert event1.hasDeliveryMethod == DeliveryMethod.LockerDelivery
18+
assert event1.hasDeliveryMethod.value == "LockerDelivery"
19+
20+
# 2. Test passing a string value (not automatically converted to enum)
21+
event2 = DeliveryEvent(hasDeliveryMethod="LockerDelivery")
22+
assert isinstance(event2.hasDeliveryMethod, str)
23+
assert event2.hasDeliveryMethod == "LockerDelivery"
24+
25+
# 3. Test string values still compare equal to enum values
26+
assert event2.hasDeliveryMethod == DeliveryMethod.LockerDelivery.value
27+
28+
# 4. Test passing a list of enum values
29+
event3 = DeliveryEvent(hasDeliveryMethod=[
30+
DeliveryMethod.LockerDelivery,
31+
DeliveryMethod.ParcelService
32+
])
33+
assert isinstance(event3.hasDeliveryMethod, list)
34+
assert all(isinstance(item, DeliveryMethod) for item in event3.hasDeliveryMethod)
35+
36+
# 5. Test passing a list of strings
37+
event4 = DeliveryEvent(hasDeliveryMethod=["LockerDelivery", "ParcelService"])
38+
assert isinstance(event4.hasDeliveryMethod, list)
39+
assert all(isinstance(item, str) for item in event4.hasDeliveryMethod)
40+
41+
# 6. Test encoding to JSON (both should serialize identically)
42+
json_bytes1 = msgspec.json.encode(event1)
43+
json_bytes2 = msgspec.json.encode(event2)
44+
assert json_bytes1 == json_bytes2
45+
46+
# In both cases, enums are serialized as their string value
47+
json_str = json_bytes1.decode()
48+
assert '"hasDeliveryMethod":"LockerDelivery"' in json_str
49+
50+
# 7. Test invalid values are allowed (no validation against enum)
51+
event5 = DeliveryEvent(hasDeliveryMethod="InvalidDeliveryMethod")
52+
assert event5.hasDeliveryMethod == "InvalidDeliveryMethod"
53+
54+
print("All enum usage tests passed!")
55+
56+
57+
if __name__ == "__main__":
58+
test_enum_usage()

0 commit comments

Comments
 (0)