コード例 #1
0
    def test_create(self):
        data = [
            {
                'name': 'a',
                'slug': 'a',
                'resourcetype': 'BlogBase'
            },
            {
                'name': 'b',
                'slug': 'b',
                'info': 'info',
                'resourcetype': 'BlogOne'
            },
            {
                'name': 'c',
                'slug': 'c',
                'resourcetype': 'BlogTwo'
            },
        ]
        serializer = BlogPolymorphicSerializer(data=data, many=True)
        assert serializer.is_valid()

        instances = serializer.save()
        assert len(instances) == 3
        assert [item.name for item in instances] == ['a', 'b', 'c']

        assert BlogBase.objects.count() == 3
        assert BlogBase.objects.instance_of(BlogOne).count() == 1
        assert BlogBase.objects.instance_of(BlogTwo).count() == 1

        assert serializer.data == data
コード例 #2
0
 def test_deserialize_with_invalid_resourcetype(self):
     data = {
         'name': 'blog',
         'resourcetype': 'Invalid',
     }
     serializers = BlogPolymorphicSerializer(data=data)
     assert not serializers.is_valid()
コード例 #3
0
 def test_deserialize(self):
     data = {
         'name': 'blog',
         'slug': 'blog',
         'resourcetype': 'BlogBase',
     }
     serializers = BlogPolymorphicSerializer(data=data)
     assert serializers.is_valid()
     assert serializers.data == data
コード例 #4
0
    def test_update(self):
        instance = BlogBase.objects.create(name='blog', slug='blog')
        data = {'name': 'new-blog', 'slug': 'blog', 'resourcetype': 'BlogBase'}

        serializer = BlogPolymorphicSerializer(instance, data=data)
        assert serializer.is_valid()

        serializer.save()
        assert instance.name == 'new-blog'
        assert instance.slug == 'blog'
コード例 #5
0
    def test_object_validators_are_applied(self):
        data = {
            'name': 'test-blog',
            'slug': 'test-blog-slug',
            'info': 'test-blog-info',
            'about': 'test-blog-about',
            'resourcetype': 'BlogThree'
        }
        serializer = BlogPolymorphicSerializer(data=data)
        assert serializer.is_valid()
        serializer.save()

        data['slug'] = 'test-blog-slug-new'
        duplicate = BlogPolymorphicSerializer(data=data)
        
        assert not duplicate.is_valid()
        assert 'non_field_errors' in duplicate.errors
        err = duplicate.errors['non_field_errors']

        assert err == ['The fields info, about must make a unique set.']
コード例 #6
0
    def test_partial_update(self):
        instance = BlogBase.objects.create(name='blog', slug='blog')
        data = {
            'name': 'new-blog',
        }

        serializer = BlogPolymorphicSerializer(instance,
                                               data=data,
                                               partial=True)
        assert serializer.is_valid()

        serializer.save()
        assert instance.name == 'new-blog'
        assert instance.slug == 'blog'