Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanic example raises no such table #1917

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,11 @@ jobs:
run: |
PYTHONPATH=$DEST_FASTAPI pytest $PYTEST_ARGS $DEST_FASTAPI/_tests.py
PYTHONPATH=$DEST_BLACKSHEEP pytest $PYTEST_ARGS $DEST_BLACKSHEEP/_tests.py
PYTHONPATH=$DEST_SANIC pytest $PYTEST_ARGS $DEST_SANIC/_tests.py
env:
DEST_FASTAPI: examples/fastapi
DEST_BLACKSHEEP: examples/blacksheep
DEST_SANIC: examples/sanic
PYTHONDEVMODE: 1
PYTEST_ARGS: "-n auto --cov=tortoise --cov-append --cov-branch --tb=native -q"
- name: Upload Coverage
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ coverage.xml
.hypothesis/
.vscode/
examples/*.sqlite3
examples/*/*.sqlite3*

# Translations
*.mo
Expand Down Expand Up @@ -108,4 +109,4 @@ ENV/
.mypy_cache/

# macos
.DS_Store
.DS_Store
38 changes: 38 additions & 0 deletions examples/sanic/_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import re
from pathlib import Path

import pytest
from sanic_testing import TestManager

try:
import main
except ImportError:
if (cwd := Path.cwd()) == (parent := Path(__file__).parent):
dirpath = "."
else:
dirpath = str(parent.relative_to(cwd))
print(f"You may need to explicitly declare python path:\n\nexport PYTHONPATH={dirpath}\n")
raise


@pytest.fixture(scope="module")
def anyio_backend() -> str:
return "asyncio"


@pytest.fixture
def app():
sanic_app = main.app
TestManager(sanic_app)
return sanic_app


@pytest.mark.anyio
async def test_basic_asgi_client(app):
request, response = await app.asgi_client.get("/")
assert response.status == 200
assert b'{"users":[' in response.body

request, response = await app.asgi_client.post("/user")
assert response.status == 200
assert re.match(rb'{"user":"User \d+: New User"}$', response.body)
6 changes: 3 additions & 3 deletions examples/sanic/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ async def list_all(request):
return response.json({"users": [str(user) for user in users]})


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


register_tortoise(
app, db_url="sqlite://:memory:", modules={"models": ["models"]}, generate_schemas=True
app, db_url="sqlite://db.sqlite3", modules={"models": ["models"]}, generate_schemas=True
)


if __name__ == "__main__":
app.run(port=5000)
app.run(port=5000, debug=True)
118 changes: 25 additions & 93 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ twine = "*"
quart = "*"
# Sample integration - Sanic
sanic = "*"
sanic-testing = "*"
# Sample integration - Starlette
starlette = "*"
# Pydantic support
Expand Down
15 changes: 9 additions & 6 deletions tortoise/contrib/sanic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,20 @@ async def tortoise_init() -> None:

if generate_schemas:

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

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

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