1
1
import base64
2
2
import io
3
3
from copy import deepcopy
4
+ from dataclasses import dataclass
4
5
5
6
import pytest
6
7
from avro .io import BinaryEncoder , DatumWriter
@@ -86,57 +87,58 @@ def kafka_event_with_avro_data(avro_encoded_value, avro_encoded_key):
86
87
}
87
88
88
89
89
- def test_kafka_consumer_with_avro_and_dataclass (
90
- kafka_event_with_avro_data ,
91
- avro_value_schema ,
92
- lambda_context ,
93
- user_value_dataclass ,
94
- ):
95
- """Test Kafka consumer with Avro deserialization and dataclass output serialization."""
90
+ @dataclass
91
+ class UserValueDataClass :
92
+ name : str
93
+ age : int
94
+
95
+
96
+ @dataclass
97
+ class UserKeyClass :
98
+ user_id : str
99
+
100
+
101
+ def test_kafka_consumer_with_avro (kafka_event_with_avro_data , avro_value_schema , lambda_context ):
102
+ """Test Kafka consumer with Avro deserialization without output serialization."""
96
103
97
104
# Create dict to capture results
98
105
result_data = {}
99
106
100
- schema_config = SchemaConfig (
101
- value_schema_type = "AVRO" ,
102
- value_schema = avro_value_schema ,
103
- value_output_serializer = user_value_dataclass ,
104
- )
107
+ schema_config = SchemaConfig (value_schema_type = "AVRO" , value_schema = avro_value_schema )
105
108
106
109
@kafka_consumer (schema_config = schema_config )
107
110
def handler (event : ConsumerRecords , context ):
108
111
# Capture the results to verify
109
112
record = next (event .records )
110
113
result_data ["value_type" ] = type (record .value ).__name__
111
- result_data ["name" ] = record .value . name
112
- result_data ["age" ] = record .value . age
114
+ result_data ["name" ] = record .value [ " name" ]
115
+ result_data ["age" ] = record .value [ " age" ]
113
116
return {"processed" : True }
114
117
115
118
# Call the handler
116
119
result = handler (kafka_event_with_avro_data , lambda_context )
117
120
118
121
# Verify the results
119
122
assert result == {"processed" : True }
120
- assert result_data ["value_type" ] == "UserValueDataClass "
123
+ assert result_data ["value_type" ] == "dict "
121
124
assert result_data ["name" ] == "John Doe"
122
125
assert result_data ["age" ] == 30
123
126
124
127
125
- def test_kafka_consumer_with_avro_and_custom_object (
128
+ def test_kafka_consumer_with_avro_and_dataclass (
126
129
kafka_event_with_avro_data ,
127
130
avro_value_schema ,
128
131
lambda_context ,
129
- user_value_dict ,
130
132
):
131
- """Test Kafka consumer with Avro deserialization and custom object serialization."""
133
+ """Test Kafka consumer with Avro deserialization and dataclass output serialization."""
132
134
133
135
# Create dict to capture results
134
136
result_data = {}
135
137
136
138
schema_config = SchemaConfig (
137
139
value_schema_type = "AVRO" ,
138
140
value_schema = avro_value_schema ,
139
- value_output_serializer = user_value_dict ,
141
+ value_output_serializer = UserValueDataClass ,
140
142
)
141
143
142
144
@kafka_consumer (schema_config = schema_config )
@@ -153,34 +155,43 @@ def handler(event: ConsumerRecords, context):
153
155
154
156
# Verify the results
155
157
assert result == {"processed" : True }
156
- assert result_data ["value_type" ] == "UserValueDict "
158
+ assert result_data ["value_type" ] == "UserValueDataClass "
157
159
assert result_data ["name" ] == "John Doe"
158
160
assert result_data ["age" ] == 30
159
161
160
162
161
- def test_kafka_consumer_with_avro_raw (kafka_event_with_avro_data , avro_value_schema , lambda_context ):
162
- """Test Kafka consumer with Avro deserialization without output serialization."""
163
+ def test_kafka_consumer_with_avro_and_custom_object (
164
+ kafka_event_with_avro_data ,
165
+ avro_value_schema ,
166
+ lambda_context ,
167
+ user_value_dict ,
168
+ ):
169
+ """Test Kafka consumer with Avro deserialization and custom object serialization."""
163
170
164
171
# Create dict to capture results
165
172
result_data = {}
166
173
167
- schema_config = SchemaConfig (value_schema_type = "AVRO" , value_schema = avro_value_schema )
174
+ schema_config = SchemaConfig (
175
+ value_schema_type = "AVRO" ,
176
+ value_schema = avro_value_schema ,
177
+ value_output_serializer = user_value_dict ,
178
+ )
168
179
169
180
@kafka_consumer (schema_config = schema_config )
170
181
def handler (event : ConsumerRecords , context ):
171
182
# Capture the results to verify
172
183
record = next (event .records )
173
184
result_data ["value_type" ] = type (record .value ).__name__
174
- result_data ["name" ] = record .value [ " name" ]
175
- result_data ["age" ] = record .value [ " age" ]
185
+ result_data ["name" ] = record .value . name
186
+ result_data ["age" ] = record .value . age
176
187
return {"processed" : True }
177
188
178
189
# Call the handler
179
190
result = handler (kafka_event_with_avro_data , lambda_context )
180
191
181
192
# Verify the results
182
193
assert result == {"processed" : True }
183
- assert result_data ["value_type" ] == "dict "
194
+ assert result_data ["value_type" ] == "UserValueDict "
184
195
assert result_data ["name" ] == "John Doe"
185
196
assert result_data ["age" ] == 30
186
197
@@ -207,7 +218,7 @@ def lambda_handler(event: ConsumerRecords, context):
207
218
208
219
# The exact error message may vary depending on the Avro library's internals,
209
220
# but should indicate a deserialization problem
210
- assert "Error trying to deserializer avro data" in str (excinfo .value )
221
+ assert "Error trying to deserialize avro data" in str (excinfo .value )
211
222
212
223
213
224
def test_kafka_consumer_with_invalid_avro_schema (kafka_event_with_avro_data , lambda_context ):
@@ -245,8 +256,6 @@ def test_kafka_consumer_with_key_deserialization(
245
256
lambda_context ,
246
257
avro_value_schema ,
247
258
avro_key_schema ,
248
- user_value_dataclass ,
249
- user_key_dataclass ,
250
259
):
251
260
"""Test deserializing both key and value with different schemas and serializers."""
252
261
@@ -257,10 +266,10 @@ def test_kafka_consumer_with_key_deserialization(
257
266
schema_config = SchemaConfig (
258
267
value_schema_type = "AVRO" ,
259
268
value_schema = avro_value_schema ,
260
- value_output_serializer = user_value_dataclass ,
269
+ value_output_serializer = UserValueDataClass ,
261
270
key_schema_type = "AVRO" ,
262
271
key_schema = avro_key_schema ,
263
- key_output_serializer = user_key_dataclass ,
272
+ key_output_serializer = UserKeyClass ,
264
273
)
265
274
266
275
@kafka_consumer (schema_config = schema_config )
@@ -283,48 +292,3 @@ def lambda_handler(event: ConsumerRecords, context):
283
292
assert key_value_result ["value_type" ] == "UserValueDataClass"
284
293
assert key_value_result ["value_name" ] == "John Doe"
285
294
assert key_value_result ["value_age" ] == 30
286
-
287
-
288
- def test_kafka_consumer_with_different_serializers_for_key_and_value (
289
- kafka_event_with_avro_data ,
290
- lambda_context ,
291
- avro_value_schema ,
292
- avro_key_schema ,
293
- user_key_dataclass ,
294
- user_value_dict ,
295
- ):
296
- """Test using different serializer types for key and value."""
297
-
298
- # Create dict to capture results
299
- results = {}
300
-
301
- # Create schema config with different serializers
302
- schema_config = SchemaConfig (
303
- value_schema_type = "AVRO" ,
304
- value_schema = avro_value_schema ,
305
- value_output_serializer = user_value_dict ,
306
- key_schema_type = "AVRO" ,
307
- key_schema = avro_key_schema ,
308
- key_output_serializer = user_key_dataclass ,
309
- )
310
-
311
- @kafka_consumer (schema_config = schema_config )
312
- def handler (event : ConsumerRecords , context ):
313
- record = next (event .records )
314
- results ["key_type" ] = type (record .key ).__name__
315
- results ["key_id" ] = record .key .user_id
316
- results ["value_type" ] = type (record .value ).__name__
317
- results ["value_name" ] = record .value .name
318
- results ["value_age" ] = record .value .age
319
- return {"processed" : True }
320
-
321
- # Call the handler
322
- result = handler (kafka_event_with_avro_data , lambda_context )
323
-
324
- # Verify the results
325
- assert result == {"processed" : True }
326
- assert results ["key_type" ] == "UserKeyClass"
327
- assert results ["key_id" ] == "user-123"
328
- assert results ["value_type" ] == "UserValueDict"
329
- assert results ["value_name" ] == "John Doe"
330
- assert results ["value_age" ] == 30
0 commit comments