diff options
author | 2025-05-01 12:46:07 +0200 | |
---|---|---|
committer | 2025-05-01 12:48:34 +0200 | |
commit | 1b81162c6ae7e2a0b658436c0c0bd3debb58f8b3 (patch) | |
tree | f68ac0eff920f6ed72af8c46211d56daf97f65fa /pydis_site/apps | |
parent | Update tests for DRF 3.16.0 (diff) |
Mark unique constraint error checks as no cover
Diffstat (limited to 'pydis_site/apps')
-rw-r--r-- | pydis_site/apps/api/viewsets/bot/infraction.py | 7 | ||||
-rw-r--r-- | pydis_site/apps/api/viewsets/bot/user.py | 9 |
2 files changed, 14 insertions, 2 deletions
diff --git a/pydis_site/apps/api/viewsets/bot/infraction.py b/pydis_site/apps/api/viewsets/bot/infraction.py index 8da82822..19b6f2d8 100644 --- a/pydis_site/apps/api/viewsets/bot/infraction.py +++ b/pydis_site/apps/api/viewsets/bot/infraction.py @@ -284,7 +284,12 @@ class InfractionViewSet( """ try: return super().create(request, *args, **kwargs) - except IntegrityError as err: + except IntegrityError as err: # pragma: no cover - see below + # Not covered: DRF handles this via `UniqueTogetherValidator` these + # days, which means it's hard to test this branch specifically. + # However, in a productive deployment, it's still very much + # possible for two concurrent inserts to run into IntegrityError. + # We need to use `__cause__` here, as Django reraises the internal # UniqueViolation emitted by psycopg2 (which contains the attribute # that we actually need) diff --git a/pydis_site/apps/api/viewsets/bot/user.py b/pydis_site/apps/api/viewsets/bot/user.py index c0b4ca0f..79e867c3 100644 --- a/pydis_site/apps/api/viewsets/bot/user.py +++ b/pydis_site/apps/api/viewsets/bot/user.py @@ -395,7 +395,14 @@ class UserViewSet(ModelViewSet): raise ParseError(detail={ "source": ["The user may not be an alternate account of itself"] }) - if err.__cause__.diag.constraint_name == 'api_useraltrelationship_unique_relationships': + if ( + err.__cause__.diag.constraint_name == 'api_useraltrelationship_unique_relationships' + ): # pragma: no cover - see below + # This is not covered because newer DRF versions automatically validate this, + # however the validation is done via a SELECT query which may race concurrent + # inserts in prod. The only correct way is e.g. what Ecto does which is + # associating the validators to the unique constraint errors to match in + # errors, anything else may race. raise ParseError(detail={ "source": ["This relationship has already been established"] }) |