コード例 #1
0
 def test_auto_yaml(self):
     self.filename = os.path.join(tempfile.gettempdir(), 'config.yaml')
     with open(self.filename, 'w') as tmpfile:
         tmpfile.write('key: 1')
         tmpfile.flush()
         config_data = loader.AutoConfiguration(base_dir=tempfile.gettempdir())
         assert_equal(config_data['key'], 1)
コード例 #2
0
 def test_auto_json(self):
     self.filename = os.path.join(tempfile.gettempdir(), 'config.json')
     with open(self.filename, 'w') as tmpfile:
         tmpfile.write('{"key": "1", "second.value": "two"}')
         tmpfile.flush()
         config_data = loader.AutoConfiguration(base_dir=tempfile.gettempdir())
         assert_equal(config_data['key'], '1')
コード例 #3
0
 def test_python_configuration_reload(self):
     config_data = loader.PythonConfiguration(self.module)
     assert_equal(config_data['more_values.depth'], 'one')
     self.remove_module()
     self.create_module('two')
     config_data = loader.PythonConfiguration(self.module)
     assert config_data['more_values.depth'] == 'two'
コード例 #4
0
    def test_build_loader_without_flatten(self):
        source = {'base': {'one': 'thing', 'two': 'foo'}}
        loader_func = mock.Mock(return_value=source)
        config_loader = loader.build_loader(loader_func)

        config = config_loader(source, flatten=False)
        assert_equal(config, source)
コード例 #5
0
    def test_load(self):
        loaders = [(mock.Mock(return_value={i: 0}), 1, 2) for i in range(3)]
        composite = loader.CompositeConfiguration(loaders)
        assert_equal(composite.load(), {0: 0, 1: 0, 2: 0})

        for loader_call, arg_one, arg_two in loaders:
            loader_call.assert_called_with(arg_one, arg_two)
コード例 #6
0
 def test_get_value_cached(self):
     expected = "the other stars"
     validator = mock.Mock()
     value_proxy = proxy.ValueProxy(validator, self.value_cache, "something.string")
     value_proxy._value = expected
     assert_equal(value_proxy.value, expected)
     validator.assert_not_called()
コード例 #7
0
 def test_build_map_from_list_of_dicts(self):
     def map_by_id(d):
         return d['id'], d['value']
     map_validator = validation.build_map_type_validator(map_by_id)
     expected = {'a': 'b', 'c': 'd'}
     source = [dict(id='a', value='b'), dict(id='c', value='d')]
     assert_equal(map_validator(source), expected)
コード例 #8
0
    def test_reload_default(self):
        staticconf.DictConfiguration(dict(one='three', seven='nine'))
        one, seven = staticconf.get('one'), staticconf.get('seven')

        staticconf.DictConfiguration(dict(one='ten', seven='el'))
        staticconf.reload()
        assert_equal(one, 'ten')
        assert_equal(seven, 'el')
コード例 #9
0
 def test_build_reader(self, mock_get_namespace):
     config_key, validator, self.namespace = 'the_key', mock.Mock(), 'the_name'
     reader = readers.build_reader(validator, self.namespace)
     value = reader(config_key)
     mock_get_namespace.assert_called_with(self.namespace)
     validator.assert_called_with(
         mock_get_namespace.return_value.get.return_value)
     assert_equal(value, validator.return_value)
コード例 #10
0
    def test_mock_configuration_context_manager(self):
        one = self.getters.get('one')
        three = self.getters.get_int('three', default=3)

        with testing.MockConfiguration(dict(one=7), namespace=self.namespace):
            assert_equal(one, 7)
            assert_equal(three, 3)
        assert_raises(errors.ConfigurationError, self.getters.get('one'))
コード例 #11
0
 def test_proxy_zero(self):
     validator = mock.Mock(return_value=0)
     self.value_proxy = proxy.ValueProxy(validator, self.value_cache, 'zero')
     assert_equal(self.value_proxy, 0)
     assert not self.value_proxy
     assert not self.value_proxy and True
     assert not self.value_proxy or False
     assert not self.value_proxy ^ 0
     assert ~ self.value_proxy
コード例 #12
0
 def test_build_getter(self):
     validator = mock.Mock()
     getter = getters.build_getter(validator)
     assert callable(getter), "Getter is not callable"
     value_proxy = getter('the_name')
     namespace = config.get_namespace(config.DEFAULT)
     assert_in(id(value_proxy), namespace.value_proxies)
     assert_equal(value_proxy.config_key, "the_name")
     assert_equal(value_proxy.namespace, namespace)
コード例 #13
0
 def test_build_loader_callable(self):
     load_func, filename = mock.Mock(), mock.Mock()
     loader_callable = config.build_loader_callable(
             load_func, filename, self.namespace)
     result = loader_callable()
     self.mock_get_namespace.assert_called_with(self.namespace)
     mock_namespace = self.mock_get_namespace.return_value
     mock_namespace.clear.assert_called_with()
     load_func.assert_called_with(filename, namespace=self.namespace)
     assert_equal(result, load_func.return_value)
コード例 #14
0
 def test_build_getter_with_getter_namespace(self):
     validator = mock.Mock()
     name = 'the stars'
     getter = getters.build_getter(validator, getter_namespace=name)
     assert callable(getter), "Getter is not callable"
     value_proxy = getter('the_name')
     namespace = config.get_namespace(name)
     assert_in(id(value_proxy), namespace.value_proxies)
     assert_equal(value_proxy.config_key, "the_name")
     assert_equal(value_proxy.namespace, namespace)
コード例 #15
0
    def test_get_value_proxies_does_not_contain_out_of_scope_proxies(self):
        assert not self.namespace.get_value_proxies()
        def a_scope():
            mock_proxy = mock.create_autospec(proxy.ValueProxy)
            self.namespace.register_proxy(mock_proxy)

        a_scope()
        a_scope()
        gc.collect()
        assert_equal(len(self.namespace.get_value_proxies()), 0)
コード例 #16
0
 def test_init_nested(self):
     conf = {
         'a': {
             'b': 'two',
         },
         'c': 'three'
     }
     with testing.MockConfiguration(conf):
         assert_equal(staticconf.get('a.b'), 'two')
         assert_equal(staticconf.get('c'), 'three')
コード例 #17
0
    def test_load_end_to_end(self):
        loader = staticconf.YamlConfiguration
        callback = mock.Mock()
        facade = staticconf.ConfigFacade.load(self.file.name, self.namespace, loader)
        facade.add_callback('one', callback)
        assert_equal(staticconf.get('one', namespace=self.namespace), "A")

        self.write(b"one: B", 10)
        facade.reload_if_changed()
        assert_equal(staticconf.get('one', namespace=self.namespace), "B")
        callback.assert_called_with()
コード例 #18
0
    def test_mock_configuration(self):
        two = self.getters.get_string('two')
        stars = self.getters.get_bool('stars')

        mock_config = testing.MockConfiguration(
            dict(two=2, stars=False), namespace=self.namespace)
        mock_config.setup()
        assert_equal(two, '2')
        assert not stars
        mock_config.teardown()
        assert_raises(errors.ConfigurationError, self.getters.get('two'))
コード例 #19
0
    def test_reload_single(self):
        name = 'another_one'
        staticconf.DictConfiguration(dict(one='three'))
        staticconf.DictConfiguration(dict(two='three'), namespace=name)
        one, two = staticconf.get('one'), staticconf.get('two', namespace=name)
        # access the values to set the value_proxy cache
        one.value, two.value

        staticconf.DictConfiguration(dict(one='four'))
        staticconf.DictConfiguration(dict(two='five'), namespace=name)
        staticconf.reload()
        assert_equal(one, 'four')
        assert_equal(two, 'three')
コード例 #20
0
    def test_load(self):
        filename, namespace = "filename", "namespace"
        loader = mock.Mock()

        with mock.patch(
                'staticconf.config.ConfigurationWatcher',
                autospec=True) as mock_watcher_class:
            facade = config.ConfigFacade.load(filename, namespace, loader)

        facade.watcher.load_config.assert_called_with()
        assert_equal(facade.watcher, mock_watcher_class.return_value)
        reloader = facade.callback_chain
        assert_equal(reloader, facade.watcher.get_reloader())
コード例 #21
0
 def test_proxy_with_datetime(self):
     the_date = datetime.datetime(2012, 12, 1, 5, 5, 5)
     validator = mock.Mock(return_value=the_date)
     value_proxy = proxy.ValueProxy(validator, self.value_cache, 'something')
     assert_equal(value_proxy, the_date)
     assert value_proxy < datetime.datetime(2012, 12, 3)
     assert value_proxy > datetime.datetime(2012, 1, 4)
     four_days_ahead = datetime.datetime(2012, 12, 4, 5, 5, 5)
     assert_equal(value_proxy + datetime.timedelta(days=3), four_days_ahead)
     assert_equal(repr(value_proxy), repr(the_date))
     assert_equal(str(value_proxy), str(the_date))
     assert_equal(hash(value_proxy), hash(the_date))
     assert bool(value_proxy)
コード例 #22
0
    def test_nested(self):
        with testing.MockConfiguration(a='one', b='two'):
            with testing.PatchConfiguration(a='three'):
                assert_equal(staticconf.get('a'), 'three')
                assert_equal(staticconf.get('b'), 'two')

            assert_equal(staticconf.get('a'), 'one')
            assert_equal(staticconf.get('b'), 'two')
コード例 #23
0
 def test_load_and_validate_namespace(self):
     real_config = {'SomeClass.min': 20, 'SomeClass.max': 25}
     staticconf.DictConfiguration(self.config, namespace=SomeClass.namespace)
     staticconf.DictConfiguration(real_config, namespace=SomeClass.alt_name)
     some_class = SomeClass()
     assert_equal(some_class.max, 100)
     assert_equal(some_class.min, 0)
     assert_equal(some_class.real_min, 20)
     assert_equal(some_class.real_max, 25)
コード例 #24
0
 def test_validate_keys_unknown_log_keys_only(self):
     with mock.patch('staticconf.config.log') as mock_log:
         self.namespace.validate_keys(
             self.config_data,
             False,
             log_keys_only=True,
         )
         assert_equal(len(mock_log.info.mock_calls), 1)
         log_msg = mock_log.info.call_args[0][0]
         unknown = config.remove_by_keys(
             self.config_data,
             self.namespace.get_known_keys(),
         )
         for k, v in unknown:
             # Have to cast to strings here, since log_msg is a string
             key_string, val_string = str(k), str(v)
             assert key_string in log_msg
             assert val_string not in log_msg
コード例 #25
0
 def test_load(self):
     config_data = loader.ObjectConfiguration(StubObject)
     assert_equal(config_data['year'], 2012)
     assert_equal(config_data['month'], 3)
     assert_equal(config_data['hour'], 15)
     assert_not_in('_private', config_data)
     assert_not_in('__really_private', config_data)
コード例 #26
0
 def test_proxy_list(self):
     the_list = range(3)
     validator = mock.Mock(return_value=the_list)
     value_proxy = proxy.ValueProxy(validator, self.value_cache, 'the_list')
     assert_equal(value_proxy, the_list)
     assert_in(2, value_proxy)
     assert_equal(value_proxy[:1], range(0, 1))
     assert_equal(len(value_proxy), 3)
コード例 #27
0
 def test_build_value_type(self):
     help_text = 'what?'
     config_key = 'one'
     float_type = schema.build_value_type(validation.validate_float)
     assert callable(float_type)
     value_def = float_type(default=5, config_key=config_key, help=help_text)
     assert_equal(value_def.default, 5)
     assert_equal(value_def.help, help_text)
     assert_equal(value_def.config_key, config_key)
コード例 #28
0
 def test_build_attributes(self, meta_schema):
     meta, _, mock_getters = meta_schema
     value_def = mock.create_autospec(schema.ValueTypeDefinition)
     attributes = {
         'not_a_token': None,
         'a_token': value_def
     }
     namespace = mock.create_autospec(config.ConfigNamespace)
     attributes = meta.build_attributes(attributes, namespace)
     assert_equal(attributes['not_a_token'], None)
     assert_equal(list(attributes['_tokens'].keys()), ['a_token'])
     token = attributes['_tokens']['a_token']
     assert_equal(token.config_key, value_def.config_key)
     assert_equal(token.default, value_def.default)
     assert isinstance(attributes['a_token'], property)
     mock_getters.register_value_proxy.assert_called_with(
         namespace, token, value_def.help)
コード例 #29
0
 def test_get_config_dict(self):
     self.namespace['one.two.three.four'] = 5
     self.namespace['one.two.three.five'] = 'six'
     self.namespace['one.b.cats'] = [1, 2, 3]
     self.namespace['a.two'] = 'c'
     self.namespace['first'] = True
     d = self.namespace.get_config_dict()
     assert_equal(d, {
         'one': {
             'b': {
                 'cats': [1, 2, 3],
             },
             'two': {
                 'three': {
                     'four': 5,
                     'five': 'six',
                 },
             },
         },
         'a': {
             'two': 'c',
         },
         'first': True,
     })
コード例 #30
0
    def test_reload_end_to_end(self):
        loader = mock.Mock()
        facade = staticconf.ConfigFacade.load(
                self.file.name,
                self.namespace,
                loader)

        assert_equal(loader.call_count, 1)
        time.sleep(1)

        facade.reload_if_changed()
        assert_equal(loader.call_count, 1)
        os.utime(self.file.name, None)
        facade.reload_if_changed()
        assert_equal(loader.call_count, 2)
コード例 #31
0
 def test_build_list_of_type_empty_list(self):
     validator = validation.build_list_type_validator(
         validation.validate_string)
     assert_equal(validator([]), [])