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

def test_many_one_basic(self):

    cl = self.cloned_library
    cache = self.init_cache(cl)
    f = cache.fields[u'publisher']
    item_ids = {f.ids_for_book(1)[0], f.ids_for_book(2)[0]}
    val = u'Changed'
    self.assertEqual(cache.set_field(u'publisher', {1: val, 2: val}), {1, 2})
    cache2 = self.init_cache(cl)
    for book_id in (1, 2):
        for c in (cache, cache2):
            self.assertEqual(c.field_for(u'publisher', book_id), val)
            self.assertFalse(item_ids.intersection(set(c.fields[u'publisher'].table.id_map)))
    del cache2
    self.assertFalse(cache.set_field(u'publisher', {1: val, 2: val}))
    val = val.lower()
    self.assertFalse(cache.set_field(u'publisher', {1: val, 2: val}, allow_case_change=False))
    self.assertEqual(cache.set_field(u'publisher', {1: val, 2: val}), {1, 2})
    cache2 = self.init_cache(cl)
    for book_id in (1, 2):
        for c in (cache, cache2):
            self.assertEqual(c.field_for(u'publisher', book_id), val)
    del cache2
    self.assertEqual(cache.set_field(u'publisher', {1: u'new', 2: u'New'}), {1, 2})
    self.assertEqual(cache.field_for(u'publisher', 1).lower(), u'new')
    self.assertEqual(cache.field_for(u'publisher', 2).lower(), u'new')
    self.assertEqual(cache.set_field(u'publisher', {1: None, 2: u'NEW'}), {1, 2})
    self.assertEqual(len(f.table.id_map), 1)
    self.assertEqual(cache.set_field(u'publisher', {2: None}), {2})
    self.assertEqual(len(f.table.id_map), 0)
    cache2 = self.init_cache(cl)
    self.assertEqual(len(cache2.fields[u'publisher'].table.id_map), 0)
    del cache2
    self.assertEqual(cache.set_field(u'publisher', {1: u'one', 2: u'two', 3: u'three'}), {1, 2, 3})
    self.assertEqual(cache.set_field(u'publisher', {1: u''}), set([1]))
    self.assertEqual(cache.set_field(u'publisher', {1: u'two'}), set([1]))
    self.assertEqual(tuple(map(f.for_book, (1, 2, 3))), (u'two', u'two', u'three'))
    self.assertEqual(cache.set_field(u'publisher', {1: u'Two'}), {1, 2})
    cache2 = self.init_cache(cl)
    self.assertEqual(tuple(map(f.for_book, (1, 2, 3))), (u'Two', u'Two', u'three'))
    del cache2
    self.assertFalse(cache.set_field(u'#enum', {1: u'Not    allowed'}))
    self.assertEqual(cache.set_field(u'#enum', {1: u'One', 2: u'One', 3: u'Three'}), {1, 3})
    self.assertEqual(cache.set_field(u'#enum', {1: None}), set([1]))
    cache2 = self.init_cache(cl)
    for c in (cache, cache2):
        for (i, val) in {1: None, 2: u'One', 3: u'Three'}.iteritems():
            self.assertEqual(c.field_for(u'#enum', i), val)
    del cache2
    self.assertFalse(cache.set_field(u'rating', {1: 6, 2: 4}))
    self.assertEqual(cache.set_field(u'rating', {1: 0, 3: 2}), {1, 3})
    self.assertEqual(cache.set_field(u'#rating', {1: None, 2: 4, 3: 8}), {1, 2, 3})
    cache2 = self.init_cache(cl)
    for c in (cache, cache2):
        for (i, val) in {1: None, 2: 4, 3: 2}.iteritems():
            self.assertEqual(c.field_for(u'rating', i), val)
        for (i, val) in {1: None, 2: 4, 3: 8}.iteritems():
            self.assertEqual(c.field_for(u'#rating', i), val)
    del cache2
    self.assertFalse(cache.set_field(u'series', {1: u'a    series    one', 2: u'a    series    one'}, allow_case_change=False))
    self.assertEqual(cache.set_field(u'series', {3: u'Series    [3]'}), set([3]))
    self.assertEqual(cache.set_field(u'#series', {1: u'Series', 3: u'Series'}), {1, 3})
    self.assertEqual(cache.set_field(u'#series', {2: u'Series    [0]'}), set([2]))
    cache2 = self.init_cache(cl)
    for c in (cache, cache2):
        for (i, val) in {1: u'A    Series    One', 2: u'A    Series    One', 3: u'Series'}.iteritems():
            self.assertEqual(c.field_for(u'series', i), val)
        cs_indices = {1: c.field_for(u'#series_index', 1), 3: c.field_for(u'#series_index', 3)}
        for i in (1, 2, 3):
            self.assertEqual(c.field_for(u'#series', i), u'Series')
        for (i, val) in {1: 2, 2: 1, 3: 3}.iteritems():
            self.assertEqual(c.field_for(u'series_index', i), val)
        for (i, val) in {1: cs_indices[1], 2: 0, 3: cs_indices[3]}.iteritems():
            self.assertEqual(c.field_for(u'#series_index', i), val)
    del cache2
