Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TestUniqueConstraintValidation failing on foreign key field #9678

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,15 @@ def filter(self, **kwargs):
assert queryset.called_with == {'race_name': 'bar', 'position': 1}


class FancyConditionModel(models.Model):
id = models.IntegerField(primary_key=True)


class UniqueConstraintModel(models.Model):
race_name = models.CharField(max_length=100)
position = models.IntegerField()
global_id = models.IntegerField()
fancy_conditions = models.IntegerField()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind keeping this model as it was and add a new model to cover the new ForeignKey use case. Would avoid a potential fix breaking a behaviour with non-foreign key fields. Thanks

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I reverted the changes I made to the test and created another one covering the ForeignKey use case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 🙌🏻

fancy_conditions = models.ForeignKey(FancyConditionModel, on_delete=models.CASCADE)

class Meta:
constraints = [
Expand Down Expand Up @@ -578,23 +582,24 @@ class Meta:

class TestUniqueConstraintValidation(TestCase):
def setUp(self):
fancy_model_condition = FancyConditionModel.objects.create(id=1)
self.instance = UniqueConstraintModel.objects.create(
race_name='example',
position=1,
global_id=1,
fancy_conditions=1
fancy_conditions=fancy_model_condition
)
UniqueConstraintModel.objects.create(
race_name='example',
position=2,
global_id=2,
fancy_conditions=1
fancy_conditions=fancy_model_condition
)
UniqueConstraintModel.objects.create(
race_name='other',
position=1,
global_id=3,
fancy_conditions=1
fancy_conditions=fancy_model_condition
)

def test_repr(self):
Expand All @@ -618,24 +623,27 @@ def test_unique_together_condition(self):
Fields used in UniqueConstraint's condition must be included
into queryset existence check
"""
fancy_model_condition_9 = FancyConditionModel.objects.create(id=9)
fancy_model_condition_10 = FancyConditionModel.objects.create(id=10)
fancy_model_condition_11 = FancyConditionModel.objects.create(id=11)
UniqueConstraintModel.objects.create(
race_name='condition',
position=1,
global_id=10,
fancy_conditions=10,
fancy_conditions=fancy_model_condition_10,
)
serializer = UniqueConstraintSerializer(data={
'race_name': 'condition',
'position': 1,
'global_id': 11,
'fancy_conditions': 9,
'fancy_conditions': fancy_model_condition_9,
})
assert serializer.is_valid()
serializer = UniqueConstraintSerializer(data={
'race_name': 'condition',
'position': 1,
'global_id': 11,
'fancy_conditions': 11,
'fancy_conditions': fancy_model_condition_11,
})
assert not serializer.is_valid()

Expand Down
Loading