|
| 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 Transaction |
| 7 | + |
| 8 | +from ..phase_manager import TestPhase, TestPhaseManager |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture(autouse=True) |
| 12 | +def reset_phase_manager() -> None: |
| 13 | + """Reset TestPhaseManager singleton state before each test.""" |
| 14 | + TestPhaseManager.reset() |
| 15 | + |
| 16 | + |
| 17 | +def test_test_phase_enum_values() -> None: |
| 18 | + """Test that TestPhase enum has correct values.""" |
| 19 | + assert TestPhase.SETUP.value == "setup" |
| 20 | + assert TestPhase.EXECUTION.value == "execution" |
| 21 | + |
| 22 | + |
| 23 | +def test_phase_manager_class_state() -> None: |
| 24 | + """Test TestPhaseManager uses class-level state.""" |
| 25 | + # All access is through class methods, no instance needed |
| 26 | + assert TestPhaseManager.get_current_phase() is None |
| 27 | + |
| 28 | + # Setting phase through class method |
| 29 | + with TestPhaseManager.setup(): |
| 30 | + assert TestPhaseManager.get_current_phase() == TestPhase.SETUP |
| 31 | + |
| 32 | + # Phase persists at class level |
| 33 | + assert TestPhaseManager.get_current_phase() is None |
| 34 | + |
| 35 | + |
| 36 | +def test_default_phase_is_none() -> None: |
| 37 | + """Test that default phase is None (no context set).""" |
| 38 | + assert TestPhaseManager.get_current_phase() is None |
| 39 | + |
| 40 | + |
| 41 | +def test_transaction_auto_detects_execution_phase() -> None: |
| 42 | + """Test that transactions default to 'execution' when no phase set.""" |
| 43 | + tx = Transaction(to=Address(0x123), value=100, gas_limit=21000) |
| 44 | + assert tx.test_phase == "execution" |
| 45 | + |
| 46 | + |
| 47 | +def test_transaction_auto_detects_setup_phase() -> None: |
| 48 | + """Test that transactions created in setup context get SETUP phase.""" |
| 49 | + with TestPhaseManager.setup(): |
| 50 | + tx = Transaction(to=Address(0x456), value=50, gas_limit=21000) |
| 51 | + assert tx.test_phase == "setup" |
| 52 | + |
| 53 | + |
| 54 | +def test_phase_context_switching() -> None: |
| 55 | + """Test that phase switching works correctly.""" |
| 56 | + # Start with no phase set (defaults to execution) |
| 57 | + tx1 = Transaction(to=Address(0x100), value=100, gas_limit=21000) |
| 58 | + assert tx1.test_phase == "execution" |
| 59 | + |
| 60 | + # Switch to SETUP |
| 61 | + with TestPhaseManager.setup(): |
| 62 | + assert TestPhaseManager.get_current_phase() == TestPhase.SETUP |
| 63 | + tx2 = Transaction(to=Address(0x200), value=200, gas_limit=21000) |
| 64 | + assert tx2.test_phase == "setup" |
| 65 | + |
| 66 | + # Back to None after context (transactions default to execution) |
| 67 | + assert TestPhaseManager.get_current_phase() is None |
| 68 | + tx3 = Transaction(to=Address(0x300), value=300, gas_limit=21000) |
| 69 | + assert tx3.test_phase == "execution" |
| 70 | + |
| 71 | + |
| 72 | +def test_nested_phase_contexts() -> None: |
| 73 | + """Test that nested phase contexts work correctly.""" |
| 74 | + with TestPhaseManager.setup(): |
| 75 | + tx1 = Transaction(to=Address(0x100), value=100, gas_limit=21000) |
| 76 | + assert tx1.test_phase == "setup" |
| 77 | + |
| 78 | + # Nested execution context |
| 79 | + with TestPhaseManager.execution(): |
| 80 | + tx2 = Transaction(to=Address(0x200), value=200, gas_limit=21000) |
| 81 | + assert tx2.test_phase == "execution" |
| 82 | + |
| 83 | + # Back to setup after nested context |
| 84 | + tx3 = Transaction(to=Address(0x300), value=300, gas_limit=21000) |
| 85 | + assert tx3.test_phase == "setup" |
| 86 | + |
| 87 | + |
| 88 | +@pytest.mark.parametrize( |
| 89 | + ["num_setup_txs", "num_exec_txs"], |
| 90 | + [ |
| 91 | + pytest.param(0, 1, id="exec_only"), |
| 92 | + pytest.param(1, 0, id="setup_only"), |
| 93 | + pytest.param(3, 5, id="mixed"), |
| 94 | + pytest.param(10, 10, id="many"), |
| 95 | + ], |
| 96 | +) |
| 97 | +def test_multiple_transactions_phase_tagging(num_setup_txs: int, num_exec_txs: int) -> None: |
| 98 | + """Test that multiple transactions are correctly tagged by phase.""" |
| 99 | + setup_txs = [] |
| 100 | + exec_txs = [] |
| 101 | + |
| 102 | + # Create setup transactions |
| 103 | + with TestPhaseManager.setup(): |
| 104 | + for i in range(num_setup_txs): |
| 105 | + tx = Transaction(to=Address(0x1000 + i), value=i * 10, gas_limit=21000) |
| 106 | + setup_txs.append(tx) |
| 107 | + |
| 108 | + # Create execution transactions |
| 109 | + for i in range(num_exec_txs): |
| 110 | + tx = Transaction(to=Address(0x2000 + i), value=i * 20, gas_limit=21000) |
| 111 | + exec_txs.append(tx) |
| 112 | + |
| 113 | + # Verify all setup transactions have SETUP phase |
| 114 | + for tx in setup_txs: |
| 115 | + assert tx.test_phase == "setup" |
| 116 | + |
| 117 | + # Verify all execution transactions have EXECUTION phase |
| 118 | + for tx in exec_txs: |
| 119 | + assert tx.test_phase == "execution" |
| 120 | + |
| 121 | + |
| 122 | +def test_phase_reset() -> None: |
| 123 | + """Test that reset() restores default phase.""" |
| 124 | + # Change phase |
| 125 | + with TestPhaseManager.setup(): |
| 126 | + pass |
| 127 | + |
| 128 | + # Manually set to SETUP |
| 129 | + TestPhaseManager._current_phase = TestPhase.SETUP |
| 130 | + assert TestPhaseManager.get_current_phase() == TestPhase.SETUP |
| 131 | + |
| 132 | + # Reset should restore None |
| 133 | + TestPhaseManager.reset() |
| 134 | + assert TestPhaseManager.get_current_phase() is None |
| 135 | + |
| 136 | + |
| 137 | +def test_class_state_shared() -> None: |
| 138 | + """Test that phase state is shared at class level.""" |
| 139 | + # Phase changes are visible globally since it's class-level state |
| 140 | + assert TestPhaseManager.get_current_phase() is None |
| 141 | + |
| 142 | + with TestPhaseManager.setup(): |
| 143 | + # All access to the class sees the same phase |
| 144 | + assert TestPhaseManager.get_current_phase() == TestPhase.SETUP |
| 145 | + |
| 146 | + # Transactions created during this context get "setup" phase |
| 147 | + tx = Transaction(to=Address(0x789), value=75, gas_limit=21000) |
| 148 | + assert tx.test_phase == "setup" |
| 149 | + |
| 150 | + # After context, phase returns to None |
| 151 | + assert TestPhaseManager.get_current_phase() is None |
0 commit comments