| 1 | """ |
|---|
| 2 | test ordering options |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from elixir import * |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | def setup(): |
|---|
| 9 | global Record, Artist, Genre |
|---|
| 10 | |
|---|
| 11 | class Record(Entity): |
|---|
| 12 | title = Field(String(100)) |
|---|
| 13 | year = Field(Integer) |
|---|
| 14 | artist = ManyToOne('Artist') |
|---|
| 15 | genres = ManyToMany('Genre') |
|---|
| 16 | |
|---|
| 17 | # order titles descending by year, then by title |
|---|
| 18 | using_options(order_by=['-year', 'title']) |
|---|
| 19 | |
|---|
| 20 | def __str__(self): |
|---|
| 21 | return "%s - %s (%d)" % (self.artist.name, self.title, self.year) |
|---|
| 22 | |
|---|
| 23 | class Artist(Entity): |
|---|
| 24 | name = Field(String(30)) |
|---|
| 25 | records = OneToMany('Record', order_by=['year', '-title']) |
|---|
| 26 | |
|---|
| 27 | class Genre(Entity): |
|---|
| 28 | name = Field(String(30)) |
|---|
| 29 | records = ManyToMany('Record', order_by='-title') |
|---|
| 30 | |
|---|
| 31 | metadata.bind = 'sqlite://' |
|---|
| 32 | setup_all(True) |
|---|
| 33 | |
|---|
| 34 | # insert some data |
|---|
| 35 | artist = Artist(name="Dream Theater") |
|---|
| 36 | genre = Genre(name="Progressive metal") |
|---|
| 37 | titles = ( |
|---|
| 38 | ("A Change Of Seasons", 1995), |
|---|
| 39 | ("Awake", 1994), |
|---|
| 40 | ("Falling Into Infinity", 1997), |
|---|
| 41 | ("Images & Words", 1992), |
|---|
| 42 | ("Metropolis Pt. 2: Scenes From A Memory", 1999), |
|---|
| 43 | ("Octavarium", 2005), |
|---|
| 44 | # 2005 is a mistake to make the test more interesting |
|---|
| 45 | ("Six Degrees Of Inner Turbulence", 2005), |
|---|
| 46 | ("Train Of Thought", 2003), |
|---|
| 47 | ("When Dream And Day Unite", 1989) |
|---|
| 48 | ) |
|---|
| 49 | |
|---|
| 50 | for title, year in titles: |
|---|
| 51 | Record(title=title, artist=artist, year=year, genres=[genre]) |
|---|
| 52 | |
|---|
| 53 | session.commit() |
|---|
| 54 | session.expunge_all() |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | def teardown(): |
|---|
| 58 | cleanup_all() |
|---|
| 59 | |
|---|
| 60 | |
|---|
| 61 | class TestOrderBy(object): |
|---|
| 62 | def teardown(self): |
|---|
| 63 | session.expunge_all() |
|---|
| 64 | |
|---|
| 65 | def test_mapper_order_by(self): |
|---|
| 66 | records = Record.query.all() |
|---|
| 67 | |
|---|
| 68 | assert records[0].year == 2005 |
|---|
| 69 | assert records[2].year >= records[5].year |
|---|
| 70 | assert records[3].year >= records[4].year |
|---|
| 71 | assert records[-1].year == 1989 |
|---|
| 72 | |
|---|
| 73 | def test_o2m_order_by(self): |
|---|
| 74 | records = Artist.get_by(name="Dream Theater").records |
|---|
| 75 | |
|---|
| 76 | assert records[0].year == 1989 |
|---|
| 77 | assert records[2].year <= records[5].year |
|---|
| 78 | assert records[3].year <= records[4].year |
|---|
| 79 | assert records[-1].title == 'Octavarium' |
|---|
| 80 | assert records[-1].year == 2005 |
|---|
| 81 | |
|---|
| 82 | def test_m2m_order_by(self): |
|---|
| 83 | records = Genre.get_by(name="Progressive metal").records |
|---|
| 84 | |
|---|
| 85 | assert records[0].year == 1989 |
|---|
| 86 | assert records[2].title >= records[5].title |
|---|
| 87 | assert records[3].title >= records[4].title |
|---|
| 88 | assert records[-1].year == 1995 |
|---|