From bac90c8fa759ae0c780b9c0f08252c212aa18439 Mon Sep 17 00:00:00 2001 From: sheldy <85823514+sheldygg@users.noreply.github.com> Date: Sun, 12 Feb 2023 01:26:49 +0200 Subject: [PATCH] add new method for Router (#1117) * add new method for Router * add type hint, tests, changes file * update tests * Update aiogram/dispatcher/router.py * Update tests/test_dispatcher/test_router.py * Update router.py --------- Co-authored-by: Alex Root Junior --- CHANGES/1117.feature.rst | 1 + aiogram/dispatcher/router.py | 6 ++++++ tests/test_dispatcher/test_router.py | 14 ++++++++++++++ 3 files changed, 21 insertions(+) create mode 100644 CHANGES/1117.feature.rst diff --git a/CHANGES/1117.feature.rst b/CHANGES/1117.feature.rst new file mode 100644 index 00000000..cdcf0999 --- /dev/null +++ b/CHANGES/1117.feature.rst @@ -0,0 +1 @@ +Added a method that allows you to compactly register routers \ No newline at end of file diff --git a/aiogram/dispatcher/router.py b/aiogram/dispatcher/router.py index f1ddc1c4..86359ce0 100644 --- a/aiogram/dispatcher/router.py +++ b/aiogram/dispatcher/router.py @@ -179,6 +179,12 @@ class Router: self._parent_router = router router.sub_routers.append(self) + def include_routers(self, *routers: Router) -> None: + if not routers: + raise ValueError("At least one router must be provided") + for router in routers: + self.include_router(router) + def include_router(self, router: Router) -> Router: """ Attach another router. diff --git a/tests/test_dispatcher/test_router.py b/tests/test_dispatcher/test_router.py index 673bb46d..3404822d 100644 --- a/tests/test_dispatcher/test_router.py +++ b/tests/test_dispatcher/test_router.py @@ -33,6 +33,20 @@ class TestRouter: assert router3.parent_router is router2 assert router3.sub_routers == [] + def test_including_many_routers(self): + router = Router() + router1 = Router() + router2 = Router() + + router.include_routers(router1, router2) + + assert router.sub_routers == [router1, router2] + + def test_including_many_routers_bad_type(self): + router = Router() + with pytest.raises(ValueError, match="At least one router must be provided"): + router.include_routers() + def test_include_router_by_string_bad_type(self): router = Router() with pytest.raises(ValueError, match=r"router should be instance of Router"):