|
| 1 | +"""Test the simplified consume behavior for different input types.""" |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from unittest.mock import MagicMock, patch |
| 5 | + |
| 6 | +from pytest_plugins.consume.consume import FixturesSource |
| 7 | + |
| 8 | + |
| 9 | +class TestSimplifiedConsumeBehavior: |
| 10 | + """Test suite for the simplified consume behavior.""" |
| 11 | + |
| 12 | + def test_fixtures_source_from_release_url_no_api_calls(self): |
| 13 | + """Test that direct release URLs do not make API calls for release page.""" |
| 14 | + test_url = "https://github.com/ethereum/execution-spec-tests/releases/download/v3.0.0/fixtures_develop.tar.gz" |
| 15 | + |
| 16 | + with patch("pytest_plugins.consume.consume.FixtureDownloader") as mock_downloader: |
| 17 | + mock_instance = MagicMock() |
| 18 | + mock_instance.download_and_extract.return_value = (False, Path("/tmp/test")) |
| 19 | + mock_downloader.return_value = mock_instance |
| 20 | + |
| 21 | + source = FixturesSource.from_release_url(test_url) |
| 22 | + |
| 23 | + # Verify no release page is set for direct URLs |
| 24 | + assert source.release_page == "" |
| 25 | + assert source.url == test_url |
| 26 | + assert source.input_option == test_url |
| 27 | + |
| 28 | + def test_fixtures_source_from_release_spec_makes_api_calls(self): |
| 29 | + """Test that release specs still make API calls and get release page.""" |
| 30 | + test_spec = "stable@latest" |
| 31 | + |
| 32 | + with patch("pytest_plugins.consume.consume.get_release_url") as mock_get_url: |
| 33 | + mock_get_url.return_value = "https://github.com/ethereum/execution-spec-tests/releases/download/v3.0.0/fixtures_stable.tar.gz" |
| 34 | + with patch("pytest_plugins.consume.consume.get_release_page_url") as mock_get_page: |
| 35 | + mock_get_page.return_value = ( |
| 36 | + "https://github.com/ethereum/execution-spec-tests/releases/tag/v3.0.0" |
| 37 | + ) |
| 38 | + with patch("pytest_plugins.consume.consume.FixtureDownloader") as mock_downloader: |
| 39 | + mock_instance = MagicMock() |
| 40 | + mock_instance.download_and_extract.return_value = (False, Path("/tmp/test")) |
| 41 | + mock_downloader.return_value = mock_instance |
| 42 | + |
| 43 | + source = FixturesSource.from_release_spec(test_spec) |
| 44 | + |
| 45 | + # Verify API calls were made and release page is set |
| 46 | + mock_get_url.assert_called_once_with(test_spec) |
| 47 | + mock_get_page.assert_called_once_with( |
| 48 | + "https://github.com/ethereum/execution-spec-tests/releases/download/v3.0.0/fixtures_stable.tar.gz" |
| 49 | + ) |
| 50 | + assert ( |
| 51 | + source.release_page |
| 52 | + == "https://github.com/ethereum/execution-spec-tests/releases/tag/v3.0.0" |
| 53 | + ) |
| 54 | + |
| 55 | + def test_fixtures_source_from_regular_url_no_release_page(self): |
| 56 | + """Test that regular URLs (non-GitHub) don't have release page.""" |
| 57 | + test_url = "http://example.com/fixtures.tar.gz" |
| 58 | + |
| 59 | + with patch("pytest_plugins.consume.consume.FixtureDownloader") as mock_downloader: |
| 60 | + mock_instance = MagicMock() |
| 61 | + mock_instance.download_and_extract.return_value = (False, Path("/tmp/test")) |
| 62 | + mock_downloader.return_value = mock_instance |
| 63 | + |
| 64 | + source = FixturesSource.from_url(test_url) |
| 65 | + |
| 66 | + # Verify no release page for regular URLs |
| 67 | + assert source.release_page == "" |
| 68 | + assert source.url == test_url |
| 69 | + |
| 70 | + def test_output_formatting_without_release_page_for_direct_urls(self): |
| 71 | + """Test output formatting when release page is empty for direct URLs.""" |
| 72 | + from unittest.mock import MagicMock |
| 73 | + |
| 74 | + from pytest import Config |
| 75 | + |
| 76 | + config = MagicMock(spec=Config) |
| 77 | + config.fixtures_source = MagicMock() |
| 78 | + config.fixtures_source.was_cached = False |
| 79 | + config.fixtures_source.is_local = False |
| 80 | + config.fixtures_source.path = Path("/tmp/test") |
| 81 | + config.fixtures_source.url = "https://github.com/ethereum/execution-spec-tests/releases/download/v3.0.0/fixtures_develop.tar.gz" |
| 82 | + config.fixtures_source.release_page = "" # Empty for direct URLs |
| 83 | + |
| 84 | + # Simulate the output generation logic from pytest_configure |
| 85 | + reason = "" |
| 86 | + if config.fixtures_source.was_cached: |
| 87 | + reason += "Fixtures already cached." |
| 88 | + elif not config.fixtures_source.is_local: |
| 89 | + reason += "Fixtures downloaded and cached." |
| 90 | + reason += f"\nPath: {config.fixtures_source.path}" |
| 91 | + reason += f"\nInput: {config.fixtures_source.url or config.fixtures_source.path}" |
| 92 | + if config.fixtures_source.release_page: |
| 93 | + reason += f"\nRelease page: {config.fixtures_source.release_page}" |
| 94 | + |
| 95 | + assert "Release page:" not in reason |
| 96 | + assert "Path:" in reason |
| 97 | + assert "Input:" in reason |
| 98 | + |
| 99 | + def test_output_formatting_with_release_page_for_specs(self): |
| 100 | + """Test output formatting when release page is present for release specs.""" |
| 101 | + from unittest.mock import MagicMock |
| 102 | + |
| 103 | + from pytest import Config |
| 104 | + |
| 105 | + config = MagicMock(spec=Config) |
| 106 | + config.fixtures_source = MagicMock() |
| 107 | + config.fixtures_source.was_cached = False |
| 108 | + config.fixtures_source.is_local = False |
| 109 | + config.fixtures_source.path = Path("/tmp/test") |
| 110 | + config.fixtures_source.url = "https://github.com/ethereum/execution-spec-tests/releases/download/v3.0.0/fixtures_stable.tar.gz" |
| 111 | + config.fixtures_source.release_page = ( |
| 112 | + "https://github.com/ethereum/execution-spec-tests/releases/tag/v3.0.0" |
| 113 | + ) |
| 114 | + |
| 115 | + # Simulate the output generation logic from pytest_configure |
| 116 | + reason = "" |
| 117 | + if config.fixtures_source.was_cached: |
| 118 | + reason += "Fixtures already cached." |
| 119 | + elif not config.fixtures_source.is_local: |
| 120 | + reason += "Fixtures downloaded and cached." |
| 121 | + reason += f"\nPath: {config.fixtures_source.path}" |
| 122 | + reason += f"\nInput: {config.fixtures_source.url or config.fixtures_source.path}" |
| 123 | + if config.fixtures_source.release_page: |
| 124 | + reason += f"\nRelease page: {config.fixtures_source.release_page}" |
| 125 | + |
| 126 | + assert ( |
| 127 | + "Release page: https://github.com/ethereum/execution-spec-tests/releases/tag/v3.0.0" |
| 128 | + in reason |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +class TestFixturesSourceFromInput: |
| 133 | + """Test the from_input method without no_api_calls parameter.""" |
| 134 | + |
| 135 | + def test_from_input_handles_release_url(self): |
| 136 | + """Test that from_input properly handles release URLs.""" |
| 137 | + test_url = "https://github.com/ethereum/execution-spec-tests/releases/download/v3.0.0/fixtures_develop.tar.gz" |
| 138 | + |
| 139 | + with patch.object(FixturesSource, "from_release_url") as mock_from_release_url: |
| 140 | + mock_from_release_url.return_value = MagicMock() |
| 141 | + |
| 142 | + FixturesSource.from_input(test_url) |
| 143 | + |
| 144 | + mock_from_release_url.assert_called_once_with(test_url) |
| 145 | + |
| 146 | + def test_from_input_handles_release_spec(self): |
| 147 | + """Test that from_input properly handles release specs.""" |
| 148 | + test_spec = "stable@latest" |
| 149 | + |
| 150 | + with patch.object(FixturesSource, "from_release_spec") as mock_from_release_spec: |
| 151 | + mock_from_release_spec.return_value = MagicMock() |
| 152 | + |
| 153 | + FixturesSource.from_input(test_spec) |
| 154 | + |
| 155 | + mock_from_release_spec.assert_called_once_with(test_spec) |
| 156 | + |
| 157 | + def test_from_input_handles_regular_url(self): |
| 158 | + """Test that from_input properly handles regular URLs.""" |
| 159 | + test_url = "http://example.com/fixtures.tar.gz" |
| 160 | + |
| 161 | + with patch.object(FixturesSource, "from_url") as mock_from_url: |
| 162 | + mock_from_url.return_value = MagicMock() |
| 163 | + |
| 164 | + FixturesSource.from_input(test_url) |
| 165 | + |
| 166 | + mock_from_url.assert_called_once_with(test_url) |
0 commit comments