|
15 | 15 | """Tests for MusicBrainz API wrapper.""" |
16 | 16 |
|
17 | 17 | import unittest |
| 18 | +import uuid |
18 | 19 | from typing import ClassVar |
19 | 20 | from unittest import mock |
20 | 21 |
|
21 | 22 | import pytest |
| 23 | +import requests |
22 | 24 |
|
23 | 25 | from beets import config |
24 | 26 | from beets.library import Item |
@@ -1106,3 +1108,32 @@ def test_candidates(self, monkeypatch, mb): |
1106 | 1108 | assert len(candidates) == 1 |
1107 | 1109 | assert candidates[0].tracks[0].track_id == self.RECORDING["id"] |
1108 | 1110 | assert candidates[0].album == "hi" |
| 1111 | + |
| 1112 | + def test_import_handles_404_gracefully(self, mb, requests_mock): |
| 1113 | + id_ = uuid.uuid4() |
| 1114 | + response = requests.Response() |
| 1115 | + response.status_code = 404 |
| 1116 | + requests_mock.get( |
| 1117 | + f"/ws/2/release/{id_}", |
| 1118 | + exc=requests.exceptions.HTTPError(response=response), |
| 1119 | + ) |
| 1120 | + res = mb.album_for_id(str(id_)) |
| 1121 | + assert res is None |
| 1122 | + |
| 1123 | + def test_import_propagates_non_404_errors(self, mb): |
| 1124 | + class DummyResponse: |
| 1125 | + status_code = 500 |
| 1126 | + |
| 1127 | + error = requests.exceptions.HTTPError(response=DummyResponse()) |
| 1128 | + |
| 1129 | + def raise_error(*args, **kwargs): |
| 1130 | + raise error |
| 1131 | + |
| 1132 | + # Simulate mb.mb_api.get_release raising a non-404 HTTP error |
| 1133 | + mb.mb_api.get_release = raise_error |
| 1134 | + |
| 1135 | + with pytest.raises(requests.exceptions.HTTPError) as excinfo: |
| 1136 | + mb.album_for_id(str(uuid.uuid4())) |
| 1137 | + |
| 1138 | + # Ensure the exact error is propagated, not swallowed |
| 1139 | + assert excinfo.value is error |
0 commit comments