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

Method test_dict

test/orm/test_collection.py:2417–2458  ·  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

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