Skip to content

Commit

Permalink
[feat]: Adding handlers structure
Browse files Browse the repository at this point in the history
  • Loading branch information
hentt30 committed Jul 20, 2021
1 parent 7589157 commit 25c8c3c
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions minushalf/handlers/__init__.py
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
24 changes: 24 additions & 0 deletions minushalf/handlers/base_handler.py
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 ''
1 change: 1 addition & 0 deletions minushalf/interfaces/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
from .band_projection_file import BandProjectionFile
from .runner import Runner
from .correction import Correction
from .handler import Handler
19 changes: 19 additions & 0 deletions minushalf/interfaces/handler.py
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
49 changes: 49 additions & 0 deletions tests/unit/handlers/test_base_handler.py
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({})

0 comments on commit 25c8c3c

Please sign in to comment.