Exemple #1
0
    def test_in_not_config(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

        assert 'x.y' not in config
Exemple #2
0
    def test_not_in(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

        assert 'z' not in config
Exemple #3
0
 def test_add_field_config_type(self, mock_ct_field_cls):
     mock_ct_field = mock_ct_field_cls.return_value = MagicMock()
     mock_ct_field.__setkey__ = MagicMock()
     schema = Schema()
     schema._add_field('x', ConfigType)
     assert schema._fields == {'x': mock_ct_field}
     mock_ct_field.__setkey__.assert_called_once_with(schema, 'x')
Exemple #4
0
 def test_ref_path_container_error(self):
     parent = Config(Schema(key='root'))
     child = Config(Schema(key='child'), parent=parent)
     child._container = MagicMock()
     child._container._get_item_position = MagicMock(
         side_effect=ValueError())
     assert child._ref_path == 'root.child'
Exemple #5
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 #6
0
 def test_get_item_position_not_exists(self):
     schema = Schema()
     schema.lst = ListField(IntField())
     config = schema()
     proxy = ListProxy(config, schema.lst)
     proxy.extend([1, 2, 3])
     assert proxy._get_item_position(10) == '3'
Exemple #7
0
    def test_in_flat(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

        assert 'x' in config
Exemple #8
0
 def test_validate_not_field(self):
     schema = Schema()
     schema.lst = ListField(int)
     config = schema()
     proxy = ListProxy(config, schema.lst)
     with pytest.raises(TypeError):
         proxy._validate(10)
Exemple #9
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 #10
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 #11
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 #12
0
 def test_ref_path_container(self):
     parent = Config(Schema(key='root'))
     child = Config(Schema(key='child'), parent=parent)
     child._container = MagicMock()
     child._container._get_item_position = MagicMock()
     child._container._get_item_position.return_value = '1'
     assert child._ref_path == 'root.child[1]'
     child._container._get_item_position.assert_called_once_with(child)
Exemple #13
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 #14
0
 def test_get_fields_config(self):
     schema = Schema(dynamic=True)
     schema.x = Field()
     schema.sub1.y = Field()
     config = schema()
     config.z = 2
     assert get_fields(config) == [('z', config._fields['z']),
                                   ('x', schema.x), ('sub1', schema.sub1)]
Exemple #15
0
    def test_validate_feature_flag_disabled(self):
        cfg = MagicMock()
        schema = Schema()
        schema._validators.append(MagicMock(side_effect=ValueError()))

        schema._is_feature_enabled = MagicMock(return_value=False)
        schema._validate(cfg)
        schema._is_feature_enabled.assert_called_once_with(cfg)
Exemple #16
0
 def test_default_keys(self):
     schema = Schema()
     schema.a.b = Field(default=0)
     schema.x = Field(default=1)
     schema.y = Field(default=2)
     schema.z = Field(default=3)
     config = schema(y=2, z=4)
     assert config._default_value_keys == set(['a', 'x'])
Exemple #17
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 #18
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 #19
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 #20
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 #21
0
    def test_make_type(self):
        schema = Schema()
        schema.x = Field(default=2)
        schema.y = Field()

        CustomConfig = make_type(schema, 'CustomConfig')
        cfg = CustomConfig(y=10)
        assert isinstance(cfg, Config)
        assert cfg.x == 2
        assert cfg.y == 10
Exemple #22
0
 def test_asdict(self):
     schema = Schema(dynamic=True)
     schema.x = Field()
     schema.sub1.y = Field()
     schema.a = VirtualField(lambda cfg: 100)
     config = schema()
     config.x = 1
     config.sub1.y = 2
     config.z = 3
     assert asdict(config) == {'x': 1, 'z': 3, 'sub1': {'y': 2}}
Exemple #23
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.core.ConfigType):' in stub
        assert '    x: typing.Any' in stub
        assert '    y: str' in stub
        assert '    def __init__(self, y: str): ...' in stub
Exemple #24
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 #25
0
    def test_get_method_annotation_kwonly(self):
        def meth(obj, x: int, y: str = None, *, z=None, **kwargs) -> int:
            pass

        schema = Schema()
        schema.instance_method('hello')(meth)
        instance_method = schema.hello

        expected = 'def hello(self, x: int, y: str, *, z: typing.Any, **kwargs) -> int: ...'
        assert get_method_annotation('hello', instance_method) == expected
Exemple #26
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 #27
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 #28
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
        print(config._fields)
        print(config._data)
        assert config.to_tree() == {'x': 2, 'blah': {'y': 3}, 'z': 4}
Exemple #29
0
 def test_load_tree_ignore_env(self, mock_os):
     env = mock_os.environ.get.return_value = object()
     schema = Schema()
     schema.x = Field(env='ASDF')
     schema.x.__setdefault__ = MagicMock()
     cfg = schema()
     cfg._data = {'x': 'qwer'}
     cfg.load_tree({'x': 'asdf'})
     assert cfg._data == {'x': 'qwer'}
     mock_os.environ.get.assert_called_once_with('ASDF')
Exemple #30
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')