-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
""" | ||
handlers to process the data passed by the user in the CLI | ||
""" | ||
from .base_handler import BaseHandler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
""" | ||
Base class to all Handlers | ||
""" | ||
from minushalf.interfaces import Handler | ||
|
||
|
||
class BaseHandler(Handler): | ||
""" | ||
The default chaining behavior can be implemented inside a base handler | ||
class. | ||
""" | ||
|
||
_next_handler: Handler = None | ||
|
||
def set_next(self, handler: Handler) -> Handler: | ||
self._next_handler = handler | ||
# Returning a handler from here will let us link handlers in a useful way | ||
return handler | ||
|
||
def handle(self, request: any) -> str: | ||
if self._next_handler: | ||
return self._next_handler.handle(request) | ||
|
||
return '' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
""" | ||
Interface for the handler class | ||
""" | ||
from abc import ABC, abstractmethod | ||
from typing import Optional | ||
|
||
|
||
class Handler(ABC): | ||
""" | ||
The Handler interface declares a method for building the chain of handlers. | ||
It also declares a method for executing a request. | ||
""" | ||
@abstractmethod | ||
def set_next(self, handler: any) -> any: | ||
pass | ||
|
||
@abstractmethod | ||
def handle(self, request) -> Optional[str]: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
""" | ||
Test base handler class | ||
""" | ||
from minushalf.handlers import BaseHandler | ||
from pytest_mock import MockerFixture | ||
|
||
|
||
def text_set_next(): | ||
""" | ||
Test function that sets the next handler | ||
""" | ||
base_handler = BaseHandler() | ||
next_handler = BaseHandler() | ||
|
||
res = base_handler.set_next(next_handler) | ||
|
||
assert base_handler._next_handler == next_handler | ||
assert res == next_handler | ||
|
||
|
||
def test_handle_when_next_handler_not_defined(): | ||
""" | ||
Test function that activates the next handler when | ||
next handler isn't defined | ||
""" | ||
base_handler = BaseHandler() | ||
|
||
assert base_handler.handle({}) == '' | ||
|
||
|
||
def test_handle_when_next_handler_is_defined(mocker: MockerFixture): | ||
""" | ||
Test function that activates the next handler when | ||
next handler is defined | ||
""" | ||
base_handler = BaseHandler() | ||
next_handler = BaseHandler() | ||
|
||
## mock and spy next handler | ||
mocker.patch.object(next_handler, | ||
'handle', | ||
return_value='Test', | ||
autospec=True) | ||
spy = mocker.spy(next_handler, 'handle') | ||
|
||
base_handler.set_next(next_handler) | ||
|
||
assert base_handler.handle({}) == 'Test' | ||
assert spy.assert_called_once_with({}) |