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

Method test_weakref

test/aaa_profiling/test_memusage.py:1840–1884  ·  view source on GitHub ↗

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

(self)

Source from the content-addressed store, hash-verified

1838 run_inserts = None
1839
1840 def test_weakref(self):
1841 """test the weak-referencing identity map, which strongly-
1842 references modified items."""
1843
1844 users, User = self.tables.users, self.classes.User
1845 self.mapper_registry.map_imperatively(User, users)
1846
1847 with Session(testing.db) as s:
1848 gc_collect()
1849
1850 s.add(User(name="ed"))
1851 s.flush()
1852 assert not s.dirty
1853
1854 user = s.query(User).one()
1855
1856 user_is = user._sa_instance_state
1857 del user
1858 user_is._force_dereference()
1859
1860 assert len(s.identity_map) == 0
1861
1862 user = s.query(User).one()
1863 user_is = user._sa_instance_state
1864 user.name = "fred"
1865
1866 del user
1867 gc_collect()
1868
1869 # object was not gc'ed
1870 assert user_is.obj()
1871
1872 assert len(s.identity_map) == 1
1873 assert len(s.dirty) == 1
1874 assert None not in s.dirty
1875 s.flush()
1876 gc_collect()
1877 user_is._force_dereference()
1878
1879 assert not s.dirty
1880 assert not s.identity_map
1881
1882 user = s.query(User).one()
1883 assert user.name == "fred"
1884 assert s.identity_map
1885
1886 def test_weakref_pickled(self):
1887 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