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")
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=[]), ]
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
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)] )
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
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
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
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)
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
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))
def something_upload_example(): return Something( id='attachment', user=User(id='test', name='test_user') )
def something_with_status_example(): return Something(id="test", user=User(id="u1", name="test_user"), status=Status(ts=datetime.now(), type=StatusType.NEW))
def something_example(): return Something( id="test", user=User(id="u1", name="test_user") )
def something_with_status_example(): return Something( id="test", user=User(id="u1", name="test_user"), status=Status(ts=EVENT_TS, type=StatusType.NEW) )
def something_submitted(): return Something(id="test", user=User(id="u1", name="test_user"), status=Status(ts=datetime.now(), type=StatusType.SUBMITTED))
def something_submitted(): return Something( id="test", user=User(id="u1", name="test_user"), status=Status(ts=EVENT_TS, type=StatusType.SUBMITTED) )