11'''
2- python-lambda-local: Test Direct Inovactions
2+ python-lambda-local: Test Direct Invocations
33(command-line and direct).
44
55Meant for use with py.test.
1313import os
1414from lambda_local .main import run as lambda_run
1515from lambda_local .main import call as lambda_call
16+ from lambda_local .main import ERR_TYPE_EXCEPTION
1617from lambda_local .context import Context
1718
1819
@@ -21,6 +22,10 @@ def my_lambda_function(event, context):
2122 return 42
2223
2324
25+ def my_failing_lambda_function (event , context ):
26+ raise Exception ('Oh no' )
27+ 28+ 2429def test_function_call_for_pytest ():
2530 (result , error_type ) = lambda_call (
2631 my_lambda_function , {}, Context (1 ))
@@ -30,6 +35,13 @@ def test_function_call_for_pytest():
3035 assert result == 42
3136
3237
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+ 3345def test_check_command_line ():
3446 request = json .dumps ({})
3547 request_file = 'check_command_line_event.json'
@@ -51,3 +63,26 @@ def test_check_command_line():
5163
5264 os .remove (request_file )
5365 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