Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for method on instance as function name with '.' separator syntax #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lambda_local/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,12 @@ def load_source(request_id, path, function_name):
else:
raise Exception("unsupported python version")

func = getattr(mod, function_name)
return func
object_names = function_name.split('.')
object = mod
for name in object_names:
object = getattr(object, name)

return object


def execute(func, event, context):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_direct_invocations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ 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))
Expand Down Expand Up @@ -86,3 +95,26 @@ def test_check_command_line_error():

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