171 lines
5.6 KiB
Python
171 lines
5.6 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
|
|
from app.features.content.service import ContentService
|
|
|
|
|
|
class _DummyMappings:
|
|
def __init__(self, rows: list[dict]):
|
|
self._rows = rows
|
|
|
|
def mappings(self):
|
|
return self._rows
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_resolve_content_item_collection_counts_prioritizes_structured_collections() -> None:
|
|
cid_points = uuid4()
|
|
cid_gao_wu = uuid4()
|
|
cid_patterns = uuid4()
|
|
cid_narrative = uuid4()
|
|
|
|
session = AsyncMock()
|
|
rows = [
|
|
# 12 multivalue points rows
|
|
*[
|
|
{"content_id": cid_points, "attr_code": "points", "is_multivalue": True, "value_json": None}
|
|
for _ in range(12)
|
|
],
|
|
# Gao Wu has 6 gao_wu_command_points rows AND 1 stale point_codes row -> must pick 6
|
|
*[
|
|
{"content_id": cid_gao_wu, "attr_code": "gao_wu_command_points", "is_multivalue": True, "value_json": None}
|
|
for _ in range(6)
|
|
],
|
|
{"content_id": cid_gao_wu, "attr_code": "point_codes", "is_multivalue": True, "value_json": None},
|
|
# Scalar JSON array for patterns (Four Levels)
|
|
{
|
|
"content_id": cid_patterns,
|
|
"attr_code": "patterns",
|
|
"is_multivalue": False,
|
|
"value_json": [{"name": "P1"}, {"name": "P2"}, {"name": "P3"}, {"name": "P4"}],
|
|
},
|
|
# Non-collection attribute (e.g. description) should not yield an override
|
|
]
|
|
session.execute = AsyncMock(return_value=_DummyMappings(rows))
|
|
repo = ContentRepository(session)
|
|
|
|
counts = await repo.resolve_content_item_collection_counts(
|
|
content_ids={cid_points, cid_gao_wu, cid_patterns, cid_narrative}
|
|
)
|
|
|
|
assert counts[cid_points] == 12
|
|
assert counts[cid_gao_wu] == 6
|
|
assert counts[cid_patterns] == 4
|
|
assert cid_narrative not in counts
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_resolve_template_content_counts_filters_statuses_and_label_sums() -> None:
|
|
session = AsyncMock()
|
|
count_rows = [
|
|
{"module_code": "m0012", "template_code": "m0012_ct0005", "item_count": 9},
|
|
{"module_code": "m0001", "template_code": "m0001_ct0002", "item_count": 15},
|
|
]
|
|
sum_rows = [
|
|
{
|
|
"module_code": "m0001",
|
|
"template_code": "m0001_ct0002",
|
|
"content_label": "points",
|
|
"total_count": 409,
|
|
}
|
|
]
|
|
session.execute = AsyncMock(
|
|
side_effect=[_DummyMappings(count_rows), _DummyMappings(sum_rows)]
|
|
)
|
|
repo = ContentRepository(session)
|
|
|
|
counts, sums = await repo.resolve_template_content_counts(
|
|
template_refs={("m0012", "m0012_ct0005"), ("m0001", "m0001_ct0002")},
|
|
include_draft=False,
|
|
)
|
|
|
|
assert counts == {
|
|
("m0012", "m0012_ct0005"): 9,
|
|
("m0001", "m0001_ct0002"): 15,
|
|
}
|
|
assert sums == {(("m0001", "m0001_ct0002"), "points"): 409}
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_hydrate_dynamic_content_counts_handles_template_and_content_refs_safely() -> None:
|
|
xi_cleft_id = uuid4()
|
|
meridian_id = uuid4()
|
|
|
|
repo = MagicMock()
|
|
repo.resolve_template_content_counts = AsyncMock(
|
|
return_value=(
|
|
{
|
|
("m0001", "m0001_ct0002"): 15,
|
|
("m0012", "m0012_ct0005"): 9,
|
|
("m0042", "m0042_ct0002"): 11,
|
|
},
|
|
{(("m0001", "m0001_ct0002"), "points"): 409},
|
|
)
|
|
)
|
|
repo.resolve_content_item_collection_counts = AsyncMock(
|
|
return_value={xi_cleft_id: 16}
|
|
)
|
|
|
|
service = ContentService(
|
|
session=AsyncMock(),
|
|
repository=repo,
|
|
cache=MagicMock(),
|
|
invalidator=MagicMock(),
|
|
search_projection_writer=MagicMock(),
|
|
)
|
|
|
|
payloads = [
|
|
# Nested index matching child content_label -> sums to 409
|
|
{
|
|
"name": "Acupoints",
|
|
"content_count": 100,
|
|
"content_label": "points",
|
|
"link": {"type": "template_ref", "target": {"module_code": "m0001", "template_code": "m0001_ct0002"}},
|
|
},
|
|
# Template index with differing label -> uses target template row count 9
|
|
{
|
|
"name": "Spleen",
|
|
"content_count": 10,
|
|
"content_label": "patterns",
|
|
"link": {"type": "template_ref", "target": {"module_code": "m0012", "template_code": "m0012_ct0005"}},
|
|
},
|
|
# Content ref with points collection -> updates 10 -> 16
|
|
{
|
|
"name": "Xi Cleft",
|
|
"content_count": 10,
|
|
"content_label": "points",
|
|
"link": {
|
|
"type": "content_ref",
|
|
"target": {"module_code": "m0001", "template_code": "m0001_ct0011", "content_id": str(xi_cleft_id)},
|
|
},
|
|
},
|
|
# Content ref without collection count (e.g. Meridian) -> preserves authored 10
|
|
{
|
|
"name": "Lung",
|
|
"content_count": 10,
|
|
"link": {
|
|
"type": "content_ref",
|
|
"target": {"module_code": "m0001", "template_code": "m0001_ct0005", "content_id": str(meridian_id)},
|
|
},
|
|
},
|
|
# Item without link -> untouched
|
|
{
|
|
"name": "Lung Points",
|
|
"content_count": 11,
|
|
"content_label": "points",
|
|
},
|
|
]
|
|
|
|
await service._hydrate_dynamic_content_counts(payloads, preview=False)
|
|
|
|
assert payloads[0]["content_count"] == 409
|
|
assert payloads[1]["content_count"] == 9
|
|
assert payloads[2]["content_count"] == 16
|
|
assert payloads[3]["content_count"] == 10
|
|
assert payloads[4]["content_count"] == 11
|
|
|