Files
FabledCurator/tests/test_ml_aliases.py
T
bvandeusen 03c6a61673 feat(fc2b): add AliasService — (name, category) -> canonical tag
resolve() / resolve_many() (batch, used by the suggestion read path),
idempotent create, remove, list_all. Category-scoped so 'naruto' as
character vs copyright map to different canonicals. Tests marked
integration (real DB).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-15 07:37:13 -04:00

86 lines
2.8 KiB
Python

import pytest
from backend.app.models import TagKind
from backend.app.services.ml.aliases import AliasService
from backend.app.services.tag_service import TagService
pytestmark = pytest.mark.integration
@pytest.mark.asyncio
async def test_create_and_resolve(db):
tags = TagService(db)
canonical = await tags.find_or_create("Sasuke Uchiha", TagKind.character)
aliases = AliasService(db)
await aliases.create("uchiha_sasuke", "character", canonical.id)
resolved = await aliases.resolve("uchiha_sasuke", "character")
assert resolved is not None
assert resolved.id == canonical.id
@pytest.mark.asyncio
async def test_resolve_returns_none_when_no_alias(db):
aliases = AliasService(db)
assert await aliases.resolve("nonexistent", "general") is None
@pytest.mark.asyncio
async def test_category_scoped(db):
tags = TagService(db)
char = await tags.find_or_create("Naruto", TagKind.character)
copy = await tags.find_or_create("Naruto", TagKind.fandom)
aliases = AliasService(db)
await aliases.create("naruto", "character", char.id)
await aliases.create("naruto", "copyright", copy.id)
assert (await aliases.resolve("naruto", "character")).id == char.id
assert (await aliases.resolve("naruto", "copyright")).id == copy.id
@pytest.mark.asyncio
async def test_create_idempotent(db):
tags = TagService(db)
t = await tags.find_or_create("X", TagKind.general)
aliases = AliasService(db)
await aliases.create("x_alias", "general", t.id)
await aliases.create("x_alias", "general", t.id) # no error
assert (await aliases.resolve("x_alias", "general")).id == t.id
@pytest.mark.asyncio
async def test_resolve_many(db):
tags = TagService(db)
a = await tags.find_or_create("Alice", TagKind.character)
b = await tags.find_or_create("Bob", TagKind.artist)
aliases = AliasService(db)
await aliases.create("alice_x", "character", a.id)
await aliases.create("bob_x", "artist", b.id)
out = await aliases.resolve_many(
[("alice_x", "character"), ("bob_x", "artist"), ("unknown", "general")]
)
assert out[("alice_x", "character")].id == a.id
assert out[("bob_x", "artist")].id == b.id
assert ("unknown", "general") not in out
@pytest.mark.asyncio
async def test_remove(db):
tags = TagService(db)
t = await tags.find_or_create("Y", TagKind.general)
aliases = AliasService(db)
await aliases.create("y_alias", "general", t.id)
await aliases.remove("y_alias", "general")
assert await aliases.resolve("y_alias", "general") is None
@pytest.mark.asyncio
async def test_list_all(db):
tags = TagService(db)
t = await tags.find_or_create("Z", TagKind.general)
aliases = AliasService(db)
await aliases.create("z1", "general", t.id)
rows = await aliases.list_all()
assert any(r.alias_string == "z1" and r.canonical_tag_name == "Z" for r in rows)