| 1 | """ |
|---|
| 2 | test integrating Elixir entities with plain SQLAlchemy defined classes |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from sqlalchemy.orm import * |
|---|
| 6 | from sqlalchemy import * |
|---|
| 7 | from elixir import * |
|---|
| 8 | |
|---|
| 9 | def setup(): |
|---|
| 10 | metadata.bind = 'sqlite:///' |
|---|
| 11 | |
|---|
| 12 | class TestSAIntegration(object): |
|---|
| 13 | def teardown(self): |
|---|
| 14 | cleanup_all(True) |
|---|
| 15 | |
|---|
| 16 | def test_sa_to_elixir(self): |
|---|
| 17 | class A(Entity): |
|---|
| 18 | name = Field(String(60)) |
|---|
| 19 | |
|---|
| 20 | # Remember the entity need to be setup before you can refer to it from |
|---|
| 21 | # SQLAlchemy. |
|---|
| 22 | setup_all(True) |
|---|
| 23 | |
|---|
| 24 | b_table = Table('b', metadata, |
|---|
| 25 | Column('id', Integer, primary_key=True), |
|---|
| 26 | Column('name', String(60)), |
|---|
| 27 | Column('a_id', Integer, ForeignKey(A.c.id)) |
|---|
| 28 | ) |
|---|
| 29 | b_table.create() |
|---|
| 30 | |
|---|
| 31 | class B(object): |
|---|
| 32 | pass |
|---|
| 33 | |
|---|
| 34 | mapper(B, b_table, properties={ |
|---|
| 35 | 'a': relation(A) |
|---|
| 36 | }) |
|---|
| 37 | |
|---|
| 38 | b1 = B() |
|---|
| 39 | b1.name = 'b1' |
|---|
| 40 | b1.a = A(name='a1') |
|---|
| 41 | |
|---|
| 42 | session.save(b1) |
|---|
| 43 | session.flush() |
|---|
| 44 | session.clear() |
|---|
| 45 | |
|---|
| 46 | b = session.query(B).one() |
|---|
| 47 | |
|---|
| 48 | assert b.a.name == 'a1' |
|---|
| 49 | |
|---|
| 50 | # def test_elxir_to_sa(self): |
|---|
| 51 | # a_table = Table('a', metadata, |
|---|
| 52 | # Column('id', Integer, primary_key=True), |
|---|
| 53 | # Column('name', String(60)), |
|---|
| 54 | # ) |
|---|
| 55 | # a_table.create() |
|---|
| 56 | # |
|---|
| 57 | # class A(object): |
|---|
| 58 | # pass |
|---|
| 59 | # |
|---|
| 60 | # mapper(A, a_table) |
|---|
| 61 | # |
|---|
| 62 | # class B(Entity): |
|---|
| 63 | # name = Field(String(60)) |
|---|
| 64 | # a = ManyToOne('A') |
|---|
| 65 | # |
|---|
| 66 | # setup_all(True) |
|---|
| 67 | # |
|---|
| 68 | # a1 = A() |
|---|
| 69 | # a1.name = 'a1' |
|---|
| 70 | # b1 = B(name='b1', a=a1) |
|---|
| 71 | # |
|---|
| 72 | # session.save(b1) |
|---|
| 73 | # session.flush() |
|---|
| 74 | # session.clear() |
|---|
| 75 | # |
|---|
| 76 | # b = B.query.one() |
|---|
| 77 | # |
|---|
| 78 | # assert b.a.name == 'a1' |
|---|
| 79 | |
|---|