|
| 1 | +"""Unit tests for artifact-related domain models.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from solc_select.models.artifacts import SolcArtifact |
| 8 | +from solc_select.models.platforms import Platform |
| 9 | +from solc_select.models.versions import SolcVersion |
| 10 | + |
| 11 | + |
| 12 | +def create_artifact( |
| 13 | + version: str = "0.8.19", |
| 14 | + platform: Platform | None = None, |
| 15 | + checksum_sha256: str = "abc123", |
| 16 | + checksum_keccak256: str | None = None, |
| 17 | + download_url: str = "https://example.com/solc", |
| 18 | +) -> SolcArtifact: |
| 19 | + """Helper to create SolcArtifact with sensible defaults.""" |
| 20 | + if platform is None: |
| 21 | + platform = Platform("linux", "amd64") |
| 22 | + return SolcArtifact( |
| 23 | + version=SolcVersion(version), |
| 24 | + platform=platform, |
| 25 | + file_path=Path(f"/tmp/solc-{version}"), |
| 26 | + download_url=download_url, |
| 27 | + checksum_sha256=checksum_sha256, |
| 28 | + checksum_keccak256=checksum_keccak256, |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +class TestSolcArtifact: |
| 33 | + """Tests for SolcArtifact domain model.""" |
| 34 | + |
| 35 | + @pytest.mark.parametrize( |
| 36 | + "version,expected", |
| 37 | + [ |
| 38 | + ("0.6.12", True), # Old Windows version - should be ZIP |
| 39 | + ("0.7.1", True), # Boundary - still ZIP |
| 40 | + ("0.7.2", False), # First non-ZIP version |
| 41 | + ("0.8.19", False), # Modern version |
| 42 | + ], |
| 43 | + ) |
| 44 | + def test_is_zip_archive_windows(self, version: str, expected: bool) -> None: |
| 45 | + """Windows ZIP archive detection based on version boundary (0.7.1).""" |
| 46 | + artifact = create_artifact(version=version, platform=Platform("windows", "amd64")) |
| 47 | + assert artifact.is_zip_archive == expected |
| 48 | + |
| 49 | + @pytest.mark.parametrize("os_type", ["linux", "darwin"]) |
| 50 | + def test_is_zip_archive_non_windows(self, os_type: str) -> None: |
| 51 | + """Non-Windows platforms should never have ZIP archives.""" |
| 52 | + artifact = create_artifact(version="0.6.12", platform=Platform(os_type, "amd64")) |
| 53 | + assert not artifact.is_zip_archive |
| 54 | + |
| 55 | + def test_checksum_prefix_removal(self) -> None: |
| 56 | + """Checksums with 0x prefix should be stripped in __post_init__.""" |
| 57 | + artifact = create_artifact( |
| 58 | + checksum_sha256="0xabc123def456", |
| 59 | + checksum_keccak256="0x789xyz", |
| 60 | + ) |
| 61 | + assert artifact.checksum_sha256 == "abc123def456" |
| 62 | + assert artifact.checksum_keccak256 == "789xyz" |
| 63 | + |
| 64 | + def test_get_binary_name_in_zip(self) -> None: |
| 65 | + """Binary name inside ZIP archives should be solc.exe.""" |
| 66 | + artifact = create_artifact(version="0.6.12", platform=Platform("windows", "amd64")) |
| 67 | + assert artifact.get_binary_name_in_zip() == "solc.exe" |
| 68 | + |
| 69 | + def test_get_binary_name_in_zip_raises_for_non_zip(self) -> None: |
| 70 | + """Calling get_binary_name_in_zip on non-ZIP should raise ValueError.""" |
| 71 | + artifact = create_artifact(version="0.8.19", platform=Platform("windows", "amd64")) |
| 72 | + with pytest.raises(ValueError, match="Not a ZIP archive"): |
| 73 | + artifact.get_binary_name_in_zip() |
| 74 | + |
| 75 | + @pytest.mark.parametrize( |
| 76 | + "download_url,checksum,error_msg", |
| 77 | + [ |
| 78 | + ("", "abc123", "Download URL cannot be empty"), |
| 79 | + ("https://example.com", "", "SHA256 checksum cannot be empty"), |
| 80 | + ], |
| 81 | + ) |
| 82 | + def test_validation_errors(self, download_url: str, checksum: str, error_msg: str) -> None: |
| 83 | + """Empty URL or checksum should raise ValueError.""" |
| 84 | + with pytest.raises(ValueError, match=error_msg): |
| 85 | + SolcArtifact( |
| 86 | + version=SolcVersion("0.8.19"), |
| 87 | + platform=Platform("linux", "amd64"), |
| 88 | + file_path=Path("/tmp/solc-0.8.19"), |
| 89 | + download_url=download_url, |
| 90 | + checksum_sha256=checksum, |
| 91 | + checksum_keccak256=None, |
| 92 | + ) |
0 commit comments