File: test_sorting.py

package info (click to toggle)
sqlobject 3.1.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 9,280 kB
  • ctags: 17,912
  • sloc: python: 16,713; sh: 18; makefile: 13
file content (80 lines) | stat: -rw-r--r-- 2,612 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from sqlobject import DESC, SQLObject, StringCol, sqlmeta
from sqlobject.tests.dbtest import inserts, setupClass


class Names(SQLObject):

    class sqlmeta(sqlmeta):
        table = 'names_table'
        defaultOrder = ['lastName', 'firstName']

    firstName = StringCol(length=30)
    lastName = StringCol(length=30)


def setupNames():
    setupClass(Names)
    inserts(Names, [('aj', 'baker'), ('joe', 'robbins'),
                    ('tim', 'jackson'), ('joe', 'baker'),
                    ('zoe', 'robbins')],
            schema='firstName lastName')


def nameList(names):
    result = []
    for name in names:
        result.append('%s %s' % (name.firstName, name.lastName))
    return result


def firstList(names):
    return [n.firstName for n in names]


def test_defaultOrder():
    setupNames()
    assert nameList(Names.select()) == \
        ['aj baker', 'joe baker',
         'tim jackson', 'joe robbins',
         'zoe robbins']


def test_otherOrder():
    setupNames()
    assert nameList(Names.select().orderBy(['firstName', 'lastName'])) == \
        ['aj baker', 'joe baker',
         'joe robbins', 'tim jackson',
         'zoe robbins']


def test_untranslatedColumnOrder():
    setupNames()
    assert nameList(Names.select().orderBy(['first_name', 'last_name'])) == \
        ['aj baker', 'joe baker',
         'joe robbins', 'tim jackson',
         'zoe robbins']


def test_singleUntranslatedColumnOrder():
    setupNames()
    assert firstList(Names.select().orderBy('firstName')) == \
        ['aj', 'joe', 'joe', 'tim', 'zoe']
    assert firstList(Names.select().orderBy('first_name')) == \
        ['aj', 'joe', 'joe', 'tim', 'zoe']
    assert firstList(Names.select().orderBy('-firstName')) == \
        ['zoe', 'tim', 'joe', 'joe', 'aj']
    assert firstList(Names.select().orderBy(u'-first_name')) == \
        ['zoe', 'tim', 'joe', 'joe', 'aj']
    assert firstList(Names.select().orderBy(Names.q.firstName)) == \
        ['aj', 'joe', 'joe', 'tim', 'zoe']
    assert firstList(Names.select().orderBy('firstName').reversed()) == \
        ['zoe', 'tim', 'joe', 'joe', 'aj']
    assert firstList(Names.select().orderBy('-firstName').reversed()) == \
        ['aj', 'joe', 'joe', 'tim', 'zoe']
    assert firstList(Names.select().orderBy(DESC(Names.q.firstName))) == \
        ['zoe', 'tim', 'joe', 'joe', 'aj']
    assert firstList(Names.select().orderBy(Names.q.firstName).reversed()) == \
        ['zoe', 'tim', 'joe', 'joe', 'aj']
    assert firstList(
        Names.select().orderBy(DESC(Names.q.firstName)).reversed()) == \
        ['aj', 'joe', 'joe', 'tim', 'zoe']