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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
|
"""test the inspection registry system."""
from test.lib.testing import eq_, assert_raises_message, is_
from sqlalchemy import exc, util
from sqlalchemy import inspect
from test.orm import _fixtures
from sqlalchemy.orm import class_mapper, synonym, Session, aliased
from sqlalchemy.orm.attributes import instance_state, NO_VALUE
from test.lib import testing
class TestORMInspection(_fixtures.FixtureTest):
@classmethod
def setup_mappers(cls):
cls._setup_stock_mapping()
inspect(cls.classes.User).add_property(
"name_syn",synonym("name")
)
def test_class_mapper(self):
User = self.classes.User
assert inspect(User) is class_mapper(User)
def test_column_collection_iterate(self):
User = self.classes.User
user_table = self.tables.users
insp = inspect(User)
eq_(
list(insp.columns),
[user_table.c.id, user_table.c.name]
)
is_(
insp.columns.id, user_table.c.id
)
def test_primary_key(self):
User = self.classes.User
user_table = self.tables.users
insp = inspect(User)
eq_(insp.primary_key,
(user_table.c.id,)
)
def test_local_table(self):
User = self.classes.User
user_table = self.tables.users
insp = inspect(User)
is_(insp.local_table, user_table)
def test_mapped_table(self):
User = self.classes.User
user_table = self.tables.users
insp = inspect(User)
is_(insp.mapped_table, user_table)
def test_mapper_selectable(self):
User = self.classes.User
user_table = self.tables.users
insp = inspect(User)
is_(insp.selectable, user_table)
assert not insp.is_selectable
assert not insp.is_aliased_class
def test_aliased_class(self):
Address = self.classes.Address
ualias = aliased(Address)
insp = inspect(ualias)
is_(insp.mapper, inspect(Address))
is_(insp.selectable, ualias._AliasedClass__alias)
assert not insp.is_selectable
assert insp.is_aliased_class
def test_not_mapped_class(self):
class Foo(object):
pass
assert_raises_message(
exc.NoInspectionAvailable,
"No inspection system is available for object of type",
inspect, Foo
)
def test_not_mapped_instance(self):
class Foo(object):
pass
assert_raises_message(
exc.NoInspectionAvailable,
"No inspection system is available for object of type",
inspect, Foo()
)
def test_property(self):
User = self.classes.User
insp = inspect(User)
is_(insp.attr.id, class_mapper(User).get_property('id'))
def test_with_polymorphic(self):
User = self.classes.User
insp = inspect(User)
eq_(insp.with_polymorphic_mappers, [insp])
def test_col_property(self):
User = self.classes.User
user_table = self.tables.users
insp = inspect(User)
id_prop = insp.attr.id
eq_(id_prop.columns, [user_table.c.id])
is_(id_prop.expression, user_table.c.id)
assert not hasattr(id_prop, 'mapper')
def test_attr_keys(self):
User = self.classes.User
insp = inspect(User)
eq_(
set(insp.attr.keys()),
set(['addresses', 'orders', 'id', 'name', 'name_syn'])
)
def test_col_filter(self):
User = self.classes.User
insp = inspect(User)
eq_(
list(insp.column_attrs),
[insp.get_property('id'), insp.get_property('name')]
)
eq_(
insp.column_attrs.keys(),
['id', 'name']
)
is_(
insp.column_attrs.id,
User.id.property
)
def test_synonym_filter(self):
User = self.classes.User
syn = inspect(User).synonyms
eq_(
list(syn.keys()), ['name_syn']
)
is_(syn.name_syn, User.name_syn.original_property)
eq_(dict(syn), {
"name_syn":User.name_syn.original_property
})
def test_relationship_filter(self):
User = self.classes.User
rel = inspect(User).relationships
eq_(
rel.addresses,
User.addresses.property
)
eq_(
set(rel.keys()),
set(['orders', 'addresses'])
)
def test_insp_prop(self):
User = self.classes.User
prop = inspect(User.addresses)
is_(prop, User.addresses.property)
def test_rel_accessors(self):
User = self.classes.User
Address = self.classes.Address
prop = inspect(User.addresses)
is_(prop.parent, class_mapper(User))
is_(prop.mapper, class_mapper(Address))
assert not hasattr(prop, 'columns')
assert not hasattr(prop, 'expression')
def test_instance_state(self):
User = self.classes.User
u1 = User()
insp = inspect(u1)
is_(insp, instance_state(u1))
def test_instance_state_attr(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
eq_(
set(insp.attr.keys()),
set(['id', 'name', 'name_syn', 'addresses', 'orders'])
)
eq_(
insp.attr.name.value,
'ed'
)
eq_(
insp.attr.name.loaded_value,
'ed'
)
def test_instance_state_attr_passive_value_scalar(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
# value was not set, NO_VALUE
eq_(
insp.attr.id.loaded_value,
NO_VALUE
)
# regular accessor sets it
eq_(
insp.attr.id.value,
None
)
# now the None is there
eq_(
insp.attr.id.loaded_value,
None
)
def test_instance_state_attr_passive_value_collection(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
# value was not set, NO_VALUE
eq_(
insp.attr.addresses.loaded_value,
NO_VALUE
)
# regular accessor sets it
eq_(
insp.attr.addresses.value,
[]
)
# now the None is there
eq_(
insp.attr.addresses.loaded_value,
[]
)
def test_instance_state_attr_hist(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
hist = insp.attr.addresses.history
eq_(
hist.unchanged, None
)
u1.addresses
hist = insp.attr.addresses.history
eq_(
hist.unchanged, []
)
def test_instance_state_ident_transient(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
is_(insp.identity, None)
def test_instance_state_ident_persistent(self):
User = self.classes.User
u1 = User(name='ed')
s = Session(testing.db)
s.add(u1)
s.flush()
insp = inspect(u1)
eq_(insp.identity, (u1.id,))
is_(s.query(User).get(insp.identity), u1)
def test_is_instance(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
assert insp.is_instance
insp = inspect(User)
assert not insp.is_instance
insp = inspect(aliased(User))
assert not insp.is_instance
def test_identity_key(self):
User = self.classes.User
u1 = User(name='ed')
s = Session(testing.db)
s.add(u1)
s.flush()
insp = inspect(u1)
eq_(
insp.identity_key,
(User, (u1.id, ))
)
def test_persistence_states(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
eq_(
(insp.transient, insp.pending,
insp.persistent, insp.detached),
(True, False, False, False)
)
s = Session(testing.db)
s.add(u1)
eq_(
(insp.transient, insp.pending,
insp.persistent, insp.detached),
(False, True, False, False)
)
s.flush()
eq_(
(insp.transient, insp.pending,
insp.persistent, insp.detached),
(False, False, True, False)
)
s.expunge(u1)
eq_(
(insp.transient, insp.pending,
insp.persistent, insp.detached),
(False, False, False, True)
)
def test_session_accessor(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
is_(insp.session, None)
s = Session()
s.add(u1)
is_(insp.session, s)
def test_object_accessor(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
is_(insp.object, u1)
|