Skip to content

Commit

Permalink
Add tests for non-mb source
Browse files Browse the repository at this point in the history
  • Loading branch information
kernitus committed Oct 29, 2023
1 parent cf5b1b6 commit 7f0035f
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
7 changes: 4 additions & 3 deletions beetsplug/oldestdate.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import mediafile
import musicbrainzngs
from beets import ui, config
from beets.autotag import hooks
from beets.autotag import hooks, TrackInfo
from beets.importer import action
from beets.library import Item
from beets.plugins import BeetsPlugin

from .date_wrapper import DateWrapper
Expand Down Expand Up @@ -127,7 +128,7 @@ def _import_trackinfo(self, info):
if 'track_id' in info:
self._fetch_recording(info.track_id)

def track_distance(self, session, info):
def track_distance(self, _, info: TrackInfo):
dist = hooks.Distance()
if info.data_source != 'MusicBrainz':
self._log.debug('Skipping track with non MusicBrainz data source {0.artist} - {0.title}', info)
Expand Down Expand Up @@ -187,7 +188,7 @@ def _on_import(self, session, task):
for item in task.imported_items():
self._process_file(item)

def _process_file(self, item):
def _process_file(self, item: Item):
if not item.mb_trackid or item.data_source != 'MusicBrainz':
self._log.info('Skipping track with no mb_trackid: {0.artist} - {0.title}', item)
return
Expand Down
60 changes: 60 additions & 0 deletions tests/test_oldestdate.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import unittest
from unittest import mock
from unittest.mock import patch, PropertyMock

from beets.plugins import BeetsPlugin

from beetsplug import oldestdate
from beetsplug.date_wrapper import DateWrapper
from beets.library import Item


class OldestDatePluginTest(unittest.TestCase):
Expand Down Expand Up @@ -245,6 +251,60 @@ def test_iterate_dates_both(self):
self.assertEqual(expected_date, result)
self.oldestdateplugin.config['approach'] = "releases"

# Test data_source not being Musicbrainz
def test_track_distance_skip_non_musicbrainz_source(self):
self.oldestdateplugin.config['filter_on_import'] = True
# Create a mock track info with a non-MusicBrainz source
mock_info = mock.Mock()
mock_info.data_source = "NonMusicBrainz"

# Make sure track does not have work id
with patch.object(self.oldestdateplugin, '_has_work_id', return_value=False):
dist = self.oldestdateplugin.track_distance(None, mock_info)

# Assert that the distance is zero, indicating that the track was skipped
self.assertEqual(0, dist.distance)

def test_track_distance_dont_skip_musicbrainz_source(self):
self.oldestdateplugin.config['filter_on_import'] = True
# Create a mock track info with a MusicBrainz source
mock_info = mock.Mock()
mock_info.data_source = "MusicBrainz"

# Make sure track does not have work id
with patch.object(self.oldestdateplugin, '_has_work_id', return_value=False):
dist = self.oldestdateplugin.track_distance(None, mock_info)

# Assert that the distance is not zero, indicating that the track was used
self.assertEqual(1, dist.distance)

@patch('logging.Logger.info')
def test_process_file_musicbrainz(self, mock_log):
# Create a mock item with a MusicBrainz track ID and source
self.oldestdateplugin.config['force'] = False
item = Item(mb_trackid="some_track_id", data_source="MusicBrainz", artist="Test Artist", title="Test Title",
recording_year="2022")

# Call _process_file method
self.oldestdateplugin._process_file(item)

# Assert that the log method was not called, indicating that the track was not skipped
mock_log.info.assert_not_called()

@patch('logging.Logger.info')
def test_process_file_non_musicbrainz(self, mock_log):
# Create a mock item with a non MusicBrainz track ID and source
self.oldestdateplugin.config['force'] = False
item = Item(mb_trackid="some_track_id", data_source="NonMusicBrainz", artist="Test Artist", title="Test Title")

# Call _process_file method
self.oldestdateplugin._process_file(item)

# Assert that the log method was called, indicating that the track was skipped
mock_log.assert_called_once_with(
'Skipping track with no mb_trackid: {0.artist} - {0.title}', item
)


if __name__ == '__main__':
unittest.main()

0 comments on commit 7f0035f

Please sign in to comment.