115 lines
3.5 KiB
Python
115 lines
3.5 KiB
Python
from __future__ import annotations
|
|
|
|
from unittest.mock import AsyncMock, MagicMock
|
|
from uuid import uuid4
|
|
import pytest
|
|
|
|
from app.features.content.repository import ContentRepository, ContentReferenceLookup
|
|
from app.features.search.repository import SearchRepository
|
|
from app.features.search.schema import SearchScope
|
|
|
|
|
|
class _DummyMappingsResult:
|
|
def __init__(self, rows=None):
|
|
self._rows = rows or []
|
|
|
|
def mappings(self):
|
|
return self
|
|
|
|
def all(self):
|
|
return self._rows
|
|
|
|
def __iter__(self):
|
|
return iter(self._rows)
|
|
|
|
def scalar_one(self):
|
|
return len(self._rows)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_search_list_modules_scoped_allows_inactive_modules() -> None:
|
|
session = AsyncMock()
|
|
session.execute = AsyncMock(return_value=_DummyMappingsResult())
|
|
repo = SearchRepository(session)
|
|
|
|
await repo.list_modules(SearchScope(module_codes=["MOD_INACTIVE"]))
|
|
sql_text = str(session.execute.call_args[0][0])
|
|
assert "status in ('active', 'inactive')" in sql_text
|
|
assert "is_active = true" not in sql_text
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_search_list_modules_unscoped_requires_active() -> None:
|
|
session = AsyncMock()
|
|
session.execute = AsyncMock(return_value=_DummyMappingsResult())
|
|
repo = SearchRepository(session)
|
|
|
|
await repo.list_modules(SearchScope())
|
|
sql_text = str(session.execute.call_args[0][0])
|
|
assert "is_active = true" in sql_text
|
|
assert "status = 'active'" in sql_text
|
|
|
|
|
|
def test_search_scope_sql_allows_inactive_modules() -> None:
|
|
session = AsyncMock()
|
|
repo = SearchRepository(session)
|
|
params: dict = {}
|
|
|
|
sql = repo._scope_sql(SearchScope(module_codes=["MOD_INACTIVE"]), params)
|
|
assert "scope_m.status in ('active', 'inactive')" in sql
|
|
assert "scope_m.is_active = true" not in sql
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_search_scoped_query_allows_inactive_modules() -> None:
|
|
session = AsyncMock()
|
|
session.execute = AsyncMock(return_value=_DummyMappingsResult())
|
|
repo = SearchRepository(session)
|
|
|
|
await repo.search(
|
|
scope=SearchScope(module_codes=["MOD_INACTIVE"]),
|
|
query="acupoint",
|
|
filters=[],
|
|
filter_logic="and",
|
|
sort_field="relevance",
|
|
sort_direction="desc",
|
|
page=1,
|
|
page_size=20,
|
|
)
|
|
|
|
executed_sqls = [str(call[0][0]) for call in session.execute.call_args_list]
|
|
assert any("m.status in ('active', 'inactive')" in sql for sql in executed_sqls)
|
|
assert all("m.is_active = true" not in sql for sql in executed_sqls)
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_content_reference_resolution_allows_inactive_modules() -> None:
|
|
session = AsyncMock()
|
|
dummy_result = MagicMock()
|
|
dummy_result.mappings.return_value = [
|
|
{
|
|
"content_id": uuid4(),
|
|
"module_code": "MOD_INACTIVE",
|
|
"template_code": "TPL_1",
|
|
"source_key": "ITEM_1",
|
|
}
|
|
]
|
|
session.execute = AsyncMock(return_value=dummy_result)
|
|
repo = ContentRepository(session)
|
|
|
|
await repo.resolve_content_reference_targets(
|
|
references={
|
|
ContentReferenceLookup(
|
|
module_code="MOD_INACTIVE",
|
|
template_code="TPL_1",
|
|
source_key="ITEM_1",
|
|
)
|
|
},
|
|
include_draft=False,
|
|
)
|
|
|
|
stmt = session.execute.call_args[0][0]
|
|
compiled = str(stmt.compile(compile_kwargs={"literal_binds": True}))
|
|
assert "inactive" in compiled.lower()
|
|
assert "modules.is_active IS true" not in compiled
|