diff --git a/tests/rmediator/decorators/test_request.py b/tests/rmediator/decorators/test_request.py index 52dcd5d..5f445ba 100644 --- a/tests/rmediator/decorators/test_request.py +++ b/tests/rmediator/decorators/test_request.py @@ -1,18 +1,21 @@ -import pytest +from types import FunctionType from rmediator.decorators import request class TestRequest: - def test__request__invalid_no_response__raises_value_error(self): - with pytest.raises(AttributeError): - - @request - class InvalidRequest: - pass + def test__request__invalid_registration_no_response__raises_value_error(self): + @request + class InvalidRequest: + pass - def test__request__valid(self): + assert type(InvalidRequest) is FunctionType + assert type(InvalidRequest) is not type - @request + def test__request__valid_registration__successful(self): + @request(str) class ValidRequest: - response = str + pass + + assert ValidRequest._response is str + assert hasattr(ValidRequest, "_rmediator_decorated_request") is True diff --git a/tests/rmediator/decorators/test_request_handler.py b/tests/rmediator/decorators/test_request_handler.py index d613dee..50024be 100644 --- a/tests/rmediator/decorators/test_request_handler.py +++ b/tests/rmediator/decorators/test_request_handler.py @@ -4,96 +4,87 @@ class TestRequestHandler: - def test__request_handler__no_request_attribute__raises_value_error(self): + def test__request_handler__no_handle_method__raises_value_error( + self, get_valid_request, get_response + ): with pytest.raises(AttributeError): - @request_handler + @request_handler(get_valid_request, get_response) class InvalidRequestHandler: pass - def test__request_handler__no_response_attribute__raises_value_error( - self, get_valid_request + def test__request_handler__request_type_mismatch__raises_value_error( + self, get_valid_request, get_response ): - request_class = get_valid_request - with pytest.raises(AttributeError): - @request_handler + @request_handler(get_valid_request, get_response) class InvalidRequestHandler: - request = request_class + def handle(self, request: get_valid_request.response) -> None: + pass - def test__request_handler__no_handle_method__raises_value_error( - self, get_valid_request + def test__request_handler__response_type_mismatch__raises_value_error( + self, get_valid_request, get_response ): - request_class, response_class = get_valid_request, get_valid_request.response - - with pytest.raises(AttributeError): + with pytest.raises(ValueError): - @request_handler + @request_handler(get_valid_request, get_response) class InvalidRequestHandler: - request = request_class - response = response_class - - def test__request_handler__request_type_mismatch__raises_value_error( - self, get_valid_request - ): - request_class, response_class = get_valid_request, get_valid_request.response + def handle(self, request: get_valid_request) -> get_valid_request: + pass - with pytest.raises(ValueError): + def test__request_handler__none_type_request__raises_value_error(self): + with pytest.raises(AttributeError): - @request_handler + @request_handler(None, str) # type: ignore class InvalidRequestHandler: - request = request_class - response = response_class + def handle(self, request: None) -> str: + return "" - def handle(self, request: get_valid_request.response): - pass + def test__request_handler__valid_handler__successful( + self, get_valid_request, get_response + ): + @request_handler(get_valid_request, get_response) + class ValidRequestHandler: + def handle(self, request: get_valid_request) -> get_response: + pass - def test__request_handler__response_type_mismatch__raises_value_error( - self, get_valid_request + def test__request_handler__valid_handler_different_request_parameter_name__successful( + self, get_valid_request, get_response ): - request_class, response_class = get_valid_request, get_valid_request.response + @request_handler(get_valid_request, get_response) + class ValidRequestHandler: + def handle(self, some_request: get_valid_request) -> get_response: + pass + def test__request_handler__invalid_handler_request_parameter_wrong_type__successful( + self, get_valid_request, get_response + ): with pytest.raises(ValueError): - @request_handler + @request_handler(get_valid_request, get_response) class InvalidRequestHandler: - request = request_class - response = response_class - - def handle(self, request: get_valid_request) -> get_valid_request: + def handle(self, some_request: str) -> get_response: pass - def test__request_handler__none_type_request__raises_value_error( - self, + def test__request_handler__invalid_handler_multiple_parameters_in_handle__successful( + self, get_valid_request, get_response ): with pytest.raises(AttributeError): - @request_handler + @request_handler(get_valid_request, get_response) class InvalidRequestHandler: - request = None - response = str - - def handle(self, request: None) -> str: - return "" - - def test__request_handler__valid_handler__successful(self, get_valid_request): - request_class, response_class = get_valid_request, get_valid_request.response - - @request_handler - class InvalidRequestHandler: - request = request_class - response = response_class - - def handle(self, request: get_valid_request) -> get_valid_request.response: - pass + def handle( + self, request: get_valid_request, additonal: int + ) -> get_response: + pass @pytest.fixture def get_valid_request(get_response): - @request + @request(get_response) class Request: - response = get_response + pass return Request diff --git a/tests/rmediator/test_mediator.py b/tests/rmediator/test_mediator.py index ec4c78a..b52590c 100644 --- a/tests/rmediator/test_mediator.py +++ b/tests/rmediator/test_mediator.py @@ -20,7 +20,17 @@ def test__mediator__valid_registration_valid_handling__successful( ) response = mediator.send(get_valid_request()) - assert type(response) is get_valid_request.response + assert type(response) is get_valid_request._response + + def test__mediator__valid_duplicate_handlers_registration__successful( + self, mediator, get_valid_request, get_valid_request_handler_first_type + ): + mediator.register_handler( + get_valid_request, get_valid_request_handler_first_type() + ) + mediator.register_handler( + get_valid_request, get_valid_request_handler_first_type() + ) def test__mediator__undecorated_request__raises_value_error( self, mediator, get_undecorated_request, get_valid_request_handler_first_type @@ -44,16 +54,6 @@ def test__mediator__invalid_types_given_to_register_funciton__raises_value_error with pytest.raises(ValueError): mediator.register_handler(str, bool) - def test__mediator__valid_duplicate_handlers_registration__successful( - self, mediator, get_valid_request, get_valid_request_handler_first_type - ): - mediator.register_handler( - get_valid_request, get_valid_request_handler_first_type() - ) - mediator.register_handler( - get_valid_request, get_valid_request_handler_first_type() - ) - def test__mediator__invalid_same_request_different_handlers_registration__raises_value_error( self, mediator, @@ -105,11 +105,19 @@ def mediator(): return Mediator() +@pytest.fixture +def get_response(): + class Response: + pass + + return Response + + @pytest.fixture def get_valid_request(get_response): - @request + @request(get_response) class Request: - response = get_response + pass return Request @@ -122,20 +130,11 @@ class Request: return Request -@pytest.fixture -def get_response(): - class Response: - pass - - return Response - - @pytest.fixture def get_valid_request_handler_first_type(get_valid_request, get_response): - @request_handler + @request_handler(get_valid_request, get_response) class RequestHandler: - request = get_valid_request - response = get_response + pass def handle(self, request: get_valid_request) -> get_response: return get_response() @@ -169,7 +168,7 @@ def handle(self, request: None) -> get_response: @pytest.fixture def get_valid_request_handler_second_type(get_valid_request, get_response): - @request_handler + @request_handler(get_valid_request, get_response) class RequestHandler: request = get_valid_request response = get_response @@ -182,10 +181,8 @@ def handle(self, request: get_valid_request) -> get_response: @pytest.fixture def get_valid_request_handler_third_type(get_valid_request, get_response): - @request_handler + @request_handler(get_valid_request, None) class RequestHandler: - request = get_valid_request - response = None def handle(self, request: get_valid_request) -> None: pass