diff options
| author | Carlton Gibson | 2014-03-06 22:48:16 +0100 | 
|---|---|---|
| committer | Carlton Gibson | 2014-03-06 22:48:16 +0100 | 
| commit | d48e8ca8d6372ae73a6b420e92c546b017fd0b46 (patch) | |
| tree | 391e321bf37e1dd257c35a4ba4e68d1b59b1f54f /rest_framework/tests | |
| parent | 2090f452b224e60853b40e73d1d0e9aad58cd24a (diff) | |
| parent | 51e6982397cc032d6b3fd66f452713d448eb9084 (diff) | |
| download | django-rest-framework-d48e8ca8d6372ae73a6b420e92c546b017fd0b46.tar.bz2 | |
Merge pull request #1459 from linovia/bugfix/optional_unique_validation
Unique constraint are validated even if the field is optional.
Diffstat (limited to 'rest_framework/tests')
| -rw-r--r-- | rest_framework/tests/models.py | 2 | ||||
| -rw-r--r-- | rest_framework/tests/test_serializer.py | 9 | 
2 files changed, 7 insertions, 4 deletions
diff --git a/rest_framework/tests/models.py b/rest_framework/tests/models.py index bf988312..6c8f2342 100644 --- a/rest_framework/tests/models.py +++ b/rest_framework/tests/models.py @@ -103,7 +103,7 @@ class BlogPostComment(RESTFrameworkModel):  class Album(RESTFrameworkModel):      title = models.CharField(max_length=100, unique=True) - +    ref = models.CharField(max_length=10, unique=True, null=True, blank=True)  class Photo(RESTFrameworkModel):      description = models.TextField() diff --git a/rest_framework/tests/test_serializer.py b/rest_framework/tests/test_serializer.py index 198c269f..44163011 100644 --- a/rest_framework/tests/test_serializer.py +++ b/rest_framework/tests/test_serializer.py @@ -161,7 +161,7 @@ class AlbumsSerializer(serializers.ModelSerializer):      class Meta:          model = Album -        fields = ['title']  # lists are also valid options +        fields = ['title', 'ref']  # lists are also valid options  class PositiveIntegerAsChoiceSerializer(serializers.ModelSerializer): @@ -611,12 +611,15 @@ class ModelValidationTests(TestCase):          """          Just check if serializers.ModelSerializer handles unique checks via .full_clean()          """ -        serializer = AlbumsSerializer(data={'title': 'a'}) +        serializer = AlbumsSerializer(data={'title': 'a', 'ref': '1'})          serializer.is_valid()          serializer.save()          second_serializer = AlbumsSerializer(data={'title': 'a'})          self.assertFalse(second_serializer.is_valid()) -        self.assertEqual(second_serializer.errors,  {'title': ['Album with this Title already exists.']}) +        self.assertEqual(second_serializer.errors,  {'title': ['Album with this Title already exists.'],}) +        third_serializer = AlbumsSerializer(data=[{'title': 'b', 'ref': '1'}, {'title': 'c'}]) +        self.assertFalse(third_serializer.is_valid()) +        self.assertEqual(third_serializer.errors,  [{'ref': ['Album with this Ref already exists.']}, {}])      def test_foreign_key_is_null_with_partial(self):          """  | 
