|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | +from unittest.mock import MagicMock, patch |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +from chia.plotting.prover import PlotVersion, V1Prover, V2Prover, get_prover_from_bytes, get_prover_from_file |
| 10 | + |
| 11 | + |
| 12 | +class TestProver: |
| 13 | + def test_v2_prover_init_with_nonexistent_file(self) -> None: |
| 14 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 15 | + assert prover.get_version() == PlotVersion.V2 |
| 16 | + assert prover.get_filename() == "/nonexistent/path/test.plot2" |
| 17 | + |
| 18 | + def test_v2_prover_get_size_raises_error(self) -> None: |
| 19 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 20 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 21 | + prover.get_size() |
| 22 | + |
| 23 | + def test_v2_prover_get_memo_raises_error(self) -> None: |
| 24 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 25 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 26 | + prover.get_memo() |
| 27 | + |
| 28 | + def test_v2_prover_get_compression_level_raises_error(self) -> None: |
| 29 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 30 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 31 | + prover.get_compression_level() |
| 32 | + |
| 33 | + def test_v2_prover_get_id_raises_error(self) -> None: |
| 34 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 35 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 36 | + prover.get_id() |
| 37 | + |
| 38 | + def test_v2_prover_get_qualities_for_challenge_raises_error(self) -> None: |
| 39 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 40 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 41 | + prover.get_qualities_for_challenge(b"challenge") |
| 42 | + |
| 43 | + def test_v2_prover_get_full_proof_raises_error(self) -> None: |
| 44 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 45 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 46 | + prover.get_full_proof(b"challenge", 0) |
| 47 | + |
| 48 | + def test_v2_prover_bytes_raises_error(self) -> None: |
| 49 | + prover = V2Prover("/nonexistent/path/test.plot2") |
| 50 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 51 | + bytes(prover) |
| 52 | + |
| 53 | + def test_v2_prover_from_bytes_raises_error(self) -> None: |
| 54 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 55 | + V2Prover.from_bytes(b"test_data") |
| 56 | + |
| 57 | + def test_get_prover_from_file(self) -> None: |
| 58 | + prover = get_prover_from_file("/nonexistent/path/test.plot2") |
| 59 | + assert prover.get_version() == PlotVersion.V2 |
| 60 | + with pytest.raises(NotImplementedError, match="V2 plot format is not yet implemented"): |
| 61 | + prover.get_size() |
| 62 | + |
| 63 | + def test_get_prover_from_file_with_plot1_still_works(self) -> None: |
| 64 | + with tempfile.NamedTemporaryFile(suffix=".plot", delete=False) as f: |
| 65 | + temp_path = f.name |
| 66 | + try: |
| 67 | + with pytest.raises(Exception) as exc_info: |
| 68 | + get_prover_from_file(temp_path) |
| 69 | + assert not isinstance(exc_info.value, NotImplementedError) |
| 70 | + finally: |
| 71 | + Path(temp_path).unlink() |
| 72 | + |
| 73 | + def test_unsupported_file_extension_raises_value_error(self) -> None: |
| 74 | + with pytest.raises(ValueError, match="Unsupported plot file"): |
| 75 | + get_prover_from_file("/nonexistent/path/test.txt") |
| 76 | + |
| 77 | + |
| 78 | +class TestV1Prover: |
| 79 | + def test_v1_prover_get_version(self) -> None: |
| 80 | + """Test that V1Prover.get_version() returns PlotVersion.V1""" |
| 81 | + mock_disk_prover = MagicMock() |
| 82 | + prover = V1Prover(mock_disk_prover) |
| 83 | + assert prover.get_version() == PlotVersion.V1 |
| 84 | + |
| 85 | + |
| 86 | +class TestGetProverFromBytes: |
| 87 | + def test_get_prover_from_bytes_v2_plot(self) -> None: |
| 88 | + with patch("chia.plotting.prover.V2Prover.from_bytes") as mock_v2_from_bytes: |
| 89 | + mock_prover = MagicMock() |
| 90 | + mock_v2_from_bytes.return_value = mock_prover |
| 91 | + result = get_prover_from_bytes("test.plot2", b"test_data") |
| 92 | + assert result == mock_prover |
| 93 | + |
| 94 | + def test_get_prover_from_bytes_v1_plot(self) -> None: |
| 95 | + with patch("chia.plotting.prover.DiskProver") as mock_disk_prover_class: |
| 96 | + mock_disk_prover = MagicMock() |
| 97 | + mock_disk_prover_class.from_bytes.return_value = mock_disk_prover |
| 98 | + result = get_prover_from_bytes("test.plot", b"test_data") |
| 99 | + assert isinstance(result, V1Prover) |
| 100 | + |
| 101 | + def test_get_prover_from_bytes_unsupported_extension(self) -> None: |
| 102 | + with pytest.raises(ValueError, match="Unsupported plot file"): |
| 103 | + get_prover_from_bytes("test.txt", b"test_data") |
0 commit comments