"""Shared pytest fixtures for the beer_data test suite.""" from __future__ import annotations import os import sys import pytest # The Flask app lives in src/flask and imports its siblings by bare name # (``import brewing``), so that directory has to be on the path. APP_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "src", "flask") if APP_DIR not in sys.path: sys.path.insert(0, APP_DIR) @pytest.fixture(autouse=True) def secret_key_env(monkeypatch): """The app refuses to start without SECRET_KEY — give the tests one.""" monkeypatch.setenv("SECRET_KEY", "test-secret-key-not-a-real-secret") @pytest.fixture() def mongo_client(): """An in-memory Mongo, shared by the repository the app builds.""" import mongomock return mongomock.MongoClient() @pytest.fixture() def client(mongo_client): """A Flask test client backed by an in-memory Mongo (mongomock).""" from app_factory import create_app app = create_app({ "TESTING": True, "WTF_CSRF_ENABLED": False, "SECRET_KEY": "test-secret", "MONGO_CLIENT": mongo_client, }) with app.test_client() as test_client: yield test_client @pytest.fixture() def repo(mongo_client): """A repository backed by an in-memory Mongo.""" from mongo.repository import BeerRepository return BeerRepository(client=mongo_client) @pytest.fixture() def logged_in(client, repo): """A client with an authenticated session and a user in the database.""" repo.users.insert_one({"username": "beer", "password": "beer_pass"}) response = client.post("/login", data={"username": "beer", "password": "beer_pass"}, follow_redirects=True) assert response.status_code == 200 return client def seed_readings(repository, rows): """Insert reading documents directly, bypassing the app. Each row is ``(beer_run_id, beer_type, sg, date, final_reading, comment)``. """ docs = [] for run_id, beer_type, sg, date, final, comment in rows: docs.append({ "beer_run_id": str(run_id), "beer_type": beer_type, "sg": str(sg), "date": date, "final_reading": "True" if final else "False", "comment": comment, }) if docs: repository.readings.insert_many(docs) return docs