def test_subclass_definition(self): # type: () -> None class ImmutableDict(Mapping): def __init__(self, underlying): self.underlying = underlying def __contains__(self, item): return item in self.underlying def __getitem__(self, k): return self.underlying[k] def get(self, k, default=None): return self.underlying.get(k, default) def __iter__(self): return iter(self.underlying) def __len__(self): return len(self.underlying) def keys(self): return self.underlying.keys() def items(self): return self.underlying.items() def values(self): return self.underlying.values() class ExtendedSchema(ClassConfigurationSchema): base_class = BaseSomething default_path = 'tests.test_fields_meta.AnotherSomething' description = 'Neat-o schema thing' schema = ExtendedSchema() config = {} # type: dict with pytest.raises(ValidationError) as error_context: schema.instantiate_from(config) assert error_context.value.args[0] == [ Error('Missing key: baz', code='MISSING', pointer='kwargs.baz'), ] assert config['object'] == AnotherSomething config = {'kwargs': {'baz': None}} value = schema.instantiate_from(config) assert isinstance(value, AnotherSomething) assert value.baz is None assert value.qux == 'unset' assert config['object'] == AnotherSomething config2 = ImmutableDict({'kwargs': {'baz': None}}) assert schema.errors(config2) == [] assert 'object' not in config2 assert schema.introspect() == { 'type': 'class_config_dictionary', 'description': 'Neat-o schema thing', 'default_path': 'tests.test_fields_meta.AnotherSomething', 'base_class': 'BaseSomething', 'switch_field': 'path', 'switch_field_schema': TypePath(base_classes=BaseSomething).introspect(), 'kwargs_field': 'kwargs', 'kwargs_contents_map': { 'tests.test_fields_meta.AnotherSomething': Dictionary( { 'baz': Nullable(UnicodeString()), 'qux': Boolean() }, optional_keys=('qux', ), ).introspect(), }, }
def test_nullable(self): constant = Constant('one', 'two') schema = Nullable(constant) self.assertEqual([], schema.errors(None)) self.assertEqual([], schema.errors('one')) self.assertEqual([], schema.errors('two')) self.assertEqual(1, len(schema.errors('three'))) self.assertEqual( { 'type': 'nullable', 'nullable': constant.introspect() }, schema.introspect()) boolean = Boolean(description='This is a test description') schema = Nullable(boolean) self.assertEqual([], schema.errors(None)) self.assertIsNone(schema.errors(True)) self.assertIsNone(schema.errors(False)) self.assertEqual(1, len(schema.errors('true'))) self.assertEqual(1, len(schema.errors(1))) self.assertEqual({ 'type': 'nullable', 'nullable': boolean.introspect() }, schema.introspect()) string = UnicodeString() schema = Nullable(string) self.assertEqual([], schema.errors(None)) self.assertIsNone(schema.errors('hello, world')) self.assertEqual(1, len(schema.errors(b'hello, world'))) self.assertEqual({ 'type': 'nullable', 'nullable': string.introspect() }, schema.introspect())
@ClassConfigurationSchema.provider( Dictionary({ 'foo': Boolean(), 'bar': Constant('walk', 'run') })) class SpecificSomething(BaseSomething): def __init__(self, foo, bar): self.foo = foo self.bar = bar @ClassConfigurationSchema.provider( Dictionary({ 'baz': Nullable(UnicodeString()), 'qux': Boolean() }, optional_keys=('qux', )), ) class AnotherSomething(BaseSomething): def __init__(self, baz, qux='unset'): self.baz = baz self.qux = qux class ExtendedAnotherSomething(AnotherSomething): pass @ClassConfigurationSchema.provider( Dictionary({