'Test the ProxyMetadata object used for composite columns'

def test_proxy_metadata(self):

    from calibre.ebooks.metadata.book.base import STANDARD_METADATA_FIELDS
    cache = self.init_cache()
    for book_id in cache.all_book_ids():
        mi = cache.get_metadata(book_id, get_user_categories=True)
        pmi = cache.get_proxy_metadata(book_id)
        self.assertSetEqual(set(mi.custom_field_keys()), set(pmi.custom_field_keys()))
        for field in (STANDARD_METADATA_FIELDS | {u'#series_index'}):
            f = (lambda x: x)
            if (field == u'formats'):
                f = (lambda x: (x if (x is None) else tuple(x)))
            self.assertEqual(f(getattr(mi, field)), f(getattr(pmi, field)), (u'Standard    field:    %s    not    the    same    for    book    %s' % (field, book_id)))
            self.assertEqual(mi.format_field(field), pmi.format_field(field), (u'Standard    field    format:    %s    not    the    same    for    book    %s' % (field, book_id)))
            def f(x):
                try:
                    x.pop(u'rec_index', None)
                except:
                    pass
                return x
            if (field not in {u'#series_index'}):
                v = pmi.get_standard_metadata(field)
                self.assertTrue(((v is None) or isinstance(v, dict)))
                self.assertEqual(f(mi.get_standard_metadata(field, False)), f(v), (u'get_standard_metadata()    failed    for    field    %s' % field))
        for (field, meta) in cache.field_metadata.custom_iteritems():
            if (meta[u'datatype'] != u'composite'):
                self.assertEqual(f(getattr(mi, field)), f(getattr(pmi, field)), (u'Custom    field:    %s    not    the    same    for    book    %s' % (field, book_id)))
                self.assertEqual(mi.format_field(field), pmi.format_field(field), (u'Custom    field    format:    %s    not    the    same    for    book    %s' % (field, book_id)))
    cache.create_custom_column(u'comp2', u'comp2', u'composite', False, display={u'composite_template': u'{title}'})
    cache.create_custom_column(u'comp1', u'comp1', u'composite', False, display={u'composite_template': u'foo{#comp2}'})
    cache.close()
    cache = self.init_cache()
    (mi, pmi) = (cache.get_metadata(1), cache.get_proxy_metadata(1))
    self.assertEqual(mi.get(u'#comp1'), pmi.get(u'#comp1'))
