Skip to content

Commit

Permalink
Fix broken Tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
brainbot-devops committed Sep 6, 2019
1 parent 7041b21 commit 9dd1838
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 38 deletions.
17 changes: 0 additions & 17 deletions scenario_player/scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -34,26 +33,10 @@ 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

@property
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
10 changes: 7 additions & 3 deletions scenario_player/utils/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 0 additions & 6 deletions tests/unittests/utils/configuration/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
35 changes: 23 additions & 12 deletions tests/unittests/utils/test_token.py
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -24,18 +24,29 @@
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

token_import_path = "scenario_player.utils.token"
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)
Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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",
}
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 9dd1838

Please sign in to comment.