FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
test.py 2.29 KiB
Newer Older
Mike Knee's avatar
Mike Knee committed
from main import receive_event, say_hello
Mike Knee's avatar
Mike Knee committed

# 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():
    """
    Test the say_hello 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"


Mike Knee's avatar
Mike Knee committed
def test_client_say_hello(event_app_client):
Mike Knee's avatar
Mike Knee committed
    """
    Test the say_hello 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")
    response = eac.get("/")
    assert response.status_code == 200
Mike Knee's avatar
Mike Knee committed


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")
    response = eac.get("/healthy")
    assert response.status_code == 200
    assert response.text == "ok"

    response = eac.get("/status")
    assert response.status_code == 200
    assert response.text == "ok"