Skip to content

Commit

Permalink
feat: Improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ffekirnew committed Jun 6, 2024
1 parent d0d9090 commit a6e4150
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 94 deletions.
23 changes: 13 additions & 10 deletions tests/rmediator/decorators/test_request.py
Original file line number Diff line number Diff line change
@@ -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
103 changes: 47 additions & 56 deletions tests/rmediator/decorators/test_request_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
53 changes: 25 additions & 28 deletions tests/rmediator/test_mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a6e4150

Please sign in to comment.