diff --git a/test/test_mediafile.py b/test/test_mediafile.py index 32d93f815..b17702fa8 100644 --- a/test/test_mediafile.py +++ b/test/test_mediafile.py @@ -27,13 +27,9 @@ from six import assertCountEqual from test import _common from test._common import unittest -from beets.mediafile import MediaFile, MediaField, Image, \ - MP3DescStorageStyle, StorageStyle, MP4StorageStyle, \ - ASFStorageStyle, ImageType, CoverArtField, UnreadableFileError -from beets.library import Item -from beets.plugins import BeetsPlugin +from beets.mediafile import MediaFile, Image, \ + ImageType, CoverArtField, UnreadableFileError from beets.util import bytestring_path -import six class ArtTestMixin(object): @@ -298,80 +294,7 @@ class GenreListTestMixin(object): assertCountEqual(self, mediafile.genres, [u'the genre', u'another']) -field_extension = MediaField( - MP3DescStorageStyle(u'customtag'), - MP4StorageStyle('----:com.apple.iTunes:customtag'), - StorageStyle('customtag'), - ASFStorageStyle('customtag'), -) - - -class ExtendedFieldTestMixin(object): - - def test_extended_field_write(self): - plugin = BeetsPlugin() - plugin.add_media_field('customtag', field_extension) - - try: - mediafile = self._mediafile_fixture('empty') - mediafile.customtag = u'F#' - mediafile.save() - - mediafile = MediaFile(mediafile.path) - self.assertEqual(mediafile.customtag, u'F#') - - finally: - delattr(MediaFile, 'customtag') - Item._media_fields.remove('customtag') - - def test_write_extended_tag_from_item(self): - plugin = BeetsPlugin() - plugin.add_media_field('customtag', field_extension) - - try: - mediafile = self._mediafile_fixture('empty') - self.assertIsNone(mediafile.customtag) - - item = Item(path=mediafile.path, customtag=u'Gb') - item.write() - mediafile = MediaFile(mediafile.path) - self.assertEqual(mediafile.customtag, u'Gb') - - finally: - delattr(MediaFile, 'customtag') - Item._media_fields.remove('customtag') - - def test_read_flexible_attribute_from_file(self): - plugin = BeetsPlugin() - plugin.add_media_field('customtag', field_extension) - - try: - mediafile = self._mediafile_fixture('empty') - mediafile.update({'customtag': u'F#'}) - mediafile.save() - - item = Item.from_path(mediafile.path) - self.assertEqual(item['customtag'], u'F#') - - finally: - delattr(MediaFile, 'customtag') - Item._media_fields.remove('customtag') - - def test_invalid_descriptor(self): - with self.assertRaises(ValueError) as cm: - MediaFile.add_field('somekey', True) - self.assertIn(u'must be an instance of MediaField', - six.text_type(cm.exception)) - - def test_overwrite_property(self): - with self.assertRaises(ValueError) as cm: - MediaFile.add_field('artist', MediaField()) - self.assertIn(u'property "artist" already exists', - six.text_type(cm.exception)) - - -class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin, - ExtendedFieldTestMixin): +class ReadWriteTestBase(ArtTestMixin, GenreListTestMixin): """Test writing and reading tags. Subclasses must set ``extension`` and ``audio_properties``. """ diff --git a/test/test_plugin_mediafield.py b/test/test_plugin_mediafield.py new file mode 100644 index 000000000..d9d2b0a1f --- /dev/null +++ b/test/test_plugin_mediafield.py @@ -0,0 +1,107 @@ +# -*- coding: utf-8 -*- +# This file is part of beets. +# Copyright 2016, Adrian Sampson. +# +# 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. + +"""Tests the facility that lets plugins add custom field to MediaFile. +""" +from __future__ import division, absolute_import, print_function + +import os +import six +import shutil + +from test import _common +from beets.library import Item +from beets import mediafile +from beets.plugins import BeetsPlugin +from beets.util import bytestring_path + + +field_extension = mediafile.MediaField( + mediafile.MP3DescStorageStyle(u'customtag'), + mediafile.MP4StorageStyle('----:com.apple.iTunes:customtag'), + mediafile.StorageStyle('customtag'), + mediafile.ASFStorageStyle('customtag'), +) + + +class ExtendedFieldTestMixin(_common.TestCase): + + def _mediafile_fixture(self, name, extension='mp3'): + name = bytestring_path(name + '.' + extension) + src = os.path.join(_common.RSRC, name) + target = os.path.join(self.temp_dir, name) + shutil.copy(src, target) + return mediafile.MediaFile(target) + + def test_extended_field_write(self): + plugin = BeetsPlugin() + plugin.add_media_field('customtag', field_extension) + + try: + mf = self._mediafile_fixture('empty') + mf.customtag = u'F#' + mf.save() + + mf = mediafile.MediaFile(mf.path) + self.assertEqual(mf.customtag, u'F#') + + finally: + delattr(mediafile.MediaFile, 'customtag') + Item._media_fields.remove('customtag') + + def test_write_extended_tag_from_item(self): + plugin = BeetsPlugin() + plugin.add_media_field('customtag', field_extension) + + try: + mf = self._mediafile_fixture('empty') + self.assertIsNone(mf.customtag) + + item = Item(path=mf.path, customtag=u'Gb') + item.write() + mf = mediafile.MediaFile(mf.path) + self.assertEqual(mf.customtag, u'Gb') + + finally: + delattr(mediafile.MediaFile, 'customtag') + Item._media_fields.remove('customtag') + + def test_read_flexible_attribute_from_file(self): + plugin = BeetsPlugin() + plugin.add_media_field('customtag', field_extension) + + try: + mf = self._mediafile_fixture('empty') + mf.update({'customtag': u'F#'}) + mf.save() + + item = Item.from_path(mf.path) + self.assertEqual(item['customtag'], u'F#') + + finally: + delattr(mediafile.MediaFile, 'customtag') + Item._media_fields.remove('customtag') + + def test_invalid_descriptor(self): + with self.assertRaises(ValueError) as cm: + mediafile.MediaFile.add_field('somekey', True) + self.assertIn(u'must be an instance of MediaField', + six.text_type(cm.exception)) + + def test_overwrite_property(self): + with self.assertRaises(ValueError) as cm: + mediafile.MediaFile.add_field('artist', mediafile.MediaField()) + self.assertIn(u'property "artist" already exists', + six.text_type(cm.exception))