'Test the operation of the thumbnail cache'

def test_thumbnail_cache(self):

    c = self.init_tc()
    self.assertFalse(hasattr(c, u'total_size'), u'index    read    on    initialization')
    c.invalidate(666)
    self.assertFalse(hasattr(c, u'total_size'), u'index    read    on    invalidate')
    self.assertEqual(self.basic_fill(c), c.total_size)
    self.assertEqual(5, len(c))
    for i in (3, 4, 2, 5, 1):
        (data, ts) = c[i]
        self.assertEqual(i, ts, u'timestamp    not    correct')
        self.assertEqual(((u'%d' % i) * (i * 1000)).encode(u'ascii'), data)
    c.set_group_id(u'a')
    self.basic_fill(c)
    order = tuple(c.items)
    ts = c.current_size
    c.shutdown()
    c = self.init_tc()
    self.assertEqual(c.current_size, ts, u'size    not    preserved    after    restart')
    self.assertEqual(order, tuple(c.items), u'order    not    preserved    after    restart')
    c.shutdown()
    c = self.init_tc()
    c.invalidate((1,))
    self.assertIsNone(c[1][1], u'invalidate    before    load_index()    failed')
    c.invalidate((2,))
    self.assertIsNone(c[2][1], u'invalidate    after    load_index()    failed')
    c.set_group_id(u'a')
    c[1]
    c.set_size(0.001)
    self.assertLessEqual(c.current_size, 1024, u'set_size()    failed')
    self.assertEqual(len(c), 1)
    self.assertIn(1, c)
    c.insert(9, 9, ('x' * (c.max_size - 1)))
    self.assertEqual(len(c), 1)
    self.assertLessEqual(c.current_size, c.max_size, u'insert()    did    not    prune')
    self.assertIn(9, c)
    c.empty()
    self.assertEqual(c.total_size, 0)
    self.assertEqual(len(c), 0)
    self.assertEqual(tuple(walk(c.location)), ())
    c = self.init_tc()
    self.basic_fill(c)
    self.assertEqual(len(c), 5)
    c.set_thumbnail_size(200, 201)
    self.assertIsNone(c[1][0])
    self.assertEqual(len(c), 0)
    self.assertEqual(tuple(walk(c.location)), ())
