mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Add utility decorator for registering backend dispatches based on modules
This commit is contained in:
parent
2eb83b9993
commit
daa0fe1f51
20
bigchaindb/backend/util.py
Normal file
20
bigchaindb/backend/util.py
Normal file
@ -0,0 +1,20 @@
|
||||
class BackendModuleDispatchRegisterError(Exception):
|
||||
"""Raised when there is a problem registering dispatched functions for a
|
||||
backend module"""
|
||||
|
||||
|
||||
def make_module_dispatch_registrar(module):
|
||||
def dispatch_wrapper(obj_type):
|
||||
def wrapper(func):
|
||||
func_name = func.__name__
|
||||
try:
|
||||
dispatch_registrar = getattr(module, func_name)
|
||||
return dispatch_registrar.register(obj_type)(func)
|
||||
except AttributeError as ex:
|
||||
raise BackendModuleDispatchRegisterError(
|
||||
("'{module}' does not contain a single-dispatchable "
|
||||
"function named '{func}'. The backend being used has not "
|
||||
"been implemented correctly!").format(
|
||||
func=func_name, module=module.__name__)) from ex
|
||||
return wrapper
|
||||
return dispatch_wrapper
|
73
tests/backend/test_util.py
Normal file
73
tests/backend/test_util.py
Normal file
@ -0,0 +1,73 @@
|
||||
import pytest
|
||||
|
||||
from types import ModuleType
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def mock_module():
|
||||
return ModuleType('mock_module')
|
||||
|
||||
|
||||
def test_module_dispatch_registers(mock_module):
|
||||
from functools import singledispatch
|
||||
from bigchaindb.backend.utils import make_module_dispatch_registrar
|
||||
|
||||
@singledispatch
|
||||
def dispatcher(t):
|
||||
pass
|
||||
mock_module.dispatched = dispatcher
|
||||
mock_dispatch = make_module_dispatch_registrar(mock_module)
|
||||
|
||||
@mock_dispatch(str)
|
||||
def dispatched(t):
|
||||
pass
|
||||
|
||||
assert mock_module.dispatched.registry[str] == dispatched
|
||||
|
||||
|
||||
def test_module_dispatch_dispatches(mock_module):
|
||||
from functools import singledispatch
|
||||
from bigchaindb.backend.utils import make_module_dispatch_registrar
|
||||
|
||||
@singledispatch
|
||||
def dispatcher(t):
|
||||
return False
|
||||
mock_module.dispatched = dispatcher
|
||||
mock_dispatch = make_module_dispatch_registrar(mock_module)
|
||||
|
||||
@mock_dispatch(str)
|
||||
def dispatched(t):
|
||||
return True
|
||||
|
||||
assert mock_module.dispatched(1) is False # Goes to dispatcher()
|
||||
assert mock_module.dispatched('1') is True # Goes to dispatched()
|
||||
|
||||
|
||||
def test_module_dispatch_errors_on_missing_func(mock_module):
|
||||
from bigchaindb.backend.utils import (
|
||||
make_module_dispatch_registrar,
|
||||
BackendModuleDispatchRegisterError,
|
||||
)
|
||||
mock_dispatch = make_module_dispatch_registrar(mock_module)
|
||||
|
||||
with pytest.raises(BackendModuleDispatchRegisterError):
|
||||
@mock_dispatch(str)
|
||||
def dispatched():
|
||||
pass
|
||||
|
||||
|
||||
def test_module_dispatch_errors_on_non_dispatchable_func(mock_module):
|
||||
from bigchaindb.backend.utils import (
|
||||
make_module_dispatch_registrar,
|
||||
BackendModuleDispatchRegisterError,
|
||||
)
|
||||
|
||||
def dispatcher():
|
||||
pass
|
||||
mock_module.dispatched = dispatcher
|
||||
mock_dispatch = make_module_dispatch_registrar(mock_module)
|
||||
|
||||
with pytest.raises(BackendModuleDispatchRegisterError):
|
||||
@mock_dispatch(str)
|
||||
def dispatched():
|
||||
pass
|
Loading…
x
Reference in New Issue
Block a user