1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import testing
from sqlalchemy.orm import attributes
from sqlalchemy.orm import class_mapper
from sqlalchemy.orm import exc as orm_exc
from sqlalchemy.orm import sync
from sqlalchemy.orm import unitofwork
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
from sqlalchemy.testing.fixtures import fixture_session
from sqlalchemy.testing.schema import Column
from sqlalchemy.testing.schema import Table
class AssertsUOW:
def _get_test_uow(self, session):
uow = unitofwork.UOWTransaction(session)
deleted = set(session._deleted)
new = set(session._new)
dirty = set(session._dirty_states).difference(deleted)
for s in new.union(dirty):
uow.register_object(s)
for d in deleted:
uow.register_object(d, isdelete=True)
return uow
class SyncTest(
fixtures.MappedTest, testing.AssertsExecutionResults, AssertsUOW
):
@classmethod
def define_tables(cls, metadata):
Table(
"t1",
metadata,
Column("id", Integer, primary_key=True),
Column("foo", Integer),
)
Table(
"t2",
metadata,
Column("id", Integer, ForeignKey("t1.id"), primary_key=True),
Column("t1id", Integer, ForeignKey("t1.id")),
)
@classmethod
def setup_classes(cls):
class A(cls.Basic):
pass
class B(cls.Basic):
pass
@classmethod
def setup_mappers(cls):
cls.mapper_registry.map_imperatively(cls.classes.A, cls.tables.t1)
cls.mapper_registry.map_imperatively(cls.classes.B, cls.tables.t2)
def _fixture(self):
A, B = self.classes.A, self.classes.B
session = fixture_session()
uowcommit = self._get_test_uow(session)
a_mapper = class_mapper(A)
b_mapper = class_mapper(B)
self.a1 = a1 = A()
self.b1 = b1 = B()
uowcommit = self._get_test_uow(session)
return (
uowcommit,
attributes.instance_state(a1),
attributes.instance_state(b1),
a_mapper,
b_mapper,
)
def test_populate(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(a_mapper.c.id, b_mapper.c.id)]
a1.obj().id = 7
assert "id" not in b1.obj().__dict__
sync.populate(a1, a_mapper, b1, b_mapper, pairs, uowcommit, False)
eq_(b1.obj().id, 7)
eq_(b1.obj().__dict__["id"], 7)
assert ("pk_cascaded", b1, b_mapper.c.id) not in uowcommit.attributes
def test_populate_flag_cascaded(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(a_mapper.c.id, b_mapper.c.id)]
a1.obj().id = 7
assert "id" not in b1.obj().__dict__
sync.populate(a1, a_mapper, b1, b_mapper, pairs, uowcommit, True)
eq_(b1.obj().id, 7)
eq_(b1.obj().__dict__["id"], 7)
eq_(uowcommit.attributes[("pk_cascaded", b1, b_mapper.c.id)], True)
def test_populate_unmapped_source(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(b_mapper.c.id, b_mapper.c.id)]
assert_raises_message(
orm_exc.UnmappedColumnError,
"Can't execute sync rule for source column 't2.id'; "
r"mapper 'Mapper\[A\(t1\)\]' does not map this column.",
sync.populate,
a1,
a_mapper,
b1,
b_mapper,
pairs,
uowcommit,
False,
)
def test_populate_unmapped_dest(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(a_mapper.c.id, a_mapper.c.id)]
assert_raises_message(
orm_exc.UnmappedColumnError,
r"Can't execute sync rule for destination "
r"column 't1.id'; "
r"mapper 'Mapper\[B\(t2\)\]' does not map this column.",
sync.populate,
a1,
a_mapper,
b1,
b_mapper,
pairs,
uowcommit,
False,
)
def test_clear(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(a_mapper.c.id, b_mapper.c.t1id)]
b1.obj().t1id = 8
eq_(b1.obj().__dict__["t1id"], 8)
sync.clear(b1, b_mapper, pairs)
eq_(b1.obj().__dict__["t1id"], None)
def test_clear_pk(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(a_mapper.c.id, b_mapper.c.id)]
b1.obj().id = 8
eq_(b1.obj().__dict__["id"], 8)
assert_raises_message(
AssertionError,
"Dependency rule tried to blank-out primary key "
"column 't2.id' on instance '<B",
sync.clear,
b1,
b_mapper,
pairs,
)
def test_clear_unmapped(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(a_mapper.c.id, a_mapper.c.foo)]
assert_raises_message(
orm_exc.UnmappedColumnError,
"Can't execute sync rule for destination "
r"column 't1.foo'; mapper 'Mapper\[B\(t2\)\]' does not "
"map this column.",
sync.clear,
b1,
b_mapper,
pairs,
)
def test_update(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
a1.obj().id = 10
a1._commit_all(a1.dict)
a1.obj().id = 12
pairs = [(a_mapper.c.id, b_mapper.c.id)]
dest = {}
sync.update(a1, a_mapper, dest, "old_", pairs)
eq_(dest, {"id": 12, "old_id": 10})
def test_update_unmapped(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(b_mapper.c.id, b_mapper.c.id)]
dest = {}
assert_raises_message(
orm_exc.UnmappedColumnError,
"Can't execute sync rule for source column 't2.id'; "
r"mapper 'Mapper\[A\(t1\)\]' does not map this column.",
sync.update,
a1,
a_mapper,
dest,
"old_",
pairs,
)
def test_populate_dict(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
a1.obj().id = 10
pairs = [(a_mapper.c.id, b_mapper.c.id)]
dest = {}
sync.populate_dict(a1, a_mapper, dest, pairs)
eq_(dest, {"id": 10})
def test_populate_dict_unmapped(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
a1.obj().id = 10
pairs = [(b_mapper.c.id, b_mapper.c.id)]
dest = {}
assert_raises_message(
orm_exc.UnmappedColumnError,
"Can't execute sync rule for source column 't2.id'; "
r"mapper 'Mapper\[A\(t1\)\]' does not map this column.",
sync.populate_dict,
a1,
a_mapper,
dest,
pairs,
)
def test_source_modified_unmodified(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
a1.obj().id = 10
pairs = [(a_mapper.c.id, b_mapper.c.id)]
eq_(sync.source_modified(uowcommit, a1, a_mapper, pairs), False)
def test_source_modified_no_pairs(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
eq_(sync.source_modified(uowcommit, a1, a_mapper, []), False)
def test_source_modified_modified(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
a1.obj().id = 10
a1._commit_all(a1.dict)
a1.obj().id = 12
pairs = [(a_mapper.c.id, b_mapper.c.id)]
eq_(sync.source_modified(uowcommit, a1, a_mapper, pairs), True)
def test_source_modified_composite(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
a1.obj().foo = 10
a1._commit_all(a1.dict)
a1.obj().foo = 12
pairs = [
(a_mapper.c.id, b_mapper.c.id),
(a_mapper.c.foo, b_mapper.c.id),
]
eq_(sync.source_modified(uowcommit, a1, a_mapper, pairs), True)
def test_source_modified_composite_unmodified(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
a1.obj().foo = 10
a1._commit_all(a1.dict)
pairs = [
(a_mapper.c.id, b_mapper.c.id),
(a_mapper.c.foo, b_mapper.c.id),
]
eq_(sync.source_modified(uowcommit, a1, a_mapper, pairs), False)
def test_source_modified_no_unmapped(self):
uowcommit, a1, b1, a_mapper, b_mapper = self._fixture()
pairs = [(b_mapper.c.id, b_mapper.c.id)]
assert_raises_message(
orm_exc.UnmappedColumnError,
"Can't execute sync rule for source column 't2.id'; "
r"mapper 'Mapper\[A\(t1\)\]' does not map this column.",
sync.source_modified,
uowcommit,
a1,
a_mapper,
pairs,
)
|