"""Tests for :class:`app.services.pages.PageService`. Uses the session-scoped seeded ``db_engine`` fixture. """ from __future__ import annotations from sqlalchemy import Engine from app.models.entities import Page from app.services.pages import PageService def test_get_by_slug_returns_seeded_about_page(db_engine: Engine) -> None: """The seeded About page loads as a :class:`Page` dataclass.""" service = PageService(db_engine) page = service.get_by_slug("about") assert page is not None assert isinstance(page, Page) assert page.slug == "about" assert page.title == "About the Farm" # The sanitized HTML must contain a

since the seed Markdown # has multiple paragraphs; this also proves the Markdown pipeline # ran at seed time. assert "

" in page.body_html_cached assert page.published is True def test_get_by_slug_returns_none_for_unknown_slug(db_engine: Engine) -> None: """Unknown slugs return ``None`` rather than raising.""" service = PageService(db_engine) assert service.get_by_slug("does-not-exist") is None def test_get_by_slug_is_cached(db_engine: Engine) -> None: """The TTL cache wraps page lookups keyed by slug.""" service = PageService(db_engine) first = service.get_by_slug("about") second = service.get_by_slug("about") assert first is second def test_invalidate_all_forces_reload(db_engine: Engine) -> None: """After :meth:`invalidate_all` the next call re-hits the DB.""" service = PageService(db_engine) first = service.get_by_slug("about") service.invalidate_all() second = service.get_by_slug("about") assert first is not second # Same slug, same row — content equal, identity different. assert first is not None and second is not None assert first.slug == second.slug == "about"