def test_remove_books(self): # {{{ 'Test removal of books' cl = self.cloned_library cache = self.init_cache() af, ae = self.assertFalse, self.assertEqual authors = cache.fields['authors'].table # Delete a single book, with no formats and check cleaning self.assertIn('Unknown', set(itervalues(authors.id_map))) olen = len(authors.id_map) item_id = {v:k for k, v in iteritems(authors.id_map)}['Unknown'] cache.remove_books((3,)) for c in (cache, self.init_cache()): table = c.fields['authors'].table self.assertNotIn(3, c.all_book_ids()) self.assertNotIn('Unknown', set(itervalues(table.id_map))) self.assertNotIn(item_id, table.asort_map) self.assertNotIn(item_id, table.alink_map) ae(len(table.id_map), olen-1) # Check that files are removed fmtpath = cache.format_abspath(1, 'FMT1') bookpath = os.path.dirname(fmtpath) authorpath = os.path.dirname(bookpath) os.mkdir(os.path.join(authorpath, '.DS_Store')) open(os.path.join(authorpath, 'Thumbs.db'), 'wb').close() item_id = {v:k for k, v in iteritems(cache.fields['#series'].table.id_map)}['My Series Two'] cache.remove_books((1,), permanent=True) for x in (fmtpath, bookpath, authorpath): af(os.path.exists(x), 'The file %s exists, when it should not' % x) for c in (cache, self.init_cache()): table = c.fields['authors'].table self.assertNotIn(1, c.all_book_ids()) self.assertNotIn('Author Two', set(itervalues(table.id_map))) self.assertNotIn(6, set(itervalues(c.fields['rating'].table.id_map))) self.assertIn('A Series One', set(itervalues(c.fields['series'].table.id_map))) self.assertNotIn('My Series Two', set(itervalues(c.fields['#series'].table.id_map))) self.assertNotIn(item_id, c.fields['#series'].table.col_book_map) self.assertNotIn(1, c.fields['#series'].table.book_col_map) # Test emptying the db cache.remove_books(cache.all_book_ids(), permanent=True) for f in ('authors', 'series', '#series', 'tags'): table = cache.fields[f].table self.assertFalse(table.id_map) self.assertFalse(table.book_col_map) self.assertFalse(table.col_book_map) # Test the delete service from calibre.db.delete_service import delete_service cache = self.init_cache(cl) # Check that files are removed fmtpath = cache.format_abspath(1, 'FMT1') bookpath = os.path.dirname(fmtpath) authorpath = os.path.dirname(bookpath) item_id = {v:k for k, v in iteritems(cache.fields['#series'].table.id_map)}['My Series Two'] cache.remove_books((1,)) delete_service().wait() for x in (fmtpath, bookpath, authorpath): af(os.path.exists(x), 'The file %s exists, when it should not' % x)
def implementation(db, notify_changes, book_id, fmt): is_remote = notify_changes is not None fmt_map = {book_id: (fmt, )} db.remove_formats(fmt_map) delete_service().wait() if is_remote: notify_changes(formats_removed(fmt_map))
def test_remove_books(self): # {{{ "Test removal of books" cl = self.cloned_library cache = self.init_cache() af, ae, at = self.assertFalse, self.assertEqual, self.assertTrue authors = cache.fields["authors"].table # Delete a single book, with no formats and check cleaning self.assertIn(_("Unknown"), set(authors.id_map.itervalues())) olen = len(authors.id_map) item_id = {v: k for k, v in authors.id_map.iteritems()}[_("Unknown")] cache.remove_books((3,)) for c in (cache, self.init_cache()): table = c.fields["authors"].table self.assertNotIn(3, c.all_book_ids()) self.assertNotIn(_("Unknown"), set(table.id_map.itervalues())) self.assertNotIn(item_id, table.asort_map) self.assertNotIn(item_id, table.alink_map) ae(len(table.id_map), olen - 1) # Check that files are removed fmtpath = cache.format_abspath(1, "FMT1") bookpath = os.path.dirname(fmtpath) authorpath = os.path.dirname(bookpath) item_id = {v: k for k, v in cache.fields["#series"].table.id_map.iteritems()}["My Series Two"] cache.remove_books((1,), permanent=True) for x in (fmtpath, bookpath, authorpath): af(os.path.exists(x), "The file %s exists, when it should not" % x) for c in (cache, self.init_cache()): table = c.fields["authors"].table self.assertNotIn(1, c.all_book_ids()) self.assertNotIn("Author Two", set(table.id_map.itervalues())) self.assertNotIn(6, set(c.fields["rating"].table.id_map.itervalues())) self.assertIn("A Series One", set(c.fields["series"].table.id_map.itervalues())) self.assertNotIn("My Series Two", set(c.fields["#series"].table.id_map.itervalues())) self.assertNotIn(item_id, c.fields["#series"].table.col_book_map) self.assertNotIn(1, c.fields["#series"].table.book_col_map) # Test emptying the db cache.remove_books(cache.all_book_ids(), permanent=True) for f in ("authors", "series", "#series", "tags"): table = cache.fields[f].table self.assertFalse(table.id_map) self.assertFalse(table.book_col_map) self.assertFalse(table.col_book_map) # Test the delete service from calibre.db.delete_service import delete_service cache = self.init_cache(cl) # Check that files are removed fmtpath = cache.format_abspath(1, "FMT1") bookpath = os.path.dirname(fmtpath) authorpath = os.path.dirname(bookpath) item_id = {v: k for k, v in cache.fields["#series"].table.id_map.iteritems()}["My Series Two"] cache.remove_books((1,)) delete_service().wait() for x in (fmtpath, bookpath, authorpath): af(os.path.exists(x), "The file %s exists, when it should not" % x)
def implementation(db, notify_changes, ids, permanent): db.remove_books(ids, permanent=permanent) if not permanent: delete_service().wait() if notify_changes is not None: notify_changes(books_deleted(ids))