import os import tempfile import pytest @pytest.fixture() def app(): """Create app with a temporary file-based SQLite DB, removed after the test.""" db_fd, db_path = tempfile.mkstemp(suffix=".db") os.close(db_fd) class TestConfig: SQLALCHEMY_DATABASE_URI = f"sqlite:///{db_path}" SECRET_KEY = "test-secret" SQLALCHEMY_TRACK_MODIFICATIONS = False TESTING = True from app import create_app flask_app = create_app(TestConfig) yield flask_app os.unlink(db_path) @pytest.fixture() def client(app): return app.test_client() @pytest.fixture() def seeded_client(app): """Test client pre-loaded with 2 devices and 3 batteries (2 available, 1 retired).""" with app.test_client() as c: c.post("/device/add", data={"name": "Device A", "battery_slots": "2"}) c.post("/device/add", data={"name": "Device B", "battery_slots": "1"}) c.post("/battery/add", data={"label": "TST-01", "brand": "BrandX", "status": "available"}) c.post("/battery/add", data={"label": "TST-02", "brand": "BrandY", "status": "available"}) c.post("/battery/add", data={"label": "TST-03", "brand": "BrandX", "status": "retired"}) yield c