'Test the creation of new book entries'

def test_create_book_entry(self):

    from calibre.ebooks.metadata.book.base import Metadata
    cache = self.init_cache()
    mi = Metadata(u'Created    One', authors=(u'Creator    One', u'Creator    Two'))
    book_id = cache.create_book_entry(mi)
    self.assertIsNot(book_id, None)
    def do_test(cache, book_id):
        for field in (u'path', u'uuid', u'author_sort', u'timestamp', u'pubdate', u'title', u'authors', u'series_index', u'sort'):
            self.assertTrue(cache.field_for(field, book_id))
        for field in (u'size', u'cover'):
            self.assertFalse(cache.field_for(field, book_id))
        self.assertEqual(book_id, cache.fields[u'uuid'].table.uuid_to_id_map[cache.field_for(u'uuid', book_id)])
        self.assertLess((now() - cache.field_for(u'timestamp', book_id)), timedelta(seconds=30))
        self.assertEqual((u'Created    One', (u'Creator    One', u'Creator    Two')), (cache.field_for(u'title', book_id), cache.field_for(u'authors', book_id)))
        self.assertEqual(cache.field_for(u'series_index', book_id), 1.0)
        self.assertEqual(cache.field_for(u'pubdate', book_id), UNDEFINED_DATE)
    do_test(cache, book_id)
    cache = self.init_cache()
    do_test(cache, book_id)
    self.assertIs(None, cache.create_book_entry(mi, add_duplicates=False), u'Duplicate    added    incorrectly')
    book_id = cache.create_book_entry(mi, cover=IMG)
    self.assertIsNot(book_id, None)
    self.assertEqual(IMG, cache.cover(book_id))
    import calibre.db.cache as c
    orig = c.prefs
    c.prefs = {u'new_book_tags': (u'newbook', u'newbook2')}
    try:
        book_id = cache.create_book_entry(mi)
        self.assertEqual((u'newbook', u'newbook2'), cache.field_for(u'tags', book_id))
        mi.tags = (u'one', u'two')
        book_id = cache.create_book_entry(mi)
        self.assertEqual(((u'one', u'two') + (u'newbook', u'newbook2')), cache.field_for(u'tags', book_id))
        mi.tags = ()
    finally:
        c.prefs = orig
    mi.uuid = u'a    preserved    uuid'
    book_id = cache.create_book_entry(mi, preserve_uuid=True)
    self.assertEqual(mi.uuid, cache.field_for(u'uuid', book_id))
