Ejemplo n.º 1
0
    def test_not_in(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

        assert 'z' not in config
Ejemplo n.º 2
0
    def test_in_not_config(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

        assert 'x.y' not in config
Ejemplo n.º 3
0
    def test_in_flat(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

        assert 'x' in config
Ejemplo n.º 4
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)
Ejemplo n.º 5
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
Ejemplo n.º 6
0
    def test_setdefault(self):
        schema = Schema()
        field = Field()
        field.__setdefault__ = MagicMock()
        schema.x = field

        config = schema()
        field.__setdefault__.assert_called_once_with(config)
Ejemplo n.º 7
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'])
Ejemplo n.º 8
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)]
Ejemplo n.º 9
0
 def test_is_feature_disabled_false(self):
     schema = Schema()
     schema.x = Field()
     schema.y = FeatureFlagField()
     schema.z = FeatureFlagField()
     config = schema()
     config._data.update({'y': True, 'z': False})
     assert not schema._is_feature_enabled(config)
Ejemplo n.º 10
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
Ejemplo n.º 11
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()
Ejemplo n.º 12
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)
Ejemplo n.º 13
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)
Ejemplo n.º 14
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)]
Ejemplo n.º 15
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')
Ejemplo n.º 16
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
Ejemplo n.º 17
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
Ejemplo n.º 18
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
Ejemplo n.º 19
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}}
Ejemplo n.º 20
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}
Ejemplo n.º 21
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')
Ejemplo n.º 22
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)
Ejemplo n.º 23
0
    def test_validate_collect_errors(self):
        schema_error = ValidationError(None, None, None)
        field_error = ValidationError(None, None, None)
        schema = Schema()
        schema._validators.append(MagicMock(side_effect=schema_error))
        schema.x = Field()
        schema.x.validate = MagicMock(side_effect=field_error)

        config = schema()

        errors = schema._validate(config, collect_errors=True)
        assert errors == [field_error, schema_error]
Ejemplo n.º 24
0
    def test_validate_schema_dict(self):
        schema = Schema()
        schema.x = IntField(default=1)
        schema.y = IntField(default=2)
        cfg = MockConfig()
        proxy = ListProxy(cfg, ListField(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
Ejemplo n.º 25
0
    def test_validate_schema_config(self):
        schema = Schema()
        schema.x = IntField(default=1)
        schema.y = IntField(default=2)
        cfg = MockConfig()
        proxy = ListProxy(cfg, ListField(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
Ejemplo n.º 26
0
 def test_asdict_virtual(self):
     schema = Schema()
     schema.x = Field()
     schema.sub1.y = Field()
     schema.a = VirtualField(lambda cfg: 100)
     config = schema()
     config.x = 1
     config.sub1.y = 2
     assert asdict(config, virtual=True) == {
         'x': 1,
         'sub1': {
             'y': 2
         },
         'a': 100
     }
Ejemplo n.º 27
0
    def test_validate_collect_errors_other(self):
        schema_error = ValueError()
        field_error = ValueError()
        schema = Schema()
        schema._validators.append(MagicMock(side_effect=schema_error))
        schema.x = Field()
        schema.x.validate = MagicMock(side_effect=field_error)

        config = schema()

        errors = schema._validate(config, collect_errors=True)
        assert len(errors) == 2
        assert isinstance(errors[0], ValidationError)
        assert errors[0].exc is field_error
        assert isinstance(errors[1], ValidationError)
        assert errors[1].exc is schema_error
Ejemplo n.º 28
0
    def test_get_all_fields_schema(self):
        schema = Schema()
        schema.x = Field()
        schema.sub1.y = Field()
        schema.sub1.sub2.z = Field()
        schema.sub1.a = Field()
        schema.sub3.b = Field()

        check = get_all_fields(schema)
        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)]
Ejemplo n.º 29
0
    def test_dumps_to_tree_args(self, fr_get):
        fmt = MockFormatter()
        fr_get.return_value = fmt
        schema = Schema()
        schema.x = Field(default=2)
        config = schema()

        virtual = object()
        sensitive_mask = object()

        mock_to_tree = MagicMock(returnvalue={})
        object.__setattr__(config, 'to_tree', mock_to_tree)
        config.dumps(format='blah',
                     virtual=virtual,
                     sensitive_mask=sensitive_mask)
        mock_to_tree.assert_called_once_with(virtual=virtual,
                                             sensitive_mask=sensitive_mask)
Ejemplo n.º 30
0
    def test_cmdline_args_override(self):
        parser = argparse.ArgumentParser()
        schema = Schema()
        schema.w = Field(default='w')
        schema.x = Field(default='x')
        schema.y.z = Field(default='z')
        config = schema()

        parser.add_argument('-x', action='store')
        parser.add_argument('-z', action='store', dest='y.z')
        parser.add_argument('-i', action='store')
        parser.add_argument('-j', action='store')
        args = parser.parse_args(['-x', '1', '-z', '2', '-j', '3'])

        cmdline_args_override(config, args, ignore=['j'])

        assert config.x == '1'
        assert config.y.z == '2'
        assert config.w == 'w'