def create_mediafile_fixture(self, ext='mp3', images=[]): """Copies a fixture mediafile with the extension to a temporary location and returns the path. It keeps track of the created locations and will delete the with `remove_mediafile_fixtures()` `images` is a subset of 'png', 'jpg', and 'tiff'. For each specified extension a cover art image is added to the media file. """ src = os.path.join(_common.RSRC, util.bytestring_path('full.' + ext)) handle, path = mkstemp() os.close(handle) shutil.copyfile(src, path) if images: mediafile = MediaFile(path) imgs = [] for img_ext in images: file = util.bytestring_path('image-2x3.{0}'.format(img_ext)) img_path = os.path.join(_common.RSRC, file) with open(img_path, 'rb') as f: imgs.append(Image(f.read())) mediafile.images = imgs mediafile.save() if not hasattr(self, '_mediafile_fixtures'): self._mediafile_fixtures = [] self._mediafile_fixtures.append(path) return path
def test_set_image_structure(self): mediafile = self._mediafile_fixture('empty') image = Image(data=self.png_data, desc='album cover', type=ImageType.front) mediafile.images = [image] mediafile.save() mediafile = MediaFile(mediafile.path) self.assertEqual(len(mediafile.images), 1) image = mediafile.images[0] self.assertEqual(image.data, self.png_data) self.assertEqual(image.mime_type, 'image/png') self.assertExtendedImageAttributes(image, desc='album cover', type=ImageType.front)
def test_add_image_structure(self): mediafile = self._mediafile_fixture('image') self.assertEqual(len(mediafile.images), 2) image = Image(data=self.png_data, desc='the composer', type=ImageType.composer) mediafile.images += [image] mediafile.save() mediafile = MediaFile(mediafile.path) self.assertEqual(len(mediafile.images), 3) image = next( (i for i in mediafile.images if i.desc == 'the composer'), None) self.assertExtendedImageAttributes(image, desc='the composer', type=ImageType.composer)
def test_add_tiff_image(self): mediafile = self._mediafile_fixture('image') self.assertEqual(len(mediafile.images), 2) image = Image(data=self.tiff_data, desc='the composer', type=ImageType.composer) mediafile.images += [image] mediafile.save() mediafile = MediaFile(mediafile.path) self.assertEqual(len(mediafile.images), 3) # WMA does not preserve the order, so we have to work around this image = filter(lambda i: i.mime_type == 'image/tiff', mediafile.images)[0] self.assertExtendedImageAttributes(image, desc='the composer', type=ImageType.composer)
def test_add_image_structure(self): mediafile = self._mediafile_fixture('image') self.assertEqual(len(mediafile.images), 2) image = Image(data=self.png_data, desc='the composer', type=Image.TYPES.composer) mediafile.images += [image] mediafile.save() mediafile = MediaFile(mediafile.path) self.assertEqual(len(mediafile.images), 3) # WMA does not preserve the order, so we have to work around this try: image = filter(lambda i: i.desc == 'the composer', mediafile.images)[0] except IndexError: image = None self.assertExtendedImageAttributes(image, desc='the composer', type=Image.TYPES.composer)
def test_add_tiff_image_fails(self): mediafile = self._mediafile_fixture('empty') with self.assertRaises(ValueError): mediafile.images = [Image(data=self.tiff_data)]