'Test the different code paths for writing to a many-many field'

def test_many_many_basic(self):

    cl = self.cloned_library
    cache = self.init_cache(cl)
    (ae, af, sf) = (self.assertEqual, self.assertFalse, cache.set_field)
    ae(sf(u'#tags', {1: cache.field_for(u'tags', 1), 2: cache.field_for(u'tags', 2)}), {1, 2})
    for name in (u'tags', u'#tags'):
        f = cache.fields[name]
        af(sf(name, {1: (u'News', u'tag    one')}, allow_case_change=False))
        ae(sf(name, {1: u'tag    one,    News'}), {1, 2})
        ae(sf(name, {3: (u'tag    two', u'sep,sep2')}), {2, 3})
        ae(len(f.table.id_map), 4)
        ae(sf(name, {1: None}), set([1]))
        cache2 = self.init_cache(cl)
        for c in (cache, cache2):
            ae(c.field_for(name, 3), (u'tag    two', u'sep;sep2'))
            ae(len(c.fields[name].table.id_map), 3)
            ae(len(c.fields[name].table.id_map), 3)
            ae(c.field_for(name, 1), ())
            ae(c.field_for(name, 2), (u'tag    two', u'tag    one'))
        del cache2
    ae(sf(u'#authors', {k: cache.field_for(u'authors', k) for k in (1, 2, 3)}), {1, 2, 3})
    for name in (u'authors', u'#authors'):
        f = cache.fields[name]
        ae(len(f.table.id_map), 3)
        af(cache.set_field(name, {3: (None if (name == u'authors') else u'Unknown')}))
        ae(cache.set_field(name, {3: u'Kovid    Goyal    &    Divok    Layog'}), set([3]))
        ae(cache.set_field(name, {1: u'', 2: u'An,    Author'}), {1, 2})
        cache2 = self.init_cache(cl)
        for c in (cache, cache2):
            ae(len(c.fields[name].table.id_map), (4 if (name == u'authors') else 3))
            ae(c.field_for(name, 3), (u'Kovid    Goyal', u'Divok    Layog'))
            ae(c.field_for(name, 2), (u'An,    Author',))
            ae(c.field_for(name, 1), ((u'Unknown',) if (name == u'authors') else ()))
            if (name == u'authors'):
                ae(c.field_for(u'author_sort', 1), author_to_author_sort(u'Unknown'))
                ae(c.field_for(u'author_sort', 2), author_to_author_sort(u'An,    Author'))
                ae(c.field_for(u'author_sort', 3), ((author_to_author_sort(u'Kovid    Goyal') + u'    &    ') + author_to_author_sort(u'Divok    Layog')))
        del cache2
    ae(cache.set_field(u'authors', {1: u'KoviD    GoyaL'}), {1, 3})
    ae(cache.field_for(u'author_sort', 1), u'GoyaL,    KoviD')
    ae(cache.field_for(u'author_sort', 3), u'GoyaL,    KoviD    &    Layog,    Divok')
    f = cache.fields[u'languages']
    ae(f.table.id_map, {1: u'eng', 2: u'deu'})
    ae(sf(u'languages', {1: u''}), set([1]))
    ae(cache.field_for(u'languages', 1), ())
    ae(sf(u'languages', {2: (u'und',)}), set([2]))
    af(f.table.id_map)
    ae(sf(u'languages', {1: u'eng,fra,deu', 2: u'es,Dutch', 3: u'English'}), {1, 2, 3})
    ae(cache.field_for(u'languages', 1), (u'eng', u'fra', u'deu'))
    ae(cache.field_for(u'languages', 2), (u'spa', u'nld'))
    ae(cache.field_for(u'languages', 3), (u'eng',))
    ae(sf(u'languages', {3: None}), set([3]))
    ae(cache.field_for(u'languages', 3), ())
    ae(sf(u'languages', {1: u'deu,fra,eng'}), set([1]), u'Changing    order    failed')
    ae(sf(u'languages', {2: u'deu,eng,eng'}), set([2]))
    cache2 = self.init_cache(cl)
    for c in (cache, cache2):
        ae(cache.field_for(u'languages', 1), (u'deu', u'fra', u'eng'))
        ae(cache.field_for(u'languages', 2), (u'deu', u'eng'))
    del cache2
    f = cache.fields[u'identifiers']
    ae(sf(u'identifiers', {3: u'one:1,two:2'}), set([3]))
    ae(sf(u'identifiers', {2: None}), set([2]))
    ae(sf(u'identifiers', {1: {u'test': u'1', u'two': u'2'}}), set([1]))
    cache2 = self.init_cache(cl)
    for c in (cache, cache2):
        ae(c.field_for(u'identifiers', 3), {u'one': u'1', u'two': u'2'})
        ae(c.field_for(u'identifiers', 2), {})
        ae(c.field_for(u'identifiers', 1), {u'test': u'1', u'two': u'2'})
    del cache2
    ae(sf(u'title', {1: u'The    Moose', 2: u'Cat'}), {1, 2})
    cache2 = self.init_cache(cl)
    for c in (cache, cache2):
        ae(c.field_for(u'sort', 1), u'Moose,    The')
        ae(c.field_for(u'sort', 2), u'Cat')
    ae(sf(u'tags', {3: (u'a', u'b', u'a')}), {3})
    ae(sf(u'tags', {3: (u'x', u'X')}), {3}, u'Failed    when    setting    tag    twice    with    different    cases')
    ae((u'x',), cache.field_for(u'tags', 3))
    ae(sf(u'authors', {3: (u'Some|    Author',)}), {3})
    ae((u'Some,    Author',), cache.field_for(u'authors', 3))
