Skip to content

Commit

Permalink
Create example errors when serializing dataclasses with source="*"
Browse files Browse the repository at this point in the history
  • Loading branch information
txomon authored and oxan committed Aug 21, 2023
1 parent 99a8471 commit 969997a
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions tests/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from unittest import TestCase

from rest_framework import fields
from rest_framework.serializers import Serializer

from rest_framework_dataclasses.serializers import DataclassSerializer
from rest_framework_dataclasses.types import Literal
Expand Down Expand Up @@ -269,3 +270,61 @@ class ObscureFeaturesTest(TestCase, FunctionalTestMixin):

def setUp(self):
self.instance.name = 'Bob'

# Testcase 5 (source='*')


@dataclasses.dataclass
class PersonPet:
name: str
pet: Pet


class PersonPetEmbeddedDataclassSerializer(DataclassSerializer):
class PetSerializer(DataclassSerializer):
class Meta:
dataclass = Pet
fields = ['name', 'owner_name', 'animal']

name = fields.CharField(source='pet.name')
animal = fields.CharField(source='pet.animal')
owner_name = fields.CharField(source='name')

class Meta:
dataclass = PersonPet
fields = ['name', 'pet']

name = fields.CharField()
pet = PetSerializer(source="*")


class PersonPetEmbeddedDefaultSerializer(DataclassSerializer):
class PetSerializer(Serializer):
class Meta:
dataclass = Pet
fields = ['name', 'owner_name', 'animal']

name = fields.CharField(source='pet.name')
animal = fields.CharField(source='pet.animal')
owner_name = fields.CharField(source='name')

class Meta:
dataclass = PersonPet
fields = ['name', 'pet']

name = fields.CharField()
pet = PetSerializer(source="*")


class SourceStarDataclassTest(TestCase, FunctionalTestMixin):
serializer = PersonPetEmbeddedDataclassSerializer
instance = PersonPet(name='Milo', pet=Pet(name='Katsu', animal='cat'))
representation = {'name': 'Milo', 'pet': {'name': 'Katsu', 'animal': 'cat', 'owner_name': 'Milo'}}
representation_readonly = {}


class SourceStarDefaultTest(TestCase, FunctionalTestMixin):
serializer = PersonPetEmbeddedDefaultSerializer
instance = PersonPet(name='Milo', pet=Pet(name='Katsu', animal='cat'))
representation = {'name': 'Milo', 'pet': {'name': 'Katsu', 'animal': 'cat', 'owner_name': 'Milo'}}
representation_readonly = {}

0 comments on commit 969997a

Please sign in to comment.