from __future__ import annotations from uuid import uuid4 import pytest from app.modules.content.repository_contract import TemplateContractRecord from app.modules.content.service_contract import TemplateContractConfigurationError, TemplateContractService class FakeRepository: def __init__(self, record: TemplateContractRecord): self.record = record async def get_by_code(self, **kwargs): return self.record @pytest.mark.asyncio async def test_collection_contract_is_returned(): record = TemplateContractRecord( id=uuid4(), code="M0001_CT0001", attribute_prefix_code="M0001_CT0001", metadata={ "presentation": { "mode": "collection", "renderers": { "collection": "module_catalog_collection", "collection_item": "module_catalog_card", "detail": "module_catalog_detail", }, }, "search": { "free_text_enabled": True, "default_logic": "and", "fields": [], "sort_fields": [{"field": "sort_order", "label": "Display Order"}], "default_sort": [{"field": "sort_order", "direction": "asc"}], }, }, ) service = TemplateContractService(repository=FakeRepository(record)) result = await service.get_template_contract(session=None, template_code="CT0001") assert result.template_code == "M0001_CT0001" assert result.module_code == "M0001" assert result.presentation.mode.value == "collection" @pytest.mark.asyncio async def test_missing_presentation_fails(): record = TemplateContractRecord( id=uuid4(), code="M0001_CT0001", attribute_prefix_code="M0001_CT0001", metadata={"search": {"fields": [], "sort_fields": [], "default_sort": []}}, ) service = TemplateContractService(repository=FakeRepository(record)) with pytest.raises(TemplateContractConfigurationError): await service.get_template_contract(session=None, template_code="CT0001")