feat: sqlite integrity error handle

This commit is contained in:
Chris Milne 2026-06-08 15:24:42 +01:00
parent da8917869f
commit b2e5dd2ebb
2 changed files with 7 additions and 1 deletions

View file

@ -100,7 +100,10 @@ async def create_group(db: db_dependency, org_model: org_model_root_claim_body_d
try:
db.flush()
except IntegrityError as e:
if isinstance(e.orig, errors.UniqueViolation):
if (
getattr(e.orig, "pgcode", None) == "23505" # Postgres unique violation
or "UNIQUE constraint failed" in str(e.orig) # SQLite unique violation
):
raise ConflictException("Group with this name already exists")
response = GroupSchema(**group_model.__dict__)
db.commit()

View file

@ -226,6 +226,7 @@ async def test_post_group_success(default_client: AsyncClient):
@pytest.mark.parametrize(
"body, expected_status",
[
({"organisation_id": 1, "name": "Test Group"}, 409),
({"organisation_id": 2, "name": "new group"}, 404), # Non-existent organisation, valid name
({"organisation_id": "banana", "name": "new group"}, 422), # Invalid organisation ID, valid name
({"organisation_id": "", "name": "new group"}, 422), # Blank organisation ID, valid name
@ -505,3 +506,5 @@ async def test_post_perm_search_status_checks(default_client: AsyncClient, body:
resp = await default_client.post("/iam/permissions/search", json=body)
assert resp.status_code == expected_status