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

Method test_custom_getset

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

Source from the content-addressed store, hash-verified

1211 assert a1.b_single is None
1212
1213 def test_custom_getset(self):
1214 metadata = MetaData()
1215 p = Table(
1216 "p",
1217 metadata,
1218 Column("id", Integer, primary_key=True),
1219 Column("cid", Integer, ForeignKey("c.id")),
1220 )
1221 c = Table(
1222 "c",
1223 metadata,
1224 Column("id", Integer, primary_key=True),
1225 Column("foo", String(128)),
1226 )
1227
1228 get = Mock()
1229 set_ = Mock()
1230
1231 class Parent:
1232 foo = association_proxy(
1233 "child", "foo", getset_factory=lambda cc, parent: (get, set_)
1234 )
1235
1236 class Child:
1237 def __init__(self, foo):
1238 self.foo = foo
1239
1240 self.mapper_registry.map_imperatively(
1241 Parent, p, properties={"child": relationship(Child)}
1242 )
1243 self.mapper_registry.map_imperatively(Child, c)
1244
1245 p1 = Parent()
1246
1247 eq_(p1.foo, get(None))
1248 p1.child = child = Child(foo="x")
1249 eq_(p1.foo, get(child))
1250 p1.foo = "y"
1251 eq_(set_.mock_calls, [call(child, "y")])
1252
1253
1254class LazyLoadTest(fixtures.MappedTest):

Callers

nothing calls this directly

Calls 11

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
map_imperativelyMethod · 0.80
ParentClass · 0.70
ChildClass · 0.70

Tested by

no test coverage detected