MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_one_to_many_2

Method test_one_to_many_2

test/orm/test_unitofwork.py:1577–1639  ·  view source on GitHub ↗

Modifying the child items of an object.

(self)

Source from the content-addressed store, hash-verified

1575 self.assert_(u.id == userid and a2.id == addressid)
1576
1577 def test_one_to_many_2(self):
1578 """Modifying the child items of an object."""
1579
1580 Address, addresses, users, User = (
1581 self.classes.Address,
1582 self.tables.addresses,
1583 self.tables.users,
1584 self.classes.User,
1585 )
1586
1587 self.mapper_registry.map_imperatively(
1588 User,
1589 users,
1590 properties=dict(
1591 addresses=relationship(
1592 self.mapper_registry.map_imperatively(Address, addresses),
1593 lazy="select",
1594 )
1595 ),
1596 )
1597
1598 u1 = User(name="user1")
1599 u1.addresses = []
1600 a1 = Address(email_address="emailaddress1")
1601 u1.addresses.append(a1)
1602
1603 u2 = User(name="user2")
1604 u2.addresses = []
1605 a2 = Address(email_address="emailaddress2")
1606 u2.addresses.append(a2)
1607
1608 a3 = Address(email_address="emailaddress3")
1609
1610 session = fixture_session()
1611 session.add_all((u1, u2, a3))
1612 session.flush()
1613
1614 # modify user2 directly, append an address to user1.
1615 # upon commit, user2 should be updated, user1 should not
1616 # both address1 and address3 should be updated
1617 u2.name = "user2modified"
1618 u1.addresses.append(a3)
1619 del u1.addresses[0]
1620
1621 self.assert_sql(
1622 testing.db,
1623 session.flush,
1624 [
1625 (
1626 "UPDATE users SET name=:name "
1627 "WHERE users.id = :users_id",
1628 {"users_id": u2.id, "name": "user2modified"},
1629 ),
1630 (
1631 "UPDATE addresses SET user_id=:user_id "
1632 "WHERE addresses.id = :addresses_id",
1633 [
1634 {"user_id": None, "addresses_id": a1.id},

Callers

nothing calls this directly

Calls 9

relationshipFunction · 0.90
fixture_sessionFunction · 0.90
map_imperativelyMethod · 0.80
assert_sqlMethod · 0.80
UserClass · 0.70
AddressClass · 0.70
appendMethod · 0.45
add_allMethod · 0.45
flushMethod · 0.45

Tested by

no test coverage detected