1
- import io
2
1
from http import HTTPStatus
3
2
from typing import Any , Dict , Optional
4
3
12
11
EventBridgeEnvelope ,
13
12
SqsEnvelope ,
14
13
UserEnvelope ,
15
- validator
14
+ validate ,
15
+ validator ,
16
16
)
17
17
18
18
19
+ class MyMessage (BaseModel ):
20
+ message : str
21
+ messageId : int
22
+
23
+
24
+ def test_validate_function ():
25
+ eventbridge_event = {
26
+ "version" : "0" ,
27
+ "id" : "553961c5-5017-5763-6f21-f88d5f5f4b05" ,
28
+ "detail-type" : "my func stream event json" ,
29
+ "source" : "arn:aws:lambda:eu-west-1:88888888:function:my_func" ,
30
+ "account" : "88888888" ,
31
+ "time" : "2020-02-11T08:18:09Z" ,
32
+ "region" : "eu-west-1" ,
33
+ "resources" : ["arn:aws:dynamodb:eu-west-1:88888888:table/stream/2020-02" ],
34
+ "detail" : {"message" : "hello" , "messageId" : 8 },
35
+ }
36
+ parsed_event = validate (eventbridge_event , MyMessage , EventBridgeEnvelope (), True )
37
+ assert parsed_event .dict () == {"message" : "hello" , "messageId" : 8 }
38
+
39
+
40
+ def test_validate_function_no_return_value ():
41
+ eventbridge_event = {
42
+ "version" : "0" ,
43
+ "id" : "553961c5-5017-5763-6f21-f88d5f5f4b05" ,
44
+ "detail-type" : "my func stream event json" ,
45
+ "source" : "arn:aws:lambda:eu-west-1:88888888:function:my_func" ,
46
+ "account" : "88888888" ,
47
+ "time" : "2020-02-11T08:18:09Z" ,
48
+ "region" : "eu-west-1" ,
49
+ "resources" : ["arn:aws:dynamodb:eu-west-1:88888888:table/stream/2020-02" ],
50
+ "detail" : {"message" : "hello" , "messageId" : 8 },
51
+ }
52
+ parsed_event = validate (eventbridge_event , MyMessage , EventBridgeEnvelope ())
53
+ assert parsed_event is None
54
+
55
+
56
+ def test_validate_function_fail_envelope ():
57
+ eventbridge_event = {
58
+ "version" : "0" ,
59
+ "id" : "553961c5-5017-5763-6f21-f88d5f5f4b05" ,
60
+ "detail-type" : "my func stream event json" ,
61
+ "source" : "arn:aws:lambda:eu-west-1:88888888:function:my_func" ,
62
+ "time" : "2020-02-11T08:18:09Z" ,
63
+ "region" : "eu-west-1" ,
64
+ "resources" : ["arn:aws:dynamodb:eu-west-1:88888888:table/stream/2020-02" ],
65
+ "detail" : {"message" : "hello" , "messageId" : 8 },
66
+ }
67
+ with pytest .raises (ValidationError ):
68
+ validate (eventbridge_event , MyMessage , EventBridgeEnvelope ())
69
+
70
+
71
+ def test_validate_function_fail_user_schema ():
72
+ eventbridge_event = {
73
+ "version" : "0" ,
74
+ "id" : "553961c5-5017-5763-6f21-f88d5f5f4b05" ,
75
+ "detail-type" : "my func stream event json" ,
76
+ "source" : "arn:aws:lambda:eu-west-1:88888888:function:my_func" ,
77
+ "account" : "88888888" ,
78
+ "time" : "2020-02-11T08:18:09Z" ,
79
+ "region" : "eu-west-1" ,
80
+ "resources" : ["arn:aws:dynamodb:eu-west-1:88888888:table/stream/2020-02" ],
81
+ "detail" : {"mess11age" : "hello" , "messageId" : 8 },
82
+ }
83
+ with pytest .raises (ValidationError ):
84
+ validate (eventbridge_event , MyMessage , EventBridgeEnvelope ())
85
+
86
+
19
87
class OutboundSchema (BaseModel ):
20
88
response_code : HTTPStatus
21
89
message : str
@@ -25,11 +93,6 @@ class InboundSchema(BaseModel):
25
93
greeting : str
26
94
27
95
28
- @pytest .fixture
29
- def stdout ():
30
- return io .StringIO ()
31
-
32
-
33
96
@validator (inbound_schema_model = InboundSchema , outbound_schema_model = OutboundSchema , envelope = UserEnvelope ())
34
97
def my_handler (event : Dict [str , Any ], context : LambdaContext ) -> Dict [str , Optional [Any ]]:
35
98
assert event ["custom" ]
@@ -58,11 +121,6 @@ def test_fail_inbound_validation():
58
121
my_handler ({"this_fails" : "hello" }, LambdaContext ())
59
122
60
123
61
- class MyMessage (BaseModel ):
62
- message : str
63
- messageId : int
64
-
65
-
66
124
@validator (inbound_schema_model = MyMessage , outbound_schema_model = OutboundSchema , envelope = DynamoDBEnvelope ())
67
125
def dynamodb_handler (event : Dict [str , Any ], context : LambdaContext ) -> Dict [str , Optional [Any ]]:
68
126
assert event ["custom" ]
0 commit comments