From 9dd183837bfe0cb259e0a8ff304bc913c2bd1764 Mon Sep 17 00:00:00 2001 From: brainbot-devops Date: Fri, 6 Sep 2019 10:06:45 +0200 Subject: [PATCH] Fix broken Tests. --- scenario_player/scenario.py | 17 --------- scenario_player/utils/token.py | 10 ++++-- .../utils/configuration/test_settings.py | 6 ---- tests/unittests/utils/test_token.py | 35 ++++++++++++------- 4 files changed, 30 insertions(+), 38 deletions(-) diff --git a/scenario_player/scenario.py b/scenario_player/scenario.py index df74af1b6..8c92d5342 100644 --- a/scenario_player/scenario.py +++ b/scenario_player/scenario.py @@ -6,7 +6,6 @@ import yaml from scenario_player.constants import GAS_LIMIT_FOR_TOKEN_CONTRACT_CALL -from scenario_player.exceptions.config import InsufficientMintingAmount from scenario_player.utils.configuration import ( NodesConfig, ScenarioConfig, @@ -34,7 +33,6 @@ def __init__(self, yaml_path: pathlib.Path, data_path: pathlib.Path) -> None: self.scenario = ScenarioConfig(self._loaded) self.token = TokenConfig(self._loaded, data_path.joinpath("token.info")) self.spaas = SPaaSConfig(self._loaded) - self.validate() self.gas_limit = GAS_LIMIT_FOR_TOKEN_CONTRACT_CALL * 2 @@ -42,18 +40,3 @@ def __init__(self, yaml_path: pathlib.Path, data_path: pathlib.Path) -> None: def name(self) -> str: """Return the name of the scenario file, sans extension.""" return self.path.stem - - def validate(self): - """Validate cross-config section requirements of the scenario. - - :raises InsufficientMintingAmount: - If token.min_balance < settings.services.udc.token.max_funding - """ - - # FIXME: This check seems to make no sense. The scenario token should be independent of the - # UDC token @nlsdfnbch - # Check that the amount of minted tokens is >= than the amount of deposited tokens - # try: - # assert self.token.min_balance >= self.settings.services.udc.token.max_funding - # except AssertionError: - # raise InsufficientMintingAmount diff --git a/scenario_player/utils/token.py b/scenario_player/utils/token.py index 2255833aa..474f9326a 100644 --- a/scenario_player/utils/token.py +++ b/scenario_player/utils/token.py @@ -66,7 +66,7 @@ def transact(self, action: str, parameters: dict) -> str: resp_data = resp.json() tx_hash = resp_data["tx_hash"] log.info(f"'{action}' call succeeded", tx_hash=tx_hash) - return decode_hex(tx_hash) + return decode_hex(tx_hash).decode() def mint( self, target_address, required_balance=None, max_fund_amount=None, **kwargs @@ -276,7 +276,10 @@ def use_existing(self) -> Tuple[str, int]: # Fetch the token's contract_info data. contract_info = self._local_contract_manager.get_contract(contract_name) - self.contract_data = {"token_contract": address, "name": contract_name} + self.contract_data = { + "token_contract": address, + "name": contract_info.get("name") or contract_name, + } self.contract_proxy = self._local_rpc_client.new_contract_proxy( contract_info["abi"], address ) @@ -325,7 +328,8 @@ def deploy_new(self) -> Tuple[str, int]: resp_data["contract"], resp_data["deployment_block"], ) - + print(token_contract_data) + print(deployment_block) contract_info = self._local_contract_manager.get_contract("CustomToken") # Make deployment address and block available to address/deployment_block properties. diff --git a/tests/unittests/utils/configuration/test_settings.py b/tests/unittests/utils/configuration/test_settings.py index a2e07fdbf..f85a90950 100644 --- a/tests/unittests/utils/configuration/test_settings.py +++ b/tests/unittests/utils/configuration/test_settings.py @@ -176,9 +176,3 @@ def test_balance_per_node_must_not_be_greater_than_max_funding(self, minimal_yam } with pytest.raises(UDCTokenConfigError): UDCTokenSettings(minimal_yaml_dict) - - def test_insufficient_minting(self, file_for_insufficient_minting_test): - with pytest.raises(InsufficientMintingAmount): - ScenarioYAML( - file_for_insufficient_minting_test, file_for_insufficient_minting_test.parent - ) diff --git a/tests/unittests/utils/test_token.py b/tests/unittests/utils/test_token.py index d4e8ebade..0a54a8ce1 100644 --- a/tests/unittests/utils/test_token.py +++ b/tests/unittests/utils/test_token.py @@ -1,9 +1,9 @@ import json -import pathlib + from unittest.mock import MagicMock, PropertyMock, patch import pytest -from eth_utils.address import to_checksum_address +from eth_utils.address import to_checksum_address, to_hex from requests.exceptions import ( ConnectionError, ConnectTimeout, @@ -24,7 +24,6 @@ TokenSourceCodeDoesNotExist, ) from scenario_player.scenario import ScenarioYAML -from scenario_player.utils.configuration.spaas import SPaaSConfig from scenario_player.utils.configuration.token import TokenConfig from scenario_player.utils.token import Contract, Token, UserDepositContract @@ -32,10 +31,22 @@ token_config_import_path = "scenario_player.utils.configuration.token" +# TODO: These tests mock and use dummy values even though it isn't necessary; +# For example, using "my_address" instead of a proper hex address. + + class Sentinel(Exception): """Raised when it's desired to exit a method under test early.""" +@pytest.fixture +def hex_address(): + return "0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c" + +@pytest.fixture +def contract_addr(): + return "0x5A0b54D5dc17e0AadC383d2db43B0a0D3E029c4c" + @pytest.fixture def runner(dummy_scenario_runner, minimal_yaml_dict, token_info_path, tmp_path): token_config = TokenConfig(minimal_yaml_dict, token_info_path) @@ -57,8 +68,8 @@ def token_instance(runner, tmp_path): @pytest.fixture -def contract_instance(runner, tmp_path): - return Contract(runner, "my_address") +def contract_instance(runner, tmp_path, contract_addr): + return Contract(runner, contract_addr) class TestContract: @@ -111,9 +122,9 @@ def setup_instance_with_balance(instance, current_balance): return instance @patch(f"{token_import_path}.ServiceInterface.request") - def test_mint_is_a_no_op_if_balance_is_sufficient(self, mock_request, contract_instance): + def test_mint_is_a_no_op_if_balance_is_sufficient(self, mock_request, contract_instance, hex_address): contract_instance = self.setup_instance_with_balance(contract_instance, 100000) - assert contract_instance.mint("the_address") is None + assert contract_instance.mint(hex_address) is None assert mock_request.called is False @patch(f"{token_import_path}.ServiceInterface.post", side_effect=Sentinel) @@ -374,7 +385,7 @@ class MockContractProxy: name = "my_deployed_token_name" symbol = "token_symbol" - token_instance._local_contract_manager.get_contract.return_value = MockContractProxy + token_instance._local_contract_manager.get_contract.return_value = {"abi": "contract_abi", "name": "my_deployed_token_name"} expected_deployment_receipt = {"blockNum": loaded_token_info["block"]} expected_contract_data = { @@ -455,7 +466,7 @@ def json(self): def test_deploy_new_calls_save_token_depending_on_reuse_token_property( self, _, mock_save_token, mock_request, reuse_token, token_instance ): - json_resp = {"contract": {}, "deployment_block": 1} + json_resp = {"contract": {"address": None}, "deployment_block": 1} class MockResp: def json(self): @@ -519,10 +530,10 @@ def test_update_allowance_updates_allowance_according_to_udc_token_balance_per_n self.mock_ud_token_address.return_value = "ud_token_addr" # Expect an allowance transact request to be invoked, with its amount equal to: - # (balance_per_node * node_count) - current_allowance + # balance_per_node * node_count # Targeting the UD Contract address, calling from the UD Token address. expected_params = { - "amount": 25_000, + "amount": 30_000, "target_address": "ud_contract_addr", "contract_address": "ud_token_addr", } @@ -554,7 +565,7 @@ def test_deposit_method_issues_deposit_request_if_node_funding_is_insufficient( mock_transact.assert_called_once_with("deposit", expected_params) @patch("scenario_player.utils.token.Contract.transact") - def test_deposit_methpd_is_noop_if_node_funding_is_sufficient(self, mock_transact): + def test_deposit_method_is_noop_if_node_funding_is_sufficient(self, mock_transact): self.instance.config.settings.services.udc.token.dict["balance_per_node"] = 5_000 self.mock_effective_balance.return_value = 10_000