-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DB constraint to enforce hold/position mutual exclusion
Also make hold/position mutually exclusive in the API via a union
- Loading branch information
1 parent
7a86290
commit 4623bb0
Showing
6 changed files
with
149 additions
and
20 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
api/src/core/migrations/0017_betamove_hold_position_mutually_exclusive.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Generated by Django 4.2.3 on 2023-07-29 02:14 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("core", "0016_remove_hold_boulder_remove_problem_holds_and_more"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddConstraint( | ||
model_name="betamove", | ||
constraint=models.CheckConstraint( | ||
check=models.Q( | ||
("hold_id__isnull", True), | ||
("position__isnull", True), | ||
_connector="XOR", | ||
), | ||
name="hold_position_mutually_exclusive", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from contextlib import AbstractContextManager, nullcontext | ||
|
||
import pytest | ||
from django.db import IntegrityError | ||
|
||
from core.fields import BoulderPosition | ||
from core.models import Beta, Hold | ||
from core.tests.factories import BetaMoveFactory | ||
|
||
pytestmark = pytest.mark.django_db | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"has_hold,has_position,expectation", | ||
[ | ||
(False, False, pytest.raises(IntegrityError)), | ||
(True, False, nullcontext()), | ||
(True, False, nullcontext()), | ||
(True, True, pytest.raises(IntegrityError)), | ||
], | ||
) | ||
def test_beta_move_hold_or_position( | ||
beta: Beta, | ||
hold: Hold, | ||
boulder_position: BoulderPosition, | ||
has_hold: bool, | ||
has_position: bool, | ||
expectation: AbstractContextManager, | ||
) -> None: | ||
""" | ||
Test both failure and success when creating a beta move with hold/position. | ||
Exactly one of these fields should be defined. | ||
""" | ||
with expectation: | ||
beta_move = BetaMoveFactory( | ||
beta=beta, | ||
hold=hold if has_hold else None, | ||
position=boulder_position if has_position else None, | ||
) | ||
assert beta_move.hold == hold | ||
assert beta_move.position is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters