From 2f7a362c819dc15cee82cf425ac228d8d4c7292a Mon Sep 17 00:00:00 2001 From: Juliya Smith Date: Mon, 26 Aug 2024 14:49:58 -0500 Subject: [PATCH] test: add test --- ape_etherscan/types.py | 22 +++++++++++----------- tests/conftest.py | 4 +++- tests/test_query.py | 31 +++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 12 deletions(-) create mode 100644 tests/test_query.py diff --git a/ape_etherscan/types.py b/ape_etherscan/types.py index 84ce877..30c3587 100644 --- a/ape_etherscan/types.py +++ b/ape_etherscan/types.py @@ -21,18 +21,18 @@ class EtherscanInstance: class SourceCodeResponse(BaseModel): - abi: list = Field([], alias="ABI") - name: str = Field("unknown", alias="ContractName") - source_code: str = Field("", alias="SourceCode") - compiler_version: str = Field("", alias="CompilerVersion") - optimization_used: bool = Field(True, alias="OptimizationUsed") - optimization_runs: int = Field(200, alias="Runs") - evm_version: str = Field("Default", alias="EVMVersion") - library: str = Field("", alias="Library") - license_type: str = Field("", alias="LicenseType") + abi: list = Field(default=[], alias="ABI") + name: str = Field(default="unknown", alias="ContractName") + source_code: str = Field(default="", alias="SourceCode") + compiler_version: str = Field(default="", alias="CompilerVersion") + optimization_used: bool = Field(default=True, alias="OptimizationUsed") + optimization_runs: int = Field(default=200, alias="Runs") + evm_version: str = Field(default="Default", alias="EVMVersion") + library: str = Field(default="", alias="Library") + license_type: str = Field(default="", alias="LicenseType") proxy: bool = Field(False, alias="Proxy") - implementation: str = Field("", alias="Implementation") - swarm_source: str = Field("", alias="SwarmSource") + implementation: str = Field(default="", alias="Implementation") + swarm_source: str = Field(default="", alias="SwarmSource") @field_validator("optimization_used", "proxy", mode="before") @classmethod diff --git a/tests/conftest.py b/tests/conftest.py index bbc2e4c..64851bf 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -296,6 +296,8 @@ def add_handler( ): if isinstance(return_value, (str, dict)): return_value = self.get_mock_response(return_value) + elif isinstance(return_value, list): + return_value = self.get_mock_response({"result": return_value}) def handler(self, method, base_uri, params=None, data=None, headers=None): actual_params = params if method.lower() == "get" else data @@ -455,7 +457,7 @@ def get_mock_response( # Mock wasn't set. response_data = {**kwargs} - assert isinstance(response_data, dict) + assert isinstance(response_data, (list, dict)) return self._get_mock_response(response_data=response_data, **kwargs) def _get_mock_response( diff --git a/tests/test_query.py b/tests/test_query.py new file mode 100644 index 0000000..cf25533 --- /dev/null +++ b/tests/test_query.py @@ -0,0 +1,31 @@ +import pytest +from ape.api.query import ContractCreationQuery +from ape.utils import ManagerAccessMixin + + +@pytest.fixture +def query_engine(): + return ManagerAccessMixin.query_manager.engines["etherscan"] + + +def test_contract_creation_receipt(query_engine, mock_backend): + address = "0x388C818CA8B9251b393131C08a736A67ccB19297" + creator = "0xDB65702A9b26f8a643a31a4c84b9392589e03D7c" + + # Setup backend. + params = {"action": "getcontractcreation", "contractaddresses": [address]} + return_value = [ + { + "contractAddress": address, + "contractCreator": creator.lower(), + "txHash": "0xd72cf25e4a5fe3677b6f9b2ae13771e02ad66f8d2419f333bb8bde3147bd4294", + } + ] + mock_backend.add_handler("GET", "contract", params, return_value=return_value) + + # Perform query. + query = ContractCreationQuery(contract=address, columns=["*"]) + result = list(query_engine.perform_query(query)) + + assert len(result) == 1 + assert result[0].deployer == creator