'Test sorting and searching in composite columns'

def test_composites(self):

    from calibre.utils.date import parse_only_date as p
    cache = self.init_cache()
    cache.create_custom_column(u'mult', u'CC1', u'composite', True, display={u'composite_template': u'b,a,c'})
    cache.create_custom_column(u'single', u'CC2', u'composite', False, display={u'composite_template': u'b,a,c'})
    cache.create_custom_column(u'number', u'CC3', u'composite', False, display={u'composite_template': u'{#float}', u'composite_sort': u'number'})
    cache.create_custom_column(u'size', u'CC4', u'composite', False, display={u'composite_template': u'{#float:human_readable()}', u'composite_sort': u'number'})
    cache.create_custom_column(u'ccdate', u'CC5', u'composite', False, display={u'composite_template': u'{pubdate:format_date(d-M-yy)}', u'composite_sort': u'date'})
    cache.create_custom_column(u'bool', u'CC6', u'composite', False, display={u'composite_template': u'{#yesno}', u'composite_sort': u'bool'})
    cache.create_custom_column(u'ccm', u'CC7', u'composite', True, display={u'composite_template': u'{#tags}'})
    cache.create_custom_column(u'ccp', u'CC8', u'composite', True, display={u'composite_template': u'{publisher}'})
    cache.create_custom_column(u'ccf', u'CC9', u'composite', True, display={u'composite_template': u"{:'approximate_formats()'}"})
    cache = self.init_cache()
    self.assertEqual({1, 2, 3}, cache.search(u'#mult:=a'))
    self.assertEqual(set(), cache.search(u'#mult:=b,a,c'))
    self.assertEqual({1, 2, 3}, cache.search(u'#single:=b,a,c'))
    self.assertEqual(set(), cache.search(u'#single:=b'))
    cache.set_field(u'#float', {1: 2, 2: 10, 3: 0.0001})
    self.assertEqual([3, 1, 2], cache.multisort([(u'#number', True)]))
    cache.set_field(u'#float', {1: 3, 2: (2 * 1024), 3: ((1 * 1024) * 1024)})
    self.assertEqual([1, 2, 3], cache.multisort([(u'#size', True)]))
    cache.set_field(u'pubdate', {1: p(u'2001-2-6'), 2: p(u'2001-10-6'), 3: p(u'2001-6-6')})
    self.assertEqual([1, 3, 2], cache.multisort([(u'#ccdate', True)]))
    self.assertEqual([2, 1, 3], cache.multisort([(u'#bool', True)]))
    cache.set_field(u'#tags', {1: u'b,    a,    c', 2: u'a,    b,    c', 3: u'a,    c,    b'})
    self.assertEqual([1, 2, 3], cache.multisort([(u'#ccm', True)]))
    self.assertEqual(cache.search(u'#ccp:One'), {2})
    cache.set_field(u'publisher', {2: u'One', 1: u'One'})
    self.assertEqual(cache.search(u'#ccp:One'), {1, 2})
    self.assertEqual(cache.search(u'#ccf:FMT1'), {1, 2})
    cache.remove_formats({1: (u'FMT1',)})
    self.assertEqual(u'FMT2', cache.field_for(u'#ccf', 1))
