Files
NixReaperandClaude Sonnet 5 4cf3556433
CI / lint (push) Canceled after 0s
CI / typecheck (push) Canceled after 0s
CI / unit-tests (push) Canceled after 0s
CI / integration-tests (push) Canceled after 0s
CI / container-build (push) Canceled after 0s
CI / dependency-audit (push) Canceled after 0s
Fix test fixture: seed countries before companies referencing them
companies.country_code has a real FK to countries -- real ingestion
always calls get_or_create_country first (see _replace_movie_companies),
but the company browse test fixture called get_or_create_company
directly with a raw country code, violating that FK. Found by actually
running the tests against Postgres on the devbox.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
2026-09-05 19:15:09 -05:00

117 lines
4.0 KiB
Python

"""Integration tests for GET /api/v1/companies (browse/discover, section 17
-- additive beyond the spec's minimum endpoint list): filtering and
pagination against real PostgreSQL, exercised through
company_service.list_companies directly. Mirrors test_movie_browse.py;
companies have no popularity/date field, so unlike movies/series/people
there's no sort_by choice -- always alphabetical by name."""
from __future__ import annotations
import pytest
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.enums import CompanyType
from app.repositories.provider_repository import get_or_create_provider
from app.repositories.reference_repository import get_or_create_company, get_or_create_country
from app.services import company_service
_COMPANIES = [
("2001", "Zeta Studios", CompanyType.PRODUCTION_COMPANY, "US"),
("2002", "Alpha Network", CompanyType.NETWORK, "US"),
("2003", "Mid Distributors", CompanyType.DISTRIBUTOR, "GB"),
("2004", "Beta Pictures", CompanyType.PRODUCTION_COMPANY, "GB"),
]
async def _seed_companies(db_session: AsyncSession) -> None:
provider = await get_or_create_provider(db_session, "tmdb", "TMDB")
for code in {country for _, _, _, country in _COMPANIES}:
await get_or_create_country(db_session, code, code)
for external_id, name, company_type, country in _COMPANIES:
await get_or_create_company(
db_session,
provider_id=provider.id,
external_id=external_id,
name=name,
company_type=company_type,
country_code=country,
)
await db_session.commit()
@pytest.mark.asyncio
async def test_list_companies_defaults_to_alphabetical_by_name(db_session: AsyncSession):
await _seed_companies(db_session)
out = await company_service.list_companies(
db_session, company_type=None, country=None, page=1, page_size=20
)
assert out.total_results == 4
assert [c.name for c in out.results] == [
"Alpha Network",
"Beta Pictures",
"Mid Distributors",
"Zeta Studios",
]
@pytest.mark.asyncio
async def test_list_companies_filters_by_company_type(db_session: AsyncSession):
await _seed_companies(db_session)
out = await company_service.list_companies(
db_session, company_type="production_company", country=None, page=1, page_size=20
)
assert [c.name for c in out.results] == ["Beta Pictures", "Zeta Studios"]
@pytest.mark.asyncio
async def test_list_companies_filters_by_country(db_session: AsyncSession):
await _seed_companies(db_session)
out = await company_service.list_companies(
db_session, company_type=None, country="GB", page=1, page_size=20
)
assert [c.name for c in out.results] == ["Beta Pictures", "Mid Distributors"]
@pytest.mark.asyncio
async def test_list_companies_paginates(db_session: AsyncSession):
await _seed_companies(db_session)
page1 = await company_service.list_companies(
db_session, company_type=None, country=None, page=1, page_size=2
)
page2 = await company_service.list_companies(
db_session, company_type=None, country=None, page=2, page_size=2
)
assert page1.total_results == 4
assert page1.total_pages == 2
assert [c.name for c in page1.results] == ["Alpha Network", "Beta Pictures"]
assert [c.name for c in page2.results] == ["Mid Distributors", "Zeta Studios"]
@pytest.mark.asyncio
async def test_list_companies_rejects_unknown_company_type(db_session: AsyncSession):
with pytest.raises(company_service.InvalidCompanyFilterError):
await company_service.list_companies(
db_session, company_type="not_a_real_type", country=None, page=1, page_size=20
)
@pytest.mark.asyncio
async def test_list_companies_returns_empty_page_when_nothing_matches(db_session: AsyncSession):
await _seed_companies(db_session)
out = await company_service.list_companies(
db_session, company_type=None, country="ZZ", page=1, page_size=20
)
assert out.results == []
assert out.total_results == 0
assert out.total_pages == 0