| 1 | """ |
|---|
| 2 | test many to one relationships |
|---|
| 3 | """ |
|---|
| 4 | |
|---|
| 5 | from elixir import * |
|---|
| 6 | |
|---|
| 7 | def setup(): |
|---|
| 8 | metadata.bind = 'sqlite://' |
|---|
| 9 | |
|---|
| 10 | class TestManyToOne(object): |
|---|
| 11 | def teardown(self): |
|---|
| 12 | cleanup_all(True) |
|---|
| 13 | |
|---|
| 14 | def test_simple(self): |
|---|
| 15 | class A(Entity): |
|---|
| 16 | name = Field(String(60)) |
|---|
| 17 | |
|---|
| 18 | class B(Entity): |
|---|
| 19 | name = Field(String(60)) |
|---|
| 20 | a = ManyToOne('A') |
|---|
| 21 | |
|---|
| 22 | setup_all(True) |
|---|
| 23 | |
|---|
| 24 | b1 = B(name='b1', a=A(name='a1')) |
|---|
| 25 | |
|---|
| 26 | session.commit() |
|---|
| 27 | session.clear() |
|---|
| 28 | |
|---|
| 29 | b = B.query.one() |
|---|
| 30 | |
|---|
| 31 | assert b.a.name == 'a1' |
|---|
| 32 | |
|---|
| 33 | # this test is in test_o2m.py |
|---|
| 34 | # def test_selfref(self): |
|---|
| 35 | |
|---|
| 36 | def test_with_key_pk(self): |
|---|
| 37 | class A(Entity): |
|---|
| 38 | test = Field(Integer, primary_key=True, key='testx') |
|---|
| 39 | |
|---|
| 40 | class B(Entity): |
|---|
| 41 | a = ManyToOne('A') |
|---|
| 42 | |
|---|
| 43 | setup_all(True) |
|---|
| 44 | |
|---|
| 45 | b1 = B(a=A(testx=1)) |
|---|
| 46 | |
|---|
| 47 | session.commit() |
|---|
| 48 | session.clear() |
|---|
| 49 | |
|---|
| 50 | b = B.query.one() |
|---|
| 51 | |
|---|
| 52 | assert b.a.testx == 1 |
|---|
| 53 | |
|---|
| 54 | def test_wh_key_in_m2o_col_kwargs(self): |
|---|
| 55 | class A(Entity): |
|---|
| 56 | name = Field(String(128), default="foo") |
|---|
| 57 | |
|---|
| 58 | class B(Entity): |
|---|
| 59 | # specify a different key for the column so that |
|---|
| 60 | # it doesn't override the property when the column |
|---|
| 61 | # gets created. |
|---|
| 62 | a = ManyToOne('A', colname='a', |
|---|
| 63 | column_kwargs=dict(key='a_id')) |
|---|
| 64 | |
|---|
| 65 | setup_all(True) |
|---|
| 66 | |
|---|
| 67 | assert 'id' in A.table.primary_key.columns |
|---|
| 68 | assert 'a_id' in B.table.columns |
|---|
| 69 | |
|---|
| 70 | a = A() |
|---|
| 71 | session.commit() |
|---|
| 72 | b = B(a=a) |
|---|
| 73 | session.commit() |
|---|
| 74 | session.clear() |
|---|
| 75 | |
|---|
| 76 | assert B.query.first().a == A.query.first() |
|---|
| 77 | |
|---|
| 78 | def test_specified_field(self): |
|---|
| 79 | class Person(Entity): |
|---|
| 80 | name = Field(String(30)) |
|---|
| 81 | |
|---|
| 82 | class Animal(Entity): |
|---|
| 83 | name = Field(String(30)) |
|---|
| 84 | owner_id = Field(Integer, colname='owner') |
|---|
| 85 | owner = ManyToOne('Person', field=owner_id) |
|---|
| 86 | |
|---|
| 87 | setup_all(True) |
|---|
| 88 | |
|---|
| 89 | assert 'owner' in Animal.table.c |
|---|
| 90 | assert 'owner_id' not in Animal.table.c |
|---|
| 91 | |
|---|
| 92 | homer = Person(name="Homer") |
|---|
| 93 | slh = Animal(name="Santa's Little Helper", owner=homer) |
|---|
| 94 | |
|---|
| 95 | session.commit() |
|---|
| 96 | session.clear() |
|---|
| 97 | |
|---|
| 98 | homer = Person.get_by(name="Homer") |
|---|
| 99 | animals = Animal.query.all() |
|---|
| 100 | assert animals[0].owner is homer |
|---|
| 101 | |
|---|
| 102 | def test_one_pk(self): |
|---|
| 103 | class A(Entity): |
|---|
| 104 | name = Field(String(40), primary_key=True) |
|---|
| 105 | |
|---|
| 106 | class B(Entity): |
|---|
| 107 | a = ManyToOne('A', primary_key=True) |
|---|
| 108 | |
|---|
| 109 | class C(Entity): |
|---|
| 110 | b = ManyToOne('B', primary_key=True) |
|---|
| 111 | |
|---|
| 112 | setup_all() |
|---|
| 113 | |
|---|
| 114 | assert 'name' in A.table.primary_key.columns |
|---|
| 115 | assert 'a_name' in B.table.primary_key.columns |
|---|
| 116 | assert 'b_a_name' in C.table.primary_key.columns |
|---|
| 117 | |
|---|
| 118 | def test_m2o_is_only_pk(self): |
|---|
| 119 | class A(Entity): |
|---|
| 120 | pass |
|---|
| 121 | |
|---|
| 122 | class B(Entity): |
|---|
| 123 | a = ManyToOne('A', primary_key=True) |
|---|
| 124 | |
|---|
| 125 | setup_all() |
|---|
| 126 | |
|---|
| 127 | assert 'id' in A.table.primary_key.columns |
|---|
| 128 | assert 'a_id' in B.table.primary_key.columns |
|---|
| 129 | assert 'id' not in B.table.primary_key.columns |
|---|
| 130 | |
|---|
| 131 | def test_multi_pk_in_target(self): |
|---|
| 132 | class A(Entity): |
|---|
| 133 | key1 = Field(Integer, primary_key=True) |
|---|
| 134 | key2 = Field(String(40), primary_key=True) |
|---|
| 135 | |
|---|
| 136 | class B(Entity): |
|---|
| 137 | num = Field(Integer, primary_key=True) |
|---|
| 138 | a = ManyToOne('A', primary_key=True) |
|---|
| 139 | |
|---|
| 140 | class C(Entity): |
|---|
| 141 | num = Field(Integer, primary_key=True) |
|---|
| 142 | b = ManyToOne('B', primary_key=True) |
|---|
| 143 | |
|---|
| 144 | setup_all() |
|---|
| 145 | |
|---|
| 146 | assert 'key1' in A.table.primary_key.columns |
|---|
| 147 | assert 'key2' in A.table.primary_key.columns |
|---|
| 148 | |
|---|
| 149 | assert 'num' in B.table.primary_key.columns |
|---|
| 150 | assert 'a_key1' in B.table.primary_key.columns |
|---|
| 151 | assert 'a_key2' in B.table.primary_key.columns |
|---|
| 152 | |
|---|
| 153 | assert 'num' in C.table.primary_key.columns |
|---|
| 154 | assert 'b_num' in C.table.primary_key.columns |
|---|
| 155 | assert 'b_a_key1' in C.table.primary_key.columns |
|---|
| 156 | assert 'b_a_key2' in C.table.primary_key.columns |
|---|
| 157 | |
|---|
| 158 | def test_cycle_but_use_alter(self): |
|---|
| 159 | class A(Entity): |
|---|
| 160 | c = ManyToOne('C', use_alter=True) |
|---|
| 161 | |
|---|
| 162 | class B(Entity): |
|---|
| 163 | a = ManyToOne('A', primary_key=True) |
|---|
| 164 | |
|---|
| 165 | class C(Entity): |
|---|
| 166 | b = ManyToOne('B', primary_key=True) |
|---|
| 167 | |
|---|
| 168 | setup_all() |
|---|
| 169 | |
|---|
| 170 | assert 'a_id' in B.table.primary_key.columns |
|---|
| 171 | assert 'b_a_id' in C.table.primary_key.columns |
|---|
| 172 | assert 'id' in A.table.primary_key.columns |
|---|
| 173 | assert 'c_b_a_id' in A.table.columns |
|---|
| 174 | |
|---|
| 175 | def test_multi(self): |
|---|
| 176 | class A(Entity): |
|---|
| 177 | name = Field(String(32)) |
|---|
| 178 | |
|---|
| 179 | class B(Entity): |
|---|
| 180 | name = Field(String(15)) |
|---|
| 181 | |
|---|
| 182 | a_rel1 = ManyToOne('A') |
|---|
| 183 | a_rel2 = ManyToOne('A') |
|---|
| 184 | |
|---|
| 185 | setup_all(True) |
|---|
| 186 | |
|---|
| 187 | a1 = A(name="a1") |
|---|
| 188 | a2 = A(name="a2") |
|---|
| 189 | b1 = B(name="b1", a_rel1=a1, a_rel2=a2) |
|---|
| 190 | b2 = B(name="b2", a_rel1=a1, a_rel2=a1) |
|---|
| 191 | |
|---|
| 192 | session.commit() |
|---|
| 193 | session.clear() |
|---|
| 194 | |
|---|
| 195 | a1 = A.get_by(name="a1") |
|---|
| 196 | a2 = A.get_by(name="a2") |
|---|
| 197 | b1 = B.get_by(name="b1") |
|---|
| 198 | b2 = B.get_by(name="b2") |
|---|
| 199 | |
|---|
| 200 | assert a1 == b2.a_rel1 |
|---|
| 201 | assert a2 == b1.a_rel2 |
|---|
| 202 | |
|---|
| 203 | def test_non_pk_target(self): |
|---|
| 204 | class A(Entity): |
|---|
| 205 | name = Field(String(60), unique=True) |
|---|
| 206 | |
|---|
| 207 | class B(Entity): |
|---|
| 208 | name = Field(String(60)) |
|---|
| 209 | a = ManyToOne('A', target_column=['id', 'name']) |
|---|
| 210 | # currently fails |
|---|
| 211 | # c = ManyToOne('C', target_column=['id', 'name']) |
|---|
| 212 | |
|---|
| 213 | # class C(Entity): |
|---|
| 214 | # name = Field(String(60), unique=True) |
|---|
| 215 | |
|---|
| 216 | setup_all(True) |
|---|
| 217 | |
|---|
| 218 | b1 = B(name='b1', a=A(name='a1')) |
|---|
| 219 | |
|---|
| 220 | session.commit() |
|---|
| 221 | session.clear() |
|---|
| 222 | |
|---|
| 223 | b = B.query.one() |
|---|
| 224 | |
|---|
| 225 | assert b.a.name == 'a1' |
|---|
| 226 | |
|---|
| 227 | def test_belongs_to_syntax(self): |
|---|
| 228 | class Person(Entity): |
|---|
| 229 | has_field('name', String(30)) |
|---|
| 230 | |
|---|
| 231 | class Animal(Entity): |
|---|
| 232 | has_field('name', String(30)) |
|---|
| 233 | belongs_to('owner', of_kind='Person') |
|---|
| 234 | |
|---|
| 235 | setup_all(True) |
|---|
| 236 | |
|---|
| 237 | santa = Person(name="Santa Claus") |
|---|
| 238 | rudolph = Animal(name="Rudolph", owner=santa) |
|---|
| 239 | |
|---|
| 240 | session.commit() |
|---|
| 241 | session.clear() |
|---|
| 242 | |
|---|
| 243 | assert "Claus" in Animal.get_by(name="Rudolph").owner.name |
|---|