Skip to content

Commit dbf807e

Browse files
committed
test: add TestConvertDicts
1 parent 5d2494c commit dbf807e

File tree

5 files changed

+37
-7
lines changed

5 files changed

+37
-7
lines changed

fastapi_esql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
timing,
2222
)
2323

24-
__version__ = "0.0.9"
24+
__version__ = "0.0.10"
2525

2626
__all__ = [
2727
"QsParsingError",

fastapi_esql/utils/converter.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,13 @@ def convert_dicts(dicts, converters: Dict[str, Callable]):
88
if not converters:
99
return
1010
for d in dicts:
11-
for f, cvt in converters.items():
12-
v = d[f]
11+
for field, converter in converters.items():
12+
if field not in d:
13+
logger.warning(f"Item `{field}` does not exist in dict `{d}`")
14+
continue
15+
16+
value = d[field]
1317
try:
14-
d[f] = cvt(v)
18+
d[field] = converter(value)
1519
except Exception as e:
16-
logger.warning(f"Converting value `{v}` by `{cvt.__name__}` failed => {e}")
20+
logger.warning(f"Converting value `{value}` by `{converter.__name__}` failed => {e}")

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "fastapi-efficient-sql"
3-
version = "0.0.9"
3+
version = "0.0.10"
44
description = "Generate bulk DML SQL and execute them based on Tortoise ORM and mysql8.0+, and integrated with FastAPI."
55
authors = ["BryanLee <[email protected]>"]
66
keywords = ["sql", "fastapi", "tortoise-orm", "mysql8", "bulk-operation"]

tests/test_converter.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from copy import deepcopy
2+
from json import loads
3+
from unittest import TestCase
4+
5+
from fastapi_esql import convert_dicts
6+
7+
8+
class TestConvertDicts(TestCase):
9+
10+
dicts = [{"id": 1, "value": "1"}, {"id": 2, "value": '{"k": [true, null]}'}]
11+
12+
def test_normal(self):
13+
dicts = deepcopy(self.dicts)
14+
convert_dicts(dicts, {"value": loads})
15+
assert dicts == [{"id": 1, "value": 1}, {"id": 2, "value": {"k": [True, None]}}]
16+
17+
def test_wrong_field(self):
18+
dicts = deepcopy(self.dicts)
19+
convert_dicts(dicts, {"wrong_value": loads})
20+
assert dicts == self.dicts
21+
22+
def test_wrong_converter(self):
23+
dicts = deepcopy(self.dicts)
24+
convert_dicts(dicts, {"value": int})
25+
assert dicts == [{"id": 1, "value": 1}, {"id": 2, "value": '{"k": [true, null]}'}]

tests/test_orm.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ def get_ro_conn(self):
1212
return None
1313

1414

15-
class TestOrm(TestCase):
15+
class TestORM(TestCase):
16+
1617
def test_no_model(self):
1718
with self.assertRaises(NotImplementedError):
1819
class AccountMgr(BaseManager, metaclass=DemoMetaclass):

0 commit comments

Comments
 (0)