"""Duplicate relationships are rejected (no double-linking).""" from tests.conftest import auth, register async def _setup(client, email): h = auth(await register(client, email)) tid = (await client.post("/api/v1/trees", json={"name": "T"}, headers=h)).json()["id"] async def person(given): return ( await client.post(f"/api/v1/trees/{tid}/persons", json={"given": given}, headers=h) ).json()["id"] return h, tid, person async def test_duplicate_parent_child_rejected(client): h, tid, person = await _setup(client, "dup-pc@example.com") karl = await person("Karl") kid = await person("Kid") body = {"type": "parent_child", "person_from_id": karl, "person_to_id": kid} first = await client.post(f"/api/v1/trees/{tid}/relationships", json=body, headers=h) assert first.status_code == 201 dup = await client.post(f"/api/v1/trees/{tid}/relationships", json=body, headers=h) assert dup.status_code == 409 async def test_duplicate_partnership_either_direction_rejected(client): h, tid, person = await _setup(client, "dup-sp@example.com") a = await person("A") b = await person("B") first = await client.post( f"/api/v1/trees/{tid}/relationships", json={"type": "partnership", "person_from_id": a, "person_to_id": b}, headers=h, ) assert first.status_code == 201 # Same couple, reversed order — still a duplicate. dup = await client.post( f"/api/v1/trees/{tid}/relationships", json={"type": "partnership", "person_from_id": b, "person_to_id": a}, headers=h, ) assert dup.status_code == 409 async def test_reverse_parent_child_is_allowed(client): """A->B as parent_child shouldn't block B->A (different meaning).""" h, tid, person = await _setup(client, "dup-rev@example.com") a = await person("A") b = await person("B") r1 = await client.post( f"/api/v1/trees/{tid}/relationships", json={"type": "parent_child", "person_from_id": a, "person_to_id": b}, headers=h, ) r2 = await client.post( f"/api/v1/trees/{tid}/relationships", json={"type": "parent_child", "person_from_id": b, "person_to_id": a}, headers=h, ) assert r1.status_code == 201 and r2.status_code == 201