Skip to content

Commit 8172d1f

Browse files
committed
test: add TestSQLizer.test_resolve_wheres
1 parent 87e3688 commit 8172d1f

File tree

5 files changed

+76
-3
lines changed

5 files changed

+76
-3
lines changed

Pipfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ tortoise-orm = "*"
1717
uvicorn = "*"
1818

1919
[requires]
20-
python_version = "3.7"
20+
python_version = ">=3.7"

examples/service/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def init_db(app: FastAPI):
8686
},
8787
"apps": {
8888
"demo": {
89-
"models": ["examples.service.models.demo.__init__"],
89+
"models": ["examples.service.models.demo"],
9090
"default_connection": "demo_ro"
9191
}
9292
}

requirements-dev.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
aiomysql
2+
autopep8
3+
cryptography
4+
faker
5+
fastapi
6+
pytest
7+
tortoise-orm
8+
uvicorn

tests/__init__.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import asyncio
2+
3+
from tortoise import Tortoise
4+
5+
6+
def init_test_orm():
7+
"""
8+
CREATE USER 'demo_test'@'localhost' IDENTIFIED BY 'demo_TEST#0';
9+
GRANT ALL ON demo.* TO 'demo_test'@'localhost';
10+
"""
11+
config = {
12+
"timezone": "Asia/Shanghai",
13+
"connections": {
14+
"test": {
15+
"engine": "tortoise.backends.mysql",
16+
"credentials": {
17+
"host": "localhost",
18+
"port": 3306,
19+
"user": "demo_test",
20+
"password": "demo_TEST#0",
21+
"database": "demo",
22+
"pool_recycle": 3600,
23+
"maxsize": 10,
24+
},
25+
},
26+
},
27+
"apps": {
28+
"demo": {
29+
"models": ["examples.service.models.demo"],
30+
"default_connection": "test"
31+
}
32+
}
33+
}
34+
asyncio.run(Tortoise.init(config=config))

tests/test_sqlizer.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from datetime import datetime
22
from unittest import TestCase
33

4-
from fastapi_esql import Cases, RawSQL, SQLizer
4+
from fastapi_esql import (
5+
Cases, RawSQL, SQLizer,
6+
Q, QsParsingError, WrongParamsError,
7+
)
8+
9+
from . import init_test_orm
10+
from examples.service.models.demo import Account
511

612

713
class TestRawSQL(TestCase):
@@ -27,6 +33,31 @@ def test_no_default(self):
2733

2834
class TestSQLizer(TestCase):
2935

36+
def test_resolve_wheres(self):
37+
with self.assertRaises(WrongParamsError):
38+
SQLizer.resolve_wheres(object())
39+
40+
aids = (1, 2, 3)
41+
assert SQLizer.resolve_wheres(
42+
f"id IN ({','.join(map(str, aids))}) AND gender=1"
43+
) == "id IN (1,2,3) AND gender=1"
44+
init_test_orm()
45+
assert SQLizer.resolve_wheres(
46+
Q(Q(id__in=aids), Q(gender=1), join_type="AND"), Account
47+
) == "`id` IN (1,2,3) AND `gender`=1"
48+
assert SQLizer.resolve_wheres(
49+
{"id__in": aids, "gender": 1}, Account
50+
) == "`id` IN (1,2,3) AND `gender`=1"
51+
assert SQLizer.resolve_wheres(
52+
[Q(id__in=aids), Q(gender=1)], Account
53+
) == "`id` IN (1,2,3) AND `gender`=1"
54+
55+
with self.assertRaises(WrongParamsError):
56+
SQLizer.resolve_wheres(set())
57+
58+
with self.assertRaises(QsParsingError):
59+
SQLizer.resolve_wheres({}, Account)
60+
3061
def test_resolve_orders(self):
3162
orders = SQLizer.resolve_orders(["-created_at", "name"])
3263
assert orders == "created_at DESC, name ASC"

0 commit comments

Comments
 (0)