Skip to content

Commit 8c3aff7

Browse files
authored
fix: set global fallback default in Sanic register_tortoise (tortoise#2090)
* fix: set global fallback default in Sanic register_tortoise fix: added changelog * fix: changelog and added tests
1 parent c25d0a1 commit 8c3aff7

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Changelog
1515
Fixed
1616
^^^^^
1717
- Type checking of None assignment to nullable fields (#2089)
18+
- Fix set global fallback default in Sanic register_tortoise (#2090)
1819

1920

2021
1.0.0

examples/sanic/_tests.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path
33

44
import pytest
5-
from sanic_testing import TestManager
5+
from sanic_testing.reusable import ReusableClient
66

77
try:
88
import main
@@ -21,18 +21,21 @@ def anyio_backend() -> str:
2121

2222

2323
@pytest.fixture
24-
def app():
24+
def client():
2525
sanic_app = main.app
26-
TestManager(sanic_app)
27-
return sanic_app
2826

27+
# make register_tortoise treat this as sanic-testing (ReusableClient doesn't set this flag)
28+
sanic_app._test_manager = True
29+
client = ReusableClient(sanic_app)
30+
with client:
31+
yield client
2932

30-
@pytest.mark.anyio
31-
async def test_basic_asgi_client(app):
32-
request, response = await app.asgi_client.get("/")
33+
34+
def test_basic_test_client(client):
35+
request, response = client.get("/")
3336
assert response.status == 200
3437
assert b'{"users":[' in response.body
3538

36-
request, response = await app.asgi_client.post("/user")
39+
request, response = client.post("/user")
3740
assert response.status == 200
3841
assert re.match(rb'{"user":"User \d+: New User"}$', response.body)

tortoise/contrib/sanic/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def register_tortoise(
1717
db_url: str | None = None,
1818
modules: dict[str, Iterable[str | ModuleType]] | None = None,
1919
generate_schemas: bool = False,
20+
_enable_global_fallback: bool = True,
2021
) -> None:
2122
"""
2223
Registers ``before_server_start`` and ``after_server_stop`` hooks to set-up and tear-down
@@ -81,7 +82,13 @@ def register_tortoise(
8182
"""
8283

8384
async def tortoise_init() -> None:
84-
await Tortoise.init(config=config, config_file=config_file, db_url=db_url, modules=modules)
85+
await Tortoise.init(
86+
config=config,
87+
config_file=config_file,
88+
db_url=db_url,
89+
modules=modules,
90+
_enable_global_fallback=_enable_global_fallback,
91+
)
8592
logger.info("Tortoise-ORM started, %s, %s", get_connections()._get_storage(), Tortoise.apps) # pylint: disable=W0212
8693

8794
if generate_schemas:

0 commit comments

Comments
 (0)