Пример #1
0
 def test_wo_name(self):
     serializer = SerializerFieldByAttr()
     self.assertEqual(serializer.serialize(self.obj, 'empty'), None)
     self.assertEqual(serializer.serialize(self.obj, 'field_1'), 1)
     self.assertEqual(serializer.serialize(self.obj, 'field_2'), 2)
     self.assertEqual(serializer.serialize(self.obj, 'field_3.sub_field_3'),
                      3)
Пример #2
0
    def test_serialize(self):
        serializer_empty_form = FormSerializer(self.form)
        self.assertEqual(serializer_empty_form.serialize(), {})

        serialize_right_form = type(
            'test_serializer_form', (FormSerializer, ), {
                'test_field_1': SerializerFieldByAttr(),
                'test_field_2': SerializerFieldByAttr()
            })(self.form)
        self.assertEqual(serialize_right_form.serialize(), {
            'test_field_1': 1,
            'test_field_2': 2
        })
Пример #3
0
 def setUp(self):
     self.field_by_attr_1 = SerializerFieldByAttr()
     self.field_by_attr_2 = SerializerFieldByAttr()
     self.field_method_1 = SerializerFieldMethod()
     self.field_method_2 = SerializerFieldMethod()
     self.field_set_1 = SerializerFieldSet()
     self.field_set_2 = SerializerFieldSet()
     self.fake_field_1 = None
     self.fake_field_2 = {}
     self.fake_field_3 = []
     self.right_fields = (self.field_by_attr_1, self.field_by_attr_2,
                          self.field_method_1, self.field_method_2,
                          self.field_set_1, self.field_set_2)
     self.fake_fields = (self.fake_field_1, self.fake_field_2,
                         self.fake_field_3)
Пример #4
0
    def test_with_name(self):
        serializer_1 = SerializerFieldByAttr('field_1')
        self.assertEqual(serializer_1.serialize(self.obj, 'empty'), 1)
        self.assertEqual(serializer_1.serialize(self.obj, 'field_1'), 1)
        self.assertEqual(serializer_1.serialize(self.obj, 'field_2'), 1)

        serializer_2 = SerializerFieldByAttr('field_2')
        self.assertEqual(serializer_2.serialize(self.obj, 'empty'), 2)
        self.assertEqual(serializer_2.serialize(self.obj, 'field_1'), 2)
        self.assertEqual(serializer_2.serialize(self.obj, 'field_2'), 2)

        serializer_3 = SerializerFieldByAttr('field_3.sub_field_3')
        self.assertEqual(serializer_3.serialize(self.obj, 'empty'), 3)
        self.assertEqual(serializer_3.serialize(self.obj, 'field_1'), 3)
        self.assertEqual(serializer_3.serialize(self.obj, 'field_2'), 3)

        serializer_4 = SerializerFieldByAttr('field_4')
        self.assertEqual(serializer_4.serialize(self.obj, 'empty'), None)
        self.assertEqual(serializer_4.serialize(self.obj, 'field_1'), None)
        self.assertEqual(serializer_4.serialize(self.obj, 'field_2'), None)

        serializer_5 = SerializerFieldByAttr('field_4.sub_5')
        self.assertEqual(serializer_5.serialize(self.obj, 'empty'), None)
        self.assertEqual(serializer_5.serialize(self.obj, 'field_1'), None)
        self.assertEqual(serializer_5.serialize(self.obj, 'field_2'), None)
Пример #5
0
 class CustomFieldSet(DefaultFieldSet):
     field_1 = SerializerFieldByAttr()
     field_3 = SerializerFieldByAttr()
Пример #6
0
 class DefaultFieldSet(SerializerFieldSet):
     field_1 = SerializerFieldByAttr()
     field_2 = SerializerFieldByAttr()
Пример #7
0
 class TFieldSet(SerializerFieldSet):
     field_1 = SerializerFieldByAttr()