Exemple #1
0
    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)
Exemple #2
0
 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}]
Exemple #3
0
    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
Exemple #4
0
 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)]
Exemple #5
0
    def test_setdefault(self):
        schema = Schema()
        field = Field()
        field.__setdefault__ = MagicMock()
        schema.x = field

        config = schema()
        field.__setdefault__.assert_called_once_with(config)
Exemple #6
0
    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}
Exemple #7
0
    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
Exemple #8
0
    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
Exemple #9
0
    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)
Exemple #10
0
    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()
Exemple #11
0
    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)
Exemple #12
0
    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
Exemple #13
0
    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
Exemple #14
0
    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
Exemple #15
0
    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)]
Exemple #16
0
    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
Exemple #17
0
    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
Exemple #18
0
    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')
Exemple #19
0
    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)
Exemple #20
0
    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
Exemple #21
0
    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
Exemple #22
0
    def test_setattr_config_dict(self):
        schema = Schema()
        schema.blah.x = Field()
        config = schema()

        config.blah = {'x': 2}
        assert config.blah.x == 2
Exemple #23
0
    def test_setattr_config_no_dict(self):
        schema = Schema()
        schema.blah.x = Field()
        config = schema()

        with pytest.raises(TypeError):
            config.blah = 2
Exemple #24
0
    def test_setattr_dynamic(self):
        schema = Schema(dynamic=True)
        config = schema()

        config.x = 2
        assert config.x == 2
        assert isinstance(config._fields['x'], AnyField)
Exemple #25
0
    def test_setitem_nested(self):
        schema = Schema()
        schema.x.y = AnyField(default=2)
        config = schema()

        config['x.y'] = 10
        assert config['x.y'] == 10
Exemple #26
0
    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')
Exemple #27
0
    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')
Exemple #28
0
    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)]
Exemple #29
0
    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
Exemple #30
0
    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