def test_generate_stub_no_class_name(self): schema = Schema() schema.x = VirtualField(lambda x: None) schema.y = StringField() with pytest.raises(TypeError): generate_stub(schema)
def test_to_basic_schema(self): schema = Schema() schema.x = IntField(default=1) schema.y = IntField(default=2) field = ListProxy(MockConfig(), schema) field.append(schema()) assert field.to_basic() == [{'x': 1, 'y': 2}]
def test_getitem(self): schema = Schema() schema.x = Field() schema.y.z = Field() assert schema['x'] is schema.x assert schema['y'] is schema.y assert schema['y.z'] is schema.y.z
def test_iter(self): schema = Schema() subfield = Field() topfield = Field() schema.sub.field = subfield schema.field = topfield items = sorted(list(schema.__iter__()), key=lambda x: x[0]) assert items == [('field', topfield), ('sub', schema.sub)]
def test_setdefault(self): schema = Schema() field = Field() field.__setdefault__ = MagicMock() schema.x = field config = schema() field.__setdefault__.assert_called_once_with(config)
def test_to_tree(self): schema = Schema(dynamic=True) schema.x = Field(default=2) schema.blah.y = Field(default=3) config = schema() config.z = 4 assert config.to_tree() == {'x': 2, 'blah': {'y': 3}, 'z': 4}
def test_setattr_field(self): field = Field() field.__setkey__ = MagicMock() schema = Schema() schema.field = field assert field.__setkey__.called_once_with(schema, 'field') assert schema._fields['field'] is field
def test_validate_ignore_methods(self): getter = MagicMock() schema = Schema() schema.x = InstanceMethodField(getter) schema.x.__getval__ = MagicMock() config = schema() schema._validate(config) assert not schema.x.__getval__.called
def test_validate_schema_invalid(self): schema = Schema() schema.x = IntField(default=1) schema.y = IntField(default=2) cfg = MockConfig() proxy = ListProxy(cfg, schema) with pytest.raises(ValueError): proxy._validate(100)
def test_load_tree_validate(self): schema = Schema() schema.x = Field() config = schema() mock_validate = MagicMock() object.__setattr__(config, 'validate', mock_validate) config.load_tree({'x': 1}) mock_validate.assert_called_once_with()
def test_setattr_value(self): schema = Schema() field = Field() field.__setval__ = MagicMock() schema.x = field config = schema() config.x = 2 field.__setval__.assert_called_once_with(config, 2)
def test_set_config(self): schema = Schema() schema.x.y = Field() config = schema() sub = schema.x() sub.y = 10 config.x = sub assert sub._parent is config assert config.x is sub
def test_generate_stub_schema(self): schema = Schema() schema.x = VirtualField(lambda x: None) schema.y = StringField() stub = generate_stub(schema, 'Thing').split('\n') assert 'class Thing(cincoconfig.config.ConfigType):' in stub assert ' x: typing.Any' in stub assert ' y: str' in stub assert ' def __init__(self, y: str): ...' in stub
def test_make_type(self): schema = Schema() schema.x = Field(default=2) schema.y = Field() CustomConfig = schema.make_type('CustomConfig') a = CustomConfig(y=10) assert isinstance(a, Config) assert a.x == 2 assert a.y == 10
def test_iter(self): schema = Schema(dynamic=True) schema.x = Field(default=2) schema.blah.y = Field(default=3) config = schema() config.z = 4 items = sorted(list(config.__iter__()), key=lambda x: x[0]) assert items == [('blah', config.blah), ('x', 2), ('z', 4)]
def test_get_method_annotation_kwonly_varargs(self): def meth(obj, x: int, y: 'asdf' = None, *args, z=None, **kwargs) -> int: pass schema = Schema() schema.instance_method('hello')(meth) instance_method = schema.hello expected = 'def hello(self, x: int, y: asdf, *args, z: typing.Any, **kwargs) -> int: ...' assert get_method_annotation('hello', instance_method) == expected
def test_get_method_annotation_var_kw(self): def meth(obj, x: int, *args, **kwargs): pass schema = Schema() schema.instance_method('hello')(meth) instance_method = schema.hello expected = 'def hello(self, x: int, *args, **kwargs): ...' assert get_method_annotation('hello', instance_method) == expected
def test_dumps(self, fr_get): fmt = MockFormatter() fr_get.return_value = fmt schema = Schema() schema.x = Field(default=2) config = schema() msg = config.dumps(format='blah') assert msg == b'hello, world' fmt.dumps.assert_called_once_with(config, {'x': 2}) fr_get.assert_called_once_with('blah')
def test_validator(self): validator = MagicMock() schema = Schema() schema.x = Field() @schema.validator def validate(cfg): validator(cfg) config = schema() config.validate() validator.assert_called_once_with(config)
def test_validate_schema_dict(self): schema = Schema() schema.x = IntField(default=1) schema.y = IntField(default=2) cfg = MockConfig() proxy = ListProxy(cfg, schema) check = proxy._validate({'x': 10}) assert isinstance(check, Config) assert check.x == 10 assert check.y == 2 assert check._parent is cfg assert check._schema is schema
def test_validate_schema_config(self): schema = Schema() schema.x = IntField(default=1) schema.y = IntField(default=2) cfg = MockConfig() proxy = ListProxy(cfg, schema) val = schema() val.x = 10 check = proxy._validate(val) assert isinstance(check, Config) assert check is val assert check._parent is cfg assert check._schema is schema
def test_setattr_config_dict(self): schema = Schema() schema.blah.x = Field() config = schema() config.blah = {'x': 2} assert config.blah.x == 2
def test_setattr_config_no_dict(self): schema = Schema() schema.blah.x = Field() config = schema() with pytest.raises(TypeError): config.blah = 2
def test_setattr_dynamic(self): schema = Schema(dynamic=True) config = schema() config.x = 2 assert config.x == 2 assert isinstance(config._fields['x'], AnyField)
def test_setitem_nested(self): schema = Schema() schema.x.y = AnyField(default=2) config = schema() config['x.y'] = 10 assert config['x.y'] == 10
def test_save(self, dumps, mop): dumps.return_value = b'hello' config = Config(Schema()) config.save('blah.txt', format='blah') dumps.assert_called_once_with('blah') mop.assert_called_once_with('blah.txt', 'wb') mop().write.assert_called_once_with(b'hello')
def test_load(self, loads, mop): loads.return_value = {'x': 1} config = Config(Schema()) object.__setattr__(config, 'loads', loads) assert config.load('blah.txt', format='blah') == {'x': 1} loads.assert_called_once_with(b'hello', 'blah') mop.assert_called_once_with('blah.txt', 'rb')
def test_get_all_fields(self): schema = Schema() schema.x = Field() schema.sub1.y = Field() schema.sub1.sub2.z = Field() schema.sub1.a = Field() schema.sub3.b = Field() check = schema.get_all_fields() assert check == [('x', schema, schema.x), ('sub1', schema, schema.sub1), ('sub1.y', schema.sub1, schema.sub1.y), ('sub1.sub2', schema.sub1, schema.sub1.sub2), ('sub1.sub2.z', schema.sub1.sub2, schema.sub1.sub2.z), ('sub1.a', schema.sub1, schema.sub1.a), ('sub3', schema, schema.sub3), ('sub3.b', schema.sub3, schema.sub3.b)]
def test_subschema(self): schema = Schema() sub = schema.sub config = schema() assert isinstance(config._data['sub'], Config) assert config._data['sub']._schema is sub assert config._data['sub']._parent is config
def test_instance_method_decorator(self): schema = Schema() @schema.instance_method('test') def meth(cfg): pass assert isinstance(schema._fields['test'], InstanceMethodField) assert schema._fields['test'].method is meth