Skip to content

Commit 2aa49ad

Browse files
authored
fix: sanic example raises no such table (#1917)
* fix: sanic example not work * Fix sanic testing manager not generate schemas
1 parent 529edcc commit 2aa49ad

File tree

7 files changed

+80
-103
lines changed

7 files changed

+80
-103
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,11 @@ jobs:
7373
run: |
7474
PYTHONPATH=$DEST_FASTAPI pytest $PYTEST_ARGS $DEST_FASTAPI/_tests.py
7575
PYTHONPATH=$DEST_BLACKSHEEP pytest $PYTEST_ARGS $DEST_BLACKSHEEP/_tests.py
76+
PYTHONPATH=$DEST_SANIC pytest $PYTEST_ARGS $DEST_SANIC/_tests.py
7677
env:
7778
DEST_FASTAPI: examples/fastapi
7879
DEST_BLACKSHEEP: examples/blacksheep
80+
DEST_SANIC: examples/sanic
7981
PYTHONDEVMODE: 1
8082
PYTEST_ARGS: "-n auto --cov=tortoise --cov-append --cov-branch --tb=native -q"
8183
- name: Upload Coverage

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ coverage.xml
5353
.hypothesis/
5454
.vscode/
5555
examples/*.sqlite3
56+
examples/*/*.sqlite3*
5657

5758
# Translations
5859
*.mo
@@ -108,4 +109,4 @@ ENV/
108109
.mypy_cache/
109110

110111
# macos
111-
.DS_Store
112+
.DS_Store

examples/sanic/_tests.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import re
2+
from pathlib import Path
3+
4+
import pytest
5+
from sanic_testing import TestManager
6+
7+
try:
8+
import main
9+
except ImportError:
10+
if (cwd := Path.cwd()) == (parent := Path(__file__).parent):
11+
dirpath = "."
12+
else:
13+
dirpath = str(parent.relative_to(cwd))
14+
print(f"You may need to explicitly declare python path:\n\nexport PYTHONPATH={dirpath}\n")
15+
raise
16+
17+
18+
@pytest.fixture(scope="module")
19+
def anyio_backend() -> str:
20+
return "asyncio"
21+
22+
23+
@pytest.fixture
24+
def app():
25+
sanic_app = main.app
26+
TestManager(sanic_app)
27+
return sanic_app
28+
29+
30+
@pytest.mark.anyio
31+
async def test_basic_asgi_client(app):
32+
request, response = await app.asgi_client.get("/")
33+
assert response.status == 200
34+
assert b'{"users":[' in response.body
35+
36+
request, response = await app.asgi_client.post("/user")
37+
assert response.status == 200
38+
assert re.match(rb'{"user":"User \d+: New User"}$', response.body)

examples/sanic/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ async def list_all(request):
1717
return response.json({"users": [str(user) for user in users]})
1818

1919

20-
@app.route("/user")
20+
@app.post("/user")
2121
async def add_user(request):
2222
user = await Users.create(name="New User")
2323
return response.json({"user": str(user)})
2424

2525

2626
register_tortoise(
27-
app, db_url="sqlite://:memory:", modules={"models": ["models"]}, generate_schemas=True
27+
app, db_url="sqlite://db.sqlite3", modules={"models": ["models"]}, generate_schemas=True
2828
)
2929

3030

3131
if __name__ == "__main__":
32-
app.run(port=5000)
32+
app.run(port=5000, debug=True)

poetry.lock

Lines changed: 25 additions & 93 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ twine = "*"
8181
quart = "*"
8282
# Sample integration - Sanic
8383
sanic = "*"
84+
sanic-testing = "*"
8485
# Sample integration - Starlette
8586
starlette = "*"
8687
# Pydantic support

tortoise/contrib/sanic/__init__.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,17 +87,20 @@ async def tortoise_init() -> None:
8787

8888
if generate_schemas:
8989

90-
@app.listener("main_process_start")
91-
async def init_orm_main(app, loop): # pylint: disable=W0612
90+
@app.main_process_start
91+
async def init_orm_main(app): # pylint: disable=W0612
9292
await tortoise_init()
9393
logger.info("Tortoise-ORM generating schema")
9494
await Tortoise.generate_schemas()
9595

96-
@app.listener("before_server_start")
97-
async def init_orm(app, loop): # pylint: disable=W0612
96+
@app.before_server_start
97+
async def init_orm(app):
9898
await tortoise_init()
99+
if generate_schemas and getattr(app, "_test_manager", None):
100+
# Running by sanic-testing
101+
await Tortoise.generate_schemas()
99102

100-
@app.listener("after_server_stop")
101-
async def close_orm(app, loop): # pylint: disable=W0612
103+
@app.after_server_stop
104+
async def close_orm(app): # pylint: disable=W0612
102105
await connections.close_all()
103106
logger.info("Tortoise-ORM shutdown")

0 commit comments

Comments
 (0)