Exemplo n.º 1
0
    def test_foreign_key_with_partial(self):
        """
        Test ModelSerializer validation with partial=True

        Specifically test foreign key validation.
        """

        album = Album(title='test')
        album.save()

        class PhotoSerializer(serializers.ModelSerializer):
            class Meta:
                model = Photo

        photo_serializer = PhotoSerializer(data={
            'description': 'test',
            'album': album.pk
        })
        self.assertTrue(photo_serializer.is_valid())
        photo = photo_serializer.save()

        # Updating only the album (foreign key)
        photo_serializer = PhotoSerializer(instance=photo,
                                           data={'album': album.pk},
                                           partial=True)
        self.assertTrue(photo_serializer.is_valid())
        self.assertTrue(photo_serializer.save())

        # Updating only the description
        photo_serializer = PhotoSerializer(instance=photo,
                                           data={'description': 'new'},
                                           partial=True)

        self.assertTrue(photo_serializer.is_valid())
        self.assertTrue(photo_serializer.save())
    def test_foreign_key_with_partial(self):
        """
        Test ModelSerializer validation with partial=True

        Specifically test foreign key validation.
        """

        album = Album(title='test')
        album.save()

        class PhotoSerializer(serializers.ModelSerializer):
            class Meta:
                model = Photo

        photo_serializer = PhotoSerializer(data={'description': 'test', 'album': album.pk})
        self.assertTrue(photo_serializer.is_valid())
        photo = photo_serializer.save()

        # Updating only the album (foreign key)
        photo_serializer = PhotoSerializer(instance=photo, data={'album': album.pk}, partial=True)
        self.assertTrue(photo_serializer.is_valid())
        self.assertTrue(photo_serializer.save())

        # Updating only the description
        photo_serializer = PhotoSerializer(instance=photo,
                                           data={'description': 'new'},
                                           partial=True)

        self.assertTrue(photo_serializer.is_valid())
        self.assertTrue(photo_serializer.save())
 def setUp(self):
     """
     Create 3 Album instances.
     """
     titles = ["foo", "bar", "baz"]
     for title in titles:
         album = Album(title=title)
         album.save()
     self.detail_view = AlbumDetail.as_view()
     self.data = {
         "foo": {"title": "foo", "url": "http://testserver/albums/foo/"},
         "bar": {"title": "bar", "url": "http://testserver/albums/bar/"},
         "baz": {"title": "baz", "url": "http://testserver/albums/baz/"},
     }
 def setUp(self):
     """
     Create 3 Album instances.
     """
     titles = ['foo', 'bar', 'baz']
     for title in titles:
         album = Album(title=title)
         album.save()
     self.detail_view = AlbumDetail.as_view()
     self.data = {
         'foo': {'title': 'foo', 'url': 'http://testserver/albums/foo/'},
         'bar': {'title': 'bar', 'url': 'http://testserver/albums/bar/'},
         'baz': {'title': 'baz', 'url': 'http://testserver/albums/baz/'}
     }
Exemplo n.º 5
0
 def setUp(self):
     """
     Create 3 Album instances.
     """
     titles = ['foo', 'bar', 'baz']
     for title in titles:
         album = Album(title=title)
         album.save()
     self.detail_view = AlbumDetail.as_view()
     self.data = {
         'foo': {'title': 'foo', 'url': 'http://testserver/albums/foo/'},
         'bar': {'title': 'bar', 'url': 'http://testserver/albums/bar/'},
         'baz': {'title': 'baz', 'url': 'http://testserver/albums/baz/'}
     }