'Test sorting'

def test_sorting(self):

    cache = self.init_cache()
    ae = self.assertEqual
    for (field, order) in {u'title': [2, 1, 3], u'authors': [2, 1, 3], u'series': [3, 1, 2], u'tags': [3, 1, 2], u'rating': [3, 2, 1], u'timestamp': [2, 1, 3], u'pubdate': [1, 2, 3], u'publisher': [3, 2, 1], u'languages': [3, 2, 1], u'comments': [3, 2, 1], u'#enum': [3, 2, 1], u'#authors': [3, 2, 1], u'#date': [3, 1, 2], u'#rating': [3, 2, 1], u'#series': [3, 2, 1], u'#tags': [3, 2, 1], u'#yesno': [2, 1, 3], u'#comments': [3, 2, 1], u'id': [1, 2, 3]}.iteritems():
        x = list(reversed(order))
        ae(order, cache.multisort([(field, True)], ids_to_sort=x), (u'Ascending    sort    of    %s    failed' % field))
        ae(x, cache.multisort([(field, False)], ids_to_sort=order), (u'Descending    sort    of    %s    failed' % field))
    for field in (u'authors', u'#authors'):
        ae(cache.set_field(field, {1: (u'aa    bb', u'bb    cc', u'cc    dd'), 2: (u'bb    aa', u'xx    yy'), 3: (u'aa    bb', u'bb    aa')}), {1, 2, 3})
        ae([2, 3, 1], cache.multisort([(field, True)], ids_to_sort=(1, 2, 3)))
        ae([1, 3, 2], cache.multisort([(field, False)], ids_to_sort=(1, 2, 3)))
    for field in (u'tags', u'#tags'):
        ae(cache.set_field(field, {1: (u'b', u'a'), 2: (u'c', u'y'), 3: (u'b', u'z')}), {1, 2, 3})
        ae([1, 3, 2], cache.multisort([(field, True)], ids_to_sort=(1, 2, 3)))
        ae([2, 3, 1], cache.multisort([(field, False)], ids_to_sort=(1, 2, 3)))
    from calibre.utils.date import parse_only_date as p
    from calibre.utils.config_base import Tweak
    ae(cache.set_field(u'pubdate', {1: p(u'2001-3-3'), 2: p(u'2002-2-3'), 3: p(u'2003-1-3')}), {1, 2, 3})
    ae([1, 2, 3], cache.multisort([(u'pubdate', True)]))
    with Tweak(u'gui_pubdate_display_format', u'MMM'):
        with Tweak(u'sort_dates_using_visible_fields', True):
            c2 = self.init_cache()
            ae([3, 2, 1], c2.multisort([(u'pubdate', True)]))
    cache.set_pref(u'bools_are_tristate', False)
    c2 = self.init_cache()
    ae([2, 3, 1], c2.multisort([(u'#yesno', True), (u'id', False)]))
    ae([3, 2, 1], cache.multisort([(u'identifiers', True), (u'title', True)]), u'Subsort    failed')
    from calibre.ebooks.metadata.book.base import Metadata
    for i in xrange(7):
        cache.create_book_entry(Metadata((u'title%d' % i)), apply_import_tags=False)
    cache.create_custom_column(u'one', u'CC1', u'int', False)
    cache.create_custom_column(u'two', u'CC2', u'int', False)
    cache.create_custom_column(u'three', u'CC3', u'int', False)
    cache.close()
    cache = self.init_cache()
    cache.set_field(u'#one', {(i + (5 * m)): m for m in (0, 1) for i in xrange(1, 6)})
    cache.set_field(u'#two', {(i + (m * 3)): m for m in (0, 1, 2) for i in (1, 2, 3)})
    cache.set_field(u'#two', {10: 2})
    cache.set_field(u'#three', {i: i for i in xrange(1, 11)})
    ae(list(xrange(1, 11)), cache.multisort([(u'#one', True), (u'#two', True)], ids_to_sort=sorted(cache.all_book_ids())))
    ae([4, 5, 1, 2, 3, 7, 8, 9, 10, 6], cache.multisort([(u'#one', True), (u'#two', False)], ids_to_sort=sorted(cache.all_book_ids())))
    ae([5, 4, 3, 2, 1, 10, 9, 8, 7, 6], cache.multisort([(u'#one', True), (u'#two', False), (u'#three', False)], ids_to_sort=sorted(cache.all_book_ids())))
