fix(tests): isolate SQLite test databases
Co-authored-by: OpenAI/GPT-5.6-Terra <vibecoder@antoineve.me>
This commit is contained in:
@@ -2,6 +2,7 @@ import pytest
|
||||
|
||||
from app import create_app
|
||||
from app import db as _db
|
||||
from tests.in_memory_db import IN_MEMORY_DATABASE_URI, in_memory_engine_options
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@@ -72,9 +73,12 @@ forfait = 0
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
application = create_app(config_path=str(config_path))
|
||||
application = create_app(
|
||||
config_path=str(config_path),
|
||||
database_uri=IN_MEMORY_DATABASE_URI,
|
||||
engine_options=in_memory_engine_options(),
|
||||
)
|
||||
application.config["TESTING"] = True
|
||||
application.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
|
||||
|
||||
with application.app_context():
|
||||
_db.create_all()
|
||||
|
||||
20
tests/in_memory_db.py
Normal file
20
tests/in_memory_db.py
Normal file
@@ -0,0 +1,20 @@
|
||||
"""Configuration SQLite mémoire partagée entre fixtures et helpers de tests.
|
||||
|
||||
Ce module est volontairement indépendant de ``conftest`` afin d'être
|
||||
importable de manière portable, y compris sous ``pytest --import-mode=importlib``
|
||||
(où ``conftest`` n'est pas importable comme module ordinaire). Il centralise la
|
||||
configuration mémoire partagée entre la fixture ``app`` et les helpers de tests
|
||||
qui appellent directement ``create_app(...)``, évitant qu'une factory de test
|
||||
initialise accidentellement ``instance/worklog.db``.
|
||||
"""
|
||||
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
IN_MEMORY_DATABASE_URI = "sqlite:///:memory:"
|
||||
|
||||
|
||||
def in_memory_engine_options() -> dict[str, object]:
|
||||
return {
|
||||
"poolclass": StaticPool,
|
||||
"connect_args": {"check_same_thread": False},
|
||||
}
|
||||
11
tests/test_app_factory.py
Normal file
11
tests/test_app_factory.py
Normal file
@@ -0,0 +1,11 @@
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.pool import StaticPool
|
||||
|
||||
from app import db
|
||||
|
||||
|
||||
def test_app_fixture_uses_one_in_memory_database_connection(app):
|
||||
with app.app_context():
|
||||
assert str(db.engine.url) == "sqlite:///:memory:"
|
||||
assert isinstance(db.engine.pool, StaticPool)
|
||||
assert sa.inspect(db.engine).has_table("work_entries")
|
||||
@@ -1,6 +1,7 @@
|
||||
import pytest
|
||||
|
||||
from app import create_app
|
||||
from tests.in_memory_db import IN_MEMORY_DATABASE_URI, in_memory_engine_options
|
||||
|
||||
_MINIMAL_CONFIG = """
|
||||
[vehicles.citadine]
|
||||
@@ -26,7 +27,11 @@ default_motor_vehicle_id = "{vehicle_id}"
|
||||
def _create_app_with_home_assistant_config(tmp_path, **values):
|
||||
config_path = tmp_path / "config.toml"
|
||||
config_path.write_text(_MINIMAL_CONFIG.format(**values), encoding="utf-8")
|
||||
return create_app(config_path=str(config_path))
|
||||
return create_app(
|
||||
config_path=str(config_path),
|
||||
database_uri=IN_MEMORY_DATABASE_URI,
|
||||
engine_options=in_memory_engine_options(),
|
||||
)
|
||||
|
||||
|
||||
def test_get_vehicles_returns_configured_vehicles(app):
|
||||
@@ -164,4 +169,8 @@ timezone = "Europe/Paris"
|
||||
)
|
||||
|
||||
with pytest.raises(ValueError, match="clé.*manquante"):
|
||||
create_app(config_path=str(config_path))
|
||||
create_app(
|
||||
config_path=str(config_path),
|
||||
database_uri=IN_MEMORY_DATABASE_URI,
|
||||
engine_options=in_memory_engine_options(),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user