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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
|
"""test the inspection registry system."""
from sqlalchemy.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 sqlalchemy 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_mapper_selectable_fixed(self):
from sqlalchemy.orm import mapper
class Foo(object):
pass
class Bar(Foo):
pass
user_table = self.tables.users
addresses_table = self.tables.addresses
mapper(Foo, user_table, with_polymorphic=(Bar,))
mapper(Bar, addresses_table, inherits=Foo, properties={
'address_id': addresses_table.c.id
})
i1 = inspect(Foo)
i2 = inspect(Foo)
assert i1.selectable is i2.selectable
def test_aliased_class(self):
Address = self.classes.Address
ualias = aliased(Address)
insp = inspect(ualias)
is_(insp.mapper, inspect(Address))
is_(insp.selectable, ualias._aliased_insp.selectable)
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.attrs.id, class_mapper(User).get_property('id'))
def test_with_polymorphic(self):
User = self.classes.User
insp = inspect(User)
eq_(insp.with_polymorphic_mappers, [])
def test_col_property(self):
User = self.classes.User
user_table = self.tables.users
insp = inspect(User)
id_prop = insp.attrs.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_(
list(insp.attrs.keys()),
['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_(
list(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_relationship_prop(self):
User = self.classes.User
Address = self.classes.Address
prop = inspect(User.addresses)
is_(prop, User.addresses)
is_(prop.parent, class_mapper(User))
is_(prop._parentmapper, class_mapper(User))
is_(prop.mapper, class_mapper(Address))
def test_insp_aliased_relationship_prop(self):
User = self.classes.User
Address = self.classes.Address
ua = aliased(User)
prop = inspect(ua.addresses)
is_(prop, ua.addresses)
is_(prop.property.parent.mapper, class_mapper(User))
is_(prop.property.mapper, class_mapper(Address))
is_(prop.parent.entity, ua)
is_(prop.parent.class_, User)
is_(prop._parentmapper, class_mapper(User))
is_(prop.mapper, class_mapper(Address))
is_(prop._parententity, inspect(ua))
def test_insp_column_prop(self):
User = self.classes.User
prop = inspect(User.name)
is_(prop, User.name)
is_(prop.parent, class_mapper(User))
assert not hasattr(prop, "mapper")
def test_insp_aliased_column_prop(self):
User = self.classes.User
ua = aliased(User)
prop = inspect(ua.name)
is_(prop, ua.name)
is_(prop.property.parent.mapper, class_mapper(User))
assert not hasattr(prop.property, "mapper")
is_(prop.parent.entity, ua)
is_(prop.parent.class_, User)
is_(prop._parentmapper, class_mapper(User))
assert not hasattr(prop, "mapper")
is_(prop._parententity, inspect(ua))
def test_rel_accessors(self):
User = self.classes.User
Address = self.classes.Address
prop = inspect(User.addresses)
is_(prop.property.parent, class_mapper(User))
is_(prop.property.mapper, class_mapper(Address))
is_(prop.parent, class_mapper(User))
is_(prop.mapper, class_mapper(Address))
assert not hasattr(prop, 'columns')
assert hasattr(prop, 'expression')
def test_extension_types(self):
from sqlalchemy.ext.associationproxy import \
association_proxy, ASSOCIATION_PROXY
from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method, \
HYBRID_PROPERTY, HYBRID_METHOD
from sqlalchemy import Table, MetaData, Integer, Column
from sqlalchemy.orm import mapper
from sqlalchemy.orm.interfaces import NOT_EXTENSION
class SomeClass(self.classes.User):
some_assoc = association_proxy('addresses', 'email_address')
@hybrid_property
def upper_name(self):
raise NotImplementedError()
@hybrid_method
def conv(self, fn):
raise NotImplementedError()
class SomeSubClass(SomeClass):
@hybrid_property
def upper_name(self):
raise NotImplementedError()
@hybrid_property
def foo(self):
raise NotImplementedError()
t = Table('sometable', MetaData(),
Column('id', Integer, primary_key=True))
mapper(SomeClass, t)
mapper(SomeSubClass, inherits=SomeClass)
insp = inspect(SomeSubClass)
eq_(
dict((k, v.extension_type)
for k, v in list(insp.all_orm_descriptors.items())
),
{
'id': NOT_EXTENSION,
'name': NOT_EXTENSION,
'name_syn': NOT_EXTENSION,
'addresses': NOT_EXTENSION,
'orders': NOT_EXTENSION,
'upper_name': HYBRID_PROPERTY,
'foo': HYBRID_PROPERTY,
'conv': HYBRID_METHOD,
'some_assoc': ASSOCIATION_PROXY
}
)
is_(
insp.all_orm_descriptors.upper_name,
SomeSubClass.__dict__['upper_name']
)
is_(
insp.all_orm_descriptors.some_assoc,
SomeClass.some_assoc
)
is_(
inspect(SomeClass).all_orm_descriptors.upper_name,
SomeClass.__dict__['upper_name']
)
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.attrs.keys()),
set(['id', 'name', 'name_syn', 'addresses', 'orders'])
)
eq_(
insp.attrs.name.value,
'ed'
)
eq_(
insp.attrs.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.attrs.id.loaded_value,
NO_VALUE
)
# regular accessor sets it
eq_(
insp.attrs.id.value,
None
)
# nope, still not set
eq_(
insp.attrs.id.loaded_value,
NO_VALUE
)
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.attrs.addresses.loaded_value,
NO_VALUE
)
# regular accessor sets it
eq_(
insp.attrs.addresses.value,
[]
)
# now the None is there
eq_(
insp.attrs.addresses.loaded_value,
[]
)
def test_instance_state_collection_attr_hist(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
hist = insp.attrs.addresses.history
eq_(
hist.unchanged, None
)
u1.addresses
hist = insp.attrs.addresses.history
eq_(
hist.unchanged, []
)
def test_instance_state_scalar_attr_hist(self):
User = self.classes.User
u1 = User(name='ed')
sess = Session()
sess.add(u1)
sess.commit()
assert 'name' not in u1.__dict__
insp = inspect(u1)
hist = insp.attrs.name.history
eq_(
hist.unchanged, None
)
assert 'name' not in u1.__dict__
def test_instance_state_collection_attr_load_hist(self):
User = self.classes.User
u1 = User(name='ed')
insp = inspect(u1)
hist = insp.attrs.addresses.load_history()
eq_(
hist.unchanged, ()
)
u1.addresses
hist = insp.attrs.addresses.load_history()
eq_(
hist.unchanged, []
)
def test_instance_state_scalar_attr_hist_load(self):
User = self.classes.User
u1 = User(name='ed')
sess = Session()
sess.add(u1)
sess.commit()
assert 'name' not in u1.__dict__
insp = inspect(u1)
hist = insp.attrs.name.load_history()
eq_(
hist.unchanged, ['ed']
)
assert 'name' in u1.__dict__
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)
|