Exemple #1
0
    def test_get_annotation_typestr_type_module(self):
        custom_type = type('CustomType', tuple(), {})
        custom_type.__module__ = 'a.b.c'
        field = Field(key='helo')
        field.storage_type = custom_type

        assert get_annotation_typestr(field) == 'a.b.c.CustomType'
Exemple #2
0
 def test_setdefault_env_exists(self, mock_environ_get):
     retval = mock_environ_get.return_value = object()
     cfg = Config(schema=Schema())
     field = Field(env='ASDF', key='field')
     field.__setdefault__(cfg)
     assert cfg._data == {'field': retval}
     mock_environ_get.assert_called_once_with('ASDF')
Exemple #3
0
    def test_validate_validator_invalid(self):
        def inner(cfg, value):
            raise KeyError()

        field = Field(key='key', validator=inner)
        with pytest.raises(KeyError):
            field.validate(self.cfg, 'hello')
Exemple #4
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 #5
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 #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)
Exemple #7
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 #8
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 #9
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 #10
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 #11
0
 def test_setdefault_env_exists_valid(self, mock_environ_get):
     env = mock_environ_get.return_value = object()
     retval = object()
     cfg = Config(schema=Schema())
     field = Field(env='ASDF', key='field')
     field.validate = MagicMock(return_value=retval)
     field.__setdefault__(cfg)
     field.validate.assert_called_once_with(cfg, env)
     assert cfg._data == {'field': retval}
Exemple #12
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 #13
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 #14
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 #15
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 #16
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
     }
Exemple #17
0
    def test_in_nested(self):
        schema = Schema()
        schema.x.y = Field()
        config = schema()
        config.x.y = 2

        assert 'x.y' in config
Exemple #18
0
    def test_generate_argparse_parser(self, mock_argparse):
        parser = MagicMock()
        mock_argparse.return_value = parser
        schema = Schema()
        schema.long_name = StringField(help='asdf')
        schema.sub.short_name = IntField()
        schema.enable = BoolField()
        schema.runtime = FloatField()
        schema.ignore_me2 = Field()  # no storage_type

        retval = generate_argparse_parser(schema, x=1, y=2)
        assert retval is parser
        mock_argparse.assert_called_once_with(x=1, y=2)
        parser.add_argument.call_args_list == [
            call('--long-name',
                 action='store',
                 dest='long_name',
                 help='asdf',
                 metavar='LONG_NAME'),
            call('--sub-short-name',
                 action='store',
                 dest='sub.short_name',
                 help=None,
                 metavar='SUB_SHORT_NAME'),
            call('--enable', action='store_true', help=None, dest='enable'),
            call('--no-enable', action='store_false', help=None,
                 dest='enable'),
            call('--runtime', action='store', dest='runtime', help=None)
        ]
Exemple #19
0
    def test_setattr_config_no_dict(self):
        schema = Schema()
        schema.blah.x = Field()
        config = schema()

        with pytest.raises(ValidationError):
            config.blah = 2
Exemple #20
0
    def test_in_flat(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

        assert 'x' in config
Exemple #21
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 #22
0
    def test_in_not_config(self):
        schema = Schema()
        schema.x = Field()
        config = schema()
        config.x = 2

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

        assert 'z' not in config
Exemple #24
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)]
Exemple #25
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)
Exemple #26
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 #27
0
    def test_set_value_config_exc(self):
        err = ValueError()
        schema = Schema()
        schema.x.y = Field()
        schema.x._validators.append(MagicMock(side_effect=err))
        config = schema()
        with pytest.raises(ValidationError) as exc:
            config._set_value('x', {})

        assert exc.value.exc is err
Exemple #28
0
    def test_load_tree_to_python_validation_error(self):
        err = ValidationError(None, None, None)
        schema = Schema()
        field = schema.x = Field()
        field.to_python = MagicMock(side_effect=err)
        config = schema()
        with pytest.raises(ValidationError) as exc:
            config.load_tree({'x': 2})

        assert exc.value is err
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_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'