| 1 | from sqlalchemy import Table, Column, MetaData |
|---|
| 2 | |
|---|
| 3 | from elixir import * |
|---|
| 4 | from elixir.ext.list import acts_as_list |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | class TestActsAsList(object): |
|---|
| 8 | |
|---|
| 9 | def teardown(self): |
|---|
| 10 | cleanup_all(True) |
|---|
| 11 | |
|---|
| 12 | def test_acts_as_list(self): |
|---|
| 13 | class ToDo(Entity): |
|---|
| 14 | subject = Field(String(128)) |
|---|
| 15 | owner = ManyToOne('Person') |
|---|
| 16 | |
|---|
| 17 | def qualify(self): |
|---|
| 18 | return ToDo.owner_id == self.owner_id |
|---|
| 19 | |
|---|
| 20 | acts_as_list(qualifier=qualify, column_name='position') |
|---|
| 21 | |
|---|
| 22 | def __repr__(self): |
|---|
| 23 | return '<%d:%s>' % (self.position, self.subject) |
|---|
| 24 | |
|---|
| 25 | class Person(Entity): |
|---|
| 26 | name = Field(String(64)) |
|---|
| 27 | todos = OneToMany('ToDo', order_by='position') |
|---|
| 28 | |
|---|
| 29 | metadata.bind = 'sqlite://' |
|---|
| 30 | |
|---|
| 31 | setup_all(True) |
|---|
| 32 | |
|---|
| 33 | # create a person |
|---|
| 34 | # you must create and commit this _before_ you attach todo's to it |
|---|
| 35 | # because of the way that the plugin is implemented |
|---|
| 36 | p = Person(name='Jonathan') |
|---|
| 37 | session.commit(); session.clear() |
|---|
| 38 | |
|---|
| 39 | # add three todos, in the reverse order that we want them |
|---|
| 40 | p = Person.get(1) |
|---|
| 41 | p.todos.append(ToDo(subject='Three')) |
|---|
| 42 | p.todos.append(ToDo(subject='Two')) |
|---|
| 43 | p.todos.append(ToDo(subject='One')) |
|---|
| 44 | session.commit(); session.clear() |
|---|
| 45 | |
|---|
| 46 | # move the first item lower |
|---|
| 47 | p = Person.get(1) |
|---|
| 48 | p.todos[0].move_lower() |
|---|
| 49 | session.commit(); session.clear() |
|---|
| 50 | |
|---|
| 51 | # validate it worked |
|---|
| 52 | p = Person.get(1) |
|---|
| 53 | assert p.todos[0].subject == 'Two' |
|---|
| 54 | assert p.todos[1].subject == 'Three' |
|---|
| 55 | assert p.todos[2].subject == 'One' |
|---|
| 56 | |
|---|
| 57 | # move the last item to the top to put things in correct order |
|---|
| 58 | p.todos[2].move_to_top() |
|---|
| 59 | session.commit(); session.clear() |
|---|
| 60 | |
|---|
| 61 | # validate it worked |
|---|
| 62 | p = Person.get(1) |
|---|
| 63 | assert p.todos[0].subject == 'One' |
|---|
| 64 | assert p.todos[1].subject == 'Two' |
|---|
| 65 | assert p.todos[2].subject == 'Three' |
|---|
| 66 | |
|---|
| 67 | # lets shuffle them again for the sake of testing move_to_bottom |
|---|
| 68 | p.todos[2].move_to_top() |
|---|
| 69 | session.commit(); session.clear() |
|---|
| 70 | |
|---|
| 71 | p = Person.get(1) |
|---|
| 72 | assert p.todos[0].subject == 'Three' |
|---|
| 73 | assert p.todos[1].subject == 'One' |
|---|
| 74 | assert p.todos[2].subject == 'Two' |
|---|
| 75 | |
|---|
| 76 | p.todos[1].move_to_bottom() |
|---|
| 77 | session.commit(); session.clear() |
|---|
| 78 | |
|---|
| 79 | p = Person.get(1) |
|---|
| 80 | assert p.todos[0].subject == 'Three' |
|---|
| 81 | assert p.todos[1].subject == 'Two' |
|---|
| 82 | assert p.todos[2].subject == 'One' |
|---|
| 83 | |
|---|
| 84 | # lets move everything back to how it should be |
|---|
| 85 | p = Person.get(1) |
|---|
| 86 | p.todos[0].move_to(3) |
|---|
| 87 | p.todos[2].move_to(1) |
|---|
| 88 | session.commit(); session.clear() |
|---|
| 89 | |
|---|
| 90 | # validate it worked |
|---|
| 91 | p = Person.get(1) |
|---|
| 92 | assert p.todos[0].subject == 'One' |
|---|
| 93 | assert p.todos[0].position == 1 |
|---|
| 94 | assert p.todos[1].subject == 'Two' |
|---|
| 95 | assert p.todos[1].position == 2 |
|---|
| 96 | assert p.todos[2].subject == 'Three' |
|---|
| 97 | assert p.todos[2].position == 3 |
|---|
| 98 | |
|---|
| 99 | # delete the second todo list item |
|---|
| 100 | p.todos[1].delete() |
|---|
| 101 | session.commit(); session.clear() |
|---|
| 102 | |
|---|
| 103 | # validate that the deletion worked, and sequence numebers |
|---|
| 104 | # were properly managed |
|---|
| 105 | p = Person.get(1) |
|---|
| 106 | assert p.todos[0].subject == 'One' |
|---|
| 107 | assert p.todos[0].position == 1 |
|---|
| 108 | assert p.todos[1].subject == 'Three' |
|---|
| 109 | assert p.todos[1].position == 2 |
|---|
| 110 | |
|---|
| 111 | def test_acts_as_list_autoload(self): |
|---|
| 112 | # Make autoload test fixture |
|---|
| 113 | meta = MetaData('sqlite://') |
|---|
| 114 | |
|---|
| 115 | preloaded_table = Table('preloaded', meta, |
|---|
| 116 | Column('name', String(32), primary_key=True), |
|---|
| 117 | Column('position', Integer)) |
|---|
| 118 | |
|---|
| 119 | meta.create_all() |
|---|
| 120 | |
|---|
| 121 | class Preloaded(Entity): |
|---|
| 122 | using_options(tablename='preloaded', autoload=True) |
|---|
| 123 | acts_as_list() |
|---|
| 124 | |
|---|
| 125 | metadata.bind = meta.bind |
|---|
| 126 | setup_all() |
|---|
| 127 | |
|---|
| 128 | i = Preloaded(name='Foo') |
|---|
| 129 | session.commit() |
|---|
| 130 | assert i.name == 'Foo' |
|---|
| 131 | assert i.position == 1 |
|---|
| 132 | |
|---|
| 133 | j = Preloaded(name='Bar') |
|---|
| 134 | session.commit() |
|---|
| 135 | assert j.name == 'Bar' |
|---|
| 136 | assert j.position == 2 |
|---|