def test_int_field(self): field = IntField() self.assertEqual(field.to_representation(5), 5) self.assertEqual(field.to_representation(5.4), 5) self.assertEqual(field.to_representation('5'), 5) self.assertEqual(field.to_internal_value(5), 5) self.assertEqual(field.to_internal_value(5.4), 5) self.assertEqual(field.to_internal_value('5'), 5)
def test_to_value_deprecation_warning(self): with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always', DeprecationWarning) IntField().to_value('1') self.assertEqual(len(w), 1) self.assertTrue(issubclass(w[-1].category, DeprecationWarning)) self.assertIn('deprecated', str(w[-1].message))
def test_to_value_backwards_compatibility(self): class AddOneIntField(IntField): def to_value(self, value): return super(AddOneIntField, self).to_value(value) + 1 self.assertEqual(AddOneIntField().to_value('1'), 2) self.assertEqual(IntField().to_value('1'), 1)
def test_is_to_value_overridden(self): class TransField(Field): def to_value(self, value): return value field = Field() self.assertFalse(field._is_to_value_overridden()) field = TransField() self.assertTrue(field._is_to_value_overridden()) field = IntField() self.assertTrue(field._is_to_value_overridden())
def test_int_field(self): field = IntField() self.assertEqual(field.to_representation(5), 5) self.assertEqual(field.to_representation(5.4), 5) self.assertEqual(field.to_representation('5'), 5) self.assertEqual(field.to_internal_value(5), 5) self.assertEqual(field.to_internal_value(5.4), 5) self.assertEqual(field.to_internal_value('5'), 5)
def test_int_field(self): field = IntField() self.assertEqual(field.to_value(5), 5) self.assertEqual(field.to_value(5.4), 5) self.assertEqual(field.to_value('5'), 5)
class ASerializer(Serializer): a = IntField()
class ASerializer(DictSerializer): a = IntField() b = Field(attr='foo')
class ASerializer(Serializer): a = IntField(required=False)
class ASerializer(Serializer): a = IntField() b = FloatField(call=True) c = StrField(attr='foo.bar.baz')
def test_int_field(self): field = IntField() self.assertEqual(field.to_value(5), 5) self.assertEqual(field.to_value(5.4), 5) self.assertEqual(field.to_value('5'), 5)
class ASerializer(Serializer): _cls = Obj a = IntField(read_only=True)
class ASerializer(Serializer): _cls = Obj a = IntField()
class ASerializer(Serializer): _cls = Obj a = IntField(required=False)