FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects

Add initial (trivial) pubsub module and faas module that supports pubsub

Merged E. Kirk requested to merge ek599-3-faas-and-pub-sub-terraform-module-boilerplate into main
4 files
+ 25
4
Compare changes
  • Side-by-side
  • Inline
Files
4
+ 46
34
from main import receive_event, say_hello
from unittest.mock import patch
from .main import example_cloud_event, example_raw_event
# Register the ucam_faas testing module as a pytest plugin - as it provides fixtures that we can
# make use of when testing the functions.
pytest_plugins = ["ucam_faas.testing"]
def test_say_hello():
def test_example_raw_event():
"""
Test the say_hello function using the undecorated version provided through the decorator
library interface.
Test the example_raw_event function using the undecorated version provided through the
decorator library interface.
This effectively tests the function 'raw', i.e. without any of the surrounding functions
framework code in place. This example should be the primary way most tests are run for event
functions, only in cases where the HTTP interaction is necessary should the app client be
used.
"""
assert say_hello.__wrapped__({}) == "hello world"
with patch("example.main.patch_me") as patched_patch_me:
assert example_raw_event.__wrapped__({}) is None
patched_patch_me.assert_called_once()
def test_client_say_hello(event_app_client):
def test_client_example_raw_event(event_app_test_client_factory):
"""
Test the say_hello function via a test HTTP client.
Test the example_raw_event function via a test HTTP client.
This tests the function is responding in a HTTP environment. This test support is provided
for verifying the function is registering as expected. Tests that can be written using the
'__wrapped__' interface should be preferred.
"""
eac = event_app_client(target="say_hello", source="example/main.py")
eac = event_app_test_client_factory(target="example_raw_event", source="example/main.py")
response = eac.get("/")
assert response.status_code == 200
def test_receive_event(capsys):
receive_event.__wrapped__({"event": "yes!"})
captured = capsys.readouterr()
assert captured.out == "Received event data: {'event': 'yes!'}\n"
def test_client_receive_event(capsys, event_app_client):
eac = event_app_client(target="receive_event", source="example/main.py")
eac.post(
"/",
json={
"specversion": "1.0",
"type": "example.com.cloud.event",
"source": "https://example.com/cloudevents/pull",
"subject": "123",
"id": "A234-1234-1234",
"time": "2018-04-05T17:31:00Z",
"data": {"event": "yes!"},
},
)
captured = capsys.readouterr()
assert captured.out == "Received event data: {'event': 'yes!'}\n"
def test_status_and_healthy(event_app_client):
eac = event_app_client(target="say_hello", source="example/main.py")
def test_example_cloud_event():
with patch("example.main.patch_me") as patched_patch_me:
example_cloud_event.__wrapped__({"event": "yes!"})
patched_patch_me.assert_called_once_with({"event": "yes!"})
def test_client_example_cloud_event(event_app_test_client_factory):
eac = event_app_test_client_factory(target="example_cloud_event", source="example/main.py")
# NOTE: When patching using event_app_test_client_factory, the module path will be relative
# to the source file
with patch("main.patch_me") as patched_patch_me:
eac.post(
"/",
json={
"specversion": "1.0",
"type": "example.com.cloud.event",
"source": "https://example.com/cloudevents/pull",
"subject": "123",
"id": "A234-1234-1234",
"time": "2018-04-05T17:31:00Z",
"data": {"event": "yes!"},
},
)
patched_patch_me.assert_called_once()
assert patched_patch_me.call_args.args[0] == {"event": "yes!"}
def test_status_and_healthy(event_app_test_client_factory):
eac = event_app_test_client_factory(target="example_raw_event", source="example/main.py")
response = eac.get("/healthy")
assert response.status_code == 200
assert response.text == "ok"
Loading