Skip to content

Commit

Permalink
🧹 chore: Refactor templating logic
Browse files Browse the repository at this point in the history
  • Loading branch information
raxhvl committed Oct 18, 2024
1 parent e5ce0b1 commit f83284c
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/cli/gentest/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

from ethereum_test_base_types import Hash

from .request_manager import RequestManager
from .request_manager import RPCRequest
from .test_providers import BlockchainTestProvider

template_loader = jinja2.PackageLoader("cli.gentest")
Expand All @@ -31,7 +31,7 @@ def generate(transaction_hash: str, output_file: TextIO):
OUTPUT_FILE is the path to the output python script.
"""
request = RequestManager()
request = RPCRequest()

print(
"Perform tx request: eth_get_transaction_by_hash(" + f"{transaction_hash}" + ")",
Expand Down
4 changes: 2 additions & 2 deletions src/cli/gentest/request_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def eth_get_transaction_by_hash(self, transaction_hash: Hash) -> RemoteTransacti
res.ty == 0
), f"Transaction has type {res.ty}: Currently only type 0 transactions are supported."

return RequestManager.RemoteTransaction(
return RPCRequest.RemoteTransaction(
block_number=block_number,
tx_hash=res.transaction_hash,
ty=res.ty,
Expand All @@ -92,7 +92,7 @@ def eth_get_block_by_number(self, block_number: BlockNumberType) -> RemoteBlock:
"""
res = self.rpc.get_block_by_number(block_number)

return RequestManager.RemoteBlock(
return RPCRequest.RemoteBlock(
coinbase=res["miner"],
number=res["number"],
difficulty=res["difficulty"],
Expand Down
10 changes: 6 additions & 4 deletions src/cli/gentest/templates/blockchain_test/transaction.py.j2
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

"""
{{ module_docstring }}
gentest autogenerated test with debug_traceCall of tx.hash
{{ tx_hash }}
https://etherscan.io/tx/{{tx_hash}}
"""

from typing import Dict
Expand All @@ -21,12 +22,13 @@ def env(): # noqa: D103


@pytest.mark.valid_from("Paris")
def {{ test_name }}( # noqa: SC200, E501
def test_transaction_{{ tx_hash }}( # noqa: SC200, E501
env: Environment,
blockchain_test: BlockchainTestFiller,
):
"""
{{ test_docstring }}
gentest autogenerated test for tx.hash
{{ tx_hash }}
"""
pre = {
{{ pre_state_items }}
Expand Down
22 changes: 4 additions & 18 deletions src/cli/gentest/test_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,18 @@

from ethereum_test_base_types import Account, Address, ZeroPaddedHexNumber

from .request_manager import RequestManager
from .request_manager import RPCRequest


class BlockchainTestProvider(BaseModel):
"""
Provides context required to generate a `blockchain_test` using pytest.
"""

block: RequestManager.RemoteBlock
transaction: RequestManager.RemoteTransaction
block: RPCRequest.RemoteBlock
transaction: RPCRequest.RemoteTransaction
state: Dict[Address, Account]

def _get_module_docstring(self):
return (
f"gentest autogenerated test with debug_traceCall of tx.hash "
f"{self.transaction.tx_hash}"
)

def _get_test_name(self):
return f"test_transaction_{self.transaction.tx_hash}"

def _get_test_docstring(self):
return f"gentest autogenerated test for tx.hash\n{self.transaction.tx_hash}"

def _get_environment_kwargs(self) -> str:
env_str = ""
pad = " "
Expand Down Expand Up @@ -122,10 +110,8 @@ def get_context(self) -> Dict[str, Any]:
test docstring, environment kwargs, pre-state items, and transaction items.
"""
return {
"module_docstring": self._get_module_docstring(),
"test_name": self._get_test_name(),
"test_docstring": self._get_test_docstring(),
"environment_kwargs": self._get_environment_kwargs(),
"pre_state_items": self._get_pre_state_items(),
"transaction_items": self._get_transaction_items(),
"tx_hash": self.transaction.tx_hash,
}

0 comments on commit f83284c

Please sign in to comment.