From ec58111360c9ffdeca54cb45587c3bc5a06b17e7 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 15 Dec 2018 16:08:37 +0000 Subject: [PATCH] Allow router to be tested properly --- tests/__init__.py | 15 ++++++++++++++- tests/test_router/test_router.py | 7 +++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 tests/test_router/test_router.py diff --git a/tests/__init__.py b/tests/__init__.py index 3116ecb..0d82441 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -3,11 +3,12 @@ import os import shutil import subprocess -from aiohttp.test_utils import unused_port +from aiohttp.test_utils import AioHTTPTestCase, unused_port from aiounittest import AsyncTestCase from click.testing import CliRunner from catfish.__main__ import cli +from catfish.router import get_server from catfish.utils.processes import terminate_subprocesses from catfish.utils.sockets import create_base_socket_dir, delete_base_socket_dir @@ -49,3 +50,15 @@ class BaseWorkerTestCase(BaseTestCase): ) self.assertEqual(result.returncode, 0, result.stdout) super().tearDown() + + +class BaseRouterTestCase(AsyncTestCase, AioHTTPTestCase): + async def get_application(self): + return get_server() + + def __getattribute__(self, name): + attr = super(AioHTTPTestCase, self).__getattribute__(name) + if name.startswith("test_") and callable(attr): + return lambda: self.loop.run_until_complete(attr()) + else: + return attr diff --git a/tests/test_router/test_router.py b/tests/test_router/test_router.py new file mode 100644 index 0000000..62f949b --- /dev/null +++ b/tests/test_router/test_router.py @@ -0,0 +1,7 @@ +from tests import BaseRouterTestCase + + +class RouterTestCase(BaseRouterTestCase): + async def test_accessible(self): + response = await self.client.request("GET", "/") + self.assertEqual(response.status, 200)