30
30
TABLE_NAME = "TEST_TABLE"
31
31
32
32
33
+ def get_dataclasses_lib ():
34
+ """Python 3.6 doesn't support dataclasses natively"""
35
+ import dataclasses
36
+
37
+ return dataclasses
38
+
39
+
33
40
# Using parametrize to run test twice, with two separate instances of persistence store. One instance with caching
34
41
# enabled, and one without.
35
42
@pytest .mark .parametrize ("idempotency_config" , [{"use_local_cache" : False }, {"use_local_cache" : True }], indirect = True )
@@ -1073,23 +1080,20 @@ def test_invalid_dynamodb_persistence_layer():
1073
1080
assert str (ve .value ) == "key_attr [id] and sort_key_attr [id] cannot be the same!"
1074
1081
1075
1082
1083
+ @pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "requires python3.7 or higher for dataclasses" )
1076
1084
def test_idempotent_function_dataclasses ():
1077
- try :
1078
- # Scenario _prepare_data should convert a python dataclasses to a dict
1079
- from dataclasses import asdict , dataclass
1085
+ # Scenario _prepare_data should convert a python dataclasses to a dict
1086
+ dataclasses = get_dataclasses_lib ()
1080
1087
1081
- @dataclass
1082
- class Foo :
1083
- name : str
1084
-
1085
- expected_result = {"name" : "Bar" }
1086
- data = Foo (name = "Bar" )
1087
- as_dict = _prepare_data (data )
1088
- assert as_dict == asdict (data )
1089
- assert as_dict == expected_result
1088
+ @dataclasses .dataclass
1089
+ class Foo :
1090
+ name : str
1090
1091
1091
- except ModuleNotFoundError :
1092
- pass # Python 3.6
1092
+ expected_result = {"name" : "Bar" }
1093
+ data = Foo (name = "Bar" )
1094
+ as_dict = _prepare_data (data )
1095
+ assert as_dict == dataclasses .asdict (data )
1096
+ assert as_dict == expected_result
1093
1097
1094
1098
1095
1099
def test_idempotent_function_pydantic ():
@@ -1108,3 +1112,57 @@ class Foo(BaseModel):
1108
1112
def test_idempotent_function_other (data ):
1109
1113
# All other data types should be left as is
1110
1114
assert _prepare_data (data ) == data
1115
+
1116
+
1117
+ @pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "requires python3.7 or higher for dataclasses" )
1118
+ def test_idempotent_function_dataclass_with_jmespath ():
1119
+ # GIVEN
1120
+ dataclasses = get_dataclasses_lib ()
1121
+ config = IdempotencyConfig (event_key_jmespath = "transaction_id" , use_local_cache = True )
1122
+ mock_event = {"customer_id" : "fake" , "transaction_id" : "fake-id" }
1123
+ persistence_layer = MockPersistenceLayer (
1124
+ expected_idempotency_key = "test-func.collect_payment#"
1125
+ + hashlib .md5 (serialize (mock_event ["transaction_id" ]).encode ()).hexdigest ()
1126
+ )
1127
+
1128
+ @dataclasses .dataclass
1129
+ class Payment :
1130
+ customer_id : str
1131
+ transaction_id : str
1132
+
1133
+ @idempotent_function (data_keyword_argument = "payment" , persistence_store = persistence_layer , config = config )
1134
+ def collect_payment (payment : Payment ):
1135
+ return payment .transaction_id
1136
+
1137
+ # WHEN
1138
+ payment = Payment (** mock_event )
1139
+ result = collect_payment (payment = payment )
1140
+
1141
+ # THEN idempotency key assertion happens at MockPersistenceLayer
1142
+ assert result == payment .transaction_id
1143
+
1144
+
1145
+ @pytest .mark .skipif (sys .version_info < (3 , 7 ), reason = "requires python3.7 or higher for dataclasses" )
1146
+ def test_idempotent_function_pydantic_with_jmespath ():
1147
+ # GIVEN
1148
+ config = IdempotencyConfig (event_key_jmespath = "transaction_id" , use_local_cache = True )
1149
+ mock_event = {"customer_id" : "fake" , "transaction_id" : "fake-id" }
1150
+ persistence_layer = MockPersistenceLayer (
1151
+ expected_idempotency_key = "test-func.collect_payment#"
1152
+ + hashlib .md5 (serialize (mock_event ["transaction_id" ]).encode ()).hexdigest ()
1153
+ )
1154
+
1155
+ class Payment (BaseModel ):
1156
+ customer_id : str
1157
+ transaction_id : str
1158
+
1159
+ @idempotent_function (data_keyword_argument = "payment" , persistence_store = persistence_layer , config = config )
1160
+ def collect_payment (payment : Payment ):
1161
+ return payment .transaction_id
1162
+
1163
+ # WHEN
1164
+ payment = Payment (** mock_event )
1165
+ result = collect_payment (payment = payment )
1166
+
1167
+ # THEN idempotency key assertion happens at MockPersistenceLayer
1168
+ assert result == payment .transaction_id
0 commit comments