File: test_order_by.py

package info (click to toggle)
elixir 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 640 kB
  • ctags: 1,079
  • sloc: python: 4,936; makefile: 10
file content (101 lines) | stat: -rw-r--r-- 2,703 bytes parent folder | download
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
test ordering options
"""

from elixir import *


def setup():
    global Record, Artist, Genre

    class Record(Entity):
        title = Field(String(100))
        year = Field(Integer)
        artist = ManyToOne('Artist')
        genres = ManyToMany('Genre')

        # order titles descending by year, then by title
        using_options(order_by=['-year', 'title'])

        def __str__(self):
            return "%s - %s (%d)" % (self.artist.name, self.title, self.year)

    class Artist(Entity):
        name = Field(String(30))
        records = OneToMany('Record', order_by=['year', '-title'])

    class Genre(Entity):
        name = Field(String(30))
        records = ManyToMany('Record', order_by='-title')

    metadata.bind = 'sqlite:///'
    setup_all(True)

    # insert some data
    artist = Artist(name="Dream Theater")
    genre = Genre(name="Progressive metal")
    titles = (
        ("A Change Of Seasons", 1995),
        ("Awake", 1994),
        ("Falling Into Infinity", 1997),
        ("Images & Words", 1992),
        ("Metropolis Pt. 2: Scenes From A Memory", 1999),
        ("Octavarium", 2005),
        # 2005 is a mistake to make the test more interesting
        ("Six Degrees Of Inner Turbulence", 2005),
        ("Train Of Thought", 2003),
        ("When Dream And Day Unite", 1989)
    )

    for title, year in titles:
        Record(title=title, artist=artist, year=year, genres=[genre])

    session.commit()
    session.clear()


def teardown():
    cleanup_all()


class TestOrderBy(object):
    def teardown(self):
        session.clear()

    def test_mapper_order_by(self):
        records = Record.query.all()

        print "-year, +title"
        for record in records:
            print record

        assert records[0].year == 2005
        assert records[2].year >= records[5].year
        assert records[3].year >= records[4].year
        assert records[-1].year == 1989

    def test_o2m_order_by(self):
        records = Artist.get_by(name="Dream Theater").records

        print "+year, -title"
        for record in records:
            print record

        assert records[0].year == 1989
        assert records[2].year <= records[5].year
        assert records[3].year <= records[4].year
        assert records[-1].title == 'Octavarium'
        assert records[-1].year == 2005

    def test_m2m_order_by(self):
        records = Genre.get_by(name="Progressive metal").records

        print "-title"
        for record in records:
            print record

        assert records[0].year == 1989
        assert records[2].title >= records[5].title
        assert records[3].title >= records[4].title
        assert records[-1].year == 1995