| 1 | """ |
|---|
| 2 | simple test case |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | import sqlalchemy |
|---|
| 6 | from sqlalchemy import Table, Column, ForeignKey, BoundMetaData |
|---|
| 7 | from sqlalchemy.types import * |
|---|
| 8 | from elixir import * |
|---|
| 9 | from elixir import metadata, objectstore |
|---|
| 10 | import elixir |
|---|
| 11 | import datetime |
|---|
| 12 | |
|---|
| 13 | #FIXME/TODO: I should also test many2many!!! |
|---|
| 14 | |
|---|
| 15 | # First create two tables (it would be better to user an external db) |
|---|
| 16 | engine = sqlalchemy.create_engine('sqlite:///') |
|---|
| 17 | meta = BoundMetaData(engine) |
|---|
| 18 | |
|---|
| 19 | person_table = Table('person', meta, |
|---|
| 20 | Column('id', Integer, primary_key=True), |
|---|
| 21 | Column('name', Unicode(32))) |
|---|
| 22 | person_table.create() |
|---|
| 23 | |
|---|
| 24 | animal_table = Table('animal', meta, |
|---|
| 25 | Column('id', Integer, primary_key=True), |
|---|
| 26 | Column('name', String(15)), |
|---|
| 27 | Column('color', String(15)), |
|---|
| 28 | Column('owner_id', Integer, ForeignKey('person.id')), |
|---|
| 29 | Column('feeder_id', Integer, ForeignKey('person.id'))) |
|---|
| 30 | animal_table.create() |
|---|
| 31 | |
|---|
| 32 | elixir.delay_setup = True |
|---|
| 33 | |
|---|
| 34 | class Animal(Entity): |
|---|
| 35 | # has_field('name', String(15)) |
|---|
| 36 | # has_field('color', String(15)) |
|---|
| 37 | |
|---|
| 38 | belongs_to('owner', of_kind='Person', colname='owner_id') |
|---|
| 39 | belongs_to('feeder', of_kind='Person', colname='feeder_id') |
|---|
| 40 | |
|---|
| 41 | using_options(autoload=True, shortnames=True) |
|---|
| 42 | |
|---|
| 43 | class Person(Entity): |
|---|
| 44 | # has_field('name', Unicode(32)) |
|---|
| 45 | |
|---|
| 46 | has_many('pets', of_kind='Animal', inverse='owner') |
|---|
| 47 | has_many('animals', of_kind='Animal', inverse='feeder') |
|---|
| 48 | |
|---|
| 49 | using_options(autoload=True, shortnames=True) |
|---|
| 50 | |
|---|
| 51 | def __str__(self): |
|---|
| 52 | s = '%s\n' % self.name.encode('utf-8') |
|---|
| 53 | for pet in self.pets: |
|---|
| 54 | s += ' * pet: %s\n' % pet.name |
|---|
| 55 | return s |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | elixir.delay_setup = False |
|---|
| 59 | |
|---|
| 60 | #----------- |
|---|
| 61 | |
|---|
| 62 | class TestAutoload(object): |
|---|
| 63 | def setup(self): |
|---|
| 64 | metadata.connect(engine) |
|---|
| 65 | setup_all() |
|---|
| 66 | |
|---|
| 67 | def teardown(self): |
|---|
| 68 | cleanup_all() |
|---|
| 69 | |
|---|
| 70 | def test_autoload(self): |
|---|
| 71 | snowball = Animal(name="Snowball II", color="grey") |
|---|
| 72 | slh = Animal(name="Santa's Little Helper") |
|---|
| 73 | homer = Person(name="Homer", animals=[snowball, slh], pets=[slh]) |
|---|
| 74 | lisa = Person(name="Lisa", pets=[snowball]) |
|---|
| 75 | |
|---|
| 76 | objectstore.flush() |
|---|
| 77 | objectstore.clear() |
|---|
| 78 | |
|---|
| 79 | homer = Person.get_by(name="Homer") |
|---|
| 80 | lisa = Person.get_by(name="Lisa") |
|---|
| 81 | |
|---|
| 82 | print homer |
|---|
| 83 | |
|---|
| 84 | assert len(homer.animals) == 2 |
|---|
| 85 | assert homer == lisa.pets[0].feeder |
|---|
| 86 | assert homer == slh.owner |
|---|
| 87 | |
|---|
| 88 | if __name__ == '__main__': |
|---|
| 89 | test = TestAutoload() |
|---|
| 90 | test.setup() |
|---|
| 91 | test.test_autoload() |
|---|
| 92 | test.teardown() |
|---|