MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_state_noload_to_lazy

Method test_state_noload_to_lazy

test/orm/test_deprecations.py:2356–2413  ·  view source on GitHub ↗

Behavioral test to verify the current activity of loader callables

(self)

Source from the content-addressed store, hash-verified

2354
2355class ExpireTest(_fixtures.FixtureTest):
2356 def test_state_noload_to_lazy(self):
2357 """Behavioral test to verify the current activity of
2358 loader callables
2359
2360 """
2361
2362 users, Address, addresses, User = (
2363 self.tables.users,
2364 self.classes.Address,
2365 self.tables.addresses,
2366 self.classes.User,
2367 )
2368
2369 self.mapper_registry.map_imperatively(
2370 User,
2371 users,
2372 properties={"addresses": relationship(Address, lazy="noload")},
2373 )
2374 self.mapper_registry.map_imperatively(Address, addresses)
2375
2376 sess = fixture_session(autoflush=False)
2377 with expect_noload_deprecation():
2378 u1 = sess.query(User).options(lazyload(User.addresses)).first()
2379 assert isinstance(
2380 attributes.instance_state(u1).callables["addresses"],
2381 strategies._LoadLazyAttribute,
2382 )
2383 # expire, it goes away from callables as of 1.4 and is considered
2384 # to be expired
2385 sess.expire(u1)
2386
2387 assert "addresses" in attributes.instance_state(u1).expired_attributes
2388 assert "addresses" not in attributes.instance_state(u1).callables
2389
2390 # load it
2391 sess.query(User).first()
2392 assert (
2393 "addresses" not in attributes.instance_state(u1).expired_attributes
2394 )
2395 assert "addresses" not in attributes.instance_state(u1).callables
2396
2397 sess.expunge_all()
2398 u1 = sess.query(User).options(lazyload(User.addresses)).first()
2399 sess.expire(u1, ["addresses"])
2400 assert (
2401 "addresses" not in attributes.instance_state(u1).expired_attributes
2402 )
2403 assert isinstance(
2404 attributes.instance_state(u1).callables["addresses"],
2405 strategies._LoadLazyAttribute,
2406 )
2407
2408 # load the attr, goes away
2409 u1.addresses
2410 assert (
2411 "addresses" not in attributes.instance_state(u1).expired_attributes
2412 )
2413 assert "addresses" not in attributes.instance_state(u1).callables

Callers

nothing calls this directly

Calls 10

relationshipFunction · 0.90
fixture_sessionFunction · 0.90
lazyloadFunction · 0.90
map_imperativelyMethod · 0.80
firstMethod · 0.45
optionsMethod · 0.45
queryMethod · 0.45
expireMethod · 0.45
expunge_allMethod · 0.45

Tested by

no test coverage detected