def test_should_execute_successfully_a_empty_controller_with_correlation_id_as_only_input_parameter( ): logger = FakeLogger() @controller_handler(logger=logger) def my_controller(correlation_id: CorrelationId): return Success("Hello Petisco") http_response = my_controller() assert http_response == ({"message": "OK"}, 200) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] correlation_id = json.loads(first_logging_message[1])["correlation_id"] assert first_logging_message == ( INFO, LogMessageMother.get_controller(operation="my_controller", correlation_id=correlation_id, message="Start").to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_controller( operation="my_controller", correlation_id=correlation_id, message="Result[status: success | value: Hello Petisco]", ).to_json(), )
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_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 test_should_execute_successfully_a_filtered_object_by_blacklist(): logger = FakeLogger() @controller_handler(logger=logger) def my_controller(): return Success(b"This are bytes") http_response = my_controller() assert http_response == ({"message": "OK"}, 200) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_controller(operation="my_controller", message="Start").to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_controller( operation="my_controller", message="Success result of type: bytes").to_json(), )
def test_should_log_an_exception_occurred_on_the_controller(): logger = FakeLogger() @controller_handler(logger=logger) def my_controller(): raise RuntimeError("my_controller exception") http_response = my_controller() assert http_response == ( { "error": { "message": "Internal Error.", "type": "InternalHttpError" } }, 500, ) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_controller(operation="my_controller", message="Start").to_json(), ) assert second_logging_message[0] == ERROR assert "line" in second_logging_message[1] assert "RuntimeError" in second_logging_message[1] assert "my_controller exception" in second_logging_message[1]
def test_should_execute_successfully_a_empty_controller_with_jwt_requirement_with_user( given_any_token_type_with_user, given_any_decoded_token_info_with_user ): logger = FakeLogger() jwt_config = JwtConfig(token_type=given_any_token_type_with_user, require_user=True) @controller_handler(logger=logger, jwt_config=jwt_config) def my_controller(token_info, user_id): return Success("Hello Petisco") http_response = my_controller(token_info=given_any_decoded_token_info_with_user) assert http_response == ({"message": "OK"}, 200) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_controller( operation="my_controller", message="Start" ).to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_controller( operation="my_controller", message="Result[status: success | value: Hello Petisco]", ).to_json(), )
def test_should_log_successfully_an_error_raised_by_a_meiga_handler(): logger = FakeLogger() class UserNotFoundError(Error): pass @use_case_handler(logger=logger) class MyUseCase(UseCase): def execute(self): Failure(UserNotFoundError()).handle() return isSuccess result = MyUseCase().execute() assert_failure(result, value_is_instance_of=UserNotFoundError) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_use_case(operation="MyUseCase", message="Start").to_json(), ) assert second_logging_message == ( ERROR, LogMessageMother.get_use_case( operation="MyUseCase", message="Result[status: failure | value: UserNotFoundError] ", ).to_json(), )
def test_should_log_successfully_an_error_returned_on_a_use_case(): logger = FakeLogger() @use_case_handler(logger=logger) class MyUseCase(UseCase): def execute(self): return isFailure result = MyUseCase().execute() assert_failure(result, value_is_instance_of=Error) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_use_case(operation="MyUseCase", message="Start").to_json(), ) assert second_logging_message == ( ERROR, LogMessageMother.get_use_case( operation="MyUseCase", message="Result[status: failure | value: Error] " ).to_json(), )
def test_should_log_successfully_a_large_type_with_its_repr(): logger = FakeLogger() class BinaryInfo(NamedTuple): name: str data: bytes def __repr__(self) -> str: return f"<BinaryInfo {self.name}, len(data)={len(self.data)}>" @use_case_handler(logger=logger) class MyUseCase(UseCase): def execute(self): binary_info = BinaryInfo(name="my_data", data=b"This are bytes") return Success(binary_info) result = MyUseCase().execute() 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_use_case(operation="MyUseCase", message="Start").to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_use_case( operation="MyUseCase", message="<BinaryInfo my_data, len(data)=14>" ).to_json(), )
def test_should_log_successfully_a_filtered_object_by_blacklist_with_a_tuple(): logger = FakeLogger() @use_case_handler(logger=logger, logging_types_blacklist=[tuple]) class MyUseCase(UseCase): def execute(self): binary_info = ("my_data", b"This are bytes") return Success(binary_info) result = MyUseCase().execute() 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_use_case(operation="MyUseCase", message="Start").to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_use_case( operation="MyUseCase", message="Object of type: tuple" ).to_json(), )
def test_should_raise_a_type_error_when_a_command_do_not_receive_a_event(): logger = FakeLogger() @command_handler(logger=logger) def my_command(): return Success("Hello Petisco") with pytest.raises(TypeError): my_command()
def test_should_execute_with_a_failure_a_empty_controller_with_correlation_id_as_only_input_parameter( ): logger = FakeLogger() @controller_handler(logger=logger) def my_controller(correlation_id: CorrelationId): return isFailure http_response = my_controller() assert http_response == ( { "error": { "message": "Unknown Error", "type": "HttpError" } }, 500, ) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] correlation_id = json.loads(first_logging_message[1])["correlation_id"] assert first_logging_message == ( INFO, LogMessageMother.get_controller(operation="my_controller", correlation_id=correlation_id, message="Start").to_json(), ) assert second_logging_message == ( ERROR, LogMessageMother.get_controller( operation="my_controller", correlation_id=correlation_id, message="Result[status: failure | value: Error]", ).to_json(), )
def test_should_returns_an_error_when_a_empty_controller_get_a_required_jwt_token_but_missing_user( given_any_token_type, given_any_decoded_token_info ): logger = FakeLogger() jwt_config = JwtConfig(token_type=given_any_token_type, require_user=True) @controller_handler(logger=logger, jwt_config=jwt_config) def my_controller(token_info): return Success("Hello Petisco") http_response = my_controller(token_info=given_any_decoded_token_info) assert http_response == ( { "error": { "message": "Access token is missing or invalid. This entry point expects a valid TOKEN Token", "type": "InvalidTokenHttpError", } }, 401, ) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_controller( operation="my_controller", message="Start" ).to_json(), ) assert second_logging_message == ( ERROR, LogMessageMother.get_controller( operation="my_controller", message="Result[status: failure | value: InvalidTokenError]", ).to_json(), )
def test_should_log_successfully_a_non_error_use_case_with_input_parameters(): logger = FakeLogger() @use_case_handler( logger=logger, logging_parameters_whitelist=["client_id", "user_id"] ) class MyUseCase(UseCase): def execute(self, client_id: str, user_id: str): return Success("Hello Petisco") result = MyUseCase().execute(client_id="client_id", user_id="user_id") assert_success(result) first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] third_logging_message = logger.get_logging_messages()[2] assert first_logging_message == ( INFO, LogMessageMother.get_use_case(operation="MyUseCase", message="Start").to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_use_case( operation="MyUseCase", message={"client_id": "client_id", "user_id": "user_id"}, ).to_json(), ) assert third_logging_message == ( INFO, LogMessageMother.get_use_case( operation="MyUseCase", message="Hello Petisco" ).to_json(), )
def test_should_log_successfully_a_non_error_use_case_without_input_parameters_and_returning_a_string_result(): logger = FakeLogger() @use_case_handler(logger=logger) class MyUseCase(UseCase): def execute(self): return Success("Hello Petisco") MyUseCase().execute() first_logging_message = logger.get_logging_messages()[0] second_logging_message = logger.get_logging_messages()[1] assert first_logging_message == ( INFO, LogMessageMother.get_use_case(operation="MyUseCase", message="Start").to_json(), ) assert second_logging_message == ( INFO, LogMessageMother.get_use_case( operation="MyUseCase", message="Hello Petisco" ).to_json(), )