|
| 1 | +"""Test suite for TestPhaseManager functionality.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from ethereum_test_base_types import Address |
| 6 | +from ethereum_test_tools import Block, Transaction |
| 7 | + |
| 8 | +from ..phase_manager import TestPhase, TestPhaseManager |
| 9 | + |
| 10 | + |
| 11 | +def test_test_phase_enum_values(): |
| 12 | + """Test that TestPhase enum has correct values.""" |
| 13 | + assert TestPhase.SETUP.value == "setup" |
| 14 | + assert TestPhase.EXECUTION.value == "execution" |
| 15 | + |
| 16 | + |
| 17 | +def test_phase_manager_initialization(): |
| 18 | + """Test TestPhaseManager initialization.""" |
| 19 | + manager = TestPhaseManager() |
| 20 | + assert len(manager.setup_transactions) == 0 |
| 21 | + assert len(manager.setup_blocks) == 0 |
| 22 | + assert len(manager.execution_transactions) == 0 |
| 23 | + assert len(manager.execution_blocks) == 0 |
| 24 | + assert manager.get_current_phase() == TestPhase.EXECUTION |
| 25 | + |
| 26 | + |
| 27 | +def test_phase_manager_accepts_args_kwargs(): |
| 28 | + """Test that __init__ accepts arbitrary args and kwargs.""" |
| 29 | + # These should not cause errors |
| 30 | + manager1 = TestPhaseManager() |
| 31 | + manager2 = TestPhaseManager("some_arg") |
| 32 | + manager3 = TestPhaseManager(some_kwarg="value") |
| 33 | + manager4 = TestPhaseManager("arg1", "arg2", kwarg1="val1", kwarg2="val2") |
| 34 | + |
| 35 | + # All should have the same initialization |
| 36 | + for manager in [manager1, manager2, manager3, manager4]: |
| 37 | + assert len(manager.setup_transactions) == 0 |
| 38 | + assert len(manager.setup_blocks) == 0 |
| 39 | + assert len(manager.execution_transactions) == 0 |
| 40 | + assert len(manager.execution_blocks) == 0 |
| 41 | + assert manager.get_current_phase() == TestPhase.EXECUTION |
| 42 | + |
| 43 | + |
| 44 | +def test_add_transaction_execution_phase(): |
| 45 | + """Test adding transaction in execution phase.""" |
| 46 | + manager = TestPhaseManager() |
| 47 | + tx = Transaction(to=Address(0x123), value=100, gas_limit=21000) |
| 48 | + |
| 49 | + manager.add_transaction(tx) |
| 50 | + |
| 51 | + assert len(manager.execution_transactions) == 1 |
| 52 | + assert manager.execution_transactions[0] == tx |
| 53 | + assert tx.test_phase == TestPhase.EXECUTION.value |
| 54 | + assert len(manager.setup_transactions) == 0 |
| 55 | + |
| 56 | + |
| 57 | +def test_add_transaction_setup_phase(): |
| 58 | + """Test adding transaction in setup phase.""" |
| 59 | + manager = TestPhaseManager() |
| 60 | + tx = Transaction(to=Address(0x456), value=50, gas_limit=21000) |
| 61 | + |
| 62 | + with manager.setup(): |
| 63 | + manager.add_transaction(tx) |
| 64 | + |
| 65 | + assert len(manager.setup_transactions) == 1 |
| 66 | + assert manager.setup_transactions[0] == tx |
| 67 | + assert tx.test_phase == TestPhase.SETUP.value |
| 68 | + assert len(manager.execution_transactions) == 0 |
| 69 | + |
| 70 | + |
| 71 | +def test_add_block_execution_phase(): |
| 72 | + """Test adding block in execution phase.""" |
| 73 | + manager = TestPhaseManager() |
| 74 | + tx1 = Transaction(to=Address(0x111), value=100, gas_limit=21000) |
| 75 | + tx2 = Transaction(to=Address(0x222), value=200, gas_limit=21000) |
| 76 | + block = Block(txs=[tx1, tx2]) |
| 77 | + |
| 78 | + manager.add_block(block) |
| 79 | + |
| 80 | + assert len(manager.execution_blocks) == 1 |
| 81 | + assert manager.execution_blocks[0] == block |
| 82 | + assert tx1.test_phase == TestPhase.EXECUTION.value |
| 83 | + assert tx2.test_phase == TestPhase.EXECUTION.value |
| 84 | + assert len(manager.setup_blocks) == 0 |
| 85 | + |
| 86 | + |
| 87 | +def test_add_block_setup_phase(): |
| 88 | + """Test adding block in setup phase.""" |
| 89 | + manager = TestPhaseManager() |
| 90 | + tx1 = Transaction(to=Address(0x333), value=100, gas_limit=21000) |
| 91 | + tx2 = Transaction(to=Address(0x444), value=200, gas_limit=21000) |
| 92 | + block = Block(txs=[tx1, tx2]) |
| 93 | + |
| 94 | + with manager.setup(): |
| 95 | + manager.add_block(block) |
| 96 | + |
| 97 | + assert len(manager.setup_blocks) == 1 |
| 98 | + assert manager.setup_blocks[0] == block |
| 99 | + assert tx1.test_phase == TestPhase.SETUP.value |
| 100 | + assert tx2.test_phase == TestPhase.SETUP.value |
| 101 | + assert len(manager.execution_blocks) == 0 |
| 102 | + |
| 103 | + |
| 104 | +@pytest.mark.parametrize( |
| 105 | + ["num_setup_txs", "num_setup_blocks", "num_exec_txs", "num_exec_blocks"], |
| 106 | + [ |
| 107 | + pytest.param(0, 0, 1, 0, id="exec_tx_only"), |
| 108 | + pytest.param(1, 0, 0, 0, id="setup_tx_only"), |
| 109 | + pytest.param(0, 1, 0, 0, id="setup_block_only"), |
| 110 | + pytest.param(0, 0, 0, 1, id="exec_block_only"), |
| 111 | + pytest.param(2, 1, 3, 2, id="mixed_operations"), |
| 112 | + pytest.param(5, 0, 0, 5, id="many_items"), |
| 113 | + ], |
| 114 | +) |
| 115 | +def test_mixed_operations(num_setup_txs, num_setup_blocks, num_exec_txs, num_exec_blocks): |
| 116 | + """Test mixed operations across phases.""" |
| 117 | + manager = TestPhaseManager() |
| 118 | + |
| 119 | + # Add setup items |
| 120 | + with manager.setup(): |
| 121 | + for i in range(num_setup_txs): |
| 122 | + tx = Transaction(to=Address(0x1000 + i), value=i * 10, gas_limit=21000) |
| 123 | + manager.add_transaction(tx) |
| 124 | + |
| 125 | + for i in range(num_setup_blocks): |
| 126 | + tx = Transaction(to=Address(0x2000 + i), value=i * 100, gas_limit=21000) |
| 127 | + block = Block(txs=[tx]) |
| 128 | + manager.add_block(block) |
| 129 | + |
| 130 | + # Add execution items |
| 131 | + for i in range(num_exec_txs): |
| 132 | + tx = Transaction(to=Address(0x3000 + i), value=i * 20, gas_limit=21000) |
| 133 | + manager.add_transaction(tx) |
| 134 | + |
| 135 | + for i in range(num_exec_blocks): |
| 136 | + tx = Transaction(to=Address(0x4000 + i), value=i * 200, gas_limit=21000) |
| 137 | + block = Block(txs=[tx]) |
| 138 | + manager.add_block(block) |
| 139 | + |
| 140 | + # Verify counts |
| 141 | + assert len(manager.setup_transactions) == num_setup_txs |
| 142 | + assert len(manager.setup_blocks) == num_setup_blocks |
| 143 | + assert len(manager.execution_transactions) == num_exec_txs |
| 144 | + assert len(manager.execution_blocks) == num_exec_blocks |
| 145 | + |
| 146 | + # Verify phase markers |
| 147 | + for tx in manager.setup_transactions: |
| 148 | + assert tx.test_phase == TestPhase.SETUP.value |
| 149 | + |
| 150 | + for block in manager.setup_blocks: |
| 151 | + for tx in block.txs: |
| 152 | + assert tx.test_phase == TestPhase.SETUP.value |
| 153 | + |
| 154 | + for tx in manager.execution_transactions: |
| 155 | + assert tx.test_phase == TestPhase.EXECUTION.value |
| 156 | + |
| 157 | + for block in manager.execution_blocks: |
| 158 | + for tx in block.txs: |
| 159 | + assert tx.test_phase == TestPhase.EXECUTION.value |
| 160 | + |
| 161 | + |
| 162 | +def test_empty_block_handling(): |
| 163 | + """Test handling of empty blocks.""" |
| 164 | + manager = TestPhaseManager() |
| 165 | + empty_block = Block(txs=[]) |
| 166 | + |
| 167 | + with manager.setup(): |
| 168 | + manager.add_block(empty_block) |
| 169 | + |
| 170 | + assert len(manager.setup_blocks) == 1 |
| 171 | + assert len(manager.setup_blocks[0].txs) == 0 |
| 172 | + |
| 173 | + |
| 174 | +def test_block_with_many_transactions(): |
| 175 | + """Test block with many transactions gets all transactions marked.""" |
| 176 | + manager = TestPhaseManager() |
| 177 | + |
| 178 | + # Create block with multiple transactions |
| 179 | + transactions = [ |
| 180 | + Transaction(to=Address(0x100 + i), value=i * 10, gas_limit=21000) for i in range(5) |
| 181 | + ] |
| 182 | + block = Block(txs=transactions) |
| 183 | + |
| 184 | + with manager.setup(): |
| 185 | + manager.add_block(block) |
| 186 | + |
| 187 | + # Verify all transactions in the block have the setup phase |
| 188 | + assert len(manager.setup_blocks) == 1 |
| 189 | + setup_block = manager.setup_blocks[0] |
| 190 | + assert len(setup_block.txs) == 5 |
| 191 | + |
| 192 | + for tx in setup_block.txs: |
| 193 | + assert tx.test_phase == TestPhase.SETUP.value |
| 194 | + |
| 195 | + |
| 196 | +def test_phase_switching_preserves_existing_data(): |
| 197 | + """Test that phase switching doesn't affect existing data.""" |
| 198 | + manager = TestPhaseManager() |
| 199 | + |
| 200 | + # Add data in execution phase |
| 201 | + exec_tx1 = Transaction(to=Address(0x100), value=100, gas_limit=21000) |
| 202 | + manager.add_transaction(exec_tx1) |
| 203 | + |
| 204 | + # Switch to setup and add data |
| 205 | + with manager.setup(): |
| 206 | + setup_tx = Transaction(to=Address(0x200), value=50, gas_limit=21000) |
| 207 | + manager.add_transaction(setup_tx) |
| 208 | + |
| 209 | + # Switch back to execution within setup phase |
| 210 | + with manager.execution(): |
| 211 | + exec_tx2 = Transaction(to=Address(0x300), value=200, gas_limit=21000) |
| 212 | + manager.add_transaction(exec_tx2) |
| 213 | + |
| 214 | + # Add more data in execution phase after phase changes |
| 215 | + exec_tx3 = Transaction(to=Address(0x400), value=300, gas_limit=21000) |
| 216 | + manager.add_transaction(exec_tx3) |
| 217 | + |
| 218 | + # Verify data integrity |
| 219 | + assert len(manager.setup_transactions) == 1 |
| 220 | + assert len(manager.execution_transactions) == 3 |
| 221 | + |
| 222 | + assert manager.setup_transactions[0].value == 50 |
| 223 | + exec_values = [tx.value for tx in manager.execution_transactions] |
| 224 | + assert exec_values == [100, 200, 300] |
| 225 | + |
| 226 | + |
| 227 | +@pytest.mark.parametrize( |
| 228 | + ["manager_instance", "expected_phase"], |
| 229 | + [ |
| 230 | + pytest.param(TestPhaseManager(), TestPhase.EXECUTION, id="new_instance"), |
| 231 | + ], |
| 232 | +) |
| 233 | +def test_manager_properties(manager_instance, expected_phase): |
| 234 | + """Test TestPhaseManager instance properties.""" |
| 235 | + assert isinstance(manager_instance, TestPhaseManager) |
| 236 | + assert manager_instance.get_current_phase() == expected_phase |
| 237 | + assert hasattr(manager_instance, "setup_transactions") |
| 238 | + assert hasattr(manager_instance, "setup_blocks") |
| 239 | + assert hasattr(manager_instance, "execution_transactions") |
| 240 | + assert hasattr(manager_instance, "execution_blocks") |
0 commit comments