mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-01 11:00:13 +03:00
Regression tests for unique date validators
Ensures that partial updates of serializers with unique date/month/year constraints function as expected.
This commit is contained in:
parent
5ac5784c62
commit
40a184c544
|
@ -381,11 +381,13 @@ class UniqueForDateSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
|
||||||
class TestUniquenessForDateValidation(TestCase):
|
class TestUniquenessForDateValidation(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.instance = UniqueForDateModel.objects.create(
|
self.instance = UniqueForDateModel.objects.create(
|
||||||
slug='existing',
|
slug='existing',
|
||||||
published='2000-01-01'
|
published='2000-01-01'
|
||||||
)
|
)
|
||||||
|
self.instance.refresh_from_db() # published becomes datetime
|
||||||
|
|
||||||
def test_repr(self):
|
def test_repr(self):
|
||||||
serializer = UniqueForDateSerializer()
|
serializer = UniqueForDateSerializer()
|
||||||
|
@ -422,6 +424,12 @@ class TestUniquenessForDateValidation(TestCase):
|
||||||
'published': datetime.date(2000, 1, 2)
|
'published': datetime.date(2000, 1, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def test_unique_for_date_for_partial(self):
|
||||||
|
serializer = UniqueForDateSerializer(
|
||||||
|
instance=self.instance, data={}, partial=True
|
||||||
|
)
|
||||||
|
assert serializer.is_valid(), serializer.errors
|
||||||
|
|
||||||
def test_updated_instance_excluded_from_unique_for_date(self):
|
def test_updated_instance_excluded_from_unique_for_date(self):
|
||||||
"""
|
"""
|
||||||
When performing an update, the existing instance does not count
|
When performing an update, the existing instance does not count
|
||||||
|
@ -456,6 +464,7 @@ class UniqueForMonthTests(TestCase):
|
||||||
self.instance = UniqueForMonthModel.objects.create(
|
self.instance = UniqueForMonthModel.objects.create(
|
||||||
slug='existing', published='2017-01-01'
|
slug='existing', published='2017-01-01'
|
||||||
)
|
)
|
||||||
|
self.instance.refresh_from_db() # published becomes datetime
|
||||||
|
|
||||||
def test_not_unique_for_month(self):
|
def test_not_unique_for_month(self):
|
||||||
data = {'slug': 'existing', 'published': '2017-01-01'}
|
data = {'slug': 'existing', 'published': '2017-01-01'}
|
||||||
|
@ -474,6 +483,12 @@ class UniqueForMonthTests(TestCase):
|
||||||
'published': datetime.date(2017, 2, 1)
|
'published': datetime.date(2017, 2, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def test_unique_for_month_for_partial(self):
|
||||||
|
serializer = UniqueForMonthSerializer(
|
||||||
|
instance=self.instance, data={}, partial=True
|
||||||
|
)
|
||||||
|
assert serializer.is_valid(), serializer.errors
|
||||||
|
|
||||||
# Tests for `UniqueForYearValidator`
|
# Tests for `UniqueForYearValidator`
|
||||||
# ----------------------------------
|
# ----------------------------------
|
||||||
|
|
||||||
|
@ -495,6 +510,7 @@ class UniqueForYearTests(TestCase):
|
||||||
self.instance = UniqueForYearModel.objects.create(
|
self.instance = UniqueForYearModel.objects.create(
|
||||||
slug='existing', published='2017-01-01'
|
slug='existing', published='2017-01-01'
|
||||||
)
|
)
|
||||||
|
self.instance.refresh_from_db() # published becomes datetime
|
||||||
|
|
||||||
def test_not_unique_for_year(self):
|
def test_not_unique_for_year(self):
|
||||||
data = {'slug': 'existing', 'published': '2017-01-01'}
|
data = {'slug': 'existing', 'published': '2017-01-01'}
|
||||||
|
@ -513,6 +529,12 @@ class UniqueForYearTests(TestCase):
|
||||||
'published': datetime.date(2018, 1, 1)
|
'published': datetime.date(2018, 1, 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def test_unique_for_year_for_partial(self):
|
||||||
|
serializer = UniqueForYearSerializer(
|
||||||
|
instance=self.instance, data={}, partial=True
|
||||||
|
)
|
||||||
|
assert serializer.is_valid(), serializer.errors
|
||||||
|
|
||||||
|
|
||||||
class HiddenFieldUniqueForDateModel(models.Model):
|
class HiddenFieldUniqueForDateModel(models.Model):
|
||||||
slug = models.CharField(max_length=100, unique_for_date='published')
|
slug = models.CharField(max_length=100, unique_for_date='published')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user