diff --git a/test/helper.py b/test/helper.py index f31f4545b..62801498b 100644 --- a/test/helper.py +++ b/test/helper.py @@ -12,6 +12,24 @@ # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. +"""This module includes various helpers that provide fixtures, capture +information or mock the environment. + +- The `control_stdin` and `capture_output` context managers allow one to + interact with the user interface. + +- `has_program` checks the presence of a command on the system. + +- The `generate_album_info` and `generate_track_info` functions return + fixtures to be used when mocking the autotagger. + +- The `TestImportSession` allows one to run importer code while + controlling the interactions through code. + +- The `TestHelper` class encapsulates various fixtures that can be set up. +""" + + import sys import os import os.path @@ -27,6 +45,7 @@ from beets import config import beets.plugins from beets.library import Library, Item from beets import importer +from beets.autotag.hooks import AlbumInfo, TrackInfo # TODO Move AutotagMock here import _common @@ -139,6 +158,7 @@ class TestHelper(object): # FIXME somehow close all open fd to the ilbrary self.remove_temp_dir() self.config.clear() + beets.config.read(user=False, defaults=True) def load_plugins(self, *plugins): """Load and initialize plugins by names. @@ -277,3 +297,52 @@ class TestImportSession(importer.ImportSession): return choice choose_item = choose_match + + +def generate_album_info(album_id, track_ids): + """Return `AlbumInfo` populated with mock data. + + Sets the album info's `album_id` field is set to the corresponding + argument. For each value in `track_ids` the `TrackInfo` from + `generate_track_info` is added to the album info's `tracks` field. + Most other fields of the album and track info are set to "album + info" and "track info", respectively. + """ + tracks = [generate_track_info(id) for id in track_ids] + album = AlbumInfo( + album_id='album info', + album='album info', + artist='album info', + artist_id='album info', + tracks=tracks, + ) + for field in ALBUM_INFO_FIELDS: + setattr(album, field, 'album info') + + return album + +ALBUM_INFO_FIELDS = ['album', 'album_id', 'artist', 'artist_id', + 'asin', 'albumtype', 'va','label', + 'artist_sort', 'releasegroup_id', 'catalognum', + 'language', 'country', 'albumstatus', 'media', + 'albumdisambig', 'artist_credit', + 'data_source', 'data_url'] + + +def generate_track_info(track_id='track info'): + """Return `TrackInfo` populated with mock data. + + The `track_id` field is set to the corresponding argument. All other + string fields are set to "track info". + """ + track = TrackInfo( + title='track info', + track_id=track_id, + ) + for field in TRACK_INFO_FIELDS: + setattr(track, field, 'track info') + return track + +TRACK_INFO_FIELDS = ['artist', 'artist_id', 'artist_sort', + 'disctitle', 'artist_credit', 'data_source', + 'data_url'] diff --git a/test/test_mbsync.py b/test/test_mbsync.py new file mode 100644 index 000000000..ed8d98014 --- /dev/null +++ b/test/test_mbsync.py @@ -0,0 +1,88 @@ +# This file is part of beets. +# Copyright 2014, Thomas Scholtes. +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. + +from mock import patch + +from _common import unittest +from helper import TestHelper,\ + generate_album_info, \ + generate_track_info + +from beets.library import Item + +class MbsyncCliTest(unittest.TestCase, TestHelper): + + def setUp(self): + self.setup_beets() + self.load_plugins('mbsync') + + def tearDown(self): + self.unload_plugins() + self.teardown_beets() + + @patch('beets.autotag.hooks.album_for_mbid') + def test_update_album(self, album_for_mbid): + album_for_mbid.return_value = \ + generate_album_info('album id', ['track id']) + + self.lib.add_album([ + Item( + artist='old artist', + album='old album', + title='old title', + mb_albumid='album id', + mb_trackid='track id', + path='' + ), + ]) + + item = self.lib.items().get() + album = self.lib.albums().get() + + self.assertNotEqual(item.title, 'track info') + self.assertNotEqual(item.artist, 'track info') + self.assertNotEqual(album.album, 'album info') + + self.run_command('mbsync') + + item.load() + album.load() + self.assertEqual(item.title, 'track info') + self.assertEqual(item.artist, 'track info') + self.assertEqual(album.album, 'album info') + + @patch('beets.autotag.hooks.track_for_mbid') + def test_update_singleton(self, track_for_mbid): + track_for_mbid.return_value = generate_track_info('track id') + + item = Item( + artist='old artist', + album='old album', + title='old title', + mb_trackid='track id', + path='', + ) + self.lib.add(item) + + self.assertNotEqual(item.title, 'track info') + self.run_command('mbsync') + item.load() + self.assertEqual(item.title, 'track info') + + +def suite(): + return unittest.TestLoader().loadTestsFromName(__name__) + +if __name__ == '__main__': + unittest.main(defaultTest='suite')