def test_unicode_serialization(self): field = UnicodeField() assert field.serialize(u'wow ☃') assert field.serialize('still ok') assert field.serialize(1.0) assert field.serialize(None) is None field = UnicodeField(strict=True) assert field.serialize(u'nice snowman ☃') assert field.serialize('not strict enough')
def test_optional_validation(self): """Fields are required by default but can be made optional""" fields = [ IntegerField(required=False), UnicodeField(required=False), ObjectField(required=False), ArrayField(required=False), IsoDateString(required=False), # subschema tested elsewhere... ] for field in fields: assert field.validate(None)
def test_unicode_serialization(self): field = UnicodeField() assert field.serialize(u"wow ☃") assert field.serialize("still ok") assert field.serialize(1.0) assert field.serialize(None) is None field = UnicodeField(strict=True) assert field.serialize(u"nice snowman ☃") assert field.serialize("not strict enough")
class FirstSchema(Schema): foo = UnicodeField(required=False, default=default)
def test_unicode_validation(self): field = UnicodeField() assert field.validate(u"wow ☃") assert field.validate("still ok") with self.assertRaises(Invalid): field.validate(1.0) with self.assertRaises(Invalid): field.validate(None) field = UnicodeField(required=False) assert field.validate(u"wow ☃") assert field.validate(None) with self.assertRaises(Invalid): field.validate(1) field = UnicodeField(strict=True) assert field.validate(u"nice snowman ☃") with self.assertRaises(Invalid): field.validate("not strict enough")
class RoundHole(Schema): id = UnicodeField() name = UnicodeField() diameter = NumberField()
class DefaultsSchema(Schema): not_defaulted = UnicodeField() defaulted = UnicodeField(default="testing")
class Sub(Schema): foo = UnicodeField()
class Ship(Schema): name = UnicodeField() captain = Subschema(Person)
class Person(Schema): first_name = UnicodeField() last_name = UnicodeField()
class Schema2(Schema): peas = UnicodeField() carrots = IntegerField() beans = IntegerField()
class Whoa(Schema): __lambda = UnicodeField()
class Out(Schema): wow = UnicodeField()
class In(Schema): __finally = UnicodeField()
class MySchema(Schema): content = UnicodeField()
class Inner(Schema): foo = UnicodeField()
class SquarePeg(Schema): id = IntegerField() name = UnicodeField() width = NumberField()
def test_unicode_validation(self): field = UnicodeField() assert field.validate(u'wow ☃') assert field.validate('still ok') with self.assertRaises(Invalid): field.validate(1.0) with self.assertRaises(Invalid): field.validate(None) field = UnicodeField(required=False) assert field.validate(u'wow ☃') assert field.validate(None) with self.assertRaises(Invalid): field.validate(1) field = UnicodeField(strict=True) assert field.validate(u'nice snowman ☃') if six.PY2: with self.assertRaises(Invalid): field.validate('not strict enough')
class Schema1(Schema): my_str = UnicodeField() my_int = IntegerField() another_str = UnicodeField()