def test_custom_log_handler(context, capsys, mocker: MockerFixture):
    @scheduled(handler=logging.StreamHandler())
    def app(event, api, cache):
        Logger.info('Info message!')

    event = RawScheduledEvent(
        asset_id=0,
        interval=int(),
        schedule=int(),
        schedule_start=int(),
        app_connection=1,
        app_stream=int(),
        company=int(),
    )

    mocker.patch.object(RawScheduledEvent, 'set_schedule_as_completed')

    app(
        [[event.dict(
            by_alias=True,
            exclude_unset=True,
        )]],
        context,
    )

    captured = capsys.readouterr()

    assert captured.out.endswith('Info message!\n')
    assert captured.err == 'Info message!\n'
def test_log_if_unable_to_set_completed_status(context, mocker: MockerFixture,
                                               capsys):
    @scheduled
    def scheduled_app(event, api, state):
        pass

    event = [[
        RawScheduledEvent(
            asset_id=int(),
            interval=int(),
            schedule=int(),
            schedule_start=int(),
            app_connection=int(),
            app_stream=int(),
            company=int(),
        ).dict(
            by_alias=True,
            exclude_unset=True,
        )
    ]]

    patch = mocker.patch.object(RawScheduledEvent,
                                'set_schedule_as_completed',
                                side_effect=Exception)

    scheduled_app(event, context)

    captured = capsys.readouterr()

    assert 'ASSET=0 AC=0' in captured.out
    assert 'Could not set schedule as completed.' in captured.out
    patch.assert_called_once()
def test_set_completed_status_should_not_fail_lambda(context,
                                                     mocker: MockerFixture):
    @scheduled
    def scheduled_app(event, api, state):
        pass

    event = [[
        RawScheduledEvent(
            asset_id=int(),
            interval=int(),
            schedule=int(),
            schedule_start=int(),
            app_connection=int(),
            app_stream=int(),
            company=int(),
        ).dict(
            by_alias=True,
            exclude_unset=True,
        )
    ]]

    patch = mocker.patch.object(RawScheduledEvent,
                                'set_schedule_as_completed',
                                side_effect=Exception)

    scheduled_app(event, context)

    patch.assert_called_once()
def test_set_completed_status(context, requests_mock):
    @scheduled
    def scheduled_app(event, api, state):
        pass

    event = [[
        RawScheduledEvent(
            asset_id=int(),
            interval=int(),
            schedule=int(),
            schedule_start=int(),
            app_connection=int(),
            app_stream=int(),
            company=int(),
        ).dict(
            by_alias=True,
            exclude_unset=True,
        )
    ]]

    # patch post request, that sets scheduled task as completed
    # looks for url path like /scheduler/123/completed
    post_mock = requests_mock.post(re.compile(r'/scheduler/\d+/completed'))

    scheduled_app(event, context)

    assert post_mock.called_once
    assert post_mock.last_request.path == '/scheduler/0/completed'
def test_set_start_time(schedule_start, interval, expected, context,
                        mocker: MockerFixture):
    @scheduled
    def app(event, api, state):
        return event

    event = [[
        RawScheduledEvent(
            asset_id=int(),
            interval=interval,
            schedule=int(),
            schedule_start=schedule_start,
            app_connection=int(),
            app_stream=int(),
            company=int(),
        ).dict(
            by_alias=True,
            exclude_unset=True,
        )
    ]]

    mocker.patch.object(RawScheduledEvent, 'set_schedule_as_completed')

    result_event: ScheduledEvent = app(event, context)[0]

    assert result_event.start_time == expected
Exemple #6
0
def test_scheduled_logging(context, capsys, mocker: MockerFixture):
    @scheduled
    def app(event, api, cache):
        Logger.warning('Hello, World!')

    event = RawScheduledEvent(
        asset_id=0,
        interval=int(),
        schedule=int(),
        schedule_start=int(),
        app_connection=1,
        app_stream=int(),
        company=int(),
    )

    mocker.patch.object(RawScheduledEvent, 'set_schedule_as_completed')

    with freezegun.freeze_time(datetime.datetime(2021, 1, 2, 3, 4, 5, 678910)):
        app(
            [[event.dict(
                by_alias=True,
                exclude_unset=True,
            )]],
            context,
        )

    assert (
        capsys.readouterr().out ==
        f'2021-01-02T03:04:05.678Z {context.aws_request_id} WARNING '
        f'ASSET={event.asset_id} AC={event.app_connection_id} | Hello, World!\n'
    )
def test_event_parsing(is_dict, requests_mock: RequestsMocker, context):
    @scheduled
    def scheduled_app(event, api, state):
        pass

    event = RawScheduledEvent(
        asset_id=int(),
        interval=int(),
        schedule=int(),
        schedule_start=int(),
        app_connection=int(),
        app_stream=int(),
        company=int(),
    ).dict(
        by_alias=True,
        exclude_unset=True,
    )

    if not is_dict:
        event = [[event]]

    requests_mock.post(requests_mock_lib.ANY)

    scheduled_app(event, context)