| 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 | class TestSQLAlchemyToElixir(object): |
|---|
| 10 | def setup(self): |
|---|
| 11 | metadata.bind = "sqlite://" |
|---|
| 12 | |
|---|
| 13 | def teardown(self): |
|---|
| 14 | cleanup_all(True) |
|---|
| 15 | |
|---|
| 16 | def test_simple(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.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.add(b1) |
|---|
| 43 | session.commit() |
|---|
| 44 | session.expunge_all() |
|---|
| 45 | |
|---|
| 46 | b = session.query(B).one() |
|---|
| 47 | |
|---|
| 48 | assert b.a.name == 'a1' |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | class TestElixirToSQLAlchemy(object): |
|---|
| 52 | def setup(self): |
|---|
| 53 | metadata.bind = "sqlite://" |
|---|
| 54 | |
|---|
| 55 | def teardown(self): |
|---|
| 56 | cleanup_all(True) |
|---|
| 57 | |
|---|
| 58 | def test_m2o(self): |
|---|
| 59 | a_table = Table('a', metadata, |
|---|
| 60 | Column('id', Integer, primary_key=True), |
|---|
| 61 | Column('name', String(60)), |
|---|
| 62 | ) |
|---|
| 63 | a_table.create() |
|---|
| 64 | |
|---|
| 65 | class A(object): |
|---|
| 66 | pass |
|---|
| 67 | |
|---|
| 68 | mapper(A, a_table) |
|---|
| 69 | |
|---|
| 70 | class B(Entity): |
|---|
| 71 | name = Field(String(60)) |
|---|
| 72 | a = ManyToOne(A) |
|---|
| 73 | |
|---|
| 74 | setup_all(True) |
|---|
| 75 | |
|---|
| 76 | a1 = A() |
|---|
| 77 | a1.name = 'a1' |
|---|
| 78 | b1 = B(name='b1', a=a1) |
|---|
| 79 | |
|---|
| 80 | session.add(b1) |
|---|
| 81 | session.commit() |
|---|
| 82 | session.expunge_all() |
|---|
| 83 | |
|---|
| 84 | b = B.query.one() |
|---|
| 85 | |
|---|
| 86 | assert b.a.name == 'a1' |
|---|
| 87 | |
|---|
| 88 | def test_m2o_non_pk_target(self): |
|---|
| 89 | a_table = Table('a', metadata, |
|---|
| 90 | Column('id', Integer, primary_key=True), |
|---|
| 91 | Column('name', String(60), unique=True) |
|---|
| 92 | ) |
|---|
| 93 | a_table.create() |
|---|
| 94 | |
|---|
| 95 | class A(object): |
|---|
| 96 | pass |
|---|
| 97 | |
|---|
| 98 | mapper(A, a_table) |
|---|
| 99 | |
|---|
| 100 | class B(Entity): |
|---|
| 101 | name = Field(String(60)) |
|---|
| 102 | a = ManyToOne(A, target_column=['name']) |
|---|
| 103 | # currently fails |
|---|
| 104 | # c = ManyToOne('C', target_column=['id', 'name']) |
|---|
| 105 | |
|---|
| 106 | # class C(Entity): |
|---|
| 107 | # name = Field(String(60), unique=True) |
|---|
| 108 | |
|---|
| 109 | setup_all(True) |
|---|
| 110 | |
|---|
| 111 | a1 = A() |
|---|
| 112 | a1.name = 'a1' |
|---|
| 113 | b1 = B(name='b1', a=a1) |
|---|
| 114 | |
|---|
| 115 | session.commit() |
|---|
| 116 | session.expunge_all() |
|---|
| 117 | |
|---|
| 118 | b = B.query.one() |
|---|
| 119 | |
|---|
| 120 | assert b.a.name == 'a1' |
|---|
| 121 | |
|---|
| 122 | # def test_m2m(self): |
|---|
| 123 | # a_table = Table('a', metadata, |
|---|
| 124 | # Column('id', Integer, primary_key=True), |
|---|
| 125 | # Column('name', String(60), unique=True) |
|---|
| 126 | # ) |
|---|
| 127 | # a_table.create() |
|---|
| 128 | # |
|---|
| 129 | # class A(object): |
|---|
| 130 | # pass |
|---|
| 131 | # |
|---|
| 132 | # mapper(A, a_table) |
|---|
| 133 | # |
|---|
| 134 | # class B(Entity): |
|---|
| 135 | # name = Field(String(60)) |
|---|
| 136 | # many_a = ManyToMany(A) |
|---|
| 137 | # |
|---|
| 138 | # setup_all(True) |
|---|
| 139 | # |
|---|
| 140 | # a1 = A() |
|---|
| 141 | # a1.name = 'a1' |
|---|
| 142 | # b1 = B(name='b1', many_a=[a1]) |
|---|
| 143 | # |
|---|
| 144 | # session.commit() |
|---|
| 145 | # session.expunge_all() |
|---|
| 146 | # |
|---|
| 147 | # b = B.query.one() |
|---|
| 148 | # |
|---|
| 149 | # assert b.many_a[0].name == 'a1' |
|---|