コード例 #1
0
    def test_nested_field_omit(self):
        pet = Pet(name='Garfield',
                  toys='paper ball, string',
                  species='cat',
                  owner=Person(name='Fred',
                               employer=Company(name='McDonalds')))

        expected_serializer_data = {
            'name': 'Garfield',
            'toys': 'paper ball, string',
            'species': 'cat',
            'owner': {
                'hobbies': '',
                'employer': {
                    'name': 'McDonalds'
                }
            }
        }

        serializer = PetSerializer(
            pet,
            expand=['owner.employer'],
            omit=['owner.name', 'owner.employer.public'])
        self.assertEqual(serializer.data, expected_serializer_data)

        serializer = PetSerializer(
            pet,
            expand=(field for field in ('owner.employer', )),
            omit=(field for field in ('owner.name', 'owner.employer.public')))
        self.assertEqual(serializer.data, expected_serializer_data)
コード例 #2
0
    def test_nested_field_include(self):
        pet = Pet(name='Garfield',
                  toys='paper ball, string',
                  species='cat',
                  owner=Person(name='Fred',
                               employer=Company(name='McDonalds')))

        expected_serializer_data = {
            'owner': {
                'employer': {
                    'name': 'McDonalds'
                }
            }
        }

        serializer = PetSerializer(pet,
                                   expand=['owner.employer'],
                                   fields=['owner.employer.name'])
        self.assertEqual(serializer.data, expected_serializer_data)

        serializer = PetSerializer(
            pet,
            expand=(field for field in ('owner.employer', )),
            fields=(field for field in ('owner.employer.name', )))
        self.assertEqual(serializer.data, expected_serializer_data)
コード例 #3
0
    def test_nested_field_include(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred", employer=Company(name="McDonalds")),
        )

        expected_serializer_data = {
            "owner": {
                "employer": {
                    "name": "McDonalds"
                }
            }
        }

        serializer = PetSerializer(pet,
                                   expand=["owner.employer"],
                                   fields=["owner.employer.name"])
        self.assertEqual(serializer.data, expected_serializer_data)

        serializer = PetSerializer(
            pet,
            expand=(field for field in ("owner.employer", )),
            fields=(field for field in ("owner.employer.name", )),
        )
        self.assertEqual(serializer.data, expected_serializer_data)
コード例 #4
0
    def test_expand_from_request(self):
        pet = Pet(name='Garfield',
                  toys='paper ball, string',
                  species='cat',
                  owner=Person(name='Fred',
                               hobbies='sailing',
                               employer=Company(name='McDonalds')))

        request = MockRequest(
            query_params=MultiValueDict({'expand': ['owner.employer']}))
        serializer = PetSerializer(pet, context={'request': request})

        self.assertEqual(
            serializer.data, {
                'name': 'Garfield',
                'toys': 'paper ball, string',
                'species': 'cat',
                'owner': {
                    'name': 'Fred',
                    'hobbies': 'sailing',
                    'employer': {
                        'public': False,
                        'name': 'McDonalds'
                    }
                }
            })
コード例 #5
0
    def test_all_special_value_in_serialize(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred", employer=Company(name="McDonalds")),
        )

        class PetSerializer(FlexFieldsModelSerializer):
            owner = serializers.PrimaryKeyRelatedField(
                queryset=Person.objects.all(), allow_null=True)

            class Meta:
                model = Pet
                fields = "__all__"

        serializer = PetSerializer(
            fields=("name", "toys"),
            data={
                "name": "Garfield",
                "toys": "paper ball",
                "species": "cat",
                "owner": None,
            },
        )

        serializer.is_valid(raise_exception=True)
コード例 #6
0
    def test_nested_field_omit(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred", employer=Company(name="McDonalds")),
        )

        expected_serializer_data = {
            "name": "Garfield",
            "toys": "paper ball, string",
            "species": "cat",
            "owner": {
                "hobbies": "",
                "employer": {
                    "name": "McDonalds"
                }
            },
        }

        serializer = PetSerializer(
            pet,
            expand=["owner.employer"],
            omit=["owner.name", "owner.employer.public"])
        self.assertEqual(serializer.data, expected_serializer_data)

        serializer = PetSerializer(
            pet,
            expand=(field for field in ("owner.employer", )),
            omit=(field for field in ("owner.name", "owner.employer.public")),
        )
        self.assertEqual(serializer.data, expected_serializer_data)
コード例 #7
0
    def test_expand_from_request(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred",
                         hobbies="sailing",
                         employer=Company(name="McDonalds")),
        )

        request = MockRequest(
            query_params=MultiValueDict({"expand": ["owner.employer"]}))
        serializer = PetSerializer(pet, context={"request": request})

        self.assertEqual(
            serializer.data,
            {
                "name": "Garfield",
                "toys": "paper ball, string",
                "species": "cat",
                "owner": {
                    "name": "Fred",
                    "hobbies": "sailing",
                    "employer": {
                        "public": False,
                        "name": "McDonalds"
                    },
                },
            },
        )
コード例 #8
0
def test_nested_field_include():
    pet = Pet(name='Garfield',
              toys='paper ball, string',
              species='cat',
              owner=Person(name='Fred', employer=Company(name='McDonalds')))

    serializer = PetSerializer(pet,
                               expand=['owner.employer'],
                               fields=['owner.employer.name'])
    assert serializer.data == {'owner': {'employer': {'name': 'McDonalds'}}}
コード例 #9
0
def test_nested_field_omit():
    pet = Pet(name='Garfield',
              toys='paper ball, string',
              species='cat',
              owner=Person(name='Fred', employer=Company(name='McDonalds')))

    serializer = PetSerializer(
        pet,
        expand=['owner.employer'],
        exclude=['species', 'owner.hobbies', 'owner.employer.public'])
    assert serializer.data == {
        'name': 'Garfield',
        'toys': 'paper ball, string',
        'owner': {
            'name': 'Fred',
            'employer': {
                'name': 'McDonalds'
            }
        }
    }
コード例 #10
0
    def test_nested_expand(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred",
                         hobbies="sailing",
                         employer=Company(name="McDonalds")),
        )

        expected_serializer_data = {
            "diet": "",
            "name": "Garfield",
            "toys": "paper ball, string",
            "species": "cat",
            "owner": {
                "name": "Fred",
                "hobbies": "sailing",
                "employer": {
                    "public": False,
                    "name": "McDonalds"
                },
            },
            "sold_from": None,
        }

        request = MockRequest(
            query_params=MultiValueDict({"expand": ["owner.employer"]}))
        serializer = PetSerializer(pet, context={"request": request})
        self.assertEqual(serializer.data, expected_serializer_data)
        self.assertEqual(
            serializer.fields["owner"].fields["employer"].context.get(
                "request"),
            request,
        )

        serializer = PetSerializer(pet,
                                   expand=(field
                                           for field in ("owner.employer", )))
        self.assertEqual(serializer.data, expected_serializer_data)
コード例 #11
0
def test_nested_expand(expand):
    pet = Pet(name='Garfield',
              toys='paper ball, string',
              species='cat',
              owner=Person(name='Fred',
                           hobbies='sailing',
                           employer=Company(name='McDonalds')))

    serializer = PetSerializer(pet, expand=[expand])
    assert serializer.data == {
        'name': 'Garfield',
        'toys': 'paper ball, string',
        'species': 'cat',
        'owner': {
            'name': 'Fred',
            'hobbies': 'sailing',
            'employer': {
                'public': False,
                'name': 'McDonalds'
            }
        }
    }