From e7d89abfcec81a928f87d37405b2db30d7d460ec Mon Sep 17 00:00:00 2001 From: z-bowen Date: Wed, 22 Aug 2018 18:07:14 +0200 Subject: [PATCH] Problem: Needed some of the fixtures from `tests/upsert_validator/conftest.py` for tests in another directory Solution: Moved the fixtures to `tests/conftest.py` --- tests/conftest.py | 60 ++++++++++++++++++++++++++++-- tests/upsert_validator/conftest.py | 39 ------------------- 2 files changed, 57 insertions(+), 42 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 59bfbe21..fa5e5925 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -20,6 +20,7 @@ from logging.config import dictConfig import pytest from pymongo import MongoClient +from bigchaindb import ValidatorElection from bigchaindb.common import crypto from bigchaindb.log import setup_logging from bigchaindb.tendermint_utils import key_from_base64 @@ -710,7 +711,9 @@ def priv_validator_path(node_keys): def validators(b, node_keys): from bigchaindb.backend import query - original_validators = b.get_validators() + height = get_block_height(b) + + original_validators = b.get_validators(height=height) (public_key, private_key) = list(node_keys.items())[0] @@ -721,13 +724,64 @@ def validators(b, node_keys): 'voting_power': 10}] validator_update = {'validators': validator_set, - 'height': b.get_latest_block()['height'] + 1} + 'height': height + 1} query.store_validator_set(b.connection, validator_update) yield + height = get_block_height(b) + validator_update = {'validators': original_validators, - 'height': b.get_latest_block()['height']} + 'height': height} query.store_validator_set(b.connection, validator_update) + + +def get_block_height(b): + + if b.get_latest_block(): + height = b.get_latest_block()['height'] + else: + height = 0 + + return height + + +@pytest.fixture +def b_mock(b, network_validators): + b.get_validators = mock_get_validators(network_validators) + + return b + + +def mock_get_validators(network_validators): + def validator_set(height): + validators = [] + for public_key, power in network_validators.items(): + validators.append({ + 'pub_key': {'type': 'AC26791624DE60', 'data': public_key}, + 'voting_power': power + }) + return validators + + return validator_set + + +@pytest.fixture +def valid_election(b_mock, node_key, new_validator): + voters = ValidatorElection.recipients(b_mock) + return ValidatorElection.generate([node_key.public_key], + voters, + new_validator, None).sign([node_key.private_key]) + + +@pytest.fixture +def new_validator(): + public_key = '1718D2DBFF00158A0852A17A01C78F4DCF3BA8E4FB7B8586807FAC182A535034' + power = 1 + node_id = 'fake_node_id' + + return {'public_key': public_key, + 'power': power, + 'node_id': node_id} diff --git a/tests/upsert_validator/conftest.py b/tests/upsert_validator/conftest.py index 64bf2279..364c3ac1 100644 --- a/tests/upsert_validator/conftest.py +++ b/tests/upsert_validator/conftest.py @@ -7,45 +7,6 @@ import pytest from bigchaindb.upsert_validator import ValidatorElection -@pytest.fixture -def b_mock(b, network_validators): - b.get_validators = mock_get_validators(network_validators) - - return b - - -@pytest.fixture -def new_validator(): - public_key = '1718D2DBFF00158A0852A17A01C78F4DCF3BA8E4FB7B8586807FAC182A535034' - power = 1 - node_id = 'fake_node_id' - - return {'public_key': public_key, - 'power': power, - 'node_id': node_id} - - -def mock_get_validators(network_validators): - def validator_set(height): - validators = [] - for public_key, power in network_validators.items(): - validators.append({ - 'pub_key': {'type': 'AC26791624DE60', 'data': public_key}, - 'voting_power': power - }) - return validators - - return validator_set - - -@pytest.fixture -def valid_election(b_mock, node_key, new_validator): - voters = ValidatorElection.recipients(b_mock) - return ValidatorElection.generate([node_key.public_key], - voters, - new_validator, None).sign([node_key.private_key]) - - @pytest.fixture def valid_election_b(b, node_key, new_validator): voters = ValidatorElection.recipients(b)