MCPcopy
hub / github.com/sqlalchemy/sqlalchemy / test_weakref

Method test_weakref

test/aaa_profiling/test_memusage.py:1784–1828  ·  view source on GitHub ↗

test the weak-referencing identity map, which strongly- references modified items.

(self)

Source from the content-addressed store, hash-verified

1782 run_inserts = None
1783
1784 def test_weakref(self):
1785 """test the weak-referencing identity map, which strongly-
1786 references modified items."""
1787
1788 users, User = self.tables.users, self.classes.User
1789 self.mapper_registry.map_imperatively(User, users)
1790
1791 with Session(testing.db) as s:
1792 gc_collect()
1793
1794 s.add(User(name="ed"))
1795 s.flush()
1796 assert not s.dirty
1797
1798 user = s.query(User).one()
1799
1800 user_is = user._sa_instance_state
1801 del user
1802 user_is._force_dereference()
1803
1804 assert len(s.identity_map) == 0
1805
1806 user = s.query(User).one()
1807 user_is = user._sa_instance_state
1808 user.name = "fred"
1809
1810 del user
1811 gc_collect()
1812
1813 # object was not gc'ed
1814 assert user_is.obj()
1815
1816 assert len(s.identity_map) == 1
1817 assert len(s.dirty) == 1
1818 assert None not in s.dirty
1819 s.flush()
1820 gc_collect()
1821 user_is._force_dereference()
1822
1823 assert not s.dirty
1824 assert not s.identity_map
1825
1826 user = s.query(User).one()
1827 assert user.name == "fred"
1828 assert s.identity_map
1829
1830 def test_weakref_pickled(self):
1831 users, User = self.tables.users, pickleable.User

Callers

nothing calls this directly

Calls 8

SessionClass · 0.90
map_imperativelyMethod · 0.80
_force_dereferenceMethod · 0.80
UserClass · 0.70
addMethod · 0.45
flushMethod · 0.45
oneMethod · 0.45
queryMethod · 0.45

Tested by

no test coverage detected