1
1
from dataclasses import dataclass
2
2
from datetime import datetime
3
+ from enum import Enum , IntEnum
3
4
4
5
import pytest
5
6
8
9
from temporalio .api .common .v1 import Payload as AnotherNameForPayload
9
10
10
11
11
- async def test_default ():
12
+ class NonSerializableClass :
13
+ pass
14
+
15
+
16
+ class NonSerializableEnum (Enum ):
17
+ FOO = "foo"
18
+
19
+
20
+ class SerializableEnum (IntEnum ):
21
+ FOO = 1
22
+
23
+
24
+ @dataclass
25
+ class MyDataClass :
26
+ foo : str
27
+ bar : int
28
+ baz : SerializableEnum
29
+
30
+
31
+ async def test_converter_default ():
12
32
async def assert_payload (
13
33
input ,
14
34
expected_encoding ,
@@ -33,6 +53,7 @@ async def assert_payload(
33
53
assert len (actual_inputs ) == 1
34
54
if expected_decoded_input is None :
35
55
expected_decoded_input = input
56
+ assert type (actual_inputs [0 ]) is type (expected_decoded_input )
36
57
assert actual_inputs [0 ] == expected_decoded_input
37
58
return payloads [0 ]
38
59
@@ -58,31 +79,38 @@ async def assert_payload(
58
79
59
80
# Unknown type
60
81
with pytest .raises (TypeError ) as excinfo :
61
-
62
- class NonSerializableClass :
63
- pass
64
-
65
82
await assert_payload (NonSerializableClass (), None , None )
66
83
assert "not JSON serializable" in str (excinfo .value )
67
84
68
- @dataclass
69
- class MyDataClass :
70
- foo : str
71
- bar : int
85
+ # Bad enum type. We do not allow non-int enums due to ambiguity in
86
+ # rebuilding and other confusion.
87
+ with pytest .raises (TypeError ) as excinfo :
88
+ await assert_payload (NonSerializableEnum .FOO , None , None )
89
+ assert "not JSON serializable" in str (excinfo .value )
90
+
91
+ # Good enum no type hint
92
+ await assert_payload (
93
+ SerializableEnum .FOO , "json/plain" , "1" , expected_decoded_input = 1
94
+ )
95
+
96
+ # Good enum type hint
97
+ await assert_payload (
98
+ SerializableEnum .FOO , "json/plain" , "1" , type_hint = SerializableEnum
99
+ )
72
100
73
101
# Data class without type hint is just dict
74
102
await assert_payload (
75
- MyDataClass (foo = "somestr" , bar = 123 ),
103
+ MyDataClass (foo = "somestr" , bar = 123 , baz = SerializableEnum . FOO ),
76
104
"json/plain" ,
77
- '{"bar":123,"foo":"somestr"}' ,
78
- expected_decoded_input = {"foo" : "somestr" , "bar" : 123 },
105
+ '{"bar":123,"baz":1," foo":"somestr"}' ,
106
+ expected_decoded_input = {"foo" : "somestr" , "bar" : 123 , "baz" : 1 },
79
107
)
80
108
81
109
# Data class with type hint reconstructs the class
82
110
await assert_payload (
83
- MyDataClass (foo = "somestr" , bar = 123 ),
111
+ MyDataClass (foo = "somestr" , bar = 123 , baz = SerializableEnum . FOO ),
84
112
"json/plain" ,
85
- '{"bar":123,"foo":"somestr"}' ,
113
+ '{"bar":123,"baz":1," foo":"somestr"}' ,
86
114
type_hint = MyDataClass ,
87
115
)
88
116
0 commit comments