diff options
Diffstat (limited to 'test/orm')
| -rw-r--r-- | test/orm/declarative/test_basic.py | 8 | ||||
| -rw-r--r-- | test/orm/inheritance/test_poly_linked_list.py | 62 | ||||
| -rw-r--r-- | test/orm/test_bind.py | 6 | ||||
| -rw-r--r-- | test/orm/test_bundle.py | 6 | ||||
| -rw-r--r-- | test/orm/test_cascade.py | 72 | ||||
| -rw-r--r-- | test/orm/test_composites.py | 8 | ||||
| -rw-r--r-- | test/orm/test_core_compilation.py | 9 | ||||
| -rw-r--r-- | test/orm/test_deprecations.py | 623 | ||||
| -rw-r--r-- | test/orm/test_events.py | 38 | ||||
| -rw-r--r-- | test/orm/test_lazy_relations.py | 2 | ||||
| -rw-r--r-- | test/orm/test_load_on_fks.py | 31 | ||||
| -rw-r--r-- | test/orm/test_mapper.py | 2 | ||||
| -rw-r--r-- | test/orm/test_naturalpks.py | 4 | ||||
| -rw-r--r-- | test/orm/test_query.py | 2 | ||||
| -rw-r--r-- | test/orm/test_session.py | 23 | ||||
| -rw-r--r-- | test/orm/test_transaction.py | 338 | ||||
| -rw-r--r-- | test/orm/test_versioning.py | 6 |
17 files changed, 301 insertions, 939 deletions
diff --git a/test/orm/declarative/test_basic.py b/test/orm/declarative/test_basic.py index a5c1ba08a..87ef2e62f 100644 --- a/test/orm/declarative/test_basic.py +++ b/test/orm/declarative/test_basic.py @@ -25,7 +25,7 @@ from sqlalchemy.orm import deferred from sqlalchemy.orm import descriptor_props from sqlalchemy.orm import exc as orm_exc from sqlalchemy.orm import joinedload -from sqlalchemy.orm import mapper +from sqlalchemy.orm import Mapper from sqlalchemy.orm import registry from sqlalchemy.orm import relationship from sqlalchemy.orm import Session @@ -1489,7 +1489,7 @@ class DeclarativeTest(DeclarativeTestBase): def test_custom_mapper_attribute(self): def mymapper(cls, tbl, **kwargs): - m = sa.orm.mapper(cls, tbl, **kwargs) + m = sa.orm.Mapper(cls, tbl, **kwargs) m.CHECK = True return m @@ -1504,7 +1504,7 @@ class DeclarativeTest(DeclarativeTestBase): def test_custom_mapper_argument(self): def mymapper(cls, tbl, **kwargs): - m = sa.orm.mapper(cls, tbl, **kwargs) + m = sa.orm.Mapper(cls, tbl, **kwargs) m.CHECK = True return m @@ -2214,7 +2214,7 @@ class DeclarativeTest(DeclarativeTestBase): canary = mock.Mock() - @event.listens_for(mapper, "instrument_class") + @event.listens_for(Mapper, "instrument_class") def instrument_class(mp, cls): canary.instrument_class(mp, cls) diff --git a/test/orm/inheritance/test_poly_linked_list.py b/test/orm/inheritance/test_poly_linked_list.py index a501e027a..9e973cc74 100644 --- a/test/orm/inheritance/test_poly_linked_list.py +++ b/test/orm/inheritance/test_poly_linked_list.py @@ -1,9 +1,7 @@ from sqlalchemy import ForeignKey from sqlalchemy import Integer from sqlalchemy import String -from sqlalchemy import testing from sqlalchemy.orm import backref -from sqlalchemy.orm import clear_mappers from sqlalchemy.orm import configure_mappers from sqlalchemy.orm import relationship from sqlalchemy.testing import fixtures @@ -54,24 +52,13 @@ class PolymorphicCircularTest(fixtures.MappedTest): @classmethod def setup_mappers(cls): - global Table1, Table1B, Table2, Table3, Data table1, table2, table3, data = cls.tables( "table1", "table2", "table3", "data" ) - # join = polymorphic_union( - # { - # 'table3' : table1.join(table3), - # 'table2' : table1.join(table2), - # 'table1' : table1.select(table1.c.type.in_(['table1', 'table1b'])), - # }, None, 'pjoin') - - with testing.expect_deprecated_20( - r"The Join.alias\(\) method is considered legacy" - ): - join = table1.outerjoin(table2).outerjoin(table3).alias("pjoin") - # join = None - - class Table1: + + Base = cls.Basic + + class Table1(Base): def __init__(self, name, data=None): self.name = name if data is not None: @@ -94,7 +81,7 @@ class PolymorphicCircularTest(fixtures.MappedTest): class Table3(Table1): pass - class Data: + class Data(Base): def __init__(self, data): self.data = data @@ -105,35 +92,6 @@ class PolymorphicCircularTest(fixtures.MappedTest): repr(str(self.data)), ) - try: - # this is how the mapping used to work. ensure that this raises an - # error now - table1_mapper = cls.mapper_registry.map_imperatively( - Table1, - table1, - select_table=join, - polymorphic_on=table1.c.type, - polymorphic_identity="table1", - properties={ - "nxt": relationship( - Table1, - backref=backref( - "prev", foreignkey=join.c.id, uselist=False - ), - uselist=False, - primaryjoin=join.c.id == join.c.related_id, - ), - "data": relationship( - cls.mapper_registry.map_imperatively(Data, data) - ), - }, - ) - configure_mappers() - assert False - except Exception: - assert True - clear_mappers() - # currently, the "eager" relationships degrade to lazy relationships # due to the polymorphic load. # the "nxt" relationship used to have a "lazy='joined'" on it, but the @@ -190,12 +148,17 @@ class PolymorphicCircularTest(fixtures.MappedTest): ), table1_mapper.primary_key def test_one(self): + Table1, Table2 = self.classes("Table1", "Table2") self._testlist([Table1, Table2, Table1, Table2]) def test_two(self): + Table3 = self.classes.Table3 self._testlist([Table3]) def test_three(self): + Table1, Table1B, Table2, Table3 = self.classes( + "Table1", "Table1B", "Table2", "Table3" + ) self._testlist( [ Table2, @@ -211,6 +174,9 @@ class PolymorphicCircularTest(fixtures.MappedTest): ) def test_four(self): + Table1, Table1B, Table2, Table3, Data = self.classes( + "Table1", "Table1B", "Table2", "Table3", "Data" + ) self._testlist( [ Table2("t2", [Data("data1"), Data("data2")]), @@ -221,6 +187,8 @@ class PolymorphicCircularTest(fixtures.MappedTest): ) def _testlist(self, classes): + Table1 = self.classes.Table1 + sess = fixture_session() # create objects in a linked list diff --git a/test/orm/test_bind.py b/test/orm/test_bind.py index 1d5af5064..ec62430ad 100644 --- a/test/orm/test_bind.py +++ b/test/orm/test_bind.py @@ -286,7 +286,7 @@ class BindIntegrationTest(_fixtures.FixtureTest): sess.bind_mapper(Address, e2) engine = {"e1": e1, "e2": e2, "e3": e3}[expected] - conn = sess.connection(**testcase) + conn = sess.connection(bind_arguments=testcase) is_(conn.engine, engine) sess.close() @@ -355,7 +355,7 @@ class BindIntegrationTest(_fixtures.FixtureTest): canary.get_bind(**kw) return Session.get_bind(self, **kw) - sess = GetBindSession(e3, future=True) + sess = GetBindSession(e3) sess.bind_mapper(User, e1) sess.bind_mapper(Address, e2) @@ -422,7 +422,7 @@ class BindIntegrationTest(_fixtures.FixtureTest): c = testing.db.connect() sess = Session(bind=c) sess.begin() - transaction = sess._legacy_transaction() + transaction = sess.get_transaction() u = User(name="u1") sess.add(u) sess.flush() diff --git a/test/orm/test_bundle.py b/test/orm/test_bundle.py index 92c4c19c2..6d613091d 100644 --- a/test/orm/test_bundle.py +++ b/test/orm/test_bundle.py @@ -318,11 +318,7 @@ class BundleTest(fixtures.MappedTest, AssertsCompiledSQL): stmt = select(b1).filter(b1.c.d1.between("d3d1", "d5d1")) - with testing.expect_deprecated_20( - "The Bundle.single_entity flag has no effect when " - "using 2.0 style execution." - ): - rows = sess.execute(stmt).all() + rows = sess.execute(stmt).all() eq_( rows, [(("d3d1", "d3d2"),), (("d4d1", "d4d2"),), (("d5d1", "d5d2"),)], diff --git a/test/orm/test_cascade.py b/test/orm/test_cascade.py index 51ed50255..5a74d6ad9 100644 --- a/test/orm/test_cascade.py +++ b/test/orm/test_cascade.py @@ -1076,8 +1076,6 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): m2o_cascade=True, o2m=False, m2o=False, - o2m_cascade_backrefs=True, - m2o_cascade_backrefs=True, ): Address, addresses, users, User = ( @@ -1092,12 +1090,10 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): addresses_rel = { "addresses": relationship( Address, - cascade_backrefs=o2m_cascade_backrefs, cascade=o2m_cascade and "save-update" or "", backref=backref( "user", cascade=m2o_cascade and "save-update" or "", - cascade_backrefs=m2o_cascade_backrefs, ), ) } @@ -1107,7 +1103,6 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): "addresses": relationship( Address, cascade=o2m_cascade and "save-update" or "", - cascade_backrefs=o2m_cascade_backrefs, ) } user_rel = {} @@ -1116,7 +1111,6 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): "user": relationship( User, cascade=m2o_cascade and "save-update" or "", - cascade_backrefs=m2o_cascade_backrefs, ) } addresses_rel = {} @@ -1137,8 +1131,6 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): bkd_cascade=True, fwd=False, bkd=False, - fwd_cascade_backrefs=True, - bkd_cascade_backrefs=True, ): keywords, items, item_keywords, Keyword, Item = ( @@ -1155,12 +1147,10 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): "keywords": relationship( Keyword, secondary=item_keywords, - cascade_backrefs=fwd_cascade_backrefs, cascade=fwd_cascade and "save-update" or "", backref=backref( "items", cascade=bkd_cascade and "save-update" or "", - cascade_backrefs=bkd_cascade_backrefs, ), ) } @@ -1171,7 +1161,6 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): Keyword, secondary=item_keywords, cascade=fwd_cascade and "save-update" or "", - cascade_backrefs=fwd_cascade_backrefs, ) } items_rel = {} @@ -1181,7 +1170,6 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): Item, secondary=item_keywords, cascade=bkd_cascade and "save-update" or "", - cascade_backrefs=bkd_cascade_backrefs, ) } keywords_rel = {} @@ -1404,11 +1392,7 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): sess.flush() a1 = Address(email_address="a1") - with testing.expect_deprecated( - '"Address" object is being merged into a Session along ' - 'the backref cascade path for relationship "User.addresses"' - ): - a1.user = u1 + a1.user = u1 sess.add(a1) sess.expunge(u1) assert u1 not in sess @@ -1469,11 +1453,7 @@ class NoSaveCascadeFlushTest(_fixtures.FixtureTest): sess.flush() a1 = Address(email_address="a1") - with testing.expect_deprecated( - '"Address" object is being merged into a Session along the ' - 'backref cascade path for relationship "User.addresses"' - ): - a1.user = u1 + a1.user = u1 sess.add(a1) sess.expunge(u1) assert u1 not in sess @@ -2761,20 +2741,14 @@ class NoBackrefCascadeTest(_fixtures.FixtureTest): cls.mapper_registry.map_imperatively( User, users, - properties={ - "addresses": relationship( - Address, backref="user", cascade_backrefs=False - ) - }, + properties={"addresses": relationship(Address, backref="user")}, ) cls.mapper_registry.map_imperatively( Dingaling, dingalings, properties={ - "address": relationship( - Address, backref="dingalings", cascade_backrefs=False - ) + "address": relationship(Address, backref="dingalings") }, ) @@ -2805,7 +2779,7 @@ class NoBackrefCascadeTest(_fixtures.FixtureTest): assert a1 not in sess - def test_o2m_flag_on_backref(self): + def test_o2m_on_backref_no_cascade(self): Dingaling, Address = self.classes.Dingaling, self.classes.Address sess = fixture_session() @@ -2814,15 +2788,9 @@ class NoBackrefCascadeTest(_fixtures.FixtureTest): sess.add(a1) d1 = Dingaling() - with testing.expect_deprecated( - '"Dingaling" object is being merged into a Session along the ' - 'backref cascade path for relationship "Address.dingalings"' - ): - d1.address = a1 + d1.address = a1 assert d1 in a1.dingalings - assert d1 in sess - - sess.commit() + assert d1 not in sess def test_m2o_basic(self): Dingaling, Address = self.classes.Dingaling, self.classes.Address @@ -2836,7 +2804,7 @@ class NoBackrefCascadeTest(_fixtures.FixtureTest): a1.dingalings.append(d1) assert a1 not in sess - def test_m2o_flag_on_backref(self): + def test_m2o_on_backref_no_cascade(self): User, Address = self.classes.User, self.classes.Address sess = fixture_session() @@ -2845,14 +2813,10 @@ class NoBackrefCascadeTest(_fixtures.FixtureTest): sess.add(a1) u1 = User(name="u1") - with testing.expect_deprecated( - '"User" object is being merged into a Session along the backref ' - 'cascade path for relationship "Address.user"' - ): - u1.addresses.append(a1) - assert u1 in sess + u1.addresses.append(a1) + assert u1 not in sess - def test_m2o_commit_warns(self): + def test_m2o_commit_no_cascade(self): Dingaling, Address = self.classes.Dingaling, self.classes.Address sess = fixture_session() @@ -3844,7 +3808,9 @@ class O2MConflictTest(fixtures.MappedTest): "child": relationship( Child, uselist=False, - backref=backref("parent", cascade_backrefs=False), + backref=backref( + "parent", + ), ) }, ) @@ -3910,7 +3876,7 @@ class O2MConflictTest(fixtures.MappedTest): Child, uselist=False, cascade="all, delete, delete-orphan", - backref=backref("parent", cascade_backrefs=False), + backref=backref("parent"), ) }, ) @@ -3937,7 +3903,6 @@ class O2MConflictTest(fixtures.MappedTest): single_parent=True, backref=backref("child", uselist=False), cascade="all,delete,delete-orphan", - cascade_backrefs=False, ) }, ) @@ -3963,7 +3928,6 @@ class O2MConflictTest(fixtures.MappedTest): single_parent=True, backref=backref("child", uselist=True), cascade="all,delete,delete-orphan", - cascade_backrefs=False, ) }, ) @@ -4499,7 +4463,6 @@ class CollectionCascadesNoBackrefTest(fixtures.TestBase): "B", backref="a", collection_class=collection_class, - cascade_backrefs=False, ) @registry.mapped @@ -4522,13 +4485,12 @@ class CollectionCascadesNoBackrefTest(fixtures.TestBase): (attribute_mapped_collection("key"), "update_kw"), argnames="collection_class,methname", ) - @testing.combinations((True,), (False,), argnames="future") def test_cascades_on_collection( - self, cascade_fixture, collection_class, methname, future + self, cascade_fixture, collection_class, methname ): A, B = cascade_fixture(collection_class) - s = Session(future=future) + s = Session() a1 = A() s.add(a1) diff --git a/test/orm/test_composites.py b/test/orm/test_composites.py index 67ffae75d..19e090e0e 100644 --- a/test/orm/test_composites.py +++ b/test/orm/test_composites.py @@ -90,14 +90,14 @@ class PointTest(fixtures.MappedTest, testing.AssertsCompiledSQL): }, ) - def _fixture(self, future=False): + def _fixture(self): Graph, Edge, Point = ( self.classes.Graph, self.classes.Edge, self.classes.Point, ) - sess = Session(testing.db, future=future) + sess = Session(testing.db) g = Graph( id=1, edges=[ @@ -231,7 +231,7 @@ class PointTest(fixtures.MappedTest, testing.AssertsCompiledSQL): def test_bulk_update_sql(self): Edge, Point = (self.classes.Edge, self.classes.Point) - sess = self._fixture(future=True) + sess = self._fixture() e1 = sess.execute( select(Edge).filter(Edge.start == Point(14, 5)) @@ -256,7 +256,7 @@ class PointTest(fixtures.MappedTest, testing.AssertsCompiledSQL): def test_bulk_update_evaluate(self): Edge, Point = (self.classes.Edge, self.classes.Point) - sess = self._fixture(future=True) + sess = self._fixture() e1 = sess.execute( select(Edge).filter(Edge.start == Point(14, 5)) diff --git a/test/orm/test_core_compilation.py b/test/orm/test_core_compilation.py index 28f42797e..2b0c570c4 100644 --- a/test/orm/test_core_compilation.py +++ b/test/orm/test_core_compilation.py @@ -35,7 +35,6 @@ from sqlalchemy.testing import AssertsCompiledSQL from sqlalchemy.testing import eq_ from sqlalchemy.testing import fixtures from sqlalchemy.testing import is_ -from sqlalchemy.testing.assertions import expect_raises_message from sqlalchemy.testing.fixtures import fixture_session from sqlalchemy.testing.util import resolve_lambda from sqlalchemy.util.langhelpers import hybridproperty @@ -232,14 +231,6 @@ class ColumnsClauseFromsTest(QueryTest, AssertsCompiledSQL): ) eq_(len(froms), 1) - def test_with_only_columns_unknown_kw(self): - User, Address = self.classes("User", "Address") - - stmt = select(User.id) - - with expect_raises_message(TypeError, "unknown parameters: foo"): - stmt.with_only_columns(User.id, foo="bar") - @testing.combinations((True,), (False,)) def test_replace_into_select_from_maintains_existing(self, use_flag): User, Address = self.classes("User", "Address") diff --git a/test/orm/test_deprecations.py b/test/orm/test_deprecations.py index 64db9a893..a567534c3 100644 --- a/test/orm/test_deprecations.py +++ b/test/orm/test_deprecations.py @@ -1,4 +1,3 @@ -from contextlib import nullcontext from unittest.mock import call from unittest.mock import Mock @@ -16,12 +15,10 @@ from sqlalchemy import select from sqlalchemy import String from sqlalchemy import testing from sqlalchemy import text -from sqlalchemy import true from sqlalchemy.engine import default from sqlalchemy.engine import result_tuple from sqlalchemy.orm import aliased from sqlalchemy.orm import attributes -from sqlalchemy.orm import backref from sqlalchemy.orm import clear_mappers from sqlalchemy.orm import collections from sqlalchemy.orm import column_property @@ -31,12 +28,9 @@ from sqlalchemy.orm import contains_eager from sqlalchemy.orm import defaultload from sqlalchemy.orm import defer from sqlalchemy.orm import deferred -from sqlalchemy.orm import eagerload from sqlalchemy.orm import foreign from sqlalchemy.orm import instrumentation from sqlalchemy.orm import joinedload -from sqlalchemy.orm import mapper -from sqlalchemy.orm import relation from sqlalchemy.orm import relationship from sqlalchemy.orm import scoped_session from sqlalchemy.orm import Session @@ -46,7 +40,6 @@ from sqlalchemy.orm import synonym from sqlalchemy.orm import undefer from sqlalchemy.orm import with_parent from sqlalchemy.orm import with_polymorphic -from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy.orm.collections import collection from sqlalchemy.orm.util import polymorphic_union from sqlalchemy.testing import assert_raises_message @@ -67,13 +60,11 @@ from .inheritance import _poly_fixtures from .inheritance._poly_fixtures import Manager from .inheritance._poly_fixtures import Person from .test_deferred import InheritanceTest as _deferred_InheritanceTest -from .test_dynamic import _DynamicFixture from .test_events import _RemoveListeners from .test_options import PathTest as OptionsPathTest from .test_options import PathTest from .test_options import QueryTest as OptionsQueryTest from .test_query import QueryTest -from .test_transaction import _LocalFixture from ..sql.test_compare import CacheKeyFixture if True: @@ -413,35 +404,6 @@ class DeprecatedQueryTest(_fixtures.FixtureTest, AssertsCompiledSQL): "subquery object." ) - def test_deprecated_negative_slices(self): - User = self.classes.User - - sess = fixture_session() - q = sess.query(User).order_by(User.id) - - with testing.expect_deprecated( - "Support for negative indexes for SQL index / slice operators" - ): - eq_(q[-5:-2], [User(id=7), User(id=8)]) - - with testing.expect_deprecated( - "Support for negative indexes for SQL index / slice operators" - ): - eq_(q[-1], User(id=10)) - - with testing.expect_deprecated( - "Support for negative indexes for SQL index / slice operators" - ): - eq_(q[-2], User(id=9)) - - with testing.expect_deprecated( - "Support for negative indexes for SQL index / slice operators" - ): - eq_(q[:-2], [User(id=7), User(id=8)]) - - # this doesn't evaluate anything because it's a net-negative - eq_(q[-2:-5], []) - def test_deprecated_select_coercion_join_target(self): User = self.classes.User addresses = self.tables.addresses @@ -460,44 +422,6 @@ class DeprecatedQueryTest(_fixtures.FixtureTest, AssertsCompiledSQL): "ON users.id = anon_1.user_id", ) - def test_deprecated_negative_slices_compile(self): - User = self.classes.User - - sess = fixture_session() - q = sess.query(User).order_by(User.id) - - with testing.expect_deprecated( - "Support for negative indexes for SQL index / slice operators" - ): - self.assert_sql( - testing.db, - lambda: q[-5:-2], - [ - ( - "SELECT users.id AS users_id, users.name " - "AS users_name " - "FROM users ORDER BY users.id", - {}, - ) - ], - ) - - with testing.expect_deprecated( - "Support for negative indexes for SQL index / slice operators" - ): - self.assert_sql( - testing.db, - lambda: q[-5:], - [ - ( - "SELECT users.id AS users_id, users.name " - "AS users_name " - "FROM users ORDER BY users.id", - {}, - ) - ], - ) - def test_invalid_column(self): User = self.classes.User @@ -629,129 +553,91 @@ class LazyLoadOptSpecificityTest(fixtures.DeclarativeMappedTest): self.assert_sql_count(testing.db, go, expected) -class DynamicTest(_DynamicFixture, _fixtures.FixtureTest): - def test_negative_slice_access_raises(self): - User, Address = self._user_address_fixture() - sess = fixture_session() - u1 = sess.get(User, 8) +class DeprecatedInhTest(_poly_fixtures._Polymorphic): + def test_with_polymorphic(self): + Person = _poly_fixtures.Person + Engineer = _poly_fixtures.Engineer - with testing.expect_deprecated_20( - "Support for negative indexes for SQL index / slice" - ): - eq_(u1.addresses[-1], Address(id=4)) + with DeprecatedQueryTest._expect_implicit_subquery(): + p_poly = with_polymorphic(Person, [Engineer], select(Person)) - with testing.expect_deprecated_20( - "Support for negative indexes for SQL index / slice" - ): - eq_(u1.addresses[-5:-2], [Address(id=2)]) + is_true( + sa.inspect(p_poly).selectable.compare(select(Person).subquery()) + ) - with testing.expect_deprecated_20( - "Support for negative indexes for SQL index / slice" - ): - eq_(u1.addresses[-2], Address(id=3)) - with testing.expect_deprecated_20( - "Support for negative indexes for SQL index / slice" - ): - eq_(u1.addresses[:-2], [Address(id=2)]) +class DeprecatedMapperTest( + fixtures.RemovesEvents, _fixtures.FixtureTest, AssertsCompiledSQL +): + __dialect__ = "default" + def test_listen_on_mapper_mapper_event_fn(self, registry): + from sqlalchemy.orm import mapper -class SessionTest(fixtures.RemovesEvents, _LocalFixture): - def test_transaction_attr(self): - s1 = Session(testing.db) + m1 = Mock() - with testing.expect_deprecated_20( - "The Session.transaction attribute is considered legacy as " - "of the 1.x series" + with expect_deprecated( + r"The `sqlalchemy.orm.mapper\(\)` symbol is deprecated and " + "will be removed" ): - s1.transaction - def test_textual_execute(self, connection): - """test that Session.execute() converts to text()""" + @event.listens_for(mapper, "before_configured") + def go(): + m1() - users = self.tables.users + @registry.mapped + class MyClass: + __tablename__ = "t1" + id = Column(Integer, primary_key=True) - with Session(bind=connection) as sess: - sess.execute(users.insert(), dict(id=7, name="jack")) + registry.configure() + eq_(m1.mock_calls, [call()]) - with testing.expect_deprecated_20( - "Using plain strings to indicate SQL statements " - "without using the text" - ): - # use :bindparam style - eq_( - sess.execute( - "select * from users where id=:id", {"id": 7} - ).fetchall(), - [(7, "jack")], - ) + def test_listen_on_mapper_instrumentation_event_fn(self, registry): + from sqlalchemy.orm import mapper - with testing.expect_deprecated_20( - "Using plain strings to indicate SQL statements " - "without using the text" - ): - # use :bindparam style - eq_( - sess.scalar( - "select id from users where id=:id", {"id": 7} - ), - 7, - ) + m1 = Mock() - def test_session_str(self): - s1 = Session(testing.db) - str(s1) + with expect_deprecated( + r"The `sqlalchemy.orm.mapper\(\)` symbol is deprecated and " + "will be removed" + ): - @testing.combinations( - {"mapper": None}, - {"clause": None}, - {"bind_arguments": {"mapper": None}, "clause": None}, - {"bind_arguments": {}, "clause": None}, - ) - def test_bind_kwarg_deprecated(self, kw): - s1 = Session(testing.db) - - for meth in s1.execute, s1.scalar: - m1 = mock.Mock(side_effect=s1.get_bind) - with mock.patch.object(s1, "get_bind", m1): - expr = text("select 1") - - with testing.expect_deprecated_20( - r"Passing bind arguments to Session.execute\(\) as " - "keyword " - "arguments is deprecated and will be removed SQLAlchemy " - "2.0" - ): - meth(expr, **kw) - - bind_arguments = kw.pop("bind_arguments", None) - if bind_arguments: - bind_arguments.update(kw) - - if "clause" not in kw: - bind_arguments["clause"] = expr - eq_(m1.mock_calls, [call(**bind_arguments)]) - else: - if "clause" not in kw: - kw["clause"] = expr - eq_(m1.mock_calls, [call(**kw)]) + @event.listens_for(mapper, "init") + def go(target, args, kwargs): + m1(target, args, kwargs) + @registry.mapped + class MyClass: + __tablename__ = "t1" + id = Column(Integer, primary_key=True) -class DeprecatedInhTest(_poly_fixtures._Polymorphic): - def test_with_polymorphic(self): - Person = _poly_fixtures.Person - Engineer = _poly_fixtures.Engineer + mc = MyClass(id=5) + eq_(m1.mock_calls, [call(mc, (), {"id": 5})]) - with DeprecatedQueryTest._expect_implicit_subquery(): - p_poly = with_polymorphic(Person, [Engineer], select(Person)) + def test_we_couldnt_remove_mapper_yet(self): + """test that the mapper() function is present but raises an + informative error when used. - is_true( - sa.inspect(p_poly).selectable.compare(select(Person).subquery()) - ) + The function itself was to be removed as of 2.0, however we forgot + to mark deprecated the use of the function as an event target, + so it needs to stay around for another cycle at least. + """ -class DeprecatedMapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): - __dialect__ = "default" + class MyClass: + pass + + t1 = Table("t1", MetaData(), Column("id", Integer, primary_key=True)) + + from sqlalchemy.orm import mapper + + with assertions.expect_raises_message( + sa_exc.InvalidRequestError, + r"The 'sqlalchemy.orm.mapper\(\)' function is removed as of " + "SQLAlchemy 2.0.", + ): + mapper(MyClass, t1) def test_deferred_scalar_loader_name_change(self): class Foo: @@ -1392,7 +1278,6 @@ class ViewonlyFlagWarningTest(fixtures.MappedTest): ("passive_updates", False), ("enable_typechecks", False), ("active_history", True), - ("cascade_backrefs", False), ) def test_viewonly_warning(self, flag, value): Order = self.classes.Order @@ -1504,7 +1389,7 @@ class NonPrimaryMapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): non_primary=True, ) - def test_illegal_non_primary_legacy(self): + def test_illegal_non_primary_legacy(self, registry): users, Address, addresses, User = ( self.tables.users, self.classes.Address, @@ -1512,18 +1397,12 @@ class NonPrimaryMapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): self.classes.User, ) - with testing.expect_deprecated( - "Calling the mapper.* function directly outside of a declarative " - ): - mapper(User, users) - with testing.expect_deprecated( - "Calling the mapper.* function directly outside of a declarative " - ): - mapper(Address, addresses) + registry.map_imperatively(User, users) + registry.map_imperatively(Address, addresses) with testing.expect_deprecated( "The mapper.non_primary parameter is deprecated" ): - m = mapper( # noqa F841 + m = registry.map_imperatively( # noqa F841 User, users, non_primary=True, @@ -1536,22 +1415,19 @@ class NonPrimaryMapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): configure_mappers, ) - def test_illegal_non_primary_2_legacy(self): + def test_illegal_non_primary_2_legacy(self, registry): User, users = self.classes.User, self.tables.users - with testing.expect_deprecated( - "The mapper.non_primary parameter is deprecated" - ): - assert_raises_message( - sa.exc.InvalidRequestError, - "Configure a primary mapper first", - mapper, - User, - users, - non_primary=True, - ) + assert_raises_message( + sa.exc.InvalidRequestError, + "Configure a primary mapper first", + registry.map_imperatively, + User, + users, + non_primary=True, + ) - def test_illegal_non_primary_3_legacy(self): + def test_illegal_non_primary_3_legacy(self, registry): users, addresses = self.tables.users, self.tables.addresses class Base: @@ -1560,21 +1436,16 @@ class NonPrimaryMapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): class Sub(Base): pass - with testing.expect_deprecated( - "Calling the mapper.* function directly outside of a declarative " - ): - mapper(Base, users) - with testing.expect_deprecated( - "The mapper.non_primary parameter is deprecated", - ): - assert_raises_message( - sa.exc.InvalidRequestError, - "Configure a primary mapper first", - mapper, - Sub, - addresses, - non_primary=True, - ) + registry.map_imperatively(Base, users) + + assert_raises_message( + sa.exc.InvalidRequestError, + "Configure a primary mapper first", + registry.map_imperatively, + Sub, + addresses, + non_primary=True, + ) class InstancesTest(QueryTest, AssertsCompiledSQL): @@ -1776,78 +1647,6 @@ class InstancesTest(QueryTest, AssertsCompiledSQL): self.assert_sql_count(testing.db, go, 1) -class TestDeprecation20(QueryTest): - def test_relation(self): - User = self.classes.User - with testing.expect_deprecated_20(".*relationship"): - relation(User.addresses) - - def test_eagerloading(self): - User = self.classes.User - with testing.expect_deprecated_20(".*joinedload"): - eagerload(User.addresses) - - -class DistinctOrderByImplicitTest(QueryTest, AssertsCompiledSQL): - __dialect__ = "default" - - def test_columns_augmented_roundtrip_three(self): - User, Address = self.classes.User, self.classes.Address - - sess = fixture_session() - - q = ( - sess.query(User.id, User.name.label("foo"), Address.id) - .join(Address, true()) - .filter(User.name == "jack") - .filter(User.id + Address.user_id > 0) - .distinct() - .order_by(User.id, User.name, Address.email_address) - ) - - # even though columns are added, they aren't in the result - with testing.expect_deprecated( - "ORDER BY columns added implicitly due to " - ): - eq_( - q.all(), - [ - (7, "jack", 3), - (7, "jack", 4), - (7, "jack", 2), - (7, "jack", 5), - (7, "jack", 1), - ], - ) - for row in q: - eq_(row._mapping.keys(), ["id", "foo", "id"]) - - def test_columns_augmented_sql_one(self): - User, Address = self.classes.User, self.classes.Address - - sess = fixture_session() - - q = ( - sess.query(User.id, User.name.label("foo"), Address.id) - .distinct() - .order_by(User.id, User.name, Address.email_address) - ) - - # Address.email_address is added because of DISTINCT, - # however User.id, User.name are not b.c. they're already there, - # even though User.name is labeled - with testing.expect_deprecated( - "ORDER BY columns added implicitly due to " - ): - self.assert_compile( - q, - "SELECT DISTINCT users.id AS users_id, users.name AS foo, " - "addresses.id AS addresses_id, addresses.email_address AS " - "addresses_email_address FROM users, addresses " - "ORDER BY users.id, users.name, addresses.email_address", - ) - - class SessionEventsTest(_RemoveListeners, _fixtures.FixtureTest): run_inserts = None @@ -2804,252 +2603,6 @@ class ParentTest(QueryTest, AssertsCompiledSQL): ) -class CollectionCascadesDespiteBackrefTest(fixtures.TestBase): - """test old cascade_backrefs behavior - - see test/orm/test_cascade.py::class CollectionCascadesNoBackrefTest - for the future version - - """ - - @testing.fixture - def cascade_fixture(self, registry): - def go(collection_class): - @registry.mapped - class A: - __tablename__ = "a" - - id = Column(Integer, primary_key=True) - bs = relationship( - "B", backref="a", collection_class=collection_class - ) - - @registry.mapped - class B: - __tablename__ = "b_" - id = Column(Integer, primary_key=True) - a_id = Column(ForeignKey("a.id")) - key = Column(String) - - return A, B - - yield go - - @testing.combinations( - (set, "add"), - (list, "append"), - (attribute_mapped_collection("key"), "__setitem__"), - (attribute_mapped_collection("key"), "setdefault"), - (attribute_mapped_collection("key"), "update_dict"), - (attribute_mapped_collection("key"), "update_kw"), - argnames="collection_class,methname", - ) - @testing.combinations((True,), (False,), argnames="future") - def test_cascades_on_collection( - self, cascade_fixture, collection_class, methname, future - ): - A, B = cascade_fixture(collection_class) - - s = Session(future=future) - - a1 = A() - s.add(a1) - - b1 = B(key="b1") - b2 = B(key="b2") - b3 = B(key="b3") - - if future: - dep_ctx = nullcontext - else: - - def dep_ctx(): - return assertions.expect_deprecated_20( - '"B" object is being merged into a Session along the ' - 'backref cascade path for relationship "A.bs"' - ) - - with dep_ctx(): - b1.a = a1 - with dep_ctx(): - b3.a = a1 - - if future: - assert b1 not in s - assert b3 not in s - else: - assert b1 in s - assert b3 in s - - if methname == "__setitem__": - meth = getattr(a1.bs, methname) - meth(b1.key, b1) - meth(b2.key, b2) - elif methname == "setdefault": - meth = getattr(a1.bs, methname) - meth(b1.key, b1) - meth(b2.key, b2) - elif methname == "update_dict" and isinstance(a1.bs, dict): - a1.bs.update({b1.key: b1, b2.key: b2}) - elif methname == "update_kw" and isinstance(a1.bs, dict): - a1.bs.update(b1=b1, b2=b2) - else: - meth = getattr(a1.bs, methname) - meth(b1) - meth(b2) - - assert b1 in s - assert b2 in s - - # future version: - if future: - assert b3 not in s # the event never triggers from reverse - else: - # old behavior - assert b3 in s - - -class LoadOnFKsTest(fixtures.DeclarativeMappedTest): - @classmethod - def setup_classes(cls): - Base = cls.DeclarativeBasic - - class Parent(Base): - __tablename__ = "parent" - __table_args__ = {"mysql_engine": "InnoDB"} - - id = Column( - Integer, primary_key=True, test_needs_autoincrement=True - ) - - class Child(Base): - __tablename__ = "child" - __table_args__ = {"mysql_engine": "InnoDB"} - - id = Column( - Integer, primary_key=True, test_needs_autoincrement=True - ) - parent_id = Column(Integer, ForeignKey("parent.id")) - - parent = relationship(Parent, backref=backref("children")) - - @testing.fixture - def parent_fixture(self, connection): - Parent, Child = self.classes("Parent", "Child") - - sess = fixture_session(bind=connection, autoflush=False) - p1 = Parent() - p2 = Parent() - c1, c2 = Child(), Child() - c1.parent = p1 - sess.add_all([p1, p2]) - assert c1 in sess - - yield sess, p1, p2, c1, c2 - - sess.close() - - def test_enable_rel_loading_on_persistent_allows_backref_event( - self, parent_fixture - ): - sess, p1, p2, c1, c2 = parent_fixture - Parent, Child = self.classes("Parent", "Child") - - c3 = Child() - sess.enable_relationship_loading(c3) - c3.parent_id = p1.id - with assertions.expect_deprecated_20( - '"Child" object is being merged into a Session along the ' - 'backref cascade path for relationship "Parent.children"' - ): - c3.parent = p1 - - # backref fired off when c3.parent was set, - # because the "old" value was None - # change as of [ticket:3708] - assert c3 in p1.children - - def test_enable_rel_loading_allows_backref_event(self, parent_fixture): - sess, p1, p2, c1, c2 = parent_fixture - Parent, Child = self.classes("Parent", "Child") - - c3 = Child() - sess.enable_relationship_loading(c3) - c3.parent_id = p1.id - - with assertions.expect_deprecated_20( - '"Child" object is being merged into a Session along the ' - 'backref cascade path for relationship "Parent.children"' - ): - c3.parent = p1 - - # backref fired off when c3.parent was set, - # because the "old" value was None - # change as of [ticket:3708] - assert c3 in p1.children - - -class LazyTest(_fixtures.FixtureTest): - run_inserts = "once" - run_deletes = None - - def test_backrefs_dont_lazyload(self): - users, Address, addresses, User = ( - self.tables.users, - self.classes.Address, - self.tables.addresses, - self.classes.User, - ) - - self.mapper_registry.map_imperatively( - User, - users, - properties={"addresses": relationship(Address, backref="user")}, - ) - self.mapper_registry.map_imperatively(Address, addresses) - sess = fixture_session(autoflush=False) - ad = sess.query(Address).filter_by(id=1).one() - assert ad.user.id == 7 - - def go(): - ad.user = None - assert ad.user is None - - self.assert_sql_count(testing.db, go, 0) - - u1 = sess.query(User).filter_by(id=7).one() - - def go(): - assert ad not in u1.addresses - - self.assert_sql_count(testing.db, go, 1) - - sess.expire(u1, ["addresses"]) - - def go(): - assert ad in u1.addresses - - self.assert_sql_count(testing.db, go, 1) - - sess.expire(u1, ["addresses"]) - ad2 = Address() - - def go(): - with assertions.expect_deprecated_20( - ".* object is being merged into a Session along the " - "backref cascade path for relationship " - ): - ad2.user = u1 - assert ad2.user is u1 - - self.assert_sql_count(testing.db, go, 0) - - def go(): - assert ad2 in u1.addresses - - self.assert_sql_count(testing.db, go, 1) - - class MergeResultTest(_fixtures.FixtureTest): run_setup_mappers = "once" run_inserts = "once" diff --git a/test/orm/test_events.py b/test/orm/test_events.py index 3437d7942..92ef241ed 100644 --- a/test/orm/test_events.py +++ b/test/orm/test_events.py @@ -24,7 +24,6 @@ from sqlalchemy.orm import instrumentation from sqlalchemy.orm import joinedload from sqlalchemy.orm import lazyload from sqlalchemy.orm import Mapper -from sqlalchemy.orm import mapper from sqlalchemy.orm import mapperlib from sqlalchemy.orm import query from sqlalchemy.orm import relationship @@ -672,11 +671,10 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): def init_e(target, args, kwargs): canary.append(("init_e", target)) - event.listen(mapper, "init", init_a) - event.listen(Mapper, "init", init_b) - event.listen(class_mapper(A), "init", init_c) - event.listen(A, "init", init_d) - event.listen(A, "init", init_e, propagate=True) + event.listen(Mapper, "init", init_a) + event.listen(class_mapper(A), "init", init_b) + event.listen(A, "init", init_c) + event.listen(A, "init", init_d, propagate=True) a = A() eq_( @@ -686,14 +684,13 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): ("init_b", a), ("init_c", a), ("init_d", a), - ("init_e", a), ], ) # test propagate flag canary[:] = [] b = B() - eq_(canary, [("init_a", b), ("init_b", b), ("init_e", b)]) + eq_(canary, [("init_a", b), ("init_d", b)]) def listen_all(self, mapper, **kw): canary = [] @@ -809,10 +806,10 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): canary = Mock() - event.listen(mapper, "before_configured", canary.listen1) - event.listen(mapper, "before_configured", canary.listen2, insert=True) - event.listen(mapper, "before_configured", canary.listen3) - event.listen(mapper, "before_configured", canary.listen4, insert=True) + event.listen(Mapper, "before_configured", canary.listen1) + event.listen(Mapper, "before_configured", canary.listen2, insert=True) + event.listen(Mapper, "before_configured", canary.listen3) + event.listen(Mapper, "before_configured", canary.listen4, insert=True) configure_mappers() @@ -864,7 +861,7 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): def load(obj, ctx): canary.append("load") - event.listen(mapper, "load", load) + event.listen(Mapper, "load", load) s = fixture_session() u = User(name="u1") @@ -1065,7 +1062,7 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): assert_raises_message( sa.exc.SAWarning, r"before_configured' and 'after_configured' ORM events only " - r"invoke with the mapper\(\) function or Mapper class as " + r"invoke with the Mapper class as " r"the target.", event.listen, User, @@ -1076,7 +1073,7 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): assert_raises_message( sa.exc.SAWarning, r"before_configured' and 'after_configured' ORM events only " - r"invoke with the mapper\(\) function or Mapper class as " + r"invoke with the Mapper class as " r"the target.", event.listen, User, @@ -1092,8 +1089,8 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): self.mapper_registry.map_imperatively(User, users) - event.listen(mapper, "before_configured", m1) - event.listen(mapper, "after_configured", m2) + event.listen(Mapper, "before_configured", m1) + event.listen(Mapper, "after_configured", m2) inspect(User)._post_inspect @@ -1135,7 +1132,7 @@ class MapperEventsTest(_RemoveListeners, _fixtures.FixtureTest): canary.init() # mapper level event - @event.listens_for(mapper, "instrument_class") + @event.listens_for(Mapper, "instrument_class") def instrument_class(mp, class_): canary.instrument_class(class_) @@ -2256,9 +2253,8 @@ class SessionEventsTest(_RemoveListeners, _fixtures.FixtureTest): sess.rollback() eq_(assertions, [True, True]) - @testing.combinations((True,), (False,)) - def test_autobegin_no_reentrant(self, future): - s1 = fixture_session(future=future) + def test_autobegin_no_reentrant(self): + s1 = fixture_session() canary = Mock() diff --git a/test/orm/test_lazy_relations.py b/test/orm/test_lazy_relations.py index cb83bb6f7..ee6d53652 100644 --- a/test/orm/test_lazy_relations.py +++ b/test/orm/test_lazy_relations.py @@ -954,7 +954,7 @@ class LazyTest(_fixtures.FixtureTest): properties={"addresses": relationship(Address, backref="user")}, ) self.mapper_registry.map_imperatively(Address, addresses) - sess = fixture_session(autoflush=False, future=True) + sess = fixture_session(autoflush=False) ad = sess.query(Address).filter_by(id=1).one() assert ad.user.id == 7 diff --git a/test/orm/test_load_on_fks.py b/test/orm/test_load_on_fks.py index fda8be423..f33a6881d 100644 --- a/test/orm/test_load_on_fks.py +++ b/test/orm/test_load_on_fks.py @@ -245,6 +245,37 @@ class LoadOnFKsTest(fixtures.DeclarativeMappedTest): self.assert_sql_count(testing.db, go, 0) + def test_enable_rel_loading_on_persistent_allows_backref_event( + self, parent_fixture + ): + sess, p1, p2, c1, c2 = parent_fixture + Parent, Child = self.classes("Parent", "Child") + + c3 = Child() + sess.enable_relationship_loading(c3) + c3.parent_id = p1.id + c3.parent = p1 + + # backref did not fire off when c3.parent was set. + # originally this was impacted by #3708, now does not happen + # due to backref_cascades behavior being removed + assert c3 not in p1.children + + def test_enable_rel_loading_allows_backref_event(self, parent_fixture): + sess, p1, p2, c1, c2 = parent_fixture + Parent, Child = self.classes("Parent", "Child") + + c3 = Child() + sess.enable_relationship_loading(c3) + c3.parent_id = p1.id + + c3.parent = p1 + + # backref did not fire off when c3.parent was set. + # originally this was impacted by #3708, now does not happen + # due to backref_cascades behavior being removed + assert c3 not in p1.children + def test_backref_doesnt_double(self, parent_fixture): sess, p1, p2, c1, c2 = parent_fixture Parent, Child = self.classes("Parent", "Child") diff --git a/test/orm/test_mapper.py b/test/orm/test_mapper.py index b491604f3..73288359e 100644 --- a/test/orm/test_mapper.py +++ b/test/orm/test_mapper.py @@ -340,7 +340,7 @@ class MapperTest(_fixtures.FixtureTest, AssertsCompiledSQL): m = self.mapper(User, users) session = fixture_session() - session.connection(mapper=m) + session.connection(bind_arguments=dict(mapper=m)) def test_incomplete_columns(self): """Loading from a select which does not contain all columns""" diff --git a/test/orm/test_naturalpks.py b/test/orm/test_naturalpks.py index 05df71c6a..f2700513b 100644 --- a/test/orm/test_naturalpks.py +++ b/test/orm/test_naturalpks.py @@ -871,12 +871,12 @@ class ReversePKsTest(fixtures.MappedTest): session.commit() # testing #3108 - session.begin_nested() + nt1 = session.begin_nested() a_published.status = ARCHIVED a_editable.status = PUBLISHED - session.commit() + nt1.commit() session.rollback() eq_(a_published.status, PUBLISHED) diff --git a/test/orm/test_query.py b/test/orm/test_query.py index 9bfeb36d8..a53c90ed4 100644 --- a/test/orm/test_query.py +++ b/test/orm/test_query.py @@ -2855,7 +2855,7 @@ class SliceTest(QueryTest): def test_negative_indexes_raise(self): User = self.classes.User - sess = fixture_session(future=True) + sess = fixture_session() q = sess.query(User).order_by(User.id) with expect_raises_message( diff --git a/test/orm/test_session.py b/test/orm/test_session.py index e821a7c20..a53756d63 100644 --- a/test/orm/test_session.py +++ b/test/orm/test_session.py @@ -83,6 +83,23 @@ class ExecutionTest(_fixtures.FixtureTest): [(7,), (8,), (9,)], ) + def test_no_string_execute(self, connection): + + with Session(bind=connection) as sess: + with expect_raises_message( + sa.exc.ArgumentError, + r"Textual SQL expression 'select \* from users where.*' " + "should be explicitly declared", + ): + sess.execute("select * from users where id=:id", {"id": 7}) + + with expect_raises_message( + sa.exc.ArgumentError, + r"Textual SQL expression 'select id from users .*' " + "should be explicitly declared", + ): + sess.scalar("select id from users where id=:id", {"id": 7}) + class TransScopingTest(_fixtures.FixtureTest): run_inserts = None @@ -734,7 +751,7 @@ class SessionStateTest(_fixtures.FixtureTest): assert sess.is_active def test_active_flag_autobegin_future(self): - sess = Session(bind=config.db, future=True) + sess = Session(bind=config.db) assert sess.is_active assert not sess.in_transaction() sess.begin() @@ -752,7 +769,7 @@ class SessionStateTest(_fixtures.FixtureTest): assert sess.is_active sess.begin(_subtrans=True) sess.rollback() - assert not sess.is_active + assert sess.is_active sess.rollback() assert sess.is_active @@ -888,7 +905,7 @@ class SessionStateTest(_fixtures.FixtureTest): ) self.mapper_registry.map_imperatively(Address, addresses) - session = fixture_session(future=True) + session = fixture_session() @event.listens_for(session, "after_flush") def load_collections(session, flush_context): diff --git a/test/orm/test_transaction.py b/test/orm/test_transaction.py index c5d06ba88..96a00ff54 100644 --- a/test/orm/test_transaction.py +++ b/test/orm/test_transaction.py @@ -41,26 +41,14 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): run_inserts = None __backend__ = True - @testing.fixture - def conn(self): - with testing.db.connect() as conn: - yield conn - - @testing.fixture - def future_conn(self): - - engine = testing.db - with engine.connect() as conn: - yield conn - - def test_no_close_transaction_on_flush(self, conn): + def test_no_close_transaction_on_flush(self, connection): User, users = self.classes.User, self.tables.users - c = conn + c = connection self.mapper_registry.map_imperatively(User, users) s = Session(bind=c) s.begin() - tran = s._legacy_transaction() + tran = s.get_transaction() s.add(User(name="first")) s.flush() c.exec_driver_sql("select * from users") @@ -70,15 +58,16 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): u = User(name="third") s.add(u) s.flush() - assert s._legacy_transaction() is tran + assert s.get_transaction() is tran tran.close() - def test_subtransaction_on_external_no_begin(self, conn): + def test_subtransaction_on_external_no_begin(self, connection_no_trans): users, User = self.tables.users, self.classes.User + connection = connection_no_trans self.mapper_registry.map_imperatively(User, users) - trans = conn.begin() - sess = Session(bind=conn, autoflush=True) + trans = connection.begin() + sess = Session(bind=connection, autoflush=True) u = User(name="ed") sess.add(u) sess.flush() @@ -88,12 +77,14 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): sess.close() @testing.requires.savepoints - def test_external_nested_transaction(self, conn): + def test_external_nested_transaction(self, connection_no_trans): users, User = self.tables.users, self.classes.User self.mapper_registry.map_imperatively(User, users) - trans = conn.begin() - sess = Session(bind=conn, autoflush=True) + + connection = connection_no_trans + trans = connection.begin() + sess = Session(bind=connection, autoflush=True) u1 = User(name="u1") sess.add(u1) sess.flush() @@ -107,60 +98,60 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): trans.commit() assert len(sess.query(User).all()) == 1 - def test_subtransaction_on_external_commit_future(self, future_conn): + def test_subtransaction_on_external_commit(self, connection_no_trans): users, User = self.tables.users, self.classes.User self.mapper_registry.map_imperatively(User, users) - conn = future_conn - conn.begin() + connection = connection_no_trans + connection.begin() - sess = Session(bind=conn, autoflush=True) + sess = Session(bind=connection, autoflush=True) u = User(name="ed") sess.add(u) sess.flush() sess.commit() # commit does nothing - conn.rollback() # rolls back + connection.rollback() # rolls back assert len(sess.query(User).all()) == 0 sess.close() - def test_subtransaction_on_external_rollback_future(self, future_conn): + def test_subtransaction_on_external_rollback(self, connection_no_trans): users, User = self.tables.users, self.classes.User self.mapper_registry.map_imperatively(User, users) - conn = future_conn - conn.begin() + connection = connection_no_trans + connection.begin() - sess = Session(bind=conn, autoflush=True) + sess = Session(bind=connection, autoflush=True) u = User(name="ed") sess.add(u) sess.flush() sess.rollback() # rolls back - conn.commit() # nothing to commit + connection.commit() # nothing to commit assert len(sess.query(User).all()) == 0 sess.close() @testing.requires.savepoints - def test_savepoint_on_external_future(self, future_conn): + def test_savepoint_on_external(self, connection_no_trans): users, User = self.tables.users, self.classes.User self.mapper_registry.map_imperatively(User, users) - conn = future_conn - conn.begin() - sess = Session(bind=conn, autoflush=True) + connection = connection_no_trans + connection.begin() + sess = Session(bind=connection, autoflush=True) u1 = User(name="u1") sess.add(u1) sess.flush() - sess.begin_nested() + n1 = sess.begin_nested() u2 = User(name="u2") sess.add(u2) sess.flush() - sess.rollback() + n1.rollback() - conn.commit() + connection.commit() assert len(sess.query(User).all()) == 1 @testing.requires.savepoints @@ -171,10 +162,10 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): session = fixture_session() session.begin() - session.begin_nested() + n1 = session.begin_nested() u1 = User(name="u1") session.add(u1) - session.commit() + n1.commit() assert u1 in session session.rollback() assert u1 not in session @@ -194,9 +185,9 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): session.begin() u1 = session.query(User).first() - session.begin_nested() + n1 = session.begin_nested() session.delete(u1) - session.commit() + n1.commit() assert u1 not in session session.rollback() assert u1 in session @@ -221,34 +212,6 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): assert attributes.instance_state(u1) in nt2._dirty assert attributes.instance_state(u1) not in nt1._dirty - s.commit() - assert attributes.instance_state(u1) in nt2._dirty - assert attributes.instance_state(u1) in nt1._dirty - - s.rollback() - assert attributes.instance_state(u1).expired - eq_(u1.name, "u1") - - @testing.requires.savepoints - def test_dirty_state_transferred_deep_nesting_future(self): - User, users = self.classes.User, self.tables.users - - self.mapper_registry.map_imperatively(User, users) - - with fixture_session(future=True) as s: - u1 = User(name="u1") - s.add(u1) - s.commit() - - nt1 = s.begin_nested() - nt2 = s.begin_nested() - u1.name = "u2" - assert attributes.instance_state(u1) not in nt2._dirty - assert attributes.instance_state(u1) not in nt1._dirty - s.flush() - assert attributes.instance_state(u1) in nt2._dirty - assert attributes.instance_state(u1) not in nt1._dirty - nt2.commit() assert attributes.instance_state(u1) in nt2._dirty assert attributes.instance_state(u1) in nt1._dirty @@ -341,13 +304,13 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): sess.add(u) sess.flush() - sess.begin_nested() # nested transaction + n1 = sess.begin_nested() # nested transaction u2 = User(name="u2") sess.add(u2) sess.flush() - sess.rollback() + n1.rollback() sess.commit() assert len(sess.query(User).all()) == 1 @@ -369,28 +332,6 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): sess.add(u2) sess.flush() - sess.rollback() # rolls back nested only - - sess.commit() - assert len(sess.query(User).all()) == 1 - sess.close() - - @testing.requires.savepoints - def test_nested_autotrans_future(self): - User, users = self.classes.User, self.tables.users - - self.mapper_registry.map_imperatively(User, users) - sess = fixture_session(future=True) - u = User(name="u1") - sess.add(u) - sess.flush() - - sess.begin_nested() # nested transaction - - u2 = User(name="u2") - sess.add(u2) - sess.flush() - sess.rollback() # rolls back the whole trans sess.commit() @@ -423,11 +364,11 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): sess.rollback() sess.begin() - sess.begin_nested() + n1 = sess.begin_nested() u3 = User(name="u3") sess.add(u3) - sess.commit() # commit the nested transaction + n1.commit() # commit the nested transaction sess.rollback() eq_(set(sess.query(User).all()), set([u2])) @@ -686,6 +627,10 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): eq_(session.is_active, False) session.rollback() + is_(session._transaction, None) + + session.connection() + # back to normal eq_(session._transaction._state, _session.ACTIVE) eq_(session.is_active, True) @@ -878,26 +823,18 @@ class SessionTransactionTest(fixtures.RemovesEvents, FixtureTest): sess.add(User(id=5, name="some name")) sess.commit() - def test_no_autocommit_with_explicit_commit(self): + def test_no_autobegin_after_explicit_commit(self): User, users = self.classes.User, self.tables.users self.mapper_registry.map_imperatively(User, users) session = fixture_session() session.add(User(name="ed")) - session._legacy_transaction().commit() - - is_not(session._legacy_transaction(), None) - - def test_no_autocommit_with_explicit_commit_future(self): - User, users = self.classes.User, self.tables.users + session.get_transaction().commit() - self.mapper_registry.map_imperatively(User, users) - session = fixture_session(future=True) - session.add(User(name="ed")) - session._legacy_transaction().commit() + is_(session.get_transaction(), None) - # new in 1.4 - is_(session._legacy_transaction(), None) + session.connection() + is_not(session.get_transaction(), None) class _LocalFixture(FixtureTest): @@ -989,7 +926,6 @@ def subtransaction_recipe_three(self): argnames="target_recipe,recipe_rollsback_early", id_="ns", ) -@testing.combinations((True,), (False,), argnames="future", id_="s") class SubtransactionRecipeTest(FixtureTest): run_inserts = None __backend__ = True @@ -1002,7 +938,7 @@ class SubtransactionRecipeTest(FixtureTest): def test_recipe_heavy_nesting(self, subtransaction_recipe): users = self.tables.users - with fixture_session(future=self.future) as session: + with fixture_session() as session: with subtransaction_recipe(session): session.connection().execute( users.insert().values(name="user1") @@ -1046,7 +982,7 @@ class SubtransactionRecipeTest(FixtureTest): self.mapper_registry.map_imperatively(User, users) with testing.db.connect() as conn: trans = conn.begin() - sess = Session(conn, future=self.future) + sess = Session(conn) with subtransaction_recipe(sess): u = User(name="ed") @@ -1061,7 +997,7 @@ class SubtransactionRecipeTest(FixtureTest): User, users = self.classes.User, self.tables.users self.mapper_registry.map_imperatively(User, users) - with fixture_session(future=self.future) as sess: + with fixture_session() as sess: with subtransaction_recipe(sess): u = User(name="u1") sess.add(u) @@ -1074,7 +1010,7 @@ class SubtransactionRecipeTest(FixtureTest): User, users = self.classes.User, self.tables.users self.mapper_registry.map_imperatively(User, users) - with fixture_session(future=self.future) as sess: + with fixture_session() as sess: sess.begin() with subtransaction_recipe(sess): u = User(name="u1") @@ -1090,7 +1026,7 @@ class SubtransactionRecipeTest(FixtureTest): self.mapper_registry.map_imperatively(User, users) - with fixture_session(future=self.future) as sess: + with fixture_session() as sess: sess.begin() sess.begin_nested() @@ -1111,7 +1047,7 @@ class SubtransactionRecipeTest(FixtureTest): sess.add(User(name="u2")) t2.commit() - assert sess._legacy_transaction() is t1 + assert sess.get_transaction() is t1 def test_recipe_error_on_using_inactive_session_commands( self, subtransaction_recipe @@ -1119,7 +1055,7 @@ class SubtransactionRecipeTest(FixtureTest): users, User = self.tables.users, self.classes.User self.mapper_registry.map_imperatively(User, users) - with fixture_session(future=self.future) as sess: + with fixture_session() as sess: sess.begin() try: @@ -1141,13 +1077,13 @@ class SubtransactionRecipeTest(FixtureTest): assert not sess.in_transaction() def test_recipe_multi_nesting(self, subtransaction_recipe): - with fixture_session(future=self.future) as sess: + with fixture_session() as sess: with subtransaction_recipe(sess): assert sess.in_transaction() try: with subtransaction_recipe(sess): - assert sess._legacy_transaction() + assert sess.get_transaction() raise Exception("force rollback") except: pass @@ -1160,7 +1096,7 @@ class SubtransactionRecipeTest(FixtureTest): assert not sess.in_transaction() def test_recipe_deactive_status_check(self, subtransaction_recipe): - with fixture_session(future=self.future) as sess: + with fixture_session() as sess: sess.begin() with subtransaction_recipe(sess): @@ -1217,12 +1153,12 @@ class CleanSavepointTest(FixtureTest): run_inserts = None __backend__ = True - def _run_test(self, update_fn, future=False): + def _run_test(self, update_fn): User, users = self.classes.User, self.tables.users self.mapper_registry.map_imperatively(User, users) - with fixture_session(future=future) as s: + with fixture_session() as s: u1 = User(name="u1") u2 = User(name="u2") s.add_all([u1, u2]) @@ -1236,12 +1172,8 @@ class CleanSavepointTest(FixtureTest): eq_(u2.name, "u2modified") s.rollback() - if future: - assert s._transaction is None - assert "name" not in u1.__dict__ - else: - assert s._transaction is trans - eq_(u1.__dict__["name"], "u1") + assert s._transaction is None + assert "name" not in u1.__dict__ assert "name" not in u2.__dict__ eq_(u2.name, "u2") @@ -1498,13 +1430,13 @@ class RollbackRecoverTest(_LocalFixture): u1.name = "edward" a1.email_address = "foober" - s.begin_nested() + nt1 = s.begin_nested() s.add(u2) with expect_warnings("New instance"): assert_raises(sa_exc.IntegrityError, s.commit) assert_raises(sa_exc.InvalidRequestError, s.commit) - s.rollback() + nt1.rollback() assert u2 not in s assert a2 not in s assert u1 in s @@ -1534,7 +1466,7 @@ class SavepointTest(_LocalFixture): u2 = User(name="jack") s.add_all([u1, u2]) - s.begin_nested() + nt1 = s.begin_nested() u3 = User(name="wendy") u4 = User(name="foo") u1.name = "edward" @@ -1544,7 +1476,7 @@ class SavepointTest(_LocalFixture): s.query(User.name).order_by(User.id).all(), [("edward",), ("jackward",), ("wendy",), ("foo",)], ) - s.rollback() + nt1.rollback() assert u1.name == "ed" assert u2.name == "jack" eq_(s.query(User.name).order_by(User.id).all(), [("ed",), ("jack",)]) @@ -1575,7 +1507,7 @@ class SavepointTest(_LocalFixture): u2 = User(name="jack") s.add_all([u1, u2]) - s.begin_nested() + nt1 = s.begin_nested() u3 = User(name="wendy") u4 = User(name="foo") u1.name = "edward" @@ -1585,7 +1517,7 @@ class SavepointTest(_LocalFixture): s.query(User.name).order_by(User.id).all(), [("edward",), ("jackward",), ("wendy",), ("foo",)], ) - s.commit() + nt1.commit() def go(): assert u1.name == "edward" @@ -1613,7 +1545,7 @@ class SavepointTest(_LocalFixture): u1.name = "edward" u1.addresses.append(Address(email_address="bar")) - s.begin_nested() + nt1 = s.begin_nested() u2 = User(name="jack", addresses=[Address(email_address="bat")]) s.add(u2) eq_( @@ -1629,7 +1561,7 @@ class SavepointTest(_LocalFixture): User(name="jack", addresses=[Address(email_address="bat")]), ], ) - s.rollback() + nt1.rollback() eq_( s.query(User).order_by(User.id).all(), [ @@ -1751,7 +1683,7 @@ class SavepointTest(_LocalFixture): nested_trans = trans._connections[self.bind][1] nested_trans._do_commit() - is_(s._legacy_transaction(), trans) + is_(s.get_nested_transaction(), trans) with expect_warnings("nested transaction already deassociated"): # this previously would raise @@ -1762,12 +1694,14 @@ class SavepointTest(_LocalFixture): assert u1 not in s.new is_(trans._state, _session.CLOSED) - is_not(s._legacy_transaction(), trans) - is_(s._legacy_transaction()._state, _session.ACTIVE) + is_not(s.get_transaction(), trans) - is_(s._legacy_transaction().nested, False) + s.connection() + is_(s.get_transaction()._state, _session.ACTIVE) + + is_(s.get_transaction().nested, False) - is_(s._legacy_transaction()._parent, None) + is_(s.get_transaction()._parent, None) class AccountingFlagsTest(_LocalFixture): @@ -1851,7 +1785,7 @@ class ContextManagerPlusFutureTest(FixtureTest): def test_explicit_begin(self): with fixture_session() as s1: with s1.begin() as trans: - is_(trans, s1._legacy_transaction()) + is_(trans, s1.get_transaction()) s1.connection() is_(s1._transaction, None) @@ -1866,10 +1800,10 @@ class ContextManagerPlusFutureTest(FixtureTest): ) @testing.requires.savepoints - def test_future_rollback_is_global(self): + def test_rollback_is_global(self): users = self.tables.users - with fixture_session(future=True) as s1: + with fixture_session() as s1: s1.begin() s1.connection().execute(users.insert(), [{"id": 1, "name": "n1"}]) @@ -1890,7 +1824,7 @@ class ContextManagerPlusFutureTest(FixtureTest): # rolls back the whole transaction s1.rollback() - is_(s1._legacy_transaction(), None) + is_(s1.get_transaction(), None) eq_( s1.connection().scalar( @@ -1900,79 +1834,13 @@ class ContextManagerPlusFutureTest(FixtureTest): ) s1.commit() - is_(s1._legacy_transaction(), None) - - @testing.requires.savepoints - def test_old_rollback_is_local(self): - users = self.tables.users - - with fixture_session() as s1: - - t1 = s1.begin() - - s1.connection().execute(users.insert(), [{"id": 1, "name": "n1"}]) - - s1.begin_nested() - - s1.connection().execute( - users.insert(), - [{"id": 2, "name": "n2"}, {"id": 3, "name": "n3"}], - ) - - eq_( - s1.connection().scalar( - select(func.count()).select_from(users) - ), - 3, - ) - - # rolls back only the savepoint - s1.rollback() - - is_(s1._legacy_transaction(), t1) - - eq_( - s1.connection().scalar( - select(func.count()).select_from(users) - ), - 1, - ) - - s1.commit() - eq_( - s1.connection().scalar( - select(func.count()).select_from(users) - ), - 1, - ) - is_not(s1._legacy_transaction(), None) + is_(s1.get_transaction(), None) def test_session_as_ctx_manager_one(self): users = self.tables.users with fixture_session() as sess: - is_not(sess._legacy_transaction(), None) - - sess.connection().execute( - users.insert().values(id=1, name="user1") - ) - - eq_( - sess.connection().execute(users.select()).all(), [(1, "user1")] - ) - - is_not(sess._legacy_transaction(), None) - - is_not(sess._legacy_transaction(), None) - - # did not commit - eq_(sess.connection().execute(users.select()).all(), []) - - def test_session_as_ctx_manager_future_one(self): - users = self.tables.users - - with fixture_session(future=True) as sess: - is_(sess._legacy_transaction(), None) + is_(sess.get_transaction(), None) sess.connection().execute( users.insert().values(id=1, name="user1") @@ -1982,9 +1850,9 @@ class ContextManagerPlusFutureTest(FixtureTest): sess.connection().execute(users.select()).all(), [(1, "user1")] ) - is_not(sess._legacy_transaction(), None) + is_not(sess.get_transaction(), None) - is_(sess._legacy_transaction(), None) + is_(sess.get_transaction(), None) # did not commit eq_(sess.connection().execute(users.select()).all(), []) @@ -1994,23 +1862,7 @@ class ContextManagerPlusFutureTest(FixtureTest): try: with fixture_session() as sess: - is_not(sess._legacy_transaction(), None) - - sess.connection().execute( - users.insert().values(id=1, name="user1") - ) - - raise Exception("force rollback") - except: - pass - is_not(sess._legacy_transaction(), None) - - def test_session_as_ctx_manager_two_future(self): - users = self.tables.users - - try: - with fixture_session(future=True) as sess: - is_(sess._legacy_transaction(), None) + is_(sess.get_transaction(), None) sess.connection().execute( users.insert().values(id=1, name="user1") @@ -2019,7 +1871,7 @@ class ContextManagerPlusFutureTest(FixtureTest): raise Exception("force rollback") except: pass - is_(sess._legacy_transaction(), None) + is_(sess.get_transaction(), None) def test_begin_context_manager(self): users = self.tables.users @@ -2151,15 +2003,13 @@ class ContextManagerPlusFutureTest(FixtureTest): eq_(sess.connection().execute(users.select()).all(), [(1, "user1")]) sess.close() - @testing.combinations((True,), (False,), argnames="future") - def test_interrupt_ctxmanager(self, trans_ctx_manager_fixture, future): + def test_interrupt_ctxmanager(self, trans_ctx_manager_fixture): fn = trans_ctx_manager_fixture - session = fixture_session(future=future) + session = fixture_session() fn(session, trans_on_subject=True, execute_on_subject=True) - @testing.combinations((True,), (False,), argnames="future") @testing.combinations((True,), (False,), argnames="rollback") @testing.combinations((True,), (False,), argnames="expire_on_commit") @testing.combinations( @@ -2170,15 +2020,13 @@ class ContextManagerPlusFutureTest(FixtureTest): argnames="check_operation", ) def test_interrupt_ctxmanager_ops( - self, future, rollback, expire_on_commit, check_operation + self, rollback, expire_on_commit, check_operation ): users, User = self.tables.users, self.classes.User self.mapper_registry.map_imperatively(User, users) - session = fixture_session( - future=future, expire_on_commit=expire_on_commit - ) + session = fixture_session(expire_on_commit=expire_on_commit) with session.begin(): u1 = User(id=7, name="u1") @@ -2266,8 +2114,8 @@ class TransactionFlagsTest(fixtures.TestBase): s1.rollback() - eq_(s1.in_transaction(), True) - is_(s1._transaction, trans) + eq_(s1.in_transaction(), False) + is_(s1._transaction, None) s1.rollback() @@ -2560,7 +2408,7 @@ class NewStyleJoinIntoAnExternalTransactionTest( self.A = A # bind an individual Session to the connection - self.session = Session(bind=self.connection, future=True) + self.session = Session(bind=self.connection) if testing.requires.savepoints.enabled: self.nested = self.connection.begin_nested() diff --git a/test/orm/test_versioning.py b/test/orm/test_versioning.py index 9d14ceba1..b5955e4a6 100644 --- a/test/orm/test_versioning.py +++ b/test/orm/test_versioning.py @@ -748,7 +748,7 @@ class VersionOnPostUpdateTest(fixtures.MappedTest): # outwit the database transaction isolation and SQLA's # expiration at the same time by using different Session on # same transaction - s2 = Session(bind=s.connection(mapper=Node)) + s2 = Session(bind=s.connection(bind_arguments=dict(mapper=Node))) s2.query(Node).filter(Node.id == n2.id).update({"version_id": 3}) s2.commit() @@ -770,7 +770,7 @@ class VersionOnPostUpdateTest(fixtures.MappedTest): ), patch.object( config.db.dialect, "supports_sane_multi_rowcount", False ): - s2 = Session(bind=s.connection(mapper=Node)) + s2 = Session(bind=s.connection(bind_arguments=dict(mapper=Node))) s2.query(Node).filter(Node.id == n2.id).update({"version_id": 3}) s2.commit() @@ -791,7 +791,7 @@ class VersionOnPostUpdateTest(fixtures.MappedTest): # outwit the database transaction isolation and SQLA's # expiration at the same time by using different Session on # same transaction - s2 = Session(bind=s.connection(mapper=Node)) + s2 = Session(bind=s.connection(bind_arguments=dict(mapper=Node))) s2.query(Node).filter(Node.id == n1.id).update({"version_id": 3}) s2.commit() |
