From ad535c78951f42281e8fd10a1a0be06155930d0e Mon Sep 17 00:00:00 2001 From: Matt Smith Date: Mon, 14 Mar 2016 13:27:17 -0700 Subject: [PATCH] Tests for loading consensus plugins --- tests/test_core.py | 6 ++++++ tests/utils/test_config_utils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/tests/test_core.py b/tests/test_core.py index 9857b37f..f136f4e3 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -15,6 +15,7 @@ def config(request, monkeypatch): }, 'keyring': [], 'CONFIGURED': True, + 'consensus_plugin': 'default' } monkeypatch.setattr('bigchaindb.config', config) @@ -24,6 +25,7 @@ def config(request, monkeypatch): def test_bigchain_class_default_initialization(config): from bigchaindb.core import Bigchain + from bigchaindb.consensus import BaseConsensusRules bigchain = Bigchain() assert bigchain.host == config['database']['host'] assert bigchain.port == config['database']['port'] @@ -31,11 +33,13 @@ def test_bigchain_class_default_initialization(config): assert bigchain.me == config['keypair']['public'] assert bigchain.me_private == config['keypair']['private'] assert bigchain.federation_nodes == config['keyring'] + assert bigchain.consensus == BaseConsensusRules assert bigchain._conn is None def test_bigchain_class_initialization_with_parameters(config): from bigchaindb.core import Bigchain + from bigchaindb.consensus import BaseConsensusRules init_kwargs = { 'host': 'some_node', 'port': '12345', @@ -43,6 +47,7 @@ def test_bigchain_class_initialization_with_parameters(config): 'public_key': 'white', 'private_key': 'black', 'keyring': ['key_one', 'key_two'], + 'consensus_plugin': 'default' } bigchain = Bigchain(**init_kwargs) assert bigchain.host == init_kwargs['host'] @@ -51,4 +56,5 @@ def test_bigchain_class_initialization_with_parameters(config): assert bigchain.me == init_kwargs['public_key'] assert bigchain.me_private == init_kwargs['private_key'] assert bigchain.federation_nodes == init_kwargs['keyring'] + assert bigchain.consensus == BaseConsensusRules assert bigchain._conn is None diff --git a/tests/utils/test_config_utils.py b/tests/utils/test_config_utils.py index 2cf57242..fa63158d 100644 --- a/tests/utils/test_config_utils.py +++ b/tests/utils/test_config_utils.py @@ -37,3 +37,32 @@ def test_bigchain_instance_raises_when_not_configured(monkeypatch): with pytest.raises(exceptions.KeypairNotFoundException): bigchaindb.Bigchain() + + +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', + lambda *args: [ type('entry_point', + (object), + {'load': lambda: object}) ]) + + with pytest.raises(TypeError): + config_utils.load_consensus_plugin()