MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_custom_getset

Method test_custom_getset

test/ext/test_associationproxy.py:1272–1310  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

1270 assert a1.b_single is None
1271
1272 def test_custom_getset(self):
1273 metadata = MetaData()
1274 p = Table(
1275 "p",
1276 metadata,
1277 Column("id", Integer, primary_key=True),
1278 Column("cid", Integer, ForeignKey("c.id")),
1279 )
1280 c = Table(
1281 "c",
1282 metadata,
1283 Column("id", Integer, primary_key=True),
1284 Column("foo", String(128)),
1285 )
1286
1287 get = Mock()
1288 set_ = Mock()
1289
1290 class Parent:
1291 foo = association_proxy(
1292 "child", "foo", getset_factory=lambda cc, parent: (get, set_)
1293 )
1294
1295 class Child:
1296 def __init__(self, foo):
1297 self.foo = foo
1298
1299 self.mapper_registry.map_imperatively(
1300 Parent, p, properties={"child": relationship(Child)}
1301 )
1302 self.mapper_registry.map_imperatively(Child, c)
1303
1304 p1 = Parent()
1305
1306 eq_(p1.foo, get(None))
1307 p1.child = child = Child(foo="x")
1308 eq_(p1.foo, get(child))
1309 p1.foo = "y"
1310 eq_(set_.mock_calls, [call(child, "y")])
1311
1312
1313class LazyLoadTest(fixtures.MappedTest):

Callers

nothing calls this directly

Calls 12

MetaDataClass · 0.90
TableClass · 0.90
ColumnClass · 0.90
ForeignKeyClass · 0.90
StringClass · 0.90
relationshipFunction · 0.90
eq_Function · 0.90
getFunction · 0.85
callFunction · 0.85
map_imperativelyMethod · 0.80
ParentClass · 0.70
ChildClass · 0.70

Tested by

no test coverage detected