Skip to content

Commit 4639b84

Browse files
committed
test: use asynctest to test async methods
1 parent 4e18963 commit 4639b84

File tree

6 files changed

+44
-7
lines changed

6 files changed

+44
-7
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ tortoise-orm = "*"
88

99
[dev-packages]
1010
aiomysql = "*"
11+
asynctest = "*"
1112
autopep8 = "*"
1213
cryptography = "*"
1314
faker = "*"

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,8 @@ Generate sql and execute
163163
```python
164164
await AccountMgr.bulk_update_from_dicts(
165165
[
166-
{'id': 7, 'active': False, 'gender': <GenderEnum.male: 1>},
167-
{'id': 15, 'active': True, 'gender': <GenderEnum.unknown: 0>}
166+
{'id': 7, 'active': False, 'gender': GenderEnum.male},
167+
{'id': 15, 'active': True, 'gender': GenderEnum.unknown}
168168
],
169169
join_fields=["id"],
170170
update_fields=["active", "gender"],

requirements-dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
aiomysql
2+
asynctest
23
autopep8
34
cryptography
45
faker

tests/__init__.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
from tortoise import Tortoise
44

5+
TEST_CONN = "test"
6+
57

68
def init_test_orm():
79
"""
@@ -11,7 +13,7 @@ def init_test_orm():
1113
config = {
1214
"timezone": "Asia/Shanghai",
1315
"connections": {
14-
"test": {
16+
TEST_CONN: {
1517
"engine": "tortoise.backends.mysql",
1618
"credentials": {
1719
"host": "localhost",
@@ -27,11 +29,15 @@ def init_test_orm():
2729
"apps": {
2830
"demo": {
2931
"models": ["examples.service.models.demo"],
30-
"default_connection": "test"
32+
"default_connection": TEST_CONN
3133
}
3234
}
3335
}
3436
asyncio.run(Tortoise.init(config=config))
3537

3638

39+
def get_test_conn():
40+
return Tortoise.get_connection(TEST_CONN)
41+
42+
3743
init_test_orm()

tests/test_cursor_handler.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from logging import getLogger
2+
3+
from asynctest import CoroutineMock, TestCase, patch
4+
5+
from . import get_test_conn
6+
from fastapi_esql import CursorHandler
7+
8+
logger = getLogger(__name__)
9+
10+
11+
class TestCursorHandler(TestCase):
12+
13+
conn = get_test_conn()
14+
15+
async def test_fetch_dicts(self):
16+
with patch(
17+
"tortoise.backends.mysql.client_class.execute_query_dict",
18+
# new=CoroutineMock()
19+
) as mock_exec:
20+
mock_exec.side_effect = Exception("Error")
21+
assert await CursorHandler.fetch_dicts("SELECT true", self.conn, logger) is None
22+
23+
@patch("tortoise.backends.mysql.client_class.execute_query")
24+
async def test_sum_row_cnt(self, mock_exec):
25+
mock_exec.return_value = 10, object()
26+
assert await CursorHandler.sum_row_cnt("SELECT true", self.conn, logger) == 10
27+
28+
async def test_exec_if_ok(self):
29+
# True means test connection is reachable
30+
assert await CursorHandler.exec_if_ok("SELECT true", self.conn, logger) == True

tests/test_sqlizer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
from datetime import datetime
22
from unittest import TestCase
33

4+
from examples.service.models.demo import Account
5+
from examples.service.constants.enums import GenderEnum, LocaleEnum
46
from fastapi_esql import (
57
Cases, RawSQL, SQLizer,
68
Q, QsParsingError, WrongParamsError,
79
)
810

9-
from examples.service.models.demo import Account
10-
from examples.service.constants.enums import GenderEnum, LocaleEnum
11-
1211

1312
class TestRawSQL(TestCase):
1413

0 commit comments

Comments
 (0)