MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_add_property_in_mapper_event

Method test_add_property_in_mapper_event

test/orm/test_mapper.py:1047–1156  ·  view source on GitHub ↗

test #12858, ability to add properties of all types within event hooks where the mapper is not necessarily set up with property collections.

(
        self, property_type, connection, metadata, event_name
    )

Source from the content-addressed store, hash-verified

1045 ],
1046 )
1047 def test_add_property_in_mapper_event(
1048 self, property_type, connection, metadata, event_name
1049 ):
1050 """test #12858, ability to add properties of all types within
1051 event hooks where the mapper is not necessarily set up with property
1052 collections.
1053
1054 """
1055
1056 Address = self.classes.Address
1057
1058 users = Table(
1059 "deferred_users",
1060 metadata,
1061 Column("id", Integer, primary_key=True),
1062 Column("name", String(50)),
1063 )
1064
1065 # use an ad-hoc User class. Assigning an event holder to a
1066 # class right now means that class permanently has that mapper
1067 # event associated with it, whenever a new mapper is created.
1068 # if you call event.remove(User), it removes the event from the mapper,
1069 # but not the "event hold" that associates it again on a new mapper.
1070 # this is probably a bug but not the one we came here today to fix
1071 class User:
1072 pass
1073
1074 def setup_props(mapper, class_):
1075 if property_type.ColumnProperty:
1076 col = Column("new_column", String(50))
1077 mapper.local_table.append_column(col)
1078 mapper.add_property(
1079 col.key, deferred(col, group="deferred_group")
1080 )
1081 elif property_type.Column:
1082 col = Column("new_column", String(50))
1083 mapper.local_table.append_column(col)
1084 mapper.add_property(col.key, col)
1085 elif property_type.Relationship:
1086 mapper.add_property("addresses", relationship(Address))
1087 else:
1088 property_type.fail()
1089
1090 event.listen(User, event_name.name, setup_props)
1091
1092 if property_type.ColumnProperty:
1093 self.mapper(
1094 User,
1095 users,
1096 properties={
1097 "name": deferred(users.c.name, group="deferred_group")
1098 },
1099 )
1100 else:
1101 self.mapper(User, users)
1102
1103 if property_type.Relationship:
1104 addresses_table = Table(

Callers

nothing calls this directly

Calls 15

addMethod · 0.95
commitMethod · 0.95
queryMethod · 0.95
TableClass · 0.90
ColumnClass · 0.90
StringClass · 0.90
deferredFunction · 0.90
ForeignKeyClass · 0.90
configure_mappersFunction · 0.90
fixture_sessionFunction · 0.90
SessionClass · 0.90
eq_Function · 0.90

Tested by

no test coverage detected