1
1
'''
2
- python-lambda-local: Test Direct Inovactions
2
+ python-lambda-local: Test Direct Invocations
3
3
(command-line and direct).
4
4
5
5
Meant for use with py.test.
13
13
import os
14
14
from lambda_local .main import run as lambda_run
15
15
from lambda_local .main import call as lambda_call
16
+ from lambda_local .main import ERR_TYPE_EXCEPTION
16
17
from lambda_local .context import Context
17
18
18
19
@@ -21,6 +22,10 @@ def my_lambda_function(event, context):
21
22
return 42
22
23
23
24
25
+ def my_failing_lambda_function (event , context ):
26
+ raise Exception ('Oh no' )
27
+
28
+
24
29
def test_function_call_for_pytest ():
25
30
(result , error_type ) = lambda_call (
26
31
my_lambda_function , {}, Context (1 ))
@@ -30,6 +35,13 @@ def test_function_call_for_pytest():
30
35
assert result == 42
31
36
32
37
38
+ def test_handle_exceptions_gracefully ():
39
+ (result , error_type ) = lambda_call (
40
+ my_failing_lambda_function , {}, Context (1 ))
41
+
42
+ assert error_type is ERR_TYPE_EXCEPTION
43
+
44
+
33
45
def test_check_command_line ():
34
46
request = json .dumps ({})
35
47
request_file = 'check_command_line_event.json'
@@ -51,3 +63,26 @@ def test_check_command_line():
51
63
52
64
os .remove (request_file )
53
65
assert p .exitcode == 0
66
+
67
+
68
+ def test_check_command_line_error ():
69
+ request = json .dumps ({})
70
+ request_file = 'check_command_line_event.json'
71
+ with open (request_file , "w" ) as f :
72
+ f .write (request )
73
+
74
+ args = argparse .Namespace (event = request_file ,
75
+ file = 'tests/test_direct_invocations.py' ,
76
+ function = 'my_failing_lambda_function' ,
77
+ timeout = 1 ,
78
+ environment_variables = '' ,
79
+ library = None ,
80
+ version_name = '' ,
81
+ arn_string = ''
82
+ )
83
+ p = Process (target = lambda_run , args = (args ,))
84
+ p .start ()
85
+ p .join ()
86
+
87
+ os .remove (request_file )
88
+ assert p .exitcode == 1
0 commit comments