'Test the moving of files when title/author change'

def test_metadata_move(self):

    cl = self.cloned_library
    cache = self.init_cache(cl)
    (ae, af, sf) = (self.assertEqual, self.assertFalse, cache.set_field)
    ae(sf(u'title', {3: u'moved1'}), set([3]))
    ae(sf(u'authors', {3: u'moved1'}), set([3]))
    ae(sf(u'title', {3: u'Moved1'}), set([3]))
    ae(sf(u'authors', {3: u'Moved1'}), set([3]))
    ae(cache.field_for(u'title', 3), u'Moved1')
    ae(cache.field_for(u'authors', 3), (u'Moved1',))
    orig_data = self.get_filesystem_data(cache, 1)
    orig_fpath = cache.format_abspath(1, u'FMT1')
    ae(sf(u'title', {1: u'moved'}), set([1]))
    ae(sf(u'authors', {1: u'moved'}), set([1]))
    ae(sf(u'title', {1: u'Moved'}), set([1]))
    ae(sf(u'authors', {1: u'Moved'}), set([1]))
    ae(cache.field_for(u'title', 1), u'Moved')
    ae(cache.field_for(u'authors', 1), (u'Moved',))
    cache2 = self.init_cache(cl)
    for c in (cache, cache2):
        data = self.get_filesystem_data(c, 1)
        ae(set(orig_data.iterkeys()), set(data.iterkeys()))
        ae(orig_data, data, u'Filesystem    data    does    not    match')
        ae(c.field_for(u'path', 1), u'Moved/Moved    (1)')
        ae(c.field_for(u'path', 3), u'Moved1/Moved1    (3)')
    fpath = c.format_abspath(1, u'FMT1').replace(os.sep, u'/').split(u'/')
    ae(fpath[(-3):], [u'Moved', u'Moved    (1)', u'Moved    -    Moved.fmt1'])
    af(os.path.exists(os.path.dirname(orig_fpath)), u'Original    book    folder    still    exists')
    for x in range(1, 4):
        base = os.sep.join(fpath[:(- x)])
        part = fpath[(- x):][0]
        self.assertIn(part, os.listdir(base))
