示例#1
0
async def custom_app_call_list(app_connection: str,
                               *, event: str, datatype: Type[Something],
                               payload: EventPayload, context: EventContext,
                               **kwargs) -> List[Something]:
    if app_connection == "simple_example_conn" and event == "list_somethings":
        return [
            Something(id="test1", user=User(id="test", name="test")),
            Something(id="test2", user=User(id="test", name="test")),
        ]
    raise NotImplementedError("Test case not implemented in mock_app_call")
示例#2
0
async def test_it_something_generator_service(
        app_config, something_params_example):  # noqa: F811
    results = await execute_service(app_config=app_config,
                                    event_name='service.something_generator',
                                    max_events=2)
    assert results == [
        Something(id='id1',
                  user=User(id='user1', name='user1'),
                  status=None,
                  history=[]),
        Something(id='id2',
                  user=User(id='user2', name='user2'),
                  status=None,
                  history=[]),
    ]
示例#3
0
async def spawn_many_events(payload: Something,
                            context: EventContext) -> Spawn[Something]:
    """
    Produces 3 events to be published to stream
    """
    logger.info(context,
                "spawning event 3 times",
                extra=extra(something_id=payload.id))
    if payload.status:
        payload.history.append(payload.status)
    for i in range(3):
        payload.status = Status(ts=datetime.now(tz=timezone.utc),
                                type=StatusType.SUBMITTED)
        payload.id = str(i)
        yield payload
示例#4
0
def something_with_status_submitted_example():
    return Something(
        id="test",
        user=User(id="u1", name="test_user"),
        status=Status(ts=EVENT_TS, type=StatusType.SUBMITTED),
        history=[Status(ts=EVENT_TS, type=StatusType.NEW)]
    )
示例#5
0
def something_processed():
    return Something(
        id="test",
        user=User(id="u1", name="test_user"),
        status=Status(ts=EVENT_TS, type=StatusType.PROCESSED),
        history=[Status(ts=EVENT_TS, type=StatusType.SUBMITTED)]
    )
async def update_status_history(payload: Something,
                                context: EventContext) -> Something:
    if payload.status:
        payload.history.append(payload.status)
    payload.status = Status(ts=datetime.now(tz=timezone.utc),
                            type=StatusType.LOADED)
    return payload
示例#7
0
async def test_it_something_generator(app_config,
                                      something_params_example):  # noqa: F811
    result = await execute_event(app_config=app_config,
                                 event_name='service.something_generator',
                                 payload=something_params_example)
    assert result == Something(
        something_params_example.id,
        User(something_params_example.user, something_params_example.user))
async def create_something(payload: SomethingParams,
                           context: EventContext) -> Something:
    logger.info(context,
                "Creating something...",
                extra=extra(payload_id=payload.id, user=payload.user))
    result = Something(id=payload.id,
                       user=User(id=payload.user, name=payload.user))
    return result
示例#9
0
def stream_event(payload: Something, context: EventContext) -> Something:
    logger.info(context,
                "streaming event",
                extra=extra(something_id=payload.id))
    if payload.status:
        payload.history.append(payload.status)
    payload.status = Status(ts=datetime.now(tz=timezone.utc),
                            type=StatusType.SUBMITTED)
    return payload
示例#10
0
def something_with_status_processed_example():
    return Something(id="test",
                     user=User(id="u1", name="test_user"),
                     status=Status(ts=datetime.now(),
                                   type=StatusType.PROCESSED),
                     history=[
                         Status(ts=datetime.now(), type=StatusType.NEW),
                         Status(ts=datetime.now(), type=StatusType.SUBMITTED)
                     ])
async def fork_something(payload: Something, context: EventContext) -> Spawn[Union[FirstPart, SecondPart]]:
    """
    Produces 2 variants from payload to be processed in parallel
    """
    logger.info(context, "producing 2 variants of payload", extra=extra(something_id=payload.id))
    if payload.status:
        payload.history.append(payload.status)
    payload.status = Status(
        ts=datetime.now(tz=timezone.utc),
        type=StatusType.SUBMITTED
    )
    yield FirstPart(payload)
    yield SecondPart(payload)
示例#12
0
def update_status(payload: Something, context: EventContext) -> Something:
    """
    Updates status of payload to PROCESSED and puts previous status in history.

    :param payload: Something, object
    :param context: EventContext
    """
    logger.info(context,
                "updating something status",
                extra=extra(something_id=payload.id))

    if payload.status:
        payload.history.append(payload.status)
    payload.status = Status(ts=datetime.now(), type=StatusType.PROCESSED)
    return payload
async def create_items(payload: FileUploadInfo, context: EventContext, *,
                       something_id: str) -> List[Something]:
    """
    Create Something objects to be returned for each uploaded file
    """
    result = []
    for item in payload.uploaded_files:
        logger.info(context,
                    "Creating something from uploaded item...",
                    extra=extra(file_id=item.file_id,
                                user=payload.user,
                                something_id=something_id,
                                size=item.size))
        result.append(Something(id=item.file_id, user=payload.object.user))
    return result
示例#14
0
async def create(payload: None,
                 context: EventContext,
                 *,
                 item_id: str,
                 update_status: bool = False) -> Something:
    """
    Loads json file from filesystem as `Something` instance

    :param payload: unused
    :param context: EventContext
    :param item_id: str, item id to load
    :return: Loaded `Something` object or None if not found or validation fails

    """
    rnd_id = item_id + str(randrange(0, 1999))
    await asyncio.sleep(0.001)
    return Something(id=rnd_id, user=User(id=rnd_id, name=rnd_id))
示例#15
0
def something_upload_example():
    return Something(
        id='attachment',
        user=User(id='test', name='test_user')
    )
示例#16
0
def something_with_status_example():
    return Something(id="test",
                     user=User(id="u1", name="test_user"),
                     status=Status(ts=datetime.now(), type=StatusType.NEW))
示例#17
0
def something_example():
    return Something(
        id="test",
        user=User(id="u1", name="test_user")
    )
示例#18
0
def something_with_status_example():
    return Something(
        id="test",
        user=User(id="u1", name="test_user"),
        status=Status(ts=EVENT_TS, type=StatusType.NEW)
    )
示例#19
0
def something_submitted():
    return Something(id="test",
                     user=User(id="u1", name="test_user"),
                     status=Status(ts=datetime.now(),
                                   type=StatusType.SUBMITTED))
示例#20
0
def something_submitted():
    return Something(
        id="test",
        user=User(id="u1", name="test_user"),
        status=Status(ts=EVENT_TS, type=StatusType.SUBMITTED)
    )