Open-source personal finance manager. Self-hosted, privacy-first.
import uuid
from pathlib import Path
import pytest
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from app.models.category import Category
from app.models.category_group import CategoryGroup
from app.schemas.category import CategoryCreate, CategoryUpdate
from app.services.category_group_service import get_groups
from app.services.category_defaults import (
DEFAULT_CATEGORIES,
DEFAULT_GROUPS,
localized_name,
name_variants,
)
from app.services.category_service import (
create_category,
create_default_categories,
delete_category,
get_categories,
get_category,
update_category,
)
from app.services.rule_service import _ensure_categories_for_keys
# ---------------------------------------------------------------------------
# create_default_categories
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_create_default_categories(session: AsyncSession, test_user, test_workspace):
categories = await create_default_categories(session, test_user.id, lang="pt-BR")
assert len(categories) == len(DEFAULT_CATEGORIES)
names = {c.name for c in categories}
assert "Moradia" in names
assert "Alimentação" in names
assert "Transporte" in names
assert "Outros" in names
for cat in categories:
assert cat.is_system is True
@pytest.mark.asyncio
async def test_create_default_categories_german(session: AsyncSession, test_user, test_workspace):
categories = await create_default_categories(session, test_user.id, lang="de")
assert len(categories) == len(DEFAULT_CATEGORIES)
names = {c.name for c in categories}
assert "Wohnen" in names
assert "Essen & Trinken" in names
assert "Lebensmittel" in names
assert "Sonstiges" in names
for cat in categories:
assert cat.is_system is True
@pytest.mark.asyncio
async def test_create_default_categories_french(session: AsyncSession, test_user, test_workspace):
categories = await create_default_categories(session, test_user.id, lang="fr")
assert len(categories) == len(DEFAULT_CATEGORIES)
names = {c.name for c in categories}
assert "Logement" in names
assert "Alimentation & Restaurants" in names
assert "Courses" in names
assert "Autres" in names
for cat in categories:
assert cat.is_system is True
@pytest.mark.asyncio
async def test_create_default_categories_european_portuguese(session: AsyncSession, test_user, test_workspace):
categories = await create_default_categories(session, test_user.id, lang="pt-PT")
assert len(categories) == len(DEFAULT_CATEGORIES)
names = {c.name for c in categories}
assert "Habitação" in names
assert "Supermercado" in names
assert "Subscrições" in names
assert "Salário & Rendimentos" in names
for cat in categories:
assert cat.is_system is True
@pytest.mark.asyncio
async def test_create_default_categories_creates_groups(session: AsyncSession, test_user, test_workspace):
await create_default_categories(session, test_user.id, lang="pt-BR")
result = await session.execute(
select(CategoryGroup).where(CategoryGroup.user_id == test_user.id)
)
groups = result.scalars().all()
assert len(groups) == 6 # housing, food, transport, lifestyle, income, other
@pytest.mark.asyncio
async def test_create_default_categories_links_to_groups(session: AsyncSession, test_user, test_workspace):
categories = await create_default_categories(session, test_user.id, lang="pt-BR")
with_group = [c for c in categories if c.group_id is not None]
assert len(with_group) == len(DEFAULT_CATEGORIES)
@pytest.mark.asyncio
async def test_create_default_categories_english(session: AsyncSession, test_user, test_workspace):
categories = await create_default_categories(session, test_user.id, lang="en")
names = {c.name for c in categories}
assert "Housing" in names
assert "Food & Dining" in names
assert "Transport" in names
@pytest.mark.asyncio
@pytest.mark.parametrize(
"lang", ["es", "it", "pl", "ru", "uk", "nl", "sk", "hu", "cs", "el", "hi", "ja"]
)
async def test_create_default_categories_localized(session: AsyncSession, test_user, test_workspace, lang):
categories = await create_default_categories(session, test_user.id, lang=lang)
assert len(categories) == len(DEFAULT_CATEGORIES)
names = {c.name for c in categories}
assert names == {data["names"][lang] for data in DEFAULT_CATEGORIES.values()}
for cat in categories:
assert cat.is_system is True
groups = await get_groups(session, test_workspace.id, include_hidden=True)
group_names = {g.name for g in groups}
assert group_names == {data["names"][lang] for data in DEFAULT_GROUPS.values()}
def test_default_taxonomy_covers_all_supported_languages():
"""Read the languages off the UI bundles instead of repeating them here.
A hand-written list is a copy, and a copy goes stale the moment a locale
PR lands: the taxonomy then seeds English for the new language and nothing
says so. Deriving the list means adding `frontend/src/locales/xx.json`
fails this test until the taxonomy covers `xx` too.
"""
locales = Path(__file__).resolve().parents[2] / "frontend" / "src" / "locales"
if not locales.is_dir():
pytest.skip("frontend locales not available outside a full checkout")
langs = sorted(p.stem for p in locales.glob("*.json"))
assert "en" in langs, f"no locale bundles found in {locales}"
entries = {f"category '{k}'": d for k, d in DEFAULT_CATEGORIES.items()} | {
f"group '{k}'": d for k, d in DEFAULT_GROUPS.items()
}
for label, data in entries.items():
for lang in langs:
assert lang in data["names"], f"{label} is missing a '{lang}' translation"
def test_localized_name_falls_back_safely():
entry = {"names": {"en": "Housing", "es": "Vivienda"}}
assert localized_name(entry, "es") == "Vivienda"
assert localized_name(entry, "xx") == "Housing"
# No English either: any available translation beats an exception.
assert localized_name({"names": {"de": "Wohnen"}}, "xx") == "Wohnen"
def test_aliases_are_matched_but_never_seeded():
housing = DEFAULT_GROUPS["housing"]
assert "Alojamiento" in name_variants(housing)
assert localized_name(housing, "es") == "Vivienda"
@pytest.mark.asyncio
async def test_ensure_categories_reuses_group_under_former_spanish_name(
session: AsyncSession, test_user, test_workspace
):
"""A Spanish workspace seeded before the rename still has "Alojamiento";
importing a housing rule must file into it, not add a "Vivienda" twin."""
legacy = CategoryGroup(
user_id=test_user.id,
workspace_id=test_workspace.id,
name="Alojamiento",
icon="house",
color="#8B5CF6",
position=0,
is_system=True,
)
session.add(legacy)
await session.flush()
created = await _ensure_categories_for_keys(
session, test_workspace.id, test_user.id, {"housing"}, "es"
)
assert created == 1
groups = await get_groups(session, test_workspace.id, include_hidden=True)
assert [g.name for g in groups] == ["Alojamiento"]
housing = (
await session.execute(
select(Category).where(
Category.workspace_id == test_workspace.id,
Category.name == "Vivienda",
)
)
).scalar_one()
assert housing.group_id == legacy.id
@pytest.mark.asyncio
async def test_create_default_categories_race_guard(session: AsyncSession, test_user, test_workspace):
"""Second call should return existing instead of duplicating."""
first = await create_default_categories(session, test_user.id, lang="pt-BR")
second = await create_default_categories(session, test_user.id, lang="pt-BR")
assert len(second) == len(first)
first_ids = {c.id for c in first}
second_ids = {c.id for c in second}
assert first_ids == second_ids
# ---------------------------------------------------------------------------
# get_categories
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_get_categories_ordered(session: AsyncSession, test_user, test_workspace):
await create_default_categories(session, test_user.id)
custom = Category(
id=uuid.uuid4(),
user_id=test_user.id,
name="AAA Custom",
icon="star",
color="#FF0000",
is_system=False,
)
session.add(custom)
await session.commit()
categories = await get_categories(session, test_workspace.id)
system_cats = [c for c in categories if c.is_system]
custom_cats = [c for c in categories if not c.is_system]
if system_cats and custom_cats:
system_idx = categories.index(system_cats[0])
custom_idx = categories.index(custom_cats[0])
assert system_idx < custom_idx
@pytest.mark.asyncio
async def test_get_categories_group_ordered_by_name(session: AsyncSession, test_user, test_workspace):
await create_default_categories(session, test_user.id, lang="pt-BR")
groups = await get_groups(session, test_workspace.id)
food_group = [g for g in groups if g.name == "Alimentação"][0]
names = [c.name for c in food_group.categories]
assert names == sorted(names)
@pytest.mark.asyncio
async def test_get_categories_excludes_other_users(
session: AsyncSession, test_user, test_workspace, test_categories
):
categories = await get_categories(session, uuid.uuid4())
assert len(categories) == 0
# ---------------------------------------------------------------------------
# get_category / create_category / update_category
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_create_custom_category(session: AsyncSession, test_user, test_workspace):
data = CategoryCreate(name="Pets", icon="paw-print", color="#8B4513")
cat = await create_category(session, test_workspace.id, test_user.id, data)
assert cat.name == "Pets"
assert cat.icon == "paw-print"
assert cat.is_system is False
@pytest.mark.asyncio
async def test_create_category_with_group(session: AsyncSession, test_user, test_workspace):
from app.services.category_group_service import create_group
from app.schemas.category_group import CategoryGroupCreate
group = await create_group(
session,
test_workspace.id, test_user.id,
CategoryGroupCreate(name="CustomGroup", icon="folder", color="#000"),
)
cat = await create_category(
session,
test_workspace.id, test_user.id,
CategoryCreate(name="WithGroup", icon="star", color="#FFF", group_id=group.id),
)
assert cat.group_id == group.id
@pytest.mark.asyncio
async def test_get_category_by_id(session: AsyncSession, test_user, test_workspace):
created = await create_category(
session,
test_workspace.id, test_user.id,
CategoryCreate(name="Lookup", icon="search", color="#000000"),
)
fetched = await get_category(session, created.id, test_workspace.id)
assert fetched is not None
assert fetched.id == created.id
@pytest.mark.asyncio
async def test_get_category_not_found(session: AsyncSession, test_user, test_workspace):
result = await get_category(session, uuid.uuid4(), test_workspace.id)
assert result is None
@pytest.mark.asyncio
async def test_update_category(session: AsyncSession, test_user, test_workspace):
cat = await create_category(
session,
test_workspace.id, test_user.id,
CategoryCreate(name="OldName", icon="x", color="#111111"),
)
updated = await update_category(
session,
cat.id,
test_workspace.id,
CategoryUpdate(name="NewName", color="#222222"),
)
assert updated is not None
assert updated.name == "NewName"
assert updated.color == "#222222"
assert updated.icon == "x"
@pytest.mark.asyncio
async def test_update_category_partial(session: AsyncSession, test_user, test_workspace, test_categories):
cat = test_categories[1]
original_icon = cat.icon
original_color = cat.color
updated = await update_category(
session,
cat.id,
test_workspace.id,
CategoryUpdate(name="Mobilidade"),
)
assert updated is not None
assert updated.name == "Mobilidade"
assert updated.icon == original_icon
assert updated.color == original_color
@pytest.mark.asyncio
async def test_update_category_not_found(session: AsyncSession, test_user, test_workspace):
result = await update_category(
session,
uuid.uuid4(),
test_workspace.id,
CategoryUpdate(name="Nope"),
)
assert result is None
# ---------------------------------------------------------------------------
# delete_category
# ---------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_delete_custom_category(session: AsyncSession, test_user, test_workspace):
cat = await create_category(
session,
test_workspace.id, test_user.id,
CategoryCreate(name="ToDelete", icon="trash", color="#FF0000"),
)
assert await delete_category(session, cat.id, test_workspace.id) is True
assert await get_category(session, cat.id, test_workspace.id) is None
@pytest.mark.asyncio
async def test_delete_system_category(session: AsyncSession, test_user, test_workspace):
categories = await create_default_categories(session, test_user.id)
system_cat = categories[0]
assert system_cat.is_system is True
assert await delete_category(session, system_cat.id, test_workspace.id) is True
assert await get_category(session, system_cat.id, test_workspace.id) is None
@pytest.mark.asyncio
async def test_delete_category_not_found(session: AsyncSession, test_user, test_workspace):
assert await delete_category(session, uuid.uuid4(), test_workspace.id) is False