diff --git a/beets/attachments.py b/beets/attachments.py index f13f7db16..13324e88e 100644 --- a/beets/attachments.py +++ b/beets/attachments.py @@ -24,6 +24,12 @@ from beets import dbcore class Attachment(dbcore.db.Model): + """Represents an attachment in the database. + + An attachment has four properties that correspond to fields in the + database: ``url``, ``type``, ``ref``, and ``ref_type``. Flexible + attributes are accessed as ``attachment[key]``. + """ _fields = { 'url': dbcore.types.String(), diff --git a/test/test_attachments.py b/test/test_attachments.py new file mode 100644 index 000000000..8305b42a3 --- /dev/null +++ b/test/test_attachments.py @@ -0,0 +1,53 @@ +# 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 _common import unittest + +from beets.attachments import AttachmentFactory +from beets.library import Library, Album + +class AttachmentFactoryTest(unittest.TestCase): + + def setUp(self): + self.lib = Library(':memory:') + self.factory = AttachmentFactory(self.lib) + + def test_create(self): + attachment = self.factory.create('/path/to/attachment', 'coverart') + self.assertEqual(attachment.url, '/path/to/attachment') + self.assertEqual(attachment.type, 'coverart') + + def test_create_sets_entity(self): + album = Album() + album.add(self.lib) + attachment = self.factory.create('/path/to/attachment', 'coverart', + entity=album) + self.assertEqual(attachment.ref, album.id) + self.assertEqual(attachment.ref_type, 'album') + + def test_create_populates_metadata(self): + def collector(type, url): + return {'mime': 'image/'} + self.factory.register_collector(collector) + + attachment = self.factory.create('/path/to/attachment', 'coverart') + self.assertEqual(attachment['mime'], 'image/') + + +def suite(): + return unittest.TestLoader().loadTestsFromName(__name__) + +if __name__ == '__main__': + unittest.main(defaultTest='suite')