Beispiel #1
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)
    def test_basic_expand(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred", hobbies="sailing"),
        )

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

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

        serializer = PetSerializer(pet,
                                   expand=(field for field in ("owner", )))
        self.assertEqual(serializer.data, expected_serializer_data)
Beispiel #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)
Beispiel #4
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)
Beispiel #5
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)
Beispiel #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)
    def test_basic_field_include(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred"),
        )

        expected_serializer_data = {"name": "Garfield", "toys": "paper ball, string"}

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

        serializer = PetSerializer(pet, fields=(field for field in ("name", "toys")))
        self.assertEqual(serializer.data, expected_serializer_data)
Beispiel #8
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"
                    },
                },
            },
        )
Beispiel #9
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'
                    }
                }
            })
Beispiel #10
0
    def test_basic_field_include(self):
        pet = Pet(name='Garfield',
                  toys='paper ball, string',
                  species='cat',
                  owner=Person(name='Fred'))

        expected_serializer_data = {
            'name': 'Garfield',
            'toys': 'paper ball, string'
        }

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

        serializer = PetSerializer(pet,
                                   fields=(field
                                           for field in ('name', 'toys')))
        self.assertEqual(serializer.data, expected_serializer_data)
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'}}}
    def test_basic_expand(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred", hobbies="sailing"),
        )

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

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

        serializer = PetSerializer(pet, expand=(field for field in ("owner",)))
        self.assertEqual(serializer.data, expected_serializer_data)
def test_basic_field_omit():
    pet = Pet(name='Garfield',
              toys='paper ball, string',
              species='cat',
              owner=Person(name='Fred'))

    serializer = PetSerializer(pet, exclude=['species', 'owner'])
    assert serializer.data == {
        'name': 'Garfield',
        'toys': 'paper ball, string'
    }
    def test_basic_field_omit(self):
        pet = Pet(name='Garfield',
                  toys='paper ball, string',
                  species='cat',
                  owner=Person(name='Fred'))

        serializer = PetSerializer(pet, omit=['species', 'owner'])

        self.assertEqual(serializer.data, {
            'name': 'Garfield',
            'toys': 'paper ball, string'
        })
Beispiel #15
0
    def test_basic_expand(self):
        pet = Pet(name='Garfield',
                  toys='paper ball, string',
                  species='cat',
                  owner=Person(name='Fred', hobbies='sailing'))

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

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

        serializer = PetSerializer(pet,
                                   expand=(field for field in ('owner', )))
        self.assertEqual(serializer.data, expected_serializer_data)
    def test_basic_field_omit(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred"),
        )

        expected_serializer_data = {
            "name": "Garfield",
            "toys": "paper ball, string",
            "diet": "",
            "sold_from": None,
        }

        serializer = PetSerializer(pet, omit=["species", "owner"])
        self.assertEqual(serializer.data, expected_serializer_data)

        serializer = PetSerializer(pet,
                                   omit=(field
                                         for field in ("species", "owner")))
        self.assertEqual(serializer.data, expected_serializer_data)
Beispiel #17
0
    def test_fields_include_with_custom_param_name(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred"),
        )

        expected_serializer_data = {
            "name": "Garfield",
            "toys": "paper ball, string"
        }

        serializer = PetSerializer(pet, only=["name", "toys"])
        self.assertEqual(serializer.data, expected_serializer_data)
def test_expand(expand):
    pet = Pet(name='Garfield',
              toys='paper ball, string',
              species='cat',
              owner=Person(name='Fred', hobbies='sailing'))

    serializer = PetSerializer(pet, expand=[expand])
    assert serializer.data == {
        'name': 'Garfield',
        'toys': 'paper ball, string',
        'species': 'cat',
        'owner': {
            'name': 'Fred',
            'hobbies': 'sailing',
            'employer': None
        }
    }
    def test_omit_with_custom_param_name(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred"),
        )

        expected_serializer_data = {
            "name": "Garfield",
            "toys": "paper ball, string",
            "diet": "",
            "sold_from": None,
        }

        serializer = PetSerializer(pet, exclude=["species", "owner"])
        self.assertEqual(serializer.data, expected_serializer_data)
Beispiel #20
0
    def test_expand_with_custom_param_name(self):
        pet = Pet(
            name="Garfield",
            toys="paper ball, string",
            species="cat",
            owner=Person(name="Fred", hobbies="sailing"),
        )

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

        serializer = PetSerializer(pet, include=["owner"])
        self.assertEqual(serializer.data, expected_serializer_data)
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'
            }
        }
    }
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'
            }
        }
    }