bigchaindb/tests/utils/test_config_utils.py

139 lines
4.0 KiB
Python
Raw Normal View History

2016-02-10 19:55:33 +01:00
import copy
import pytest
import bigchaindb
2016-02-22 23:46:32 +01:00
from bigchaindb import exceptions
2016-02-10 19:55:33 +01:00
ORIGINAL_CONFIG = copy.deepcopy(bigchaindb._config)
2016-02-10 19:55:33 +01:00
@pytest.fixture(scope='function', autouse=True)
def clean_config(monkeypatch):
monkeypatch.setattr('bigchaindb.config', copy.deepcopy(ORIGINAL_CONFIG))
2016-02-10 19:55:33 +01:00
def test_bigchain_instance_is_initialized_when_conf_provided():
2016-02-15 11:25:56 +01:00
from bigchaindb import config_utils
2016-02-10 19:55:33 +01:00
assert 'CONFIGURED' not in bigchaindb.config
config_utils.dict_config({'keypair': {'public': 'a', 'private': 'b'}})
assert bigchaindb.config['CONFIGURED'] is True
2016-02-10 19:55:33 +01:00
b = bigchaindb.Bigchain()
assert b.me
assert b.me_private
def test_bigchain_instance_raises_when_not_configured(monkeypatch):
2016-02-15 11:25:56 +01:00
from bigchaindb import config_utils
2016-02-10 19:55:33 +01:00
assert 'CONFIGURED' not in bigchaindb.config
# We need to disable ``bigchaindb.config_utils.autoconfigure`` to avoid reading
# from existing configurations
monkeypatch.setattr(config_utils, 'autoconfigure', lambda: 0)
2016-02-22 23:46:32 +01:00
with pytest.raises(exceptions.KeypairNotFoundException):
2016-02-10 19:55:33 +01:00
bigchaindb.Bigchain()
2016-03-14 21:27:17 +01:00
def test_load_consensus_plugin_loads_default_rules_without_name():
from bigchaindb import config_utils
from bigchaindb.consensus import BaseConsensusRules
assert config_utils.load_consensus_plugin() == BaseConsensusRules
def test_load_consensus_plugin_raises_with_unknown_name():
from pkg_resources import ResolutionError
from bigchaindb import config_utils
with pytest.raises(ResolutionError):
config_utils.load_consensus_plugin('bogus')
def test_load_consensus_plugin_raises_with_invalid_subclass(monkeypatch):
# Monkeypatch entry_point.load to return something other than a
# ConsensusRules instance
from bigchaindb import config_utils
monkeypatch.setattr(config_utils,
'iter_entry_points',
2016-03-24 01:41:00 +01:00
lambda *args: [type('entry_point', (object), {'load': lambda: object})])
2016-03-14 21:27:17 +01:00
with pytest.raises(TypeError):
config_utils.load_consensus_plugin()
2016-03-24 01:41:00 +01:00
def test_map_leafs_iterator():
from bigchaindb import config_utils
mapping = {
'a': {'b': {'c': 1},
'd': {'z': 44}},
'b': {'d': 2},
'c': 3
}
result = config_utils.map_leafs(lambda x, path: x * 2, mapping)
assert result == {
'a': {'b': {'c': 2},
'd': {'z': 88}},
'b': {'d': 4},
'c': 6
}
result = config_utils.map_leafs(lambda x, path: path, mapping)
assert result == {
'a': {'b': {'c': ['a', 'b', 'c']},
'd': {'z': ['a', 'd', 'z']}},
'b': {'d': ['b', 'd']},
'c': ['c']
}
def test_env_config(monkeypatch):
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_HOST': 'test-host',
'BIGCHAINDB_DATABASE_PORT': 'test-port'})
from bigchaindb import config_utils
result = config_utils.env_config({'database': {'host': None, 'port': None}})
expected = {'database': {'host': 'test-host', 'port': 'test-port'}}
assert result == expected
def test_autoconfigure_read_both_from_file_and_env(monkeypatch):
monkeypatch.setattr('bigchaindb.config_utils.file_config', lambda: {})
monkeypatch.setattr('os.environ', {'BIGCHAINDB_DATABASE_HOST': 'test-host',
'BIGCHAINDB_DATABASE_PORT': '4242'})
import bigchaindb
from bigchaindb import config_utils
config_utils.autoconfigure()
assert bigchaindb.config['database']['host'] == 'test-host'
assert bigchaindb.config['database']['port'] == 4242
assert bigchaindb.config == {
'CONFIGURED': True,
'database': {
'host': 'test-host',
'port': 4242,
'name': 'bigchain',
},
'keypair': {
'public': None,
'private': None,
},
'keyring': [],
'statsd': {
'host': 'localhost',
'port': 8125,
'rate': 0.01,
},
'api_endpoint': 'http://localhost:8008/api/v1',
'consensus_plugin': 'default',
}