'Test renaming of many-(many,one) items'

def test_rename_items(self):

    cl = self.cloned_library
    cache = self.init_cache(cl)
    a = {v: k for (k, v) in cache.get_id_map(u'authors').iteritems()}[u'Unknown']
    self.assertEqual(cache.rename_items(u'authors', {a: u'New    Author'})[0], {3})
    a = {v: k for (k, v) in cache.get_id_map(u'authors').iteritems()}[u'Author    One']
    self.assertEqual(cache.rename_items(u'authors', {a: u'Author    Two'})[0], {1, 2})
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'authors'), {u'New    Author', u'Author    Two'})
        self.assertEqual(c.field_for(u'author_sort', 3), u'Author,    New')
        self.assertIn(u'New    Author/', c.field_for(u'path', 3))
        self.assertEqual(c.field_for(u'authors', 1), (u'Author    Two',))
        self.assertEqual(c.field_for(u'author_sort', 1), u'Two,    Author')
    t = {v: k for (k, v) in cache.get_id_map(u'tags').iteritems()}[u'Tag    One']
    self.assertEqual(cache.rename_items(u'tags', {t: u'tag    one'}), ({1, 2}, {t: t}))
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'tags'), {u'tag    one', u'Tag    Two', u'News'})
        self.assertEqual(set(c.field_for(u'tags', 1)), {u'tag    one', u'News'})
        self.assertEqual(set(c.field_for(u'tags', 2)), {u'tag    one', u'Tag    Two'})
    self.assertEqual(cache.rename_items(u'tags', {t: u't1'})[0], {1, 2})
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'tags'), {u't1', u'Tag    Two', u'News'})
        self.assertEqual(set(c.field_for(u'tags', 1)), {u't1', u'News'})
        self.assertEqual(set(c.field_for(u'tags', 2)), {u't1', u'Tag    Two'})
    self.assertEqual(cache.rename_items(u'tags', {t: u'Tag    Two'})[0], {1, 2})
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'tags'), {u'Tag    Two', u'News'})
        self.assertEqual(set(c.field_for(u'tags', 1)), {u'Tag    Two', u'News'})
        self.assertEqual(set(c.field_for(u'tags', 2)), {u'Tag    Two'})
    t = {v: k for (k, v) in cache.get_id_map(u'#tags').iteritems()}[u'My    Tag    One']
    self.assertEqual(cache.rename_items(u'#tags', {t: u'My    Tag    Two'})[0], {2})
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'#tags'), {u'My    Tag    Two'})
        self.assertEqual(set(c.field_for(u'#tags', 2)), {u'My    Tag    Two'})
    s = {v: k for (k, v) in cache.get_id_map(u'series').iteritems()}[u'A    Series    One']
    self.assertEqual(cache.rename_items(u'series', {s: u'a    series    one'}), ({1, 2}, {s: s}))
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'series'), {u'a    series    one'})
        self.assertEqual(c.field_for(u'series', 1), u'a    series    one')
        self.assertEqual(c.field_for(u'series_index', 1), 2.0)
    self.assertEqual(cache.rename_items(u'series', {s: u'series'})[0], {1, 2})
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'series'), {u'series'})
        self.assertEqual(c.field_for(u'series', 1), u'series')
        self.assertEqual(c.field_for(u'series', 2), u'series')
        self.assertEqual(c.field_for(u'series_index', 1), 2.0)
    s = {v: k for (k, v) in cache.get_id_map(u'#series').iteritems()}[u'My    Series    One']
    self.assertEqual(cache.rename_items(u'#series', {s: u'My    Series    Two'})[0], {2})
    for c in (cache, self.init_cache(cl)):
        self.assertEqual(c.all_field_names(u'#series'), {u'My    Series    Two'})
        self.assertEqual(c.field_for(u'#series', 2), u'My    Series    Two')
        self.assertEqual(c.field_for(u'#series_index', 1), 3.0)
        self.assertEqual(c.field_for(u'#series_index', 2), 4.0)
    cache = self.init_cache(self.cloned_library)
    t = {v: k for (k, v) in cache.get_id_map(u'tags').iteritems()}[u'Tag    One']
    (affected_books, id_map) = cache.rename_items(u'tags', {t: u'Something,    Else,    Entirely'})
    self.assertEqual({1, 2}, affected_books)
    tmap = cache.get_id_map(u'tags')
    self.assertEqual(u'Something', tmap[id_map[t]])
    self.assertEqual(1, len(id_map))
    (f1, f2) = (cache.field_for(u'tags', 1), cache.field_for(u'tags', 2))
    for f in (f1, f2):
        for t in u'Something,Else,Entirely'.split(u','):
            self.assertIn(t, f)
        self.assertNotIn(u'Tag    One', f)
    cache = self.init_cache()
    cache.set_field(u'tags', {1: u'a,b,c', 2: u'x,y,z', 3: u'a,x,z'})
    tmap = {v: k for (k, v) in cache.get_id_map(u'tags').iteritems()}
    self.assertEqual(cache.rename_items(u'tags', {tmap[u'a']: u'r'}, restrict_to_book_ids=()), (set(), {}))
    self.assertEqual(cache.rename_items(u'tags', {tmap[u'a']: u'r', tmap[u'b']: u'q'}, restrict_to_book_ids=(1,))[0], {1})
    self.assertEqual(cache.rename_items(u'tags', {tmap[u'x']: u'X'}, restrict_to_book_ids=(2,))[0], {2})
    c2 = self.init_cache()
    for c in (cache, c2):
        self.assertEqual(c.field_for(u'tags', 1), (u'r', u'q', u'c'))
        self.assertEqual(c.field_for(u'tags', 2), (u'X', u'y', u'z'))
        self.assertEqual(c.field_for(u'tags', 3), (u'a', u'X', u'z'))
