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)
Beispiel #2
0
    def test_reload_end_to_end(self):
        loader = mock.Mock()
        callback = 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)
 def setup_configuration_values(self):
     validator = mock.Mock(return_value=2)
     self.name = 'test_namespace'
     self.namespace = config.get_namespace(self.name)
     self.config_key = 'something'
     self.value_proxy = proxy.ValueProxy(validator, self.namespace,
                                         self.config_key)
 def test_reload_custom(self):
     reloader = mock.Mock()
     watcher = config.ConfigurationWatcher(self.loader,
                                           self.filename,
                                           reloader=reloader)
     watcher.reload()
     reloader.assert_called_with()
 def test_get_value_fails_validation(self):
     expected = [self.name, self.config_key]
     validator = mock.Mock(side_effect=validation.ValidationError)
     _ = proxy.ValueProxy(  # flake8: noqa
         validator, self.namespace, 'something.broken')
     assert_raises_and_contains(errors.ConfigurationError, expected,
                                lambda: self.value_proxy.value)
    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)
    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)
Beispiel #8
0
    def test_load_passes_comparators_to_configuration_watcher(self):
        filename, namespace = "filename", "namespace"
        loader = mock.Mock()
        comparator = mock.Mock(name='MockComparator')

        with mock.patch('staticconf.config.ConfigurationWatcher',
                        autospec=True) as mock_watcher_class:
            config.ConfigFacade.load(filename,
                                     namespace,
                                     loader,
                                     comparators=[comparator])
            mock_watcher_class.assert_called_with(mock.ANY,
                                                  filename,
                                                  min_interval=mock.ANY,
                                                  reloader=mock.ANY,
                                                  comparators=[comparator])
 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()
 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)
 def test_validate_keys_no_unknown_keys(self):
     proxies = [mock.Mock(config_key=i) for i in self.config_data]
     for mock_proxy in proxies:
         self.namespace.register_proxy(mock_proxy)
     with mock.patch('staticconf.config.log') as mock_log:
         self.namespace.validate_keys(self.config_data, True)
         self.namespace.validate_keys(self.config_data, False)
         assert not mock_log.warn.mock_calls
    def test_build_loader_optional(self):
        err_msg = "Failed to do"
        loader_func = mock.Mock()
        loader_func.side_effect = ValueError(err_msg)
        config_loader = loader.build_loader(loader_func)

        config_loader(optional=True)
        assert_raises(ValueError, config_loader)
 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)
Beispiel #14
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
Beispiel #15
0
 def setup_factory(self):
     self.factory = getters.ProxyFactory()
     self.validator = mock.Mock()
     self.namespace = mock.create_autospec(config.ConfigNamespace)
     self.config_key = 'some_key'
     self.default = 'bad_default'
     self.help = 'some help message no one reads'
     self.args = (self.validator, self.namespace, self.config_key,
                  self.default, self.help)
Beispiel #16
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)
Beispiel #17
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)
    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()
    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())
 def setup_mocks_and_config_watcher(self):
     self.loader = mock.Mock()
     with mock.patch('staticconf.config.time') as self.mock_time:
         with mock.patch('staticconf.config.os.stat') as self.mock_stat:
             with tempfile.NamedTemporaryFile() as file:
                 with mock.patch('staticconf.config.os.path') as self.mock_path:
                     file.flush()
                     self.mock_stat.return_value.st_ino = 1
                     self.mock_stat.return_value.st_dev = 2
                     self.filename = file.name
                     self.watcher = config.ConfigurationWatcher(
                             self.loader, self.filename)
                     yield
 def test_proxy_with_string(self):
     validator = mock.Mock(return_value='one%s')
     value_proxy = proxy.ValueProxy(validator, self.value_cache,
                                    'something')
     assert_equal(value_proxy, 'one%s')
     assert value_proxy < 'two'
     assert value_proxy > 'ab'
     assert_equal(value_proxy + '!', 'one%s!')
     assert_equal(value_proxy % '!', 'one!')
     assert_equal(repr(value_proxy), "'one%s'")
     assert_equal(str(value_proxy), "one%s")
     assert_equal(hash(value_proxy), hash("one%s"))
     assert bool(value_proxy)
Beispiel #22
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)
Beispiel #23
0
 def test_proxy(self):
     validator = mock.Mock(return_value=2)
     value_proxy = proxy.ValueProxy(validator, self.value_cache, 'something')
     assert_equal(value_proxy, 2)
     assert value_proxy < 4
     assert value_proxy > 1
     assert_equal(value_proxy + 5, 7)
     assert_equal(repr(value_proxy), "2")
     assert_equal(str(value_proxy), "2")
     assert_equal(3 % value_proxy, 1)
     assert_equal(3 ** value_proxy, 9)
     assert_equal(value_proxy ** 3, 8)
     assert_equal(hash(value_proxy), 2)
     assert_equal(abs(value_proxy), 2)
     assert_equal(hex(value_proxy), "0x2")
     assert bool(value_proxy)
     assert_equal(range(5)[value_proxy], 2)
     assert_equal(range(5)[:value_proxy], range(2))
 def test_register_get_value_proxies(self):
     proxies = [mock.Mock(), mock.Mock()]
     for mock_proxy in proxies:
         self.namespace.register_proxy(mock_proxy)
     assert_equal(self.namespace.get_value_proxies(), proxies)
 def test_build_loader(self):
     loader_func = mock.Mock()
     assert callable(loader.build_loader(loader_func))
 def setup_callback_chain(self):
     self.callbacks = list(enumerate([mock.Mock(), mock.Mock()]))
     self.callback_chain = config.ReloadCallbackChain(
         callbacks=self.callbacks)
 def test_add_remove(self):
     callback = mock.Mock()
     self.callback_chain.add('one', callback)
     assert_equal(self.callback_chain.callbacks['one'], callback)
     self.callback_chain.remove('one')
     assert 'one' not in self.callback_chain.callbacks
 def test_proxied_attributes(self):
     validator = mock.Mock(return_value=self.value_cache['the_date'])
     value_proxy = proxy.ValueProxy(validator, self.value_cache, 'the_date')
     assert_equal(value_proxy.date(), datetime.date(2012, 3, 14))
     assert_equal(value_proxy.hour, 4)
 def test_get_value(self):
     expected = "the stars"
     validator = mock.Mock(return_value=expected)
     value_proxy = proxy.ValueProxy(validator, self.value_cache,
                                    'something.string')
     assert_equal(value_proxy, expected)
 def test_add_callback(self):
     name, func = 'name', mock.Mock()
     self.facade.add_callback(name, func)
     self.facade.callback_chain.add.assert_called_with(name, func)