MCPcopy Create free account
hub / github.com/zzzeek/sqlalchemy / test_dict

Method test_dict

test/orm/test_collection.py:2418–2459  ·  view source on GitHub ↗

test that a 'dict' can be used as a collection and can lazyload.

(self)

Source from the content-addressed store, hash-verified

2416 f.bars.clear()
2417
2418 def test_dict(self):
2419 """test that a 'dict' can be used as a collection and can lazyload."""
2420
2421 someothertable, sometable = (
2422 self.tables.someothertable,
2423 self.tables.sometable,
2424 )
2425
2426 class Foo:
2427 pass
2428
2429 class Bar:
2430 pass
2431
2432 class AppenderDict(dict):
2433 @collection.appender
2434 def set(self, item):
2435 self[id(item)] = item
2436
2437 @collection.remover
2438 def remove(self, item):
2439 if id(item) in self:
2440 del self[id(item)]
2441
2442 self.mapper_registry.map_imperatively(
2443 Foo,
2444 sometable,
2445 properties={
2446 "bars": relationship(Bar, collection_class=AppenderDict)
2447 },
2448 )
2449 self.mapper_registry.map_imperatively(Bar, someothertable)
2450 f = Foo()
2451 f.bars.set(Bar())
2452 f.bars.set(Bar())
2453 sess = fixture_session()
2454 sess.add(f)
2455 sess.flush()
2456 sess.expunge_all()
2457 f = sess.get(Foo, f.col1)
2458 assert len(list(f.bars)) == 2
2459 f.bars.clear()
2460
2461 def test_dict_wrapper(self):
2462 """test that the supplied 'dict' wrapper can be used as a

Callers

nothing calls this directly

Calls 11

relationshipFunction · 0.90
fixture_sessionFunction · 0.90
map_imperativelyMethod · 0.80
FooClass · 0.70
BarClass · 0.70
setMethod · 0.45
addMethod · 0.45
flushMethod · 0.45
expunge_allMethod · 0.45
getMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected