'Test setting of metadata'

def test_set_metadata(self):

    ae = self.assertEqual
    cache = self.init_cache(self.cloned_library)
    mi = cache.get_metadata(1)
    old_path = cache.field_for(u'path', 1)
    (old_title, old_author) = (mi.title, mi.authors[0])
    ae(old_path, (u'%s/%s    (1)' % (old_author, old_title)))
    (mi.title, mi.authors) = (u'New    Title', [u'New    Author'])
    cache.set_metadata(1, mi)
    ae(cache.field_for(u'path', 1), (u'%s/%s    (1)' % (mi.authors[0], mi.title)))
    p = cache.format_abspath(1, u'FMT1')
    self.assertTrue(((mi.authors[0] in p) and (mi.title in p)))
    db = self.init_old(self.cloned_library)
    mi = db.get_metadata(1, index_is_id=True, get_cover=True, cover_as_data=True)
    mi2 = db.get_metadata(3, index_is_id=True, get_cover=True, cover_as_data=True)
    db.set_metadata(2, mi)
    db.set_metadata(1, mi2, force_changes=True)
    oldmi = db.get_metadata(2, index_is_id=True, get_cover=True, cover_as_data=True)
    oldmi2 = db.get_metadata(1, index_is_id=True, get_cover=True, cover_as_data=True)
    db.close()
    del db
    cache = self.init_cache(self.cloned_library)
    cache.set_metadata(2, mi)
    nmi = cache.get_metadata(2, get_cover=True, cover_as_data=True)
    ae(oldmi.cover_data, nmi.cover_data)
    self.compare_metadata(nmi, oldmi, exclude={u'last_modified', u'format_metadata'})
    cache.set_metadata(1, mi2, force_changes=True)
    nmi2 = cache.get_metadata(1, get_cover=True, cover_as_data=True)
    ae(nmi2.get_extra(u'#series'), 1.0)
    self.compare_metadata(nmi2, oldmi2, exclude={u'last_modified', u'format_metadata', u'#series_index'})
    cache = self.init_cache(self.cloned_library)
    mi = cache.get_metadata(1)
    otags = mi.tags
    mi.tags = [x.upper() for x in mi.tags]
    cache.set_metadata(3, mi)
    self.assertEqual(set(otags), set(cache.field_for(u'tags', 3)), u'case    changes    should    not    be    allowed    in    set_metadata')
