def test_all_on_nested_schema(self): """ All should work on nested Schema """ class ManySchema(Schema): prop = IntegerField() class SecondSchema(Schema): those = ArrayField(ManySchema) class FirstSchema(Schema): that = Subschema(SecondSchema) data = { 'that': { 'those': [{ 'prop': 1 }, { 'prop': 2 }] }, 'whatever': 'right' } sch = FirstSchema(**data) self.assertDictEqual(data, All()(sch).serialize()) del data['whatever'] self.assertDictEqual(data, All(strict=True)(sch).serialize())
def test_no_bad_implementation(self): """Don't leak state""" class Myschema(Schema): wow = IntegerField() first = {"wow": 1, "other": 2} m = Myschema(first) m2 = Myschema() self.assertEqual(first, All()(m).serialize()) self.assertEqual({'wow': None}, All()(m2).serialize(implicit_nulls=False))
def test_all_passes_nullish(self): class Myschema(Schema): maybe = ArrayField() instance = Myschema(maybe=[]) allof = All(strict=True)(instance) self.assertEqual(allof.maybe, [])
def test_all_passes_any_object(self): class Some(object): foo = "bar" wow = None instance = Some() allof = All()(instance) self.assertEqual(allof, instance)
def test_all_nonstrict_mutation(self): """ If schema instance is mutated, the output of All should reflect it. """ class MySchema(Schema): wow = IntegerField(required=False) instance = MySchema(random=2) instance.wow = 1 allof = All(strict=False)(instance) self.assertEqual({"wow": 1, "random": 2}, allof.serialize()) # strict on the other hand... instance = MySchema(wow=1, random=2) instance.wow = 2 allof = All(strict=True)(instance) self.assertEqual({"wow": 2}, allof.serialize())
def test_all_is_all_by_default(self): class Myschema(Schema): wow = IntegerField() extras = {"wow": 1, "other": 2} m = Myschema(extras) # by default extra keys are passed through allof = All()(m) self.assertEqual(extras, allof.serialize()) # without a schema, we obvs have to pass everything schemaless = All(strict=True)(extras) self.assertEqual(extras, schemaless) # the 'strict' flag means extra kwargs outside the schema # get filtered out. filtered = All(strict=True)(m) self.assertEqual({"wow": 1}, filtered.serialize())
def test_generic_schema_non_strict_all_shows_all_on_subschema(self): data = {'first': 1, 'second': {'prop': 2, 'visible': True}} class Second(GenericSchema): prop = IntegerField() class First(GenericSchema): second = Subschema(Second) sch = First(**data) self.assertDictEqual(data, All()(sch).serialize())
def test_all_passes_dict(self): source = {"foo": "bar", "wow": None} allof = All()(source) self.assertEqual(source, allof)
class Outer(Mapping): inner = Submapping(Inner, All())
class AllStrict(Mapping): obj = All(strict=True)
class AllLax(Mapping): obj = All(strict=False)