fix: preserve configured SQLite async driver
This commit is contained in:
@@ -6,7 +6,7 @@ from app.config import settings
|
|||||||
|
|
||||||
# 处理 sqlite 协议兼容
|
# 处理 sqlite 协议兼容
|
||||||
db_url = settings.DATABASE_URL
|
db_url = settings.DATABASE_URL
|
||||||
if db_url.startswith("sqlite"):
|
if db_url.startswith("sqlite://"):
|
||||||
db_url = db_url.replace("sqlite://", "sqlite+aiosqlite://")
|
db_url = db_url.replace("sqlite://", "sqlite+aiosqlite://")
|
||||||
|
|
||||||
engine = create_async_engine(db_url, echo=False, pool_pre_ping=True)
|
engine = create_async_engine(db_url, echo=False, pool_pre_ping=True)
|
||||||
|
|||||||
@@ -1,10 +1,48 @@
|
|||||||
"""Regression coverage for application dependency module imports."""
|
"""Regression coverage for application dependency module imports."""
|
||||||
|
|
||||||
|
import importlib
|
||||||
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
def test_dependency_module_compiles():
|
def test_dependency_module_compiles():
|
||||||
"""Database initialization dependencies remain valid Python syntax."""
|
"""Database initialization dependencies remain valid Python syntax."""
|
||||||
module_path = Path(__file__).parents[1] / "app" / "core" / "deps.py"
|
module_path = Path(__file__).parents[1] / "app" / "core" / "deps.py"
|
||||||
|
|
||||||
compile(module_path.read_text(encoding="utf-8"), str(module_path), "exec")
|
compile(module_path.read_text(encoding="utf-8"), str(module_path), "exec")
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("database_url", "expected_url"),
|
||||||
|
[
|
||||||
|
("sqlite:///./pingwatch.db", "sqlite+aiosqlite:///./pingwatch.db"),
|
||||||
|
(
|
||||||
|
"sqlite+aiosqlite:///./pingwatch.db",
|
||||||
|
"sqlite+aiosqlite:///./pingwatch.db",
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_dependency_module_normalizes_sqlite_url_once(
|
||||||
|
monkeypatch, database_url, expected_url
|
||||||
|
):
|
||||||
|
"""Plain SQLite URLs gain the async driver without duplicating an existing one."""
|
||||||
|
monkeypatch.setenv("DATABASE_URL", database_url)
|
||||||
|
sys.modules.pop("app.core.deps", None)
|
||||||
|
sys.modules.pop("app.config", None)
|
||||||
|
|
||||||
|
deps = importlib.import_module("app.core.deps")
|
||||||
|
|
||||||
|
assert str(deps.engine.url) == expected_url
|
||||||
|
|
||||||
|
|
||||||
|
def test_dependency_module_initializes_with_default_sqlite_url(monkeypatch):
|
||||||
|
"""The default database setting can initialize the asynchronous dependency."""
|
||||||
|
monkeypatch.delenv("DATABASE_URL", raising=False)
|
||||||
|
sys.modules.pop("app.core.deps", None)
|
||||||
|
sys.modules.pop("app.config", None)
|
||||||
|
|
||||||
|
deps = importlib.import_module("app.core.deps")
|
||||||
|
|
||||||
|
assert str(deps.engine.url) == "sqlite+aiosqlite:///./pingwatch.db"
|
||||||
|
|||||||
Reference in New Issue
Block a user