From b2e5dd2ebb0cff122d4210061821f362e006703c Mon Sep 17 00:00:00 2001 From: luxferre Date: Mon, 8 Jun 2026 15:24:42 +0100 Subject: [PATCH] feat: sqlite integrity error handle --- src/iam/router.py | 5 ++++- test/test_iam.py | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/iam/router.py b/src/iam/router.py index 016cebc..6ed3ff0 100644 --- a/src/iam/router.py +++ b/src/iam/router.py @@ -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() diff --git a/test/test_iam.py b/test/test_iam.py index 24a6478..acc8358 100644 --- a/test/test_iam.py +++ b/test/test_iam.py @@ -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 + +