From afc700b111d96bd1aac2a30afbfd90958ed05862 Mon Sep 17 00:00:00 2001 From: NotPeopling2day <32708219+NotPeopling2day@users.noreply.github.com> Date: Tue, 2 Aug 2022 23:04:24 +0200 Subject: [PATCH] test: update testing structure --- .pre-commit-config.yaml | 4 +- CONTRIBUTING.md | 4 +- setup.py | 6 +-- tests/conftest.py | 92 +++++++++++++++++++++++++++++++++++++++ tests/test_providers.py | 95 +---------------------------------------- 5 files changed, 100 insertions(+), 101 deletions(-) create mode 100644 tests/conftest.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3daf1a5..15cf210 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,7 +10,7 @@ repos: - id: isort - repo: https://github.com/psf/black - rev: 22.3.0 + rev: 22.6.0 hooks: - id: black name: black @@ -21,7 +21,7 @@ repos: - id: flake8 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.961 + rev: v0.971 hooks: - id: mypy additional_dependencies: [types-PyYAML, types-requests] diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 81166eb..566d428 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,11 +11,11 @@ cd ape-alchemy python3 -m venv venv source venv/bin/activate -# install brownie into the virtual environment +# install ape-alchemy into the virtual environment python setup.py install # install the developer dependencies (-e is interactive mode) -pip install -e .[dev] +pip install -e .'[dev]' ``` ## Pre-Commit Hooks diff --git a/setup.py b/setup.py index 27d0197..ecc95df 100644 --- a/setup.py +++ b/setup.py @@ -4,15 +4,15 @@ extras_require = { "test": [ # `test` GitHub Action jobs uses this - "pytest>=6.0,<7.0", # Core testing package + "pytest>=6.0", # Core testing package "pytest-xdist", # multi-process runner "pytest-cov", # Coverage analyzer plugin "pytest-mock", # For creating mocks "hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer ], "lint": [ - "black>=22.3.0,<23.0", # auto-formatter and linter - "mypy>=0.961,<1.0", # Static type analyzer + "black>=22.6.0,<23.0", # auto-formatter and linter + "mypy>=0.971,<1.0", # Static type analyzer "types-requests", # NOTE: Needed due to mypy typeshed "flake8>=4.0.1,<5.0", # Style linter "isort>=5.10.1,<6.0", # Import sorting linter diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..6507367 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,92 @@ +from pathlib import Path + +import pytest +from ape.api import EcosystemAPI, NetworkAPI, TransactionAPI +from ape.api.config import PluginConfig +from requests import HTTPError, Response +from web3 import Web3 + +from ape_alchemy.providers import AlchemyEthereumProvider + +FEATURE_NOT_AVAILABLE_BECAUSE_OF_TIER_RESPONSE = ( + "trace_transaction is not available on the Free tier - " + "upgrade to Growth or Enterprise for access. See available methods at " + "https://docs.alchemy.com/alchemy/documentation/apis" +) +FEATURE_NOT_AVAILABLE_BECAUSE_OF_NETWORK_RESPONSE = ( + "trace_transaction is not available on the ETH_RINKEBY. " + "For more information see our docs: " + "https://docs.alchemy.com/alchemy/documentation/apis/ethereum" +) + + +@pytest.fixture +def missing_token(mocker): + mock = mocker.patch("os.environ.get") + mock.return_value = None + return mock + + +@pytest.fixture +def token(mocker): + mock = mocker.patch("os.environ.get") + mock.return_value = "TEST_TOKEN" + return mock + + +@pytest.fixture +def mock_network(mocker): + mock = mocker.MagicMock(spec=NetworkAPI) + mock.name = "MOCK_NETWORK" + mock.ecosystem = mocker.MagicMock(spec=EcosystemAPI) + mock.ecosystem.name = "ethereum" + return mock + + +@pytest.fixture +def mock_config(mocker): + return mocker.MagicMock(spec=PluginConfig) + + +@pytest.fixture +def mock_web3(mocker): + mock = mocker.MagicMock(spec=Web3) + mock.eth = mocker.MagicMock() + mock.manager = mocker.MagicMock() + return mock + + +@pytest.fixture +def mock_transaction(mocker): + return mocker.MagicMock(spec=TransactionAPI) + + +@pytest.fixture +def txn_hash(): + return "0x55d07ce5e3f4f5742f3318cf328d700e43ee8cdb46000f2ac731a9379fca8ea7" + + +@pytest.fixture( + params=( + FEATURE_NOT_AVAILABLE_BECAUSE_OF_TIER_RESPONSE, + FEATURE_NOT_AVAILABLE_BECAUSE_OF_NETWORK_RESPONSE, + ) +) +def feature_not_available_http_error(mocker, request): + response = mocker.MagicMock(spec=Response) + response.fixture_param = request.param # For assertions + response.json.return_value = {"error": {"message": request.param}} + error = HTTPError(response=response) + return error + + +@pytest.fixture +def alchemy_provider(mock_network, mock_config) -> AlchemyEthereumProvider: + return AlchemyEthereumProvider( + name="alchemy", + network=mock_network, + config=mock_config, + request_header={}, + data_folder=Path("."), + provider_settings={}, + ) diff --git a/tests/test_providers.py b/tests/test_providers.py index 134934a..752faab 100644 --- a/tests/test_providers.py +++ b/tests/test_providers.py @@ -1,101 +1,8 @@ -from pathlib import Path - import pytest -from ape.api import EcosystemAPI, NetworkAPI, TransactionAPI -from ape.api.config import PluginConfig from ape.exceptions import ContractLogicError -from requests import HTTPError, Response -from web3 import Web3 from web3.exceptions import ContractLogicError as Web3ContractLogicError -from ape_alchemy.providers import ( - AlchemyEthereumProvider, - AlchemyFeatureNotAvailable, - MissingProjectKeyError, -) - -FEATURE_NOT_AVAILABLE_BECAUSE_OF_TIER_RESPONSE = ( - "trace_transaction is not available on the Free tier - " - "upgrade to Growth or Enterprise for access. See available methods at " - "https://docs.alchemy.com/alchemy/documentation/apis" -) -FEATURE_NOT_AVAILABLE_BECAUSE_OF_NETWORK_RESPONSE = ( - "trace_transaction is not available on the ETH_RINKEBY. " - "For more information see our docs: " - "https://docs.alchemy.com/alchemy/documentation/apis/ethereum" -) - - -@pytest.fixture -def missing_token(mocker): - mock = mocker.patch("os.environ.get") - mock.return_value = None - return mock - - -@pytest.fixture -def token(mocker): - mock = mocker.patch("os.environ.get") - mock.return_value = "TEST_TOKEN" - return mock - - -@pytest.fixture -def mock_network(mocker): - mock = mocker.MagicMock(spec=NetworkAPI) - mock.name = "MOCK_NETWORK" - mock.ecosystem = mocker.MagicMock(spec=EcosystemAPI) - mock.ecosystem.name = "ethereum" - return mock - - -@pytest.fixture -def mock_config(mocker): - return mocker.MagicMock(spec=PluginConfig) - - -@pytest.fixture -def mock_web3(mocker): - mock = mocker.MagicMock(spec=Web3) - mock.eth = mocker.MagicMock() - mock.manager = mocker.MagicMock() - return mock - - -@pytest.fixture -def mock_transaction(mocker): - return mocker.MagicMock(spec=TransactionAPI) - - -@pytest.fixture -def txn_hash(): - return "0x55d07ce5e3f4f5742f3318cf328d700e43ee8cdb46000f2ac731a9379fca8ea7" - - -@pytest.fixture( - params=( - FEATURE_NOT_AVAILABLE_BECAUSE_OF_TIER_RESPONSE, - FEATURE_NOT_AVAILABLE_BECAUSE_OF_NETWORK_RESPONSE, - ) -) -def feature_not_available_http_error(mocker, request): - response = mocker.MagicMock(spec=Response) - response.fixture_param = request.param # For assertions - response.json.return_value = {"error": {"message": request.param}} - error = HTTPError(response=response) - return error - - -@pytest.fixture -def alchemy_provider(mock_network, mock_config) -> AlchemyEthereumProvider: - return AlchemyEthereumProvider( - name="alchemy", - network=mock_network, - config=mock_config, - request_header={}, - data_folder=Path("."), - provider_settings={}, - ) +from ape_alchemy.providers import AlchemyFeatureNotAvailable, MissingProjectKeyError class TestAlchemyEthereumProvider: