def test_should_return_failure_result_when_an_exception_raises_within_command( given_any_correlation_id): logger = FakeLogger() @command_handler(logger=logger) def my_command(event: Event): raise RuntimeError("my_command exception") event = Event({"correlation_id": given_any_correlation_id}) result = my_command(event=event) assert_failure(result) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_command( operation="my_command", message=f"Event received: {event}", correlation_id=given_any_correlation_id, ).to_json(), ) assert "line" in second_logging_message[1] assert "RuntimeError" in second_logging_message[1] assert "my_command exception" in second_logging_message[1]
def test_should_load_json_when_there_is_not_attribute( given_json_event_data_without_attributes ): event = Event.from_json(given_json_event_data_without_attributes) assert not event.to_dict()["data"]["attributes"]
def test_should_execute_successfully_a_empty_command_without_input_parameters( given_any_correlation_id): logger = FakeLogger() @command_handler(logger=logger) def my_command(event: Event): return Success("Hello Petisco") event = Event({"correlation_id": given_any_correlation_id}) result = my_command(event=event) assert_success(result) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_command( operation="my_command", message=f"Event received: {event}", correlation_id=given_any_correlation_id, ).to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_command( operation="my_command", message="Result[status: success | value: Hello Petisco]", correlation_id=given_any_correlation_id, ).to_json(), )
def callback(ch, method, properties, body): event = Event.from_json(body) global received_events received_events.append(event) if isinstance(event, make_user_created_event().__class__): assert event.user_id == "user_id_1" ch.basic_ack(delivery_tag=method.delivery_tag)
def retrieve_all(self) -> Result[Events, Error]: with self.session_scope() as session: event_models = session.query(self.EventModel).all() events: Events = [] if event_models: for event_model in event_models: events.append(Event.from_json(event_model.data)) return Success(events)
def test_should_load_an_event_agnostically(given_any_info_id, given_any_name): event = UserCreated(name=given_any_name) event.add_info_id(given_any_info_id) event_json = event.to_json() agnostic_event = Event.from_json(event_json) assert event == agnostic_event
def retrieve(self, event_id: EventId) -> Result[Event, Error]: with self.session_scope() as session: event_model = (session.query(self.EventModel).filter( self.EventModel.event_id == event_id.value).first()) if not event_model: return Failure(EventNotFoundError(event_id)) event = Event.from_json(event_model.data) return Success(event)
def test_should_update_meta_on_an_event_with_info_id(given_any_info_id, given_any_name): event = UserCreated(name=given_any_name) event.add_info_id(given_any_info_id) event.update_meta({"device": {"platform": "ios", "model": "x", "version": "14.3"}}) event_json = event.to_json() agnostic_event = Event.from_json(event_json) assert event == agnostic_event
def test_should_load_an_event_agnostically_with_non_default_version_event( given_any_info_id, given_any_name ): event = VersionedUserCreated(name=given_any_name) event.add_info_id(given_any_info_id) event_json = event.to_json() agnostic_event = Event.from_json(event_json) assert event == agnostic_event
def save(self, event_id: EventId, event: Event) -> Result[bool, Error]: with self.session_scope() as session: event_model = (session.query(self.EventModel).filter( self.EventModel.event_id == event_id.value).first()) if event_model: return Failure(EventAlreadyExistError(event_id)) event_model = self.EventModel(event_id=event_id.value, type=event.event_name, data=event.to_json()) session.add(event_model) return isSuccess
def test_should_update_event_with_info_id_without_deleting_meta_info( given_any_info_id, given_any_event_dict_with_meta, given_any_meta_dict ): event = Event.from_dict(given_any_event_dict_with_meta) assert event.event_meta == given_any_meta_dict event.add_info_id(given_any_info_id) expected_event_dict = {"info_id": given_any_info_id.to_dict()} expected_event_dict.update(given_any_meta_dict) assert event.to_dict()["data"]["meta"] == expected_event_dict
def assert_recorded_events(base_url): wait_for_event() response_events = requests.get(f"{base_url}/events") assert response_events.status_code == 200 events = response_events.json().get("events") event_names = [Event.from_dict(event).event_name for event in events] expected_event_names = [ "service.deployed", "task.created", "task.retrieved", "task.created", "task.removed", ] assert sorted(expected_event_names) == sorted(event_names)