| 1 | """ |
|---|
| 2 | Test the associable statement generator |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from sqlalchemy import and_ |
|---|
| 6 | |
|---|
| 7 | from elixir import * |
|---|
| 8 | from elixir.ext.associable import associable |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | def setup(): |
|---|
| 12 | metadata.bind = 'sqlite://' |
|---|
| 13 | |
|---|
| 14 | class TestOrders(object): |
|---|
| 15 | def teardown(self): |
|---|
| 16 | cleanup_all(True) |
|---|
| 17 | |
|---|
| 18 | def test_empty(self): |
|---|
| 19 | class Foo(Entity): |
|---|
| 20 | pass |
|---|
| 21 | |
|---|
| 22 | class Bar(Entity): |
|---|
| 23 | pass |
|---|
| 24 | |
|---|
| 25 | is_fooable = associable(Foo) |
|---|
| 26 | is_barable = associable(Bar) |
|---|
| 27 | |
|---|
| 28 | class Quux(Entity): |
|---|
| 29 | is_fooable() |
|---|
| 30 | is_barable() |
|---|
| 31 | |
|---|
| 32 | setup_all(True) |
|---|
| 33 | |
|---|
| 34 | def test_basic(self): |
|---|
| 35 | class Address(Entity): |
|---|
| 36 | street = Field(String(130)) |
|---|
| 37 | city = Field(String(100)) |
|---|
| 38 | using_options(shortnames=True) |
|---|
| 39 | |
|---|
| 40 | class Comment(Entity): |
|---|
| 41 | id = Field(Integer, primary_key=True) |
|---|
| 42 | name = Field(String(200)) |
|---|
| 43 | text = Field(Text) |
|---|
| 44 | |
|---|
| 45 | is_addressable = associable(Address, 'addresses') |
|---|
| 46 | is_commentable = associable(Comment, 'comments') |
|---|
| 47 | |
|---|
| 48 | class Person(Entity): |
|---|
| 49 | id = Field(Integer, primary_key=True) |
|---|
| 50 | name = Field(String(50)) |
|---|
| 51 | orders = OneToMany('Order') |
|---|
| 52 | using_options(shortnames=True) |
|---|
| 53 | is_addressable() |
|---|
| 54 | is_commentable() |
|---|
| 55 | |
|---|
| 56 | class Order(Entity): |
|---|
| 57 | order_num = Field(Integer, primary_key=True) |
|---|
| 58 | item_count = Field(Integer) |
|---|
| 59 | person = ManyToOne('Person') |
|---|
| 60 | using_options(shortnames=True) |
|---|
| 61 | is_addressable('address', uselist=False) |
|---|
| 62 | |
|---|
| 63 | setup_all(True) |
|---|
| 64 | |
|---|
| 65 | home = Address(street='123 Elm St.', city='Spooksville') |
|---|
| 66 | work = Address(street='243 Hooper st.', city='Cupertino') |
|---|
| 67 | user = Person(name='Jane Doe') |
|---|
| 68 | user.addresses.append(home) |
|---|
| 69 | user.addresses.append(work) |
|---|
| 70 | |
|---|
| 71 | neworder = Order(item_count=4) |
|---|
| 72 | neworder.address = home |
|---|
| 73 | user.orders.append(neworder) |
|---|
| 74 | |
|---|
| 75 | session.commit() |
|---|
| 76 | session.clear() |
|---|
| 77 | |
|---|
| 78 | # Queries using the added helpers |
|---|
| 79 | people = Person.select_by_addresses(city='Cupertino') |
|---|
| 80 | assert len(people) == 1 |
|---|
| 81 | |
|---|
| 82 | streets = [adr.street for adr in people[0].addresses] |
|---|
| 83 | assert '243 Hooper st.' in streets |
|---|
| 84 | assert '123 Elm St.' in streets |
|---|
| 85 | |
|---|
| 86 | people = Person.select_addresses(and_(Address.street=='132 Elm St', |
|---|
| 87 | Address.city=='Smallville')) |
|---|
| 88 | assert len(people) == 0 |
|---|
| 89 | |
|---|
| 90 | def test_with_forward_ref(self): |
|---|
| 91 | class Checkout(Entity): |
|---|
| 92 | by = ManyToOne('Villian', ondelete='cascade') |
|---|
| 93 | stamp = Field(DateTime) |
|---|
| 94 | |
|---|
| 95 | can_checkout = associable(Checkout, 'checked_out') |
|---|
| 96 | |
|---|
| 97 | class Article(Entity): |
|---|
| 98 | title = Field(String(200)) |
|---|
| 99 | content = Field(Text) |
|---|
| 100 | can_checkout('checked_out_by', uselist=False) |
|---|
| 101 | using_options(tablename='article') |
|---|
| 102 | |
|---|
| 103 | class Villian(Entity): |
|---|
| 104 | name = Field(String(50)) |
|---|
| 105 | using_options(tablename='villian') |
|---|
| 106 | |
|---|
| 107 | setup_all(True) |
|---|
| 108 | |
|---|
| 109 | art = Article(title='Hope Soars') |
|---|
| 110 | |
|---|
| 111 | session.commit() |
|---|
| 112 | session.clear() |
|---|