mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-24 00:04:16 +03:00
Further group model inheritance tests
This commit is contained in:
parent
354fbc64ba
commit
bd3fe75e1a
|
@ -1,8 +1,11 @@
|
||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.test import TestCase
|
||||||
|
from rest_framework import serializers
|
||||||
from rest_framework.tests.models import RESTFrameworkModel
|
from rest_framework.tests.models import RESTFrameworkModel
|
||||||
|
|
||||||
|
|
||||||
|
# Models
|
||||||
class ParentModel(RESTFrameworkModel):
|
class ParentModel(RESTFrameworkModel):
|
||||||
name1 = models.CharField(max_length=100)
|
name1 = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
@ -14,3 +17,51 @@ class ChildModel(ParentModel):
|
||||||
class AssociatedModel(RESTFrameworkModel):
|
class AssociatedModel(RESTFrameworkModel):
|
||||||
ref = models.OneToOneField(ParentModel, primary_key=True)
|
ref = models.OneToOneField(ParentModel, primary_key=True)
|
||||||
name = models.CharField(max_length=100)
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
|
||||||
|
# Serializers
|
||||||
|
class DerivedModelSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = ChildModel
|
||||||
|
|
||||||
|
|
||||||
|
class AssociatedModelSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta:
|
||||||
|
model = AssociatedModel
|
||||||
|
|
||||||
|
|
||||||
|
# Tests
|
||||||
|
class IneritedModelSerializationTests(TestCase):
|
||||||
|
|
||||||
|
def test_multitable_inherited_model_fields_as_expected(self):
|
||||||
|
"""
|
||||||
|
Assert that the parent pointer field is not included in the fields
|
||||||
|
serialized fields
|
||||||
|
"""
|
||||||
|
child = ChildModel(name1='parent name', name2='child name')
|
||||||
|
serializer = DerivedModelSerializer(child)
|
||||||
|
self.assertEqual(set(serializer.data.keys()),
|
||||||
|
set(['name1', 'name2', 'id']))
|
||||||
|
|
||||||
|
def test_onetoone_primary_key_model_fields_as_expected(self):
|
||||||
|
"""
|
||||||
|
Assert that a model with a onetoone field that is the primary key is
|
||||||
|
not treated like a derived model
|
||||||
|
"""
|
||||||
|
parent = ParentModel(name1='parent name')
|
||||||
|
associate = AssociatedModel(name='hello', ref=parent)
|
||||||
|
serializer = AssociatedModelSerializer(associate)
|
||||||
|
self.assertEqual(set(serializer.data.keys()),
|
||||||
|
set(['name', 'ref']))
|
||||||
|
|
||||||
|
def test_data_is_valid_without_parent_ptr(self):
|
||||||
|
"""
|
||||||
|
Assert that the pointer to the parent table is not a required field
|
||||||
|
for input data
|
||||||
|
"""
|
||||||
|
data = {
|
||||||
|
'name1': 'parent name',
|
||||||
|
'name2': 'child name',
|
||||||
|
}
|
||||||
|
serializer = DerivedModelSerializer(data=data)
|
||||||
|
self.assertEqual(serializer.is_valid(), True)
|
||||||
|
|
|
@ -5,8 +5,6 @@ from rest_framework import serializers
|
||||||
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
from rest_framework.tests.models import (HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
|
||||||
BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel,
|
BlankFieldModel, BlogPost, Book, CallableDefaultValueModel, DefaultValueModel,
|
||||||
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo)
|
ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo)
|
||||||
from rest_framework.tests.multitable_inheritance import (ParentModel,
|
|
||||||
ChildModel, AssociatedModel)
|
|
||||||
import datetime
|
import datetime
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
|
@ -98,16 +96,6 @@ class BrokenModelSerializer(serializers.ModelSerializer):
|
||||||
fields = ['some_field']
|
fields = ['some_field']
|
||||||
|
|
||||||
|
|
||||||
class DerivedModelSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = ChildModel
|
|
||||||
|
|
||||||
|
|
||||||
class AssociatedModelSerializer(serializers.ModelSerializer):
|
|
||||||
class Meta:
|
|
||||||
model = AssociatedModel
|
|
||||||
|
|
||||||
|
|
||||||
class BasicTests(TestCase):
|
class BasicTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.comment = Comment(
|
self.comment = Comment(
|
||||||
|
@ -1122,39 +1110,3 @@ class DeserializeListTestCase(TestCase):
|
||||||
self.assertFalse(serializer.is_valid())
|
self.assertFalse(serializer.is_valid())
|
||||||
expected = [{}, {'email': ['This field is required.']}, {}]
|
expected = [{}, {'email': ['This field is required.']}, {}]
|
||||||
self.assertEqual(serializer.errors, expected)
|
self.assertEqual(serializer.errors, expected)
|
||||||
|
|
||||||
|
|
||||||
class IneritedModelSerializationTests(TestCase):
|
|
||||||
|
|
||||||
def test_multitable_inherited_model_fields_as_expected(self):
|
|
||||||
"""
|
|
||||||
Assert that the parent pointer field is not included in the fields
|
|
||||||
serialized fields
|
|
||||||
"""
|
|
||||||
child = ChildModel(name1='parent name', name2='child name')
|
|
||||||
serializer = DerivedModelSerializer(child)
|
|
||||||
self.assertEqual(set(serializer.data.keys()),
|
|
||||||
set(['name1', 'name2', 'id']))
|
|
||||||
|
|
||||||
def test_onetoone_primary_key_model_fields_as_expected(self):
|
|
||||||
"""
|
|
||||||
Assert that a model with a onetoone field that is the primary key is
|
|
||||||
not treated like a derived model
|
|
||||||
"""
|
|
||||||
parent = ParentModel(name1='parent name')
|
|
||||||
associate = AssociatedModel(name='hello', ref=parent)
|
|
||||||
serializer = AssociatedModelSerializer(associate)
|
|
||||||
self.assertEqual(set(serializer.data.keys()),
|
|
||||||
set(['name', 'ref']))
|
|
||||||
|
|
||||||
def test_data_is_valid_without_parent_ptr(self):
|
|
||||||
"""
|
|
||||||
Assert that the pointer to the parent table is not a required field
|
|
||||||
for input data
|
|
||||||
"""
|
|
||||||
data = {
|
|
||||||
'name1': 'parent name',
|
|
||||||
'name2': 'child name',
|
|
||||||
}
|
|
||||||
serializer = DerivedModelSerializer(data=data)
|
|
||||||
self.assertEqual(serializer.is_valid(), True)
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user