'Test various adding/deleting books methods'

def test_legacy_adding_books(self):

    from calibre.ebooks.metadata.book.base import Metadata
    from calibre.ptempfile import TemporaryFile
    (legacy, old) = (self.init_legacy(self.cloned_library), self.init_old(self.cloned_library))
    mi = Metadata(u'Added    Book0', authors=(u'Added    Author',))
    with TemporaryFile(suffix=u'.aff') as name:
        with open(name, u'wb') as f:
            f.write('xxx')
        T = partial(ET, u'add_books', ([name], [u'AFF'], [mi]), old=old, legacy=legacy)
        T()(self)
        book_id = T(kwargs={u'return_ids': True})(self)[1][0]
        self.assertEqual(legacy.new_api.formats(book_id), (u'AFF',))
        T(kwargs={u'add_duplicates': False})(self)
        mi.title = u'Added    Book1'
        mi.uuid = u'uuu'
        T = partial(ET, u'import_book', (mi, [name]), old=old, legacy=legacy)
        book_id = T()(self)
        self.assertNotEqual(legacy.uuid(book_id, index_is_id=True), old.uuid(book_id, index_is_id=True))
        book_id = T(kwargs={u'preserve_uuid': True})(self)
        self.assertEqual(legacy.uuid(book_id, index_is_id=True), old.uuid(book_id, index_is_id=True))
        self.assertEqual(legacy.new_api.formats(book_id), (u'AFF',))
        T = partial(ET, u'add_format', old=old, legacy=legacy)
        T((0, u'AFF', BytesIO('fffff')))(self)
        T((0, u'AFF', BytesIO('fffff')))(self)
        T((0, u'AFF', BytesIO('fffff')), {u'replace': True})(self)
    with TemporaryFile(suffix=u'.opf') as name:
        with open(name, u'wb') as f:
            f.write('zzzz')
        T = partial(ET, u'import_book', (mi, [name]), old=old, legacy=legacy)
        book_id = T()(self)
        self.assertFalse(legacy.new_api.formats(book_id))
    mi.title = u'Added    Book2'
    T = partial(ET, u'create_book_entry', (mi,), old=old, legacy=legacy)
    T()
    T({u'add_duplicates': False})
    T({u'force_id': 1000})
    with TemporaryFile(suffix=u'.txt') as name:
        with open(name, u'wb') as f:
            f.write('tttttt')
        bid = legacy.add_catalog(name, u'My    Catalog')
        self.assertEqual(old.add_catalog(name, u'My    Catalog'), bid)
        cache = legacy.new_api
        self.assertEqual(cache.formats(bid), (u'TXT',))
        self.assertEqual(cache.field_for(u'title', bid), u'My    Catalog')
        self.assertEqual(cache.field_for(u'authors', bid), (u'calibre',))
        self.assertEqual(cache.field_for(u'tags', bid), (_(u'Catalog'),))
        self.assertTrue((bid < legacy.add_catalog(name, u'Something    else')))
        self.assertEqual(legacy.add_catalog(name, u'My    Catalog'), bid)
        self.assertEqual(old.add_catalog(name, u'My    Catalog'), bid)
        bid = legacy.add_news(name, {u'title': u'Events', u'add_title_tag': True, u'custom_tags': (u'one', u'two')})
        self.assertEqual(cache.formats(bid), (u'TXT',))
        self.assertEqual(cache.field_for(u'authors', bid), (u'calibre',))
        self.assertEqual(cache.field_for(u'tags', bid), (_(u'News'), u'Events', u'one', u'two'))
    self.assertTrue(legacy.cover(1, index_is_id=True))
    origcov = legacy.cover(1, index_is_id=True)
    self.assertTrue(legacy.has_cover(1))
    legacy.remove_cover(1)
    self.assertFalse(legacy.has_cover(1))
    self.assertFalse(legacy.cover(1, index_is_id=True))
    legacy.set_cover(3, origcov)
    self.assertEqual(legacy.cover(3, index_is_id=True), origcov)
    self.assertTrue(legacy.has_cover(3))
    self.assertTrue(legacy.format(1, u'FMT1', index_is_id=True))
    legacy.remove_format(1, u'FMT1', index_is_id=True)
    self.assertIsNone(legacy.format(1, u'FMT1', index_is_id=True))
    legacy.delete_book(1)
    old.delete_book(1)
    self.assertNotIn(1, legacy.all_ids())
    legacy.dump_metadata((2, 3))
    old.close()
