|
| 1 | +"""Fixtures for unit tests with mocking.""" |
| 2 | + |
| 3 | +from unittest.mock import Mock |
| 4 | + |
| 5 | +import pytest |
| 6 | +import requests |
| 7 | + |
| 8 | +from solc_select.infrastructure.filesystem import FilesystemManager |
| 9 | +from solc_select.models.platforms import Platform |
| 10 | +from solc_select.models.versions import SolcVersion |
| 11 | +from solc_select.repositories import SolcRepository |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_session(): |
| 16 | + """Mock requests.Session for HTTP calls.""" |
| 17 | + return Mock(spec=requests.Session) |
| 18 | + |
| 19 | + |
| 20 | +@pytest.fixture |
| 21 | +def mock_filesystem(): |
| 22 | + """Mock FilesystemManager.""" |
| 23 | + return Mock(spec=FilesystemManager) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def mock_platform(): |
| 28 | + """Mock Platform (linux-amd64 by default).""" |
| 29 | + return Platform("linux", "amd64") |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture |
| 33 | +def mock_repository(): |
| 34 | + """Mock SolcRepository with common version set.""" |
| 35 | + mock = Mock(spec=SolcRepository) |
| 36 | + mock.available_versions = { |
| 37 | + SolcVersion("0.8.19"): "solc-linux-amd64-v0.8.19+commit.abc123", |
| 38 | + SolcVersion("0.8.20"): "solc-linux-amd64-v0.8.20+commit.def456", |
| 39 | + SolcVersion("0.8.21"): "solc-linux-amd64-v0.8.21+commit.ghi789", |
| 40 | + } |
| 41 | + mock.latest_version = SolcVersion("0.8.21") |
| 42 | + return mock |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def sample_versions(): |
| 47 | + """Sample version list for testing.""" |
| 48 | + return [ |
| 49 | + SolcVersion("0.8.19"), |
| 50 | + SolcVersion("0.8.20"), |
| 51 | + SolcVersion("0.8.21"), |
| 52 | + ] |
| 53 | + |
| 54 | + |
| 55 | +@pytest.fixture |
| 56 | +def temp_artifacts_dir(tmp_path, monkeypatch): |
| 57 | + """Temporary artifacts directory for testing filesystem operations.""" |
| 58 | + artifacts_dir = tmp_path / "artifacts" |
| 59 | + artifacts_dir.mkdir() |
| 60 | + monkeypatch.setattr("solc_select.constants.ARTIFACTS_DIR", artifacts_dir) |
| 61 | + return artifacts_dir |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture |
| 65 | +def temp_solc_select_dir(tmp_path, monkeypatch): |
| 66 | + """Temporary solc-select directory for testing.""" |
| 67 | + solc_select_dir = tmp_path / ".solc-select" |
| 68 | + solc_select_dir.mkdir() |
| 69 | + artifacts_dir = solc_select_dir / "artifacts" |
| 70 | + artifacts_dir.mkdir() |
| 71 | + |
| 72 | + # Patch constants module |
| 73 | + monkeypatch.setattr("solc_select.constants.SOLC_SELECT_DIR", solc_select_dir) |
| 74 | + monkeypatch.setattr("solc_select.constants.ARTIFACTS_DIR", artifacts_dir) |
| 75 | + |
| 76 | + # Patch where constants are imported in filesystem module |
| 77 | + monkeypatch.setattr("solc_select.infrastructure.filesystem.ARTIFACTS_DIR", artifacts_dir) |
| 78 | + monkeypatch.setattr("solc_select.infrastructure.filesystem.SOLC_SELECT_DIR", solc_select_dir) |
| 79 | + |
| 80 | + return solc_select_dir |
0 commit comments