-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathtest_direct_invocations.py
120 lines (91 loc) · 3.42 KB
/
test_direct_invocations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
'''
python-lambda-local: Test Direct Invocations
(command-line and direct).
Meant for use with py.test.
Copyright 2015-2020 HENNGE K.K. (formerly known as HDE, Inc.)
Licensed under MIT
'''
import json
import argparse
from multiprocessing import Process
import os
from lambda_local.main import run as lambda_run
from lambda_local.main import call as lambda_call
from lambda_local.main import ERR_TYPE_EXCEPTION
from lambda_local.context import Context
def my_lambda_function(event, context):
print("Hello World from My Lambda Function!")
return 42
def my_failing_lambda_function(event, context):
raise Exception('Oh no')
class MyLambdaClass:
def my_lambda_method(self, event, context):
print("Hello World from My Lambda Method!")
return 42
my_lambda_instance = MyLambdaClass()
def test_function_call_for_pytest():
(result, error_type) = lambda_call(
my_lambda_function, {}, Context(1))
assert error_type is None
assert result == 42
def test_handle_exceptions_gracefully():
(result, error_type) = lambda_call(
my_failing_lambda_function, {}, Context(1))
assert error_type is ERR_TYPE_EXCEPTION
def test_check_command_line():
request = json.dumps({})
request_file = 'check_command_line_event.json'
with open(request_file, "w") as f:
f.write(request)
args = argparse.Namespace(event=request_file,
file='tests/test_direct_invocations.py',
function='my_lambda_function',
timeout=1,
environment_variables='',
library=None,
version_name='',
arn_string=''
)
p = Process(target=lambda_run, args=(args,))
p.start()
p.join()
os.remove(request_file)
assert p.exitcode == 0
def test_check_command_line_error():
request = json.dumps({})
request_file = 'check_command_line_event.json'
with open(request_file, "w") as f:
f.write(request)
args = argparse.Namespace(event=request_file,
file='tests/test_direct_invocations.py',
function='my_failing_lambda_function',
timeout=1,
environment_variables='',
library=None,
version_name='',
arn_string=''
)
p = Process(target=lambda_run, args=(args,))
p.start()
p.join()
os.remove(request_file)
assert p.exitcode == 1
def test_check_command_line_nested_function_name():
request = json.dumps({})
request_file = 'check_command_line_event.json'
with open(request_file, "w") as f:
f.write(request)
args = argparse.Namespace(event=request_file,
file='tests/test_direct_invocations.py',
function='my_lambda_instance.my_lambda_method',
timeout=1,
environment_variables='',
library=None,
version_name='',
arn_string=''
)
p = Process(target=lambda_run, args=(args,))
p.start()
p.join()
os.remove(request_file)
assert p.exitcode == 0