From c94a51597b461440d8ffdcdaac3a024e73238157 Mon Sep 17 00:00:00 2001 From: Hsiao-Wei Wang Date: Mon, 13 May 2019 12:40:40 +0800 Subject: [PATCH] PR feedback, thanks to CC --- .../state-fixtures/test_minimal_state.py | 79 ++++++++++++------- 1 file changed, 52 insertions(+), 27 deletions(-) diff --git a/tests/eth2/beacon/state-fixtures/test_minimal_state.py b/tests/eth2/beacon/state-fixtures/test_minimal_state.py index e9d8bb7cdd..1f6ee45c85 100644 --- a/tests/eth2/beacon/state-fixtures/test_minimal_state.py +++ b/tests/eth2/beacon/state-fixtures/test_minimal_state.py @@ -1,13 +1,21 @@ -import os +from pathlib import Path import yaml +try: + from yaml import CLoader as Loader +except ImportError: + from yaml import Loader import pytest -from py_ecc import bls +from eth_utils import ( + to_tuple, +) +from py_ecc import bls # noqa: F401 from ssz.tools import ( from_formatted_dict, to_formatted_dict, ) + from eth2.configs import ( Eth2Config, ) @@ -22,21 +30,19 @@ ) # Test files -ROOT_PROJECT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) +ROOT_PROJECT_DIR = Path(__file__).cwd() -BASE_FIXTURE_PATH = os.path.join(ROOT_PROJECT_DIR, '../../', 'eth2-fixtures', 'state') +BASE_FIXTURE_PATH = ROOT_PROJECT_DIR / 'eth2-fixtures' / 'state' -FILE_NAMES = [ +FIXTURE_FILE_NAMES = [ "sanity-check_small-config_32-vals.yaml", - "sanity-check_default-config_100-vals.yaml", + # "sanity-check_default-config_100-vals.yaml", ] + # # Mock bls verification for these tests # -bls = bls - - def mock_bls_verify(message_hash, pubkey, signature, domain): return True @@ -50,51 +56,72 @@ def mock_bls_verify_multiple(pubkeys, @pytest.fixture(autouse=True) def mock_bls(mocker, request): - if 'noautofixt' in request.keywords: + if 'noautofixture' in request.keywords: return mocker.patch('py_ecc.bls.verify', side_effect=mock_bls_verify) mocker.patch('py_ecc.bls.verify_multiple', side_effect=mock_bls_verify_multiple) -def get_test_case(file_names): - all_test_cases = [] +# +# Helpers for generating test suite +# +def get_all_test_cases(file_names): + test_cases = [] for file_name in file_names: - with open(BASE_FIXTURE_PATH + '/' + file_name, 'U') as f: + file_to_open = BASE_FIXTURE_PATH / file_name + with open(file_to_open, 'U') as f: # TODO: `proof_of_possession` is used in v0.5.1 spec and will be renamed to `signature` # Trinity renamed it ahead due to py-ssz signing_root requirements new_text = f.read().replace('proof_of_possession', 'signature') try: - data = yaml.load(new_text) - all_test_cases += data['test_cases'] + data = yaml.load(new_text, Loader=Loader) + test_cases += data['test_cases'] except yaml.YAMLError as exc: print(exc) - return all_test_cases + return test_cases -test_cases = get_test_case(FILE_NAMES) +def state_fixture_mark_fn(fixture_name): + if fixture_name == 'test_transfer': + return pytest.mark.skip(reason="has not implemented") + else: + return None -@pytest.mark.parametrize("test_case", test_cases) +@to_tuple +def get_test_cases(fixture_file_names): + test_cases = get_all_test_cases(fixture_file_names) + for test_case in test_cases: + test_name = test_case['name'] + mark = state_fixture_mark_fn(test_name) + if mark is not None: + yield pytest.param(test_case, id=test_name, marks=(mark,)) + else: + yield pytest.param(test_case, id=test_name) + + +all_test_cases = get_test_cases(FIXTURE_FILE_NAMES) + + +@pytest.mark.parametrize( + "test_case", + all_test_cases +) def test_state(base_db, test_case): - test_name = test_case['name'] - if test_name == 'test_transfer': - print('skip') - else: - execute_state_transtion(test_case, base_db) + execute_state_transtion(test_case, base_db) def generate_config_by_dict(dict_config): dict_config['DEPOSIT_CONTRACT_ADDRESS'] = b'\x00' * 20 for key in list(dict_config): - if 'DOMAIN_' in key in key: + if 'DOMAIN_' in key: # DOMAIN is defined in SignatureDomain dict_config.pop(key, None) return Eth2Config(**dict_config) def execute_state_transtion(test_case, base_db): - test_name = test_case['name'] dict_config = test_case['config'] verify_signatures = test_case['verify_signatures'] dict_initial_state = test_case['initial_state'] @@ -104,8 +131,6 @@ def execute_state_transtion(test_case, base_db): # TODO: make it case by case assert verify_signatures is False - print(f"[{test_name}]") - # Set config config = generate_config_by_dict(dict_config)