aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorTom Christie2015-02-26 15:56:18 +0000
committerTom Christie2015-02-26 15:56:18 +0000
commitc66f23391ac669f2a9a27431e66588e9092300f2 (patch)
tree46aa41a51ba19deb0b99eaca72b443ca12d9a0e1 /tests
parentd219fc0d8179532c17745dd4aaabd38a2f3faa82 (diff)
parentaa7ed316d842c06d7eb6907d4481d72c747991d7 (diff)
downloaddjango-rest-framework-c66f23391ac669f2a9a27431e66588e9092300f2.tar.bz2
Merge pull request #2572 from Ins1ne/master
Fix UniqueTogetherValidator for NULL values
Diffstat (limited to 'tests')
-rw-r--r--tests/test_validators.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/test_validators.py b/tests/test_validators.py
index 072cec36..127ec6f8 100644
--- a/tests/test_validators.py
+++ b/tests/test_validators.py
@@ -83,11 +83,37 @@ class UniquenessTogetherModel(models.Model):
unique_together = ('race_name', 'position')
+class NullUniquenessTogetherModel(models.Model):
+ """
+ Used to ensure that null values are not included when checking
+ unique_together constraints.
+
+ Ignoring items which have a null in any of the validated fields is the same
+ behavior that database backends will use when they have the
+ unique_together constraint added.
+
+ Example case: a null position could indicate a non-finisher in the race,
+ there could be many non-finishers in a race, but all non-NULL
+ values *should* be unique against the given `race_name`.
+ """
+ date_of_birth = models.DateField(null=True) # Not part of the uniqueness constraint
+ race_name = models.CharField(max_length=100)
+ position = models.IntegerField(null=True)
+
+ class Meta:
+ unique_together = ('race_name', 'position')
+
+
class UniquenessTogetherSerializer(serializers.ModelSerializer):
class Meta:
model = UniquenessTogetherModel
+class NullUniquenessTogetherSerializer(serializers.ModelSerializer):
+ class Meta:
+ model = NullUniquenessTogetherModel
+
+
class TestUniquenessTogetherValidation(TestCase):
def setUp(self):
self.instance = UniquenessTogetherModel.objects.create(
@@ -182,6 +208,34 @@ class TestUniquenessTogetherValidation(TestCase):
""")
assert repr(serializer) == expected
+ def test_ignore_validation_for_null_fields(self):
+ # None values that are on fields which are part of the uniqueness
+ # constraint cause the instance to ignore uniqueness validation.
+ NullUniquenessTogetherModel.objects.create(
+ date_of_birth=datetime.date(2000, 1, 1),
+ race_name='Paris Marathon',
+ position=None
+ )
+ data = {
+ 'date': datetime.date(2000, 1, 1),
+ 'race_name': 'Paris Marathon',
+ 'position': None
+ }
+ serializer = NullUniquenessTogetherSerializer(data=data)
+ assert serializer.is_valid()
+
+ def test_do_not_ignore_validation_for_null_fields(self):
+ # None values that are not on fields part of the uniqueness constraint
+ # do not cause the instance to skip validation.
+ NullUniquenessTogetherModel.objects.create(
+ date_of_birth=datetime.date(2000, 1, 1),
+ race_name='Paris Marathon',
+ position=1
+ )
+ data = {'date': None, 'race_name': 'Paris Marathon', 'position': 1}
+ serializer = NullUniquenessTogetherSerializer(data=data)
+ assert not serializer.is_valid()
+
# Tests for `UniqueForDateValidator`
# ----------------------------------