Upload tests/test_tags.py

This commit is contained in:
2026-06-03 23:52:08 +00:00
committed by leocrm-bot
parent f5e91f185d
commit c6b3430ebc
+63
View File
@@ -0,0 +1,63 @@
"""Tests for FR-6 + R-2 (Tag + TagLink, polymorphic parent validation, unique constraint)."""
from __future__ import annotations
import pytest
from httpx import AsyncClient
@pytest.mark.asyncio
async def test_create_tag(
client: AsyncClient, auth_headers: dict[str, str]
) -> None:
resp = await client.post(
"/api/v1/tags/",
json={"name": "Important", "color": "#FF0000"},
headers=auth_headers,
)
assert resp.status_code == 201, resp.text
body = resp.json()
assert body["name"] == "Important"
assert body["color"] == "#FF0000"
@pytest.mark.asyncio
async def test_link_tag_to_valid_parent(
client: AsyncClient, auth_headers: dict[str, str], seed_data: dict
) -> None:
tag_id = seed_data["tag_ids"][0]
acc_id = seed_data["account_ids"][0]
resp = await client.post(
"/api/v1/tags/link",
json={"tag_id": tag_id, "parent_type": "account", "parent_id": acc_id},
headers=auth_headers,
)
assert resp.status_code == 201, resp.text
@pytest.mark.asyncio
async def test_link_tag_to_invalid_parent(
client: AsyncClient, auth_headers: dict[str, str], seed_data: dict
) -> None:
"""R-2: invalid parent → 404."""
tag_id = seed_data["tag_ids"][0]
resp = await client.post(
"/api/v1/tags/link",
json={"tag_id": tag_id, "parent_type": "account", "parent_id": 99999},
headers=auth_headers,
)
assert resp.status_code == 404
@pytest.mark.asyncio
async def test_unique_constraint_on_link(
client: AsyncClient, auth_headers: dict[str, str], seed_data: dict
) -> None:
"""Second link with same (tag, parent_type, parent_id) → 409."""
tag_id = seed_data["tag_ids"][0]
acc_id = seed_data["account_ids"][0]
payload = {"tag_id": tag_id, "parent_type": "account", "parent_id": acc_id}
r1 = await client.post("/api/v1/tags/link", json=payload, headers=auth_headers)
assert r1.status_code == 201, r1.text
r2 = await client.post("/api/v1/tags/link", json=payload, headers=auth_headers)
assert r2.status_code == 409