From ac19dd7d6bac93a3eb070d8c1c216935d6c19d1c Mon Sep 17 00:00:00 2001 From: Abhishek_Dutta3 Date: Wed, 16 Feb 2022 13:48:56 +0530 Subject: [PATCH 1/2] fixed imports in README.md --- README.md | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 767bb3f..a425dbb 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # mediatr_py [![PyPI](https://img.shields.io/pypi/v/mediatr)](https://pypi.org/project/mediatr) -[![Python](https://img.shields.io/pypi/pyversions/mediatr)](https://pypi.org/project/mediatr) -[![Downloads](https://img.shields.io/pypi/dm/mediatr)](https://pypi.org/project/mediatr) +[![Python](https://img.shields.io/pypi/pyversions/mediatr)](https://pypi.org/project/mediatr) +[![Downloads](https://img.shields.io/pypi/dm/mediatr)](https://pypi.org/project/mediatr) Buy Me A Coffee @@ -11,7 +11,8 @@ This is an async implementation of Mediator pattern with pipline behaviors. It is a port of [Mediatr](https://github.com/jbogard/MediatR) from .Net C# Requirements: -* Python >= 3.6 + +- Python >= 3.6 ## Usage: @@ -32,7 +33,7 @@ class GetArrayQuery(): ### Define your handler class or function ```py -import Mediator from mediatr +from mediatr import Mediator @Mediator.handler async def get_array_handler(request:GetArrayQuery): @@ -40,9 +41,9 @@ async def get_array_handler(request:GetArrayQuery): for i in range(0, request.items_count): items.append(i) return items - + # or just Mediator.register_handler(get_array_handler) - + ``` or class: @@ -55,14 +56,14 @@ class GetArrayQueryHandler(): for i in range(0, request.items_count): items.append(i) return items - + # or just Mediator.register_handler(GetArrayQueryHandler) ``` ### Run mediator ```py -import Mediator from mediatr +from mediatr import Mediator mediator = Mediator() @@ -80,11 +81,10 @@ print(result) // [0,1,2,3,4] > > In another case use `asyncio` module for manual manage of event loop in synchronous code - ### Run mediator statically, without instance ```py -import Mediator from mediatr +from mediatr import Mediator request = GetArrayQuery(5) @@ -97,10 +97,10 @@ print(result) // [0,1,2,3,4] ``` Note that instantiation of `Mediator(handler_class_manager = my_manager_func)` is useful if you have custom handlers creation. For example using an injector. -By default class handlers are instantiated with simple init: `SomeRequestHandler()`. handlers or behaviors as functions are executed directly. - +By default class handlers are instantiated with simple init: `SomeRequestHandler()`. handlers or behaviors as functions are executed directly. ## Using behaviors + You can define behavior class with method 'handle' or function: ```py @@ -137,12 +137,11 @@ def default_handler_class_manager(HandlerCls:type,is_behavior:bool=False): ``` - For example, if you want to instantiate them with dependency injector or custom, pass your own factory function to Mediator: ```py def my_class_handler_manager(handler_class, is_behavior=False): - + if is_behavior: # custom logic pass @@ -152,12 +151,11 @@ def my_class_handler_manager(handler_class, is_behavior=False): mediator = Mediator(handler_class_manager=my_class_handler_manager) ``` -PS: +PS: The `next` function in behavior is `async`, so if you want to take results or if your behavior is async, use `middle_results = await next()` - Handler may be async too, if you need. ## Using with generic typing support (version >= 1.2): From 61c664f2d294f66d45c10d4a07a0e8a9b567e586 Mon Sep 17 00:00:00 2001 From: Abhishek_Dutta3 Date: Wed, 16 Feb 2022 13:55:55 +0530 Subject: [PATCH 2/2] updated with black linting --- mediatr/__init__.py | 10 +-- mediatr/_version.py | 2 +- mediatr/exceptions.py | 26 ++++-- mediatr/mediator.py | 81 +++++++++++-------- run.py | 2 +- setup.py | 76 ++++++++++------- tests/example_handlers.py | 29 ++++--- tests/example_handlers_annotations.py | 8 +- tests/example_handlers_constructur.py | 8 +- tests/example_queries.py | 21 ++--- tests/test_class_handlers.py | 11 ++- tests/test_class_handlers_with_annotations.py | 7 +- tests/test_class_handlers_with_constructor.py | 12 ++- tests/test_generic_handlers.py | 5 +- tests/test_init.py | 10 +-- tests/test_main.py | 9 ++- tests/test_send_multiple.py | 6 +- tests/test_send_sync.py | 12 ++- tests/test_static_mediator.py | 22 +++-- 19 files changed, 221 insertions(+), 136 deletions(-) diff --git a/mediatr/__init__.py b/mediatr/__init__.py index 666576d..b3ff9fc 100644 --- a/mediatr/__init__.py +++ b/mediatr/__init__.py @@ -4,13 +4,13 @@ __behaviors__, GenericQuery, extract_request_type, - find_behaviors - ) + find_behaviors, +) from .exceptions import ( HandlerNotFoundError, InvalidRequest, InvalidHandlerError, - InvalidBehaviorError + InvalidBehaviorError, ) from ._version import __version__ @@ -26,5 +26,5 @@ "HandlerNotFoundError", "InvalidRequest", "InvalidHandlerError", - "InvalidBehaviorError" -] \ No newline at end of file + "InvalidBehaviorError", +] diff --git a/mediatr/_version.py b/mediatr/_version.py index bd18148..f708a9b 100644 --- a/mediatr/_version.py +++ b/mediatr/_version.py @@ -1 +1 @@ -__version__ = '1.3.2' \ No newline at end of file +__version__ = "1.3.2" diff --git a/mediatr/exceptions.py b/mediatr/exceptions.py index 1588b34..15d71df 100644 --- a/mediatr/exceptions.py +++ b/mediatr/exceptions.py @@ -1,7 +1,7 @@ import inspect -def raise_if_handler_not_found(handler,request): +def raise_if_handler_not_found(handler, request): if not handler: raise HandlerNotFoundError(request) @@ -18,7 +18,7 @@ def raise_if_handler_is_invalid(handler): if isfunc: func = handler else: - if hasattr(handler, 'handle'): + if hasattr(handler, "handle"): if inspect.isfunction(handler.handle): func = handler.handle elif inspect.ismethod(handler.handle): @@ -34,7 +34,11 @@ def raise_if_handler_is_invalid(handler): def raise_if_behavior_is_invalid(behavior): isfunc = inspect.isfunction(behavior) - func = behavior if isfunc else (behavior.handle if hasattr(behavior, 'handle') else None) + func = ( + behavior + if isfunc + else (behavior.handle if hasattr(behavior, "handle") else None) + ) if not func or not inspect.isfunction(func): raise InvalidHandlerError(func) sign = inspect.signature(func) @@ -57,14 +61,22 @@ def __init__(self): class InvalidHandlerError(Exception): def __init__(self, handler): self.handler = handler - super().__init__("Incorrect handler: '{}'. Handler must be a class, that contains 'handle' method with args:(self,request:SomeRequestType) \ + super().__init__( + "Incorrect handler: '{}'. Handler must be a class, that contains 'handle' method with args:(self,request:SomeRequestType) \ or must be a function with args:(request:SomeRequestType) \ - where 'request' is object of request class. See examples on git".format(handler)) + where 'request' is object of request class. See examples on git".format( + handler + ) + ) class InvalidBehaviorError(Exception): def __init__(self, behavior): self.behavior = behavior - super().__init__("Incorrect behavior: '{}'. Behavior must be a class, that contains 'handle' method with args:(self,request:SomeRequestTypeOrObject,next) \ + super().__init__( + "Incorrect behavior: '{}'. Behavior must be a class, that contains 'handle' method with args:(self,request:SomeRequestTypeOrObject,next) \ or must be a function with args:(request:SomeRequestTypeOrObject,next) \ - where 'next' is coroutine function. See examples on git".format(behavior)) + where 'next' is coroutine function. See examples on git".format( + behavior + ) + ) diff --git a/mediatr/mediator.py b/mediatr/mediator.py index e58bcd1..518798f 100644 --- a/mediatr/mediator.py +++ b/mediatr/mediator.py @@ -1,27 +1,35 @@ import inspect from typing import Any, Awaitable, Callable, Optional, TypeVar, Generic, Union -from mediatr.exceptions import raise_if_behavior_is_invalid, raise_if_handler_is_invalid, raise_if_handler_not_found, \ - raise_if_request_none +from mediatr.exceptions import ( + raise_if_behavior_is_invalid, + raise_if_handler_is_invalid, + raise_if_handler_not_found, + raise_if_request_none, +) __handlers__ = {} __behaviors__ = {} -TResponse = TypeVar('TResponse') +TResponse = TypeVar("TResponse") + + class GenericQuery(Generic[TResponse]): pass + @staticmethod -def default_handler_class_manager(HandlerCls:type,is_behavior:bool=False): +def default_handler_class_manager(HandlerCls: type, is_behavior: bool = False): return HandlerCls() + def extract_request_type(handler, is_behavior=False) -> type: isfunc = inspect.isfunction(handler) - + func = None if isfunc: func = handler else: - if hasattr(handler, 'handle'): + if hasattr(handler, "handle"): if inspect.isfunction(handler.handle): func = handler.handle elif inspect.ismethod(handler.handle): @@ -34,11 +42,19 @@ def extract_request_type(handler, is_behavior=False) -> type: sign = inspect.signature(func) items = list(sign.parameters) - return sign.parameters.get(items[0]).annotation if isfunc else sign.parameters.get(items[1]).annotation + return ( + sign.parameters.get(items[0]).annotation + if isfunc + else sign.parameters.get(items[1]).annotation + ) async def __return_await__(result): - return await result if inspect.isawaitable(result) or inspect.iscoroutine(result) else result + return ( + await result + if inspect.isawaitable(result) or inspect.iscoroutine(result) + else result + ) def find_behaviors(request): @@ -50,16 +66,19 @@ def find_behaviors(request): return behaviors -class Mediator(): +class Mediator: """Class of mediator as entry point to send requests and get responses""" handler_class_manager = default_handler_class_manager - + def __init__(self, handler_class_manager: Callable = None): if handler_class_manager: self.handler_class_manager = handler_class_manager - async def send_async(self: Union["Mediator",GenericQuery[TResponse]], request: Optional[GenericQuery[TResponse]] = None) -> Awaitable[TResponse]: + async def send_async( + self: Union["Mediator", GenericQuery[TResponse]], + request: Optional[GenericQuery[TResponse]] = None, + ) -> Awaitable[TResponse]: """ Send request in async mode and getting response @@ -79,8 +98,8 @@ async def send_async(self: Union["Mediator",GenericQuery[TResponse]], request: O if __handlers__.get(request.__class__): handler = __handlers__[request.__class__] elif __handlers__.get(request.__class__.__name__): - handler =__handlers__[request.__class__.__name__] - raise_if_handler_not_found(handler,request) + handler = __handlers__[request.__class__.__name__] + raise_if_handler_not_found(handler, request) handler_func = None handler_obj = None if inspect.isfunction(handler): @@ -90,9 +109,9 @@ async def send_async(self: Union["Mediator",GenericQuery[TResponse]], request: O handler_func = handler_obj.handle behaviors = find_behaviors(request) - behaviors.append(lambda r,next: handler_func(r)) + behaviors.append(lambda r, next: handler_func(r)) - async def start_func(i:int): + async def start_func(i: int): beh = behaviors[i] beh_func = None if inspect.isfunction(beh): @@ -100,24 +119,26 @@ async def start_func(i:int): else: beh_obj = self1.handler_class_manager(beh, True) beh_func = beh_obj.handle - return await __return_await__(beh_func(request,lambda: start_func(i+1))) - - return await start_func(0) + return await __return_await__(beh_func(request, lambda: start_func(i + 1))) + return await start_func(0) - def send(self: Union["Mediator", GenericQuery[TResponse]], request: Optional[GenericQuery[TResponse]] = None) -> TResponse: + def send( + self: Union["Mediator", GenericQuery[TResponse]], + request: Optional[GenericQuery[TResponse]] = None, + ) -> TResponse: """ Send request in synchronous mode and getting response - + Args: request (`object`): object of request class Returns: response object or `None` - + """ - + self1 = Mediator if not request else self request = request or self @@ -126,8 +147,8 @@ def send(self: Union["Mediator", GenericQuery[TResponse]], request: Optional[Gen if __handlers__.get(request.__class__): handler = __handlers__[request.__class__] elif __handlers__.get(request.__class__.__name__): - handler =__handlers__[request.__class__.__name__] - raise_if_handler_not_found(handler,request) + handler = __handlers__[request.__class__.__name__] + raise_if_handler_not_found(handler, request) handler_func = None handler_obj = None if inspect.isfunction(handler): @@ -136,9 +157,9 @@ def send(self: Union["Mediator", GenericQuery[TResponse]], request: Optional[Gen handler_obj = self1.handler_class_manager(handler) handler_func = handler_obj.handle behaviors = find_behaviors(request) - behaviors.append(lambda r,next: handler_func(r)) + behaviors.append(lambda r, next: handler_func(r)) - def start_func(i:int): + def start_func(i: int): beh = behaviors[i] beh_func = None if inspect.isfunction(beh): @@ -146,9 +167,9 @@ def start_func(i:int): else: beh_obj = self1.handler_class_manager(beh, True) beh_func = beh_obj.handle - return beh_func(request,lambda: start_func(i+1)) + return beh_func(request, lambda: start_func(i + 1)) + return start_func(0) - @staticmethod def register_handler(handler): @@ -177,7 +198,3 @@ def behavior(behavior): """Append behavior function or class to global behaviors dictionary""" Mediator.register_behavior(behavior) return behavior - - - - diff --git a/run.py b/run.py index 859589b..fa62749 100644 --- a/run.py +++ b/run.py @@ -1 +1 @@ -from mediatr import Mediator \ No newline at end of file +from mediatr import Mediator diff --git a/setup.py b/setup.py index 577518a..92b2e38 100644 --- a/setup.py +++ b/setup.py @@ -4,40 +4,56 @@ exec(open("mediatr/_version.py", encoding="utf-8").read()) from os import path + this_directory = path.abspath(path.dirname(__file__)) -long_description=None +long_description = None -with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: +with open(path.join(this_directory, "README.md"), encoding="utf-8") as f: long_description = f.read() setup( - name = 'mediatr', - packages = ['mediatr'], - version = __version__, - license="MIT -or- Apache License 2.0", - description="mediator and CQRS pattern implementation with pipline behaviors for Python 3.6+. Mediatr py", - long_description_content_type='text/markdown', - long_description=long_description, - author = 'Evgeniy Fetisov', - author_email = 'me@efetisov.ru', - url = 'https://github.com/megafetis/mediatr_py', - keywords = ['mediator','mediatr', 'CQRS','cqrs','mediatr_py','mediator py','mediatr py','pipline', 'behaviors', 'command', 'query', 'responsability', 'segregation','command bus','bus' ], - python_requires=">=3.6", - - classifiers=[ - 'Development Status :: 5 - Production/Stable', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package - 'Intended Audience :: Developers', # Define that your audience are developers - 'Topic :: Software Development :: Build Tools', - "License :: OSI Approved :: MIT License", - "License :: OSI Approved :: Apache Software License", - "Programming Language :: Python :: Implementation :: CPython", - "Programming Language :: Python :: Implementation :: PyPy", - "Programming Language :: Python :: 3 :: Only", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", - "Programming Language :: Python :: 3.8", - "Programming Language :: Python :: 3.9", - ], -) \ No newline at end of file + name="mediatr", + packages=["mediatr"], + version=__version__, + license="MIT -or- Apache License 2.0", + description="mediator and CQRS pattern implementation with pipline behaviors for Python 3.6+. Mediatr py", + long_description_content_type="text/markdown", + long_description=long_description, + author="Evgeniy Fetisov", + author_email="me@efetisov.ru", + url="https://github.com/megafetis/mediatr_py", + keywords=[ + "mediator", + "mediatr", + "CQRS", + "cqrs", + "mediatr_py", + "mediator py", + "mediatr py", + "pipline", + "behaviors", + "command", + "query", + "responsability", + "segregation", + "command bus", + "bus", + ], + python_requires=">=3.6", + classifiers=[ + "Development Status :: 5 - Production/Stable", # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package + "Intended Audience :: Developers", # Define that your audience are developers + "Topic :: Software Development :: Build Tools", + "License :: OSI Approved :: MIT License", + "License :: OSI Approved :: Apache Software License", + "Programming Language :: Python :: Implementation :: CPython", + "Programming Language :: Python :: Implementation :: PyPy", + "Programming Language :: Python :: 3 :: Only", + "Programming Language :: Python :: 3.6", + "Programming Language :: Python :: 3.7", + "Programming Language :: Python :: 3.8", + "Programming Language :: Python :: 3.9", + ], +) diff --git a/tests/example_handlers.py b/tests/example_handlers.py index 19727a1..1b7f2f8 100644 --- a/tests/example_handlers.py +++ b/tests/example_handlers.py @@ -1,6 +1,13 @@ -from tests.example_queries import GetArrayQuery, GetArrayQuery1, QueryWithTypedResponse, SomeQueryResponseModel +from tests.example_queries import ( + GetArrayQuery, + GetArrayQuery1, + QueryWithTypedResponse, + SomeQueryResponseModel, +) from mediatr import Mediator -from typing import Callable +from typing import Callable + + async def get_array_handler(request: GetArrayQuery): items = list() @@ -37,7 +44,7 @@ async def get_array_query_behavior_6(request: GetArrayQuery, next): return array1 -class GetArrayQueryHandler(): +class GetArrayQueryHandler: def handle(self, request: GetArrayQuery1): items = list() @@ -46,28 +53,26 @@ def handle(self, request: GetArrayQuery1): return items -class GetArrayQueryBehavior(): +class GetArrayQueryBehavior: def handle(self, request: GetArrayQuery1, next): request.items_count = 4 return next() def common_log_behavior(request: object, next): - request.updated_at = '123' + request.updated_at = "123" return next() - -def print_before(request:object,next:Callable): +def print_before(request: object, next: Callable): print(request.__class__.__name__) - if hasattr(request,'common_bahavior_handled'): + if hasattr(request, "common_bahavior_handled"): request.common_bahavior_handled = True - print('common_bahavior_handled ') + print("common_bahavior_handled ") return next() - -class QueryWithTypedResponseHandler(): - def handle(self, request:QueryWithTypedResponse): +class QueryWithTypedResponseHandler: + def handle(self, request: QueryWithTypedResponse): return SomeQueryResponseModel(request.some_name) diff --git a/tests/example_handlers_annotations.py b/tests/example_handlers_annotations.py index 06fa488..7f5323b 100644 --- a/tests/example_handlers_annotations.py +++ b/tests/example_handlers_annotations.py @@ -2,11 +2,13 @@ from tests.example_queries import GetArrayQueryWithAnnotations -class GetArrayQueryHandlerWithAnnotations(): - def handle(self: GetArrayQueryHandlerWithAnnotations, request: GetArrayQueryWithAnnotations) -> list: +class GetArrayQueryHandlerWithAnnotations: + def handle( + self: GetArrayQueryHandlerWithAnnotations, request: GetArrayQueryWithAnnotations + ) -> list: items = list() for i in range(0, request.items_count): items.append(i) - + return items diff --git a/tests/example_handlers_constructur.py b/tests/example_handlers_constructur.py index 24838e5..9689858 100644 --- a/tests/example_handlers_constructur.py +++ b/tests/example_handlers_constructur.py @@ -1,6 +1,7 @@ from tests.example_queries import GetArrayQueryWithConstructor from mediatr import Mediator -from typing import Callable +from typing import Callable + def setup_class_handler_manager(handler_class: type, is_behavior=False): if is_behavior: @@ -9,7 +10,8 @@ def setup_class_handler_manager(handler_class: type, is_behavior=False): return handler_class -class GetArrayQueryHandlerWithConstructor(): + +class GetArrayQueryHandlerWithConstructor: def __init__(self, client): self.client = client @@ -18,5 +20,5 @@ def handle(self, request: GetArrayQueryWithConstructor): for i in range(0, request.items_count): items.append(i) - + return items diff --git a/tests/example_queries.py b/tests/example_queries.py index 1d520db..a6e45c2 100644 --- a/tests/example_queries.py +++ b/tests/example_queries.py @@ -1,17 +1,19 @@ from typing import Generic from mediatr import GenericQuery -class GetArrayQuery(): + + +class GetArrayQuery: def __init__(self, items_count: int): self.items_count = items_count items_count = 0 -class BaseQuery(): +class BaseQuery: pass -class GetArrayQuery1(): +class GetArrayQuery1: def __init__(self, items_count: int): self.items_count = items_count @@ -19,7 +21,7 @@ def __init__(self, items_count: int): common_bahavior_handled = False -class GetArrayQueryWithConstructor(): +class GetArrayQueryWithConstructor: def __init__(self, items_count: int): self.items_count = items_count @@ -27,7 +29,7 @@ def __init__(self, items_count: int): common_bahavior_handled = False -class GetArrayQueryWithAnnotations(): +class GetArrayQueryWithAnnotations: def __init__(self, items_count: int): self.items_count = items_count @@ -35,12 +37,11 @@ def __init__(self, items_count: int): common_bahavior_handled = False -class SomeQueryResponseModel(): - def __init__(self,name:str) -> None: +class SomeQueryResponseModel: + def __init__(self, name: str) -> None: self.some_name = name - + class QueryWithTypedResponse(GenericQuery[SomeQueryResponseModel]): - def __init__(self,name:str) -> None: + def __init__(self, name: str) -> None: self.some_name = name - \ No newline at end of file diff --git a/tests/test_class_handlers.py b/tests/test_class_handlers.py index 55773b9..60e1299 100644 --- a/tests/test_class_handlers.py +++ b/tests/test_class_handlers.py @@ -2,14 +2,17 @@ import unittest from mediatr import Mediator -from tests.example_handlers import common_log_behavior, get_array_query_behavior_3, get_array_query_behavior_6, \ - GetArrayQueryBehavior, \ - GetArrayQueryHandler +from tests.example_handlers import ( + common_log_behavior, + get_array_query_behavior_3, + get_array_query_behavior_6, + GetArrayQueryBehavior, + GetArrayQueryHandler, +) from tests.example_queries import GetArrayQuery1 class ClassHandlersTest(unittest.TestCase): - def setUp(self): self.mediator = Mediator() self.ioloop = asyncio.get_event_loop() diff --git a/tests/test_class_handlers_with_annotations.py b/tests/test_class_handlers_with_annotations.py index 19ad7e4..0c6349b 100644 --- a/tests/test_class_handlers_with_annotations.py +++ b/tests/test_class_handlers_with_annotations.py @@ -11,10 +11,13 @@ def setUp(self): self.ioloop = asyncio.get_event_loop() return super().setUp() - @unittest.skipUnless(sys.version_info >= (3,7), "requires 3.7+") + @unittest.skipUnless(sys.version_info >= (3, 7), "requires 3.7+") def test_1(self): - from tests.example_handlers_annotations import GetArrayQueryHandlerWithAnnotations + from tests.example_handlers_annotations import ( + GetArrayQueryHandlerWithAnnotations, + ) from tests.example_queries import GetArrayQueryWithAnnotations + Mediator.register_handler(GetArrayQueryHandlerWithAnnotations) query = GetArrayQueryWithAnnotations(5) self.assertEqual(query.items_count, 5) diff --git a/tests/test_class_handlers_with_constructor.py b/tests/test_class_handlers_with_constructor.py index ead31cc..5b9a58f 100644 --- a/tests/test_class_handlers_with_constructor.py +++ b/tests/test_class_handlers_with_constructor.py @@ -2,7 +2,10 @@ import unittest from mediatr import Mediator -from tests.example_handlers_constructur import setup_class_handler_manager, GetArrayQueryHandlerWithConstructor +from tests.example_handlers_constructur import ( + setup_class_handler_manager, + GetArrayQueryHandlerWithConstructor, +) from tests.example_queries import GetArrayQueryWithConstructor @@ -15,8 +18,11 @@ def setUp(self): def test_1(self): class Client: def test_1(self): - return 'test_3' - get_array_query_handler_with_constructor = GetArrayQueryHandlerWithConstructor(Client) + return "test_3" + + get_array_query_handler_with_constructor = GetArrayQueryHandlerWithConstructor( + Client + ) Mediator.register_handler(get_array_query_handler_with_constructor) query = GetArrayQueryWithConstructor(5) self.assertEqual(query.items_count, 5) diff --git a/tests/test_generic_handlers.py b/tests/test_generic_handlers.py index e26eced..336153f 100644 --- a/tests/test_generic_handlers.py +++ b/tests/test_generic_handlers.py @@ -17,8 +17,7 @@ def tearDown(self): def test_first(self): mediator = Mediator() Mediator.register_handler(QueryWithTypedResponseHandler) - + genericQuery = QueryWithTypedResponse(name="mediatr 123") respModel = mediator.send(genericQuery) - self.assertEqual(genericQuery.some_name,respModel.some_name) - + self.assertEqual(genericQuery.some_name, respModel.some_name) diff --git a/tests/test_init.py b/tests/test_init.py index d0fc685..488fe6b 100644 --- a/tests/test_init.py +++ b/tests/test_init.py @@ -26,7 +26,7 @@ def test_is_func(self): self.assertTrue(inspect.isfunction(Mediator.register_handler)) def test_is_func_class_method(self): - class Class1(): + class Class1: async def method1(self): pass @@ -35,19 +35,19 @@ async def method1(self): def test_dict_key(self): dict1 = {} - class Class1(): + class Class1: pass dict1[Class1] = 123 self.assertEqual(123, dict1[Class1]) def test_class_method_sep(self): - class Class1(): - name = 'ff' + class Class1: + name = "ff" def func(self, arg1): return self.name + arg1 obj = Class1() meth = obj.func - self.assertEqual('fff', meth('f')) + self.assertEqual("fff", meth("f")) diff --git a/tests/test_main.py b/tests/test_main.py index 5682633..d63bb6c 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -2,8 +2,13 @@ import unittest from mediatr import __behaviors__, __handlers__, Mediator -from tests.example_handlers import get_array_handler, get_array_handler_sync, get_array_query_behavior, \ - get_array_query_behavior_3, get_array_query_behavior_6 +from tests.example_handlers import ( + get_array_handler, + get_array_handler_sync, + get_array_query_behavior, + get_array_query_behavior_3, + get_array_query_behavior_6, +) from tests.example_queries import GetArrayQuery diff --git a/tests/test_send_multiple.py b/tests/test_send_multiple.py index b197bb7..3a5c06f 100644 --- a/tests/test_send_multiple.py +++ b/tests/test_send_multiple.py @@ -2,7 +2,11 @@ import unittest from mediatr import Mediator -from tests.example_handlers import common_log_behavior, get_array_handler, GetArrayQueryHandler +from tests.example_handlers import ( + common_log_behavior, + get_array_handler, + GetArrayQueryHandler, +) from tests.example_queries import GetArrayQuery, GetArrayQuery1 diff --git a/tests/test_send_sync.py b/tests/test_send_sync.py index 5e1969e..94729e0 100644 --- a/tests/test_send_sync.py +++ b/tests/test_send_sync.py @@ -1,7 +1,12 @@ import unittest from mediatr import __behaviors__, __handlers__, Mediator -from tests.example_handlers import GetArrayQueryHandler, common_log_behavior, get_array_handler_sync,print_before +from tests.example_handlers import ( + GetArrayQueryHandler, + common_log_behavior, + get_array_handler_sync, + print_before, +) from tests.example_queries import GetArrayQuery, GetArrayQuery1 @@ -28,11 +33,10 @@ def test_dispatch_sync(self): result2 = mediator.send(query2) self.assertEqual(len(result2), 4) self.assertIsNotNone(query2.updated_at) - self.assertEqual(query1.updated_at,'123') - self.assertEqual(query2.updated_at,'123') + self.assertEqual(query1.updated_at, "123") + self.assertEqual(query2.updated_at, "123") self.assertTrue(query2.common_bahavior_handled) - def test_dispatch_sync_without_behavior(self): Mediator.register_handler(get_array_handler_sync) Mediator.register_handler(GetArrayQueryHandler) diff --git a/tests/test_static_mediator.py b/tests/test_static_mediator.py index 9def842..90ff3e8 100644 --- a/tests/test_static_mediator.py +++ b/tests/test_static_mediator.py @@ -1,7 +1,14 @@ import unittest from mediatr import __behaviors__, __handlers__, Mediator -from tests.example_handlers import QueryWithTypedResponseHandler, get_array_handler, GetArrayQueryHandler, common_log_behavior, get_array_handler_sync,print_before +from tests.example_handlers import ( + QueryWithTypedResponseHandler, + get_array_handler, + GetArrayQueryHandler, + common_log_behavior, + get_array_handler_sync, + print_before, +) from tests.example_queries import GetArrayQuery, GetArrayQuery1, QueryWithTypedResponse @@ -15,13 +22,13 @@ def tearDown(self): __behaviors__.clear() def test_first(self): - + Mediator.register_handler(get_array_handler_sync) Mediator.register_handler(GetArrayQueryHandler) Mediator.register_handler(QueryWithTypedResponseHandler) Mediator.register_behavior(common_log_behavior) Mediator.register_behavior(print_before) - + query1 = GetArrayQuery(5) result1 = Mediator.send(query1) self.assertEqual(len(result1), 5) @@ -30,14 +37,13 @@ def test_first(self): result2 = Mediator.send(query2) self.assertEqual(len(result2), 4) self.assertIsNotNone(query2.updated_at) - self.assertEqual(query1.updated_at,'123') - self.assertEqual(query2.updated_at,'123') + self.assertEqual(query1.updated_at, "123") + self.assertEqual(query2.updated_at, "123") self.assertTrue(query2.common_bahavior_handled) - def test_static_generic(self): Mediator.register_handler(QueryWithTypedResponseHandler) - + genericQuery = QueryWithTypedResponse(name="mediatr 123") respModel = Mediator.send(genericQuery) - self.assertEqual(genericQuery.some_name,respModel.some_name) + self.assertEqual(genericQuery.some_name, respModel.some_name)