diff --git a/beets/library.py b/beets/library.py index c234b9ff8..3a120b720 100644 --- a/beets/library.py +++ b/beets/library.py @@ -311,10 +311,13 @@ class Item(object): self.remove() @classmethod - def from_path(cls, path): - """Creates a new item from the media file at the specified path.""" + def from_path(cls, path, library=None): + """Creates a new item from the media file at the specified path. Sets + the item's library (but does not add the item) if library is + specified.""" i = cls({}) i.read(path) + i.library = library return i @@ -562,10 +565,10 @@ class Library(object): for f in _walk_files(path): try: - i = Item.from_path(_normpath(f)) + i = Item.from_path(_normpath(f), self) if copy: i.move(copy=True) - i.add(self) + i.add() except FileTypeError: _log(f + ' of unknown type, skipping') diff --git a/test/test_db.py b/test/test_db.py index 9af6c0dc8..07f5eeed9 100755 --- a/test/test_db.py +++ b/test/test_db.py @@ -78,16 +78,23 @@ class StoreTest(unittest.TestCase): class AddTest(unittest.TestCase): def setUp(self): - self.lib = lib() + self.lib = beets.library.Library(':memory:') self.i = item(self.lib) def tearDown(self): self.lib.conn.close() - def test_add_inserts_row(self): + def test_item_add_inserts_row(self): self.i.add() new_grouping = self.lib.conn.execute('select grouping from items ' 'where composer="the composer"').fetchone()['grouping'] self.assertEqual(new_grouping, self.i.grouping) + + def test_library_add_inserts_row(self): + self.lib.add(os.path.join('rsrc', 'full.mp3')) + new_grouping = self.lib.conn.execute('select grouping from items ' + 'where composer="the composer"').fetchone()['grouping'] + self.assertEqual(new_grouping, self.i.grouping) + class RemoveTest(unittest.TestCase): def setUp(self): diff --git a/test/test_files.py b/test/test_files.py index 07b8bf291..46e7716f9 100755 --- a/test/test_files.py +++ b/test/test_files.py @@ -123,7 +123,21 @@ class WalkTest(unittest.TestCase): self.assertTrue(f in s) s.remove(f) self.assertTrue(not s) - + +class AddTest(unittest.TestCase): + def setUp(self): + self.dir = os.path.join('rsrc', 'test_lib') + self.lib = beets.library.Library(':memory:') + self.lib.options['directory'] = self.dir + self.lib.options['path_format'] = 'item' + def tearDown(self): + if os.path.exists(self.dir): + shutil.rmtree(self.dir) + + def test_library_add_copies(self): + self.lib.add(os.path.join('rsrc', 'full.mp3'), copy=True) + self.assertTrue(os.path.isfile(os.path.join(self.dir, 'item'))) + def suite(): return unittest.TestLoader().loadTestsFromName(__name__)