MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / _test_dict_bulk

Method _test_dict_bulk

test/orm/test_collection.py:1393–1449  ·  view source on GitHub ↗
(self, typecallable, creator=None)

Source from the content-addressed store, hash-verified

1391 assert_eq()
1392
1393 def _test_dict_bulk(self, typecallable, creator=None):
1394 if creator is None:
1395 creator = self.dictable_entity
1396
1397 class Foo:
1398 pass
1399
1400 canary = Canary()
1401 instrumentation.register_class(Foo)
1402 d = _register_attribute(
1403 Foo,
1404 "attr",
1405 uselist=True,
1406 typecallable=typecallable,
1407 useobject=True,
1408 )
1409 canary.listen(d)
1410
1411 obj = Foo()
1412 direct = obj.attr
1413
1414 e1 = creator()
1415 collections.collection_adapter(direct).append_with_event(e1)
1416
1417 like_me = typecallable()
1418 e2 = creator()
1419 like_me.set(e2)
1420
1421 self.assert_(obj.attr is direct)
1422 obj.attr = like_me
1423 self.assert_(obj.attr is not direct)
1424 self.assert_(obj.attr is not like_me)
1425 self.assert_(set(collections.collection_adapter(obj.attr)) == {e2})
1426 self.assert_(e1 in canary.removed)
1427 self.assert_(e2 in canary.added)
1428
1429 # key validity on bulk assignment is a basic feature of
1430 # MappedCollection but is not present in basic, @converter-less
1431 # dict collections.
1432 e3 = creator()
1433 real_dict = dict(keyignored1=e3)
1434 obj.attr = real_dict
1435 self.assert_(obj.attr is not real_dict)
1436 self.assert_("keyignored1" not in obj.attr)
1437 eq_(set(collections.collection_adapter(obj.attr)), {e3})
1438 self.assert_(e2 in canary.removed)
1439 self.assert_(e3 in canary.added)
1440
1441 obj.attr = typecallable()
1442 eq_(list(collections.collection_adapter(obj.attr)), [])
1443
1444 e4 = creator()
1445 try:
1446 obj.attr = [e4]
1447 self.assert_(False)
1448 except TypeError:
1449 self.assert_(e4 not in canary.data)
1450

Callers 5

test_dict_subclassMethod · 0.95
test_dict_subclass2Method · 0.95
test_dict_subclass3Method · 0.95
test_dict_duckMethod · 0.95
test_dict_emulatesMethod · 0.95

Calls 9

listenMethod · 0.95
eq_Function · 0.90
CanaryClass · 0.85
creatorFunction · 0.85
append_with_eventMethod · 0.80
_register_attributeFunction · 0.70
FooClass · 0.70
setMethod · 0.45
assert_Method · 0.45

Tested by

no test coverage detected