Esempio n. 1
0
def test_invalid_queue(set_environment):
    with pytest.raises(exceptions.InvalidConfigurationError):
        process_event(
            event=None,
            context=MockContext(function_name=config("FUNCTION_NAME")),
            paths=None,
            queue_type="badqueue",
        )
Esempio n. 2
0
def test_fail_catastrophically(set_environment):
    def _fail(**kwargs):
        raise exceptions.FailCatastrophically()

    with pytest.raises(exceptions.FailCatastrophically):
        process_event(
            event=[{
                "foo": "bar"
            }],
            context=MockContext(function_name=config("FUNCTION_NAME")),
            paths={"FAIL": [Action(functions=[_fail])]},
            queue_type=QueueType.RAW,
            default_path="FAIL",
        )
Esempio n. 3
0
    def test_process_event_autogenerate_path(self, set_environment,
                                             fixture_name, fixture):
        from dummy_lambda.func.main import PATHS

        # Simulate a PATHS dictionary where the user didn't define and use an enumeration.
        _PATHS = {
            str(path).split(".")[-1]:
            [a.copy() if isinstance(a, Action) else a for a in actions]
            for path, actions in deepcopy(PATHS).items()
        }

        kwargs = {}
        if fixture.get("path", None):
            kwargs["default_path"] = fixture["path"]

        response = process_event(event=testing.raw_payload(fixture["payload"]),
                                 context=b3f.awslambda.MockContext(
                                     function_name=config("FUNCTION_NAME")),
                                 paths=_PATHS,
                                 queue_type=QueueType.RAW,
                                 debug=True,
                                 **kwargs)
        b3f.utils.emit_logs(response)
        for k, v in fixture["response"].items():
            assert response[k] == v
Esempio n. 4
0
def lambda_handler(event, context):
    return process_event(
        event=event,
        context=context,
        path_enum=Path,
        paths=PATHS,
        queue_type=QueueType.SQS,
        debug=True,
        logger=StubLogger(),
    )
Esempio n. 5
0
    def test_process_event_sqs(self, set_environment, fixture_name, fixture):
        from dummy_lambda.func.main import Path, PATHS

        response = process_event(
            event=sqs_payload(fixture["payload"]),
            context=MockContext(function_name=config("FUNCTION_NAME")),
            path_enum=Path,
            paths=PATHS,
            queue_type=QueueType.SQS,
            debug=True,
        )
        emit_logs(response)
        for k, v in fixture["response"].items():
            assert response[k] == v
Esempio n. 6
0
def test_returned_payload_execution_error(set_environment):
    from dummy_lambda.func.main import PATHS, Path

    with pytest.raises(exceptions.FailCatastrophically):
        response = process_event(
            event=[{
                "foo": "bar"
            }],
            context=b3f.awslambda.MockContext(
                function_name=config("FUNCTION_NAME")),
            path_enum=Path,
            paths=PATHS,
            event_source_type=EventSourceType.RAW,
            default_path=Path.TEST_RETURN_PAYLOAD_RAISES,
        )
        b3f.utils.emit_logs(response)
Esempio n. 7
0
    def test_process_event_fixed_function(self, set_environment, event):
        from dummy_lambda.func.main import test_func

        response = process_event(
            event=event["encoder"]([{
                "foo": "bar"
            }]),
            context=b3f.awslambda.MockContext(
                function_name=config("FUNCTION_NAME")),
            call=test_func,
            event_source_type=event["type"],
            debug=True,
        )
        b3f.utils.emit_logs(response)
        # cannibalizing a fixture for this one-off test
        fixture_response = {"stats": {"received": 1, "successes": 1}}
        for k, v in fixture_response.items():
            assert response[k] == v
Esempio n. 8
0
    def test_process_event(self, set_environment, fixture_name, fixture,
                           queue_name, queue):
        from dummy_lambda.func.main import PATHS, Path

        kwargs = {}
        if fixture.get("path", None):
            kwargs["default_path"] = fixture["path"]

        response = process_event(event=queue["encoder"](fixture["payload"]),
                                 context=b3f.awslambda.MockContext(
                                     function_name=config("FUNCTION_NAME")),
                                 path_enum=Path,
                                 paths=PATHS,
                                 queue_type=queue["type"],
                                 debug=True,
                                 exception_handler=exception_handler,
                                 **kwargs)
        b3f.utils.emit_logs(response)
        for k, v in fixture["response"].items():
            assert response[k] == v