def test_can_make_generic_piecemeal(self): generic = GenericSchema() generic.foo = 1 self.assertEqual(generic.foo, 1) self.assertIsNone(generic.qux) generic.qux = 'pah' self.assertEqual(generic.qux, 'pah')
def test_raw_property_gets_all_values_in_tree(self): third = GenericSchema(not_junk=False) second = GenericSchema(data=[1, 2, 3], thirdnested=third) first = GenericSchema(some_sub=second) _raw = first._raw self.assertIsInstance(_raw, GenericSchema) self.assertIsInstance(_raw.some_sub, GenericSchema) self.assertIsInstance(_raw.some_sub.thirdnested, GenericSchema) self.assertEqual(_raw.some_sub.data, [1, 2, 3]) self.assertEqual(_raw.some_sub.thirdnested.not_junk, False)
def test_can_serialize_recursive(self): """ When serializing, descend through arrays and subschemas. """ class Some(Schema): wow = ArrayField() some_full = Some(wow=[1, 2]) some_nullish = Some(wow=[]) inner1 = GenericSchema() inner2 = GenericSchema(foo=["wow", inner1, some_full, some_nullish], bar=inner1, baz=[inner1, inner1]) outer = GenericSchema(inner=inner2, cool="ok") expected_implicit_nulls = { "inner": { "foo": ["wow", {"wow": [1, 2]}], }, "cool": "ok" } expected_explicit_nulls = { "inner": { "foo": ["wow", {}, {"wow": [1, 2]}, {"wow": []}], "bar": {}, "baz": [{}, {}] }, "cool": "ok" } self.assertEqual(expected_implicit_nulls, outer.serialize(implicit_nulls=True)) self.assertEqual(expected_explicit_nulls, outer.serialize(implicit_nulls=False))
def test_all_on_generic_schema(self): """ All should work with GenericSchema() """ class AllLax(Mapping): obj = All(strict=False) class AllStrict(Mapping): obj = All(strict=True) source = GenericSchema(**{"target": 1}) for mapping in (AllLax, AllStrict): result = mapping().apply(source).serialize() expected = {"obj": {"target": 1}} self.assertEqual(expected, result)
def test_can_serialize_recursive(self): """ When serializing, descend through arrays and subschemas. """ class Some(Schema): wow = ArrayField() some_full = Some(wow=[1, 2]) some_nullish = Some(wow=[]) inner1 = GenericSchema() inner2 = GenericSchema(foo=["wow", inner1, some_full, some_nullish], bar=inner1, baz=[inner1, inner1]) outer = GenericSchema(inner=inner2, cool="ok") expected_implicit_nulls = { "inner": { "foo": ["wow", { "wow": [1, 2] }], }, "cool": "ok" } expected_explicit_nulls = { "inner": { "foo": ["wow", {}, { "wow": [1, 2] }, { "wow": [] }], "bar": {}, "baz": [{}, {}] }, "cool": "ok" } self.assertEqual(expected_implicit_nulls, outer.serialize(implicit_nulls=True)) self.assertEqual(expected_explicit_nulls, outer.serialize(implicit_nulls=False))
def test_can_serialize_generic(self): source = {"foo": 1, "bar": 2, "baz": [3, 4, 5]} generic = GenericSchema(**source) self.assertEqual(source, generic.serialize())
def test_generic_is_always_valid(self): generic1 = GenericSchema() assert generic1.validate() generic2 = GenericSchema(**{"foo": {}, "bar": "great"}) assert generic2.validate()
def test_can_make_a_generic_schema_from_dict(self): generic = GenericSchema(**{"foo": 1, "bar": 2, "baz": [3, 4, 5]}) self.assertEqual(generic.foo, 1) self.assertEqual(generic.bar, 2) self.assertEqual(generic.baz, [3, 4, 5]) self.assertIsNone(generic.qux)