Skip to content

Commit 1b81162

Browse files
committed
Mark unique constraint error checks as no cover
1 parent a441b93 commit 1b81162

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

pydis_site/apps/api/viewsets/bot/infraction.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,12 @@ def create(self, request: HttpRequest, *args, **kwargs) -> Response:
284284
"""
285285
try:
286286
return super().create(request, *args, **kwargs)
287-
except IntegrityError as err:
287+
except IntegrityError as err: # pragma: no cover - see below
288+
# Not covered: DRF handles this via `UniqueTogetherValidator` these
289+
# days, which means it's hard to test this branch specifically.
290+
# However, in a productive deployment, it's still very much
291+
# possible for two concurrent inserts to run into IntegrityError.
292+
288293
# We need to use `__cause__` here, as Django reraises the internal
289294
# UniqueViolation emitted by psycopg2 (which contains the attribute
290295
# that we actually need)

pydis_site/apps/api/viewsets/bot/user.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,14 @@ def add_alt(self, request: Request, pk: str) -> Response:
395395
raise ParseError(detail={
396396
"source": ["The user may not be an alternate account of itself"]
397397
})
398-
if err.__cause__.diag.constraint_name == 'api_useraltrelationship_unique_relationships':
398+
if (
399+
err.__cause__.diag.constraint_name == 'api_useraltrelationship_unique_relationships'
400+
): # pragma: no cover - see below
401+
# This is not covered because newer DRF versions automatically validate this,
402+
# however the validation is done via a SELECT query which may race concurrent
403+
# inserts in prod. The only correct way is e.g. what Ecto does which is
404+
# associating the validators to the unique constraint errors to match in
405+
# errors, anything else may race.
399406
raise ParseError(detail={
400407
"source": ["This relationship has already been established"]
401408
})

0 commit comments

Comments
 (0)