|
| 1 | +"""Tests for the gentest CLI command.""" |
| 2 | + |
| 3 | +from click.testing import CliRunner |
| 4 | + |
| 5 | +from cli.gentest.cli import generate |
| 6 | +from cli.gentest.test_context_providers import StateTestProvider |
| 7 | +from cli.pytest_commands.fill import fill |
| 8 | +from ethereum_test_base_types import Account |
| 9 | +from ethereum_test_tools import Environment, Storage, Transaction |
| 10 | + |
| 11 | + |
| 12 | +def test_generate_success(tmp_path, monkeypatch): |
| 13 | + """Test the generate command with a successful scenario.""" |
| 14 | + ## Arrange ## |
| 15 | + |
| 16 | + # This test is run in a CI environment, where connection to a node could be |
| 17 | + # unreliable. Therefore, we mock the RPC request to avoid any network issues. |
| 18 | + # This is done by patching the `get_context` method of the `StateTestProvider`. |
| 19 | + runner = CliRunner() |
| 20 | + transaction_hash = "0xa41f343be7a150b740e5c939fa4d89f3a2850dbe21715df96b612fc20d1906be" |
| 21 | + output_file = str(tmp_path / "gentest.py") |
| 22 | + |
| 23 | + def get_mock_context(self: StateTestProvider) -> dict: |
| 24 | + return { |
| 25 | + "environment": Environment( |
| 26 | + fee_recipient="0x2adc25665018aa1fe0e6bc666dac8fc2697ff9ba", |
| 27 | + gas_limit=9916577, |
| 28 | + number=9974504, |
| 29 | + timestamp=1588257377, |
| 30 | + prev_randao=None, |
| 31 | + difficulty=2315196811272822, |
| 32 | + base_fee_per_gas=None, |
| 33 | + excess_blob_gas=None, |
| 34 | + target_blobs_per_block=None, |
| 35 | + parent_difficulty=None, |
| 36 | + parent_timestamp=None, |
| 37 | + parent_base_fee_per_gas=None, |
| 38 | + parent_gas_used=None, |
| 39 | + parent_gas_limit=None, |
| 40 | + blob_gas_used=None, |
| 41 | + parent_ommers_hash="0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347", |
| 42 | + parent_blob_gas_used=None, |
| 43 | + parent_excess_blob_gas=None, |
| 44 | + parent_beacon_block_root=None, |
| 45 | + block_hashes={}, |
| 46 | + ommers=[], |
| 47 | + withdrawals=None, |
| 48 | + extra_data=b"\x00", |
| 49 | + parent_hash=None, |
| 50 | + ), |
| 51 | + "pre_state": { |
| 52 | + "0x5a0b54d5dc17e0aadc383d2db43b0a0d3e029c4c": Account( |
| 53 | + nonce=6038603, balance=23760714652307793035, code=b"", storage=Storage(root={}) |
| 54 | + ), |
| 55 | + "0x8a4a4d396a06cba2a7a4a73245991de40cdec289": Account( |
| 56 | + nonce=2, balance=816540000000000000, code=b"", storage=Storage(root={}) |
| 57 | + ), |
| 58 | + "0xc6d96786477f82491bfead8f00b8294688f77abc": Account( |
| 59 | + nonce=25, balance=29020266497911578313, code=b"", storage=Storage(root={}) |
| 60 | + ), |
| 61 | + }, |
| 62 | + "transaction": Transaction( |
| 63 | + ty=0, |
| 64 | + chain_id=1, |
| 65 | + nonce=2, |
| 66 | + gas_price=10000000000, |
| 67 | + max_priority_fee_per_gas=None, |
| 68 | + max_fee_per_gas=None, |
| 69 | + gas_limit=21000, |
| 70 | + to="0xc6d96786477f82491bfead8f00b8294688f77abc", |
| 71 | + value=668250000000000000, |
| 72 | + data=b"", |
| 73 | + access_list=None, |
| 74 | + max_fee_per_blob_gas=None, |
| 75 | + blob_versioned_hashes=None, |
| 76 | + v=38, |
| 77 | + r=57233334052658009540326312124836763247359579695589124499839562829147086216092, |
| 78 | + s=49687643984819828983661675232336138386174947240467726918882054280625462464348, |
| 79 | + sender="0x8a4a4d396a06cba2a7a4a73245991de40cdec289", |
| 80 | + authorization_list=None, |
| 81 | + secret_key=None, |
| 82 | + error=None, |
| 83 | + protected=True, |
| 84 | + rlp_override=None, |
| 85 | + wrapped_blob_transaction=False, |
| 86 | + blobs=None, |
| 87 | + blob_kzg_commitments=None, |
| 88 | + blob_kzg_proofs=None, |
| 89 | + ), |
| 90 | + "tx_hash": transaction_hash, |
| 91 | + } |
| 92 | + |
| 93 | + monkeypatch.setattr(StateTestProvider, "get_context", get_mock_context) |
| 94 | + |
| 95 | + ## Genenrate ## |
| 96 | + gentest_result = runner.invoke(generate, [transaction_hash, output_file]) |
| 97 | + assert gentest_result.exit_code == 0 |
| 98 | + |
| 99 | + ## Fill ## |
| 100 | + fill_result = runner.invoke(fill, ["-c", "pytest.ini", output_file]) |
| 101 | + assert fill_result.exit_code == 0 |
0 commit comments