Skip to content

Commit 54bda2f

Browse files
committed
Extract a base async test case
1 parent 7f746cd commit 54bda2f

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import asyncio
2+
import unittest
3+
from typing import TypeVar, Awaitable
4+
5+
T = TypeVar('T')
6+
7+
8+
class AsyncTestCase(unittest.TestCase):
9+
def await_(self, awaitable: Awaitable[T]) -> T:
10+
return self.loop.run_until_complete(awaitable)
11+
12+
def setUp(self) -> None:
13+
super().setUp()
14+
15+
self.loop = asyncio.get_event_loop()

tests/tests_app.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import io
22
import json
3-
import asyncio
43
import zipfile
54
import datetime
65
import tempfile
7-
import unittest
8-
from typing import IO, TypeVar, Awaitable
6+
from typing import IO
97
from unittest import mock
108

119
import alembic # type: ignore[import]
10+
import test_utils
1211
from sqlalchemy import create_engine
1312
from alembic.config import Config # type: ignore[import]
1413
from starlette.config import environ
1514
from starlette.testclient import TestClient
1615
from code_submitter.tables import Archive, ChoiceHistory
1716

18-
T = TypeVar('T')
19-
2017
DATABASE_FILE: IO[bytes]
2118

2219

@@ -39,10 +36,7 @@ def setUpModule() -> None:
3936
alembic.command.upgrade(Config('alembic.ini'), 'head')
4037

4138

42-
class AppTests(unittest.TestCase):
43-
def await_(self, awaitable: Awaitable[T]) -> T:
44-
return self.loop.run_until_complete(awaitable)
45-
39+
class AppTests(test_utils.AsyncTestCase):
4640
def setUp(self) -> None:
4741
super().setUp()
4842

@@ -54,7 +48,6 @@ def setUp(self) -> None:
5448
self.session.auth = ('test_user', 'test_pass')
5549
self.url_path_for = app.url_path_for
5650
self.database = database
57-
self.loop = asyncio.get_event_loop()
5851

5952
def tearDown(self) -> None:
6053
self.session.__exit__(None, None, None)

0 commit comments

Comments
 (0)