Exemplo n.º 1
0
 def test_nested_serialization(self):
     """
     Default with nested serializers is to include full serialization of
     child elements.
     """
     expected = {
         'first_name':
         'john',
         'last_name':
         'doe',
         'age':
         42,
         'siblings': [{
             'first_name': 'jane',
             'last_name': 'doe',
             'age': 44,
             'partner': {
                 'first_name': 'fred',
                 'last_name': 'bloggs',
                 'age': 41,
             }
         }, {
             'first_name': 'emily',
             'last_name': 'doe',
             'age': 37,
         }]
     }
     self.assertEquals(
         ObjectSerializer().serialize('python', self.obj, nested=True),
         expected)
Exemplo n.º 2
0
    def test_depth_one_serialization(self):
        """
        If 'nested' is greater than 0 then nested objects should be serialized
        as flat values once the specified depth has been reached.
        """
        expected = {
            'first_name':
            'john',
            'last_name':
            'doe',
            'age':
            42,
            'siblings': [{
                'first_name': 'jane',
                'last_name': 'doe',
                'age': 44,
                'partner': 'fred bloggs'
            }, {
                'first_name': 'emily',
                'last_name': 'doe',
                'age': 37,
            }]
        }

        self.assertEquals(
            ObjectSerializer().serialize('python', self.obj, nested=1),
            expected)
Exemplo n.º 3
0
    def test_serialization_only_includes_instance_properties(self):
        """
        By default only serialize instance properties, not class properties.
        """
        expected = {'first_name': 'john', 'last_name': 'doe', 'age': 42}

        self.assertEquals(ObjectSerializer().serialize('python', self.obj),
                          expected)
Exemplo n.º 4
0
    def test_serialize_basic_object(self):
        """
        Objects are serialized by converting into dictionaries.
        """
        expected = {'a': 1, 'b': 'foo', 'c': True}

        self.assertEquals(ObjectSerializer().serialize('python', self.obj),
                          expected)
Exemplo n.º 5
0
 def test_recursive_serialization(self):
     """
     If recursion occurs, serializer will fall back to flat values.
     """
     expected = {
         'first_name': 'john',
         'last_name': 'doe',
         'age': 42,
         'daughter': {
             'first_name': 'emily',
             'last_name': 'doe',
             'age': 37,
             'father': 'john doe'
         }
     }
     self.assertEquals(
         ObjectSerializer().serialize('python', self.obj, nested=True),
         expected)
Exemplo n.º 6
0
	def list(self, request):
		products = Object.objects.all()
		serializer = ObjectSerializer(products, many=True)
		data = JSONRenderer().render(serializer.data)
		return Response(data)
Exemplo n.º 7
0
 def test_dict(self):
     obj = {}
     expected = '{}'
     output = ObjectSerializer().serialize('json', obj)
     self.assertEquals(output, expected)
Exemplo n.º 8
0
 def test_list(self):
     obj = []
     expected = '[]'
     output = ObjectSerializer().serialize('json', obj)
     self.assertEquals(output, expected)
Exemplo n.º 9
0
        class CustomSerializer(ObjectSerializer):
            full_name = ObjectSerializer(label='Full name')
            details = DetailsSerializer(label='Details', source='*')

            class Meta:
                fields = ('full_name', 'details')
Exemplo n.º 10
0
        class DetailsSerializer(ObjectSerializer):
            first_name = ObjectSerializer(label='First name')
            last_name = ObjectSerializer(label='Last name')

            class Meta:
                fields = ('first_name', 'last_name')
Exemplo n.º 11
0
        class CustomSerializer(ObjectSerializer):
            full_name = ObjectSerializer(label='Full name')
            age = ObjectSerializer(label='Age')

            class Meta:
                fields = ('full_name', 'age')
Exemplo n.º 12
0
 class CustomSerializer(ObjectSerializer):
     full_name = ObjectSerializer()
Exemplo n.º 13
0
 def test_xml(self):
     expected = '<?xml version="1.0" encoding="utf-8"?>\n<object><a>1</a><b>foo</b><c>True</c></object>'
     output = ObjectSerializer().serialize('xml', self.obj)
     self.assertEquals(output, expected)
Exemplo n.º 14
0
 def test_yaml(self):
     expected = '{a: 1, b: foo, c: true}\n'
     output = ObjectSerializer().serialize('yaml', self.obj)
     self.assertEquals(output, expected)
Exemplo n.º 15
0
 def test_json(self):
     expected = '{"a": 1, "b": "foo", "c": true}'
     output = ObjectSerializer().serialize('json', self.obj)
     self.assertEquals(output, expected)