Spaces:
Runtime error
Runtime error
| """Suite B β Smart Links data model (P0). | |
| Phase 9: Updated to remove stale _get_book_links import (function removed | |
| during pipeline refactoring). Tests now directly validate LinkRepository | |
| behavior and the book.buy_url fallback pattern. | |
| """ | |
| import pytest | |
| from app.repositories.link_repo import LinkRepository | |
| async def test_link_repo_returns_none_when_no_links_row( | |
| db_session, author_user, book_with_buy_url | |
| ): | |
| """B-03: get_for_book returns None when no links row exists (fallback to book.buy_url).""" | |
| repo = LinkRepository(db_session) | |
| link = await repo.get_for_book(book_with_buy_url.id, author_user.id) | |
| # No row in links table β fallback logic must use book.buy_url directly | |
| assert link is None | |
| # Verify book itself has the buy_url (pipeline reads this as fallback) | |
| assert book_with_buy_url.buy_url == "https://store.example.com/buy" | |
| assert book_with_buy_url.preview_url == "https://store.example.com/preview" | |
| async def test_smart_links_upsert_creates_links_row( | |
| db_session, author_user, book_with_buy_url | |
| ): | |
| """B-01: upsert_for_book syncs purchase_url to links table.""" | |
| repo = LinkRepository(db_session) | |
| await repo.upsert_for_book( | |
| author_user.id, | |
| book_with_buy_url.id, | |
| { | |
| "purchase_url": "https://store.example.com/new-buy", | |
| "preview_url": "https://store.example.com/new-preview", | |
| }, | |
| ) | |
| await db_session.commit() | |
| link = await repo.get_for_book(book_with_buy_url.id, author_user.id) | |
| assert link is not None | |
| assert link.purchase_url == "https://store.example.com/new-buy" | |
| assert link.preview_url == "https://store.example.com/new-preview" | |
| async def test_smart_links_upsert_updates_existing_row( | |
| db_session, author_user, book_with_buy_url | |
| ): | |
| """B-02: upsert_for_book updates an existing links row (idempotent).""" | |
| repo = LinkRepository(db_session) | |
| # First upsert | |
| await repo.upsert_for_book( | |
| author_user.id, | |
| book_with_buy_url.id, | |
| {"purchase_url": "https://first.example.com/buy"}, | |
| ) | |
| await db_session.commit() | |
| # Second upsert β should update, not duplicate | |
| await repo.upsert_for_book( | |
| author_user.id, | |
| book_with_buy_url.id, | |
| {"purchase_url": "https://updated.example.com/buy"}, | |
| ) | |
| await db_session.commit() | |
| link = await repo.get_for_book(book_with_buy_url.id, author_user.id) | |
| assert link is not None | |
| assert link.purchase_url == "https://updated.example.com/buy" | |