Skip to content

Commit

Permalink
feat: Adding ability to invoke functions directly (Azure#238)
Browse files Browse the repository at this point in the history
* Adding new direct calling tests

* Adding comments and improving tests
  • Loading branch information
vrdmr authored Aug 19, 2024
1 parent b620229 commit 3873bab
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
26 changes: 22 additions & 4 deletions azure/functions/decorators/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ def __init__(self, func: Callable[..., Any], script_file: str):
self.http_type = 'function'
self._is_http_function = False

def __str__(self):
"""Return the function.json representation of the function"""
return self.get_function_json()

def __call__(self, *args, **kwargs):
"""This would allow the Function object to be directly callable and runnable
directly using the interpreter locally.
Example:
@app.route(route="http_trigger")
def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
return "Hello, World!"
print(http_trigger(None))
➜ python function_app.py
Hello, World!
"""
return self._func(*args, **kwargs)

def add_binding(self, binding: Binding) -> None:
"""Add a binding instance to the function.
Expand Down Expand Up @@ -201,17 +221,15 @@ def get_function_json(self) -> str:
"""
return json.dumps(self.get_dict_repr(), cls=StringifyEnumJsonEncoder)

def __str__(self):
return self.get_function_json()


class FunctionBuilder(object):

def __init__(self, func, function_script_file):
self._function = Function(func, function_script_file)

def __call__(self, *args, **kwargs):
pass
"""Call the Function object directly"""
return self._function(*args, **kwargs)

def configure_http_type(self, http_type: str) -> 'FunctionBuilder':
self._function.set_http_type(http_type)
Expand Down
16 changes: 16 additions & 0 deletions tests/decorators/test_function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,22 @@ def test_blueprint_same_function_and_method_name(name: str):
" Please change @app.function_name() or the function"
" method name to be unique.")

def test_user_function_is_directly_callable_no_args(self):
def test_validate_function_working_no_args():
return "dummy"

self.dummy = test_validate_function_working_no_args
self.fb = FunctionBuilder(self.dummy, "dummy.py")
self.assertEqual(self.fb(), "dummy")

def test_user_function_is_directly_callable_args(self):
def test_validate_function_working_sum_args(arg1: int, arg2: int):
return arg1 + arg2

self.dummy = test_validate_function_working_sum_args
self.fb = FunctionBuilder(self.dummy, "dummy.py")
self.assertEqual(self.fb(1, 2), 3)


class TestScaffold(unittest.TestCase):
def setUp(self):
Expand Down

0 comments on commit 3873bab

Please sign in to comment.