|
7 | 7 |
|
8 | 8 | import pytest |
9 | 9 |
|
| 10 | +from beets.library.models import Item |
10 | 11 | from beets.test.helper import PluginTestCase |
11 | 12 | from beetsplug.tidal import TidalPlugin |
12 | 13 |
|
@@ -397,6 +398,127 @@ def test_albums_for_ids_with_missing(self): |
397 | 398 | assert results[1] is None |
398 | 399 |
|
399 | 400 |
|
| 401 | +class TestCandidates(TidalPluginTest): |
| 402 | + """Tests for candidates method.""" |
| 403 | + |
| 404 | + def test_candidates_with_barcode(self): |
| 405 | + """Test that candidates uses barcode lookup first.""" |
| 406 | + track = _make_track("t1", "Album Track", "PT3M", "ISRC001", ["a1"]) |
| 407 | + album, track_lookup, artist_lookup = _make_album( |
| 408 | + "al1", "Barcode Album", [track], ["a1"] |
| 409 | + ) |
| 410 | + |
| 411 | + self.tidal.api.get_albums = Mock( |
| 412 | + return_value={ |
| 413 | + "data": [album], |
| 414 | + "included": [*artist_lookup.values(), *track_lookup.values()], |
| 415 | + } |
| 416 | + ) |
| 417 | + |
| 418 | + items = [Item(barcode="123456")] |
| 419 | + |
| 420 | + candidates = list( |
| 421 | + self.tidal.candidates(items, "Artist", "Album", False) |
| 422 | + ) |
| 423 | + |
| 424 | + self.tidal.api.get_albums.assert_called_once() |
| 425 | + assert len(candidates) == 1 |
| 426 | + assert candidates[0].album == "Barcode Album" |
| 427 | + |
| 428 | + def test_candidates_with_query_fallback(self): |
| 429 | + """Test that candidates falls back to query search when no barcode.""" |
| 430 | + items = [Item(title="My Song", artist="My Artist", album="My Album")] |
| 431 | + |
| 432 | + # Mock search returning album IDs |
| 433 | + self.tidal.api.search_results = Mock( |
| 434 | + return_value={ |
| 435 | + "data": { |
| 436 | + "relationships": { |
| 437 | + "albums": { |
| 438 | + "data": [{"id": "al1", "type": "albums"}], |
| 439 | + }, |
| 440 | + }, |
| 441 | + }, |
| 442 | + } |
| 443 | + ) |
| 444 | + |
| 445 | + # Mock album lookup by ID |
| 446 | + track = _make_track("t1", "Album Track", "PT3M", "ISRC001", ["a1"]) |
| 447 | + album, track_lookup, artist_lookup = _make_album( |
| 448 | + "al1", "Query Album", [track], ["a1"] |
| 449 | + ) |
| 450 | + self.tidal.api.get_albums = Mock( |
| 451 | + return_value={ |
| 452 | + "data": [album], |
| 453 | + "included": [*artist_lookup.values(), *track_lookup.values()], |
| 454 | + } |
| 455 | + ) |
| 456 | + |
| 457 | + candidates = list( |
| 458 | + self.tidal.candidates(items, "My Artist", "My Album", False) |
| 459 | + ) |
| 460 | + |
| 461 | + # Should have called search_results |
| 462 | + assert self.tidal.api.search_results.called |
| 463 | + assert len(candidates) == 1 |
| 464 | + assert candidates[0].album == "Query Album" |
| 465 | + |
| 466 | + |
| 467 | +class TestItemCandidates(TidalPluginTest): |
| 468 | + """Tests for item_candidates method.""" |
| 469 | + |
| 470 | + def test_item_candidates_with_isrc(self): |
| 471 | + """Test that item_candidates uses ISRC lookup first.""" |
| 472 | + track = _make_track( |
| 473 | + "490839595", "ISRC Track", "PT3M", "ISRC001", ["a1"] |
| 474 | + ) |
| 475 | + artist = _make_artist("a1", "ISRC Artist") |
| 476 | + |
| 477 | + self.tidal.api.get_tracks = Mock( |
| 478 | + return_value={ |
| 479 | + "data": [track], |
| 480 | + "included": [artist], |
| 481 | + } |
| 482 | + ) |
| 483 | + |
| 484 | + item = Item(isrc="ISRC001") |
| 485 | + |
| 486 | + results = list(self.tidal.item_candidates(item, "Artist", "Title")) |
| 487 | + |
| 488 | + self.tidal.api.get_tracks.assert_called_once() |
| 489 | + assert len(results) == 1 |
| 490 | + assert results[0].title == "ISRC Track" |
| 491 | + |
| 492 | + def test_item_candidates_with_query_fallback(self): |
| 493 | + """Test that item_candidates falls back to query search when no ISRC.""" |
| 494 | + item = Item(title="Query Song", artist="Query Artist") |
| 495 | + |
| 496 | + self.tidal.api.search_results = Mock( |
| 497 | + return_value={ |
| 498 | + "data": { |
| 499 | + "relationships": { |
| 500 | + "tracks": { |
| 501 | + "data": [{"id": "490839595", "type": "tracks"}], |
| 502 | + }, |
| 503 | + }, |
| 504 | + }, |
| 505 | + "included": [ |
| 506 | + _make_track( |
| 507 | + "490839595", "Query Track", "PT3M", "ISRC002", ["a1"] |
| 508 | + ), |
| 509 | + _make_artist("a1", "Query Artist"), |
| 510 | + ], |
| 511 | + } |
| 512 | + ) |
| 513 | + |
| 514 | + results = list( |
| 515 | + self.tidal.item_candidates(item, "Query Artist", "Query Song") |
| 516 | + ) |
| 517 | + |
| 518 | + assert self.tidal.api.search_results.called |
| 519 | + assert results[0].title == "Query Track" |
| 520 | + |
| 521 | + |
400 | 522 | class TestStaticHelpers: |
401 | 523 | """Tests for static helper methods.""" |
402 | 524 |
|
|
0 commit comments