Skip to content

Add unittests for generic bindings and durable functions #624

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

Merged
merged 3 commits into from
Feb 29, 2020
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scriptFile": "main.py",
"bindings": [
{
"type": "activityTrigger",
"name": "input",
"direction": "in"
}
]
}
2 changes: 2 additions & 0 deletions tests/unittests/durable_functions/activity_trigger/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main(input: str) -> str:
return input
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scriptFile": "main.py",
"bindings": [
{
"type": "activityTrigger",
"name": "input",
"direction": "in"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main(input):
return input
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scriptFile": "main.py",
"bindings": [
{
"type": "orchestrationTrigger",
"name": "context",
"direction": "in"
}
]
}
13 changes: 13 additions & 0 deletions tests/unittests/durable_functions/orchestration_trigger/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# import azure.durable_functions as df


def generator_function(context):
final_result = yield context.call_activity('activity_trigger', 'foobar')
return final_result


def main(context):
# orchestrate = df.Orchestrator.create(generator_function)
# result = orchestrate(context)
# return result
return f'{context} :)'
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"scriptFile": "main.py",
"bindings": [
{
"type": "foobar",
"name": "input",
"direction": "in",
"dataType": "binary"
},
{
"direction": "out",
"name": "$return",
"type": "foobar",
"dataType": "binary"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Input as bytes, without annotation
def main(input):
return input
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"scriptFile": "main.py",
"bindings": [
{
"type": "foobar",
"name": "input",
"direction": "in",
"dataType": "string"
},
{
"direction": "out",
"name": "$return",
"type": "foobar",
"dataType": "string"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Input as string, without annotation
def main(input):
return input
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"scriptFile": "main.py",
"bindings": [
{
"type": "foobar",
"name": "input",
"direction": "in",
"dataType": "string"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Input as string, without annotation
def main(input: str):
return input
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"scriptFile": "main.py",
"bindings": [
{
"type": "foobar",
"name": "input",
"direction": "in"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def main(input: str) -> str:
return input
87 changes: 87 additions & 0 deletions tests/unittests/test_mock_durable_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from azure_functions_worker import protos
from azure_functions_worker import testutils


class TestDurableFunctions(testutils.AsyncTestCase):
durable_functions_dir = testutils.UNIT_TESTS_FOLDER / 'durable_functions'

async def test_mock_activity_trigger(self):
async with testutils.start_mockhost(
script_root=self.durable_functions_dir) as host:

func_id, r = await host.load_function('activity_trigger')

self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'activity_trigger', [
protos.ParameterBinding(
name='input',
data=protos.TypedData(
string='test'
)
)
]
)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)
self.assertEqual(
r.response.return_value,
protos.TypedData(string='test')
)

async def test_mock_activity_trigger_no_anno(self):
async with testutils.start_mockhost(
script_root=self.durable_functions_dir) as host:

func_id, r = await host.load_function('activity_trigger_no_anno')

self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'activity_trigger_no_anno', [
protos.ParameterBinding(
name='input',
data=protos.TypedData(
bytes=b'\x34\x93\x04\x70'
)
)
]
)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)
self.assertEqual(
r.response.return_value,
protos.TypedData(bytes=b'\x34\x93\x04\x70')
)

async def test_mock_orchestration_trigger(self):
async with testutils.start_mockhost(
script_root=self.durable_functions_dir) as host:

func_id, r = await host.load_function('orchestration_trigger')

self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'orchestration_trigger', [
protos.ParameterBinding(
name='context',
data=protos.TypedData(
string='Durable functions coming soon'
)
)
]
)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)
self.assertEqual(
r.response.return_value,
protos.TypedData(string='Durable functions coming soon :)')
)
104 changes: 104 additions & 0 deletions tests/unittests/test_mock_generic_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,107 @@ async def test_mock_generic_as_bytes(self):
r.response.return_value,
protos.TypedData(bytes=b'\x00\x01')
)

async def test_mock_generic_as_str_no_anno(self):
async with testutils.start_mockhost(
script_root=self.generic_funcs_dir) as host:

func_id, r = await host.load_function('foobar_as_str_no_anno')

self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'foobar_as_str_no_anno', [
protos.ParameterBinding(
name='input',
data=protos.TypedData(
string='test'
)
)
]
)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)
self.assertEqual(
r.response.return_value,
protos.TypedData(string='test')
)

async def test_mock_generic_as_bytes_no_anno(self):
async with testutils.start_mockhost(
script_root=self.generic_funcs_dir) as host:

func_id, r = await host.load_function('foobar_as_bytes_no_anno')

self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'foobar_as_bytes_no_anno', [
protos.ParameterBinding(
name='input',
data=protos.TypedData(
bytes=b'\x00\x01'
)
)
]
)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)
self.assertEqual(
r.response.return_value,
protos.TypedData(bytes=b'\x00\x01')
)

async def test_mock_generic_should_not_support_implicit_output(self):
async with testutils.start_mockhost(
script_root=self.generic_funcs_dir) as host:

func_id, r = await host.load_function('foobar_implicit_output')

self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'foobar_as_bytes_no_anno', [
protos.ParameterBinding(
name='input',
data=protos.TypedData(
bytes=b'\x00\x01'
)
)
]
)
# It should fail here, since generic binding requires
# $return statement in function.json to pass output
self.assertEqual(r.response.result.status,
protos.StatusResult.Failure)

async def test_mock_generic_should_support_without_datatype(self):
async with testutils.start_mockhost(
script_root=self.generic_funcs_dir) as host:

func_id, r = await host.load_function('foobar_with_no_datatype')

self.assertEqual(r.response.function_id, func_id)
self.assertEqual(r.response.result.status,
protos.StatusResult.Success)

_, r = await host.invoke_function(
'foobar_with_no_datatype', [
protos.ParameterBinding(
name='input',
data=protos.TypedData(
bytes=b'\x00\x01'
)
)
]
)
# It should fail here, since the generic binding requires datatype
# to be defined in function.json
self.assertEqual(r.response.result.status,
protos.StatusResult.Failure)