'Test removal of books'

def test_remove_books(self):

    cl = self.cloned_library
    cache = self.init_cache()
    (af, ae) = (self.assertFalse, self.assertEqual)
    authors = cache.fields[u'authors'].table
    self.assertIn(_(u'Unknown'), set(authors.id_map.itervalues()))
    olen = len(authors.id_map)
    item_id = {v: k for (k, v) in authors.id_map.iteritems()}[_(u'Unknown')]
    cache.remove_books((3,))
    for c in (cache, self.init_cache()):
        table = c.fields[u'authors'].table
        self.assertNotIn(3, c.all_book_ids())
        self.assertNotIn(_(u'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))
    fmtpath = cache.format_abspath(1, u'FMT1')
    bookpath = os.path.dirname(fmtpath)
    authorpath = os.path.dirname(bookpath)
    os.mkdir(os.path.join(authorpath, u'.DS_Store'))
    open(os.path.join(authorpath, u'Thumbs.db'), u'wb').close()
    item_id = {v: k for (k, v) in cache.fields[u'#series'].table.id_map.iteritems()}[u'My    Series    Two']
    cache.remove_books((1,), permanent=True)
    for x in (fmtpath, bookpath, authorpath):
        af(os.path.exists(x), (u'The    file    %s    exists,    when    it    should    not' % x))
    for c in (cache, self.init_cache()):
        table = c.fields[u'authors'].table
        self.assertNotIn(1, c.all_book_ids())
        self.assertNotIn(u'Author    Two', set(table.id_map.itervalues()))
        self.assertNotIn(6, set(c.fields[u'rating'].table.id_map.itervalues()))
        self.assertIn(u'A    Series    One', set(c.fields[u'series'].table.id_map.itervalues()))
        self.assertNotIn(u'My    Series    Two', set(c.fields[u'#series'].table.id_map.itervalues()))
        self.assertNotIn(item_id, c.fields[u'#series'].table.col_book_map)
        self.assertNotIn(1, c.fields[u'#series'].table.book_col_map)
    cache.remove_books(cache.all_book_ids(), permanent=True)
    for f in (u'authors', u'series', u'#series', u'tags'):
        table = cache.fields[f].table
        self.assertFalse(table.id_map)
        self.assertFalse(table.book_col_map)
        self.assertFalse(table.col_book_map)
    from calibre.db.delete_service import delete_service
    cache = self.init_cache(cl)
    fmtpath = cache.format_abspath(1, u'FMT1')
    bookpath = os.path.dirname(fmtpath)
    authorpath = os.path.dirname(bookpath)
    item_id = {v: k for (k, v) in cache.fields[u'#series'].table.id_map.iteritems()}[u'My    Series    Two']
    cache.remove_books((1,))
    delete_service().wait()
    for x in (fmtpath, bookpath, authorpath):
        af(os.path.exists(x), (u'The    file    %s    exists,    when    it    should    not' % x))
