summaryrefslogtreecommitdiff
path: root/lib/sqlalchemy
diff options
context:
space:
mode:
authorGord Thompson <gord@gordthompson.com>2021-08-12 13:04:28 -0600
committerMike Bayer <mike_mp@zzzcomputing.com>2021-09-30 10:10:16 -0400
commit257f9130c321aaa948690d0e49c7352ad1188abd (patch)
tree2648c88460c3e5de847ee998edb7a6df0aa9b261 /lib/sqlalchemy
parentf7f2df607301afcd11dd49a2ccb632291de12d29 (diff)
downloadsqlalchemy-257f9130c321aaa948690d0e49c7352ad1188abd.tar.gz
Modernize tests - calling_mapper_directly
a few changes for py2k: * map_imperatively() includes the check that a class is being sent, this was only working for mapper() before * the test suite didn't place the py2k "autouse" workaround in the correct order, seemingly, tried to adjust the per-test ordering setup in pytestplugin.py Change-Id: I4cc39630724e810953cfda7b2afdadc8b948e3c2
Diffstat (limited to 'lib/sqlalchemy')
-rw-r--r--lib/sqlalchemy/orm/decl_base.py40
-rw-r--r--lib/sqlalchemy/testing/fixtures.py15
-rw-r--r--lib/sqlalchemy/testing/plugin/pytestplugin.py47
-rw-r--r--lib/sqlalchemy/testing/warnings.py5
4 files changed, 60 insertions, 47 deletions
diff --git a/lib/sqlalchemy/orm/decl_base.py b/lib/sqlalchemy/orm/decl_base.py
index e1945e8ee..bf1bc537d 100644
--- a/lib/sqlalchemy/orm/decl_base.py
+++ b/lib/sqlalchemy/orm/decl_base.py
@@ -176,19 +176,28 @@ class _MapperConfig(object):
return cfg_cls(registry, cls_, dict_, table, mapper_kw)
- def __init__(self, registry, cls_):
- self.cls = cls_
+ def __init__(self, registry, cls_, mapper_kw):
+ self.cls = util.assert_arg_type(cls_, type, "cls_")
self.classname = cls_.__name__
self.properties = util.OrderedDict()
self.declared_attr_reg = {}
- instrumentation.register_class(
- self.cls,
- finalize=False,
- registry=registry,
- declarative_scan=self,
- init_method=registry.constructor,
- )
+ if not mapper_kw.get("non_primary", False):
+ instrumentation.register_class(
+ self.cls,
+ finalize=False,
+ registry=registry,
+ declarative_scan=self,
+ init_method=registry.constructor,
+ )
+ else:
+ manager = attributes.manager_of_class(self.cls)
+ if not manager or not manager.is_mapped:
+ raise exc.InvalidRequestError(
+ "Class %s has no primary mapper configured. Configure "
+ "a primary mapper first before setting up a non primary "
+ "Mapper." % self.cls
+ )
def set_cls_attribute(self, attrname, value):
@@ -210,15 +219,18 @@ class _ImperativeMapperConfig(_MapperConfig):
table,
mapper_kw,
):
- super(_ImperativeMapperConfig, self).__init__(registry, cls_)
+ super(_ImperativeMapperConfig, self).__init__(
+ registry, cls_, mapper_kw
+ )
self.dict_ = {}
self.local_table = self.set_cls_attribute("__table__", table)
with mapperlib._CONFIGURE_MUTEX:
- clsregistry.add_class(
- self.classname, self.cls, registry._class_registry
- )
+ if not mapper_kw.get("non_primary", False):
+ clsregistry.add_class(
+ self.classname, self.cls, registry._class_registry
+ )
self._setup_inheritance(mapper_kw)
@@ -288,7 +300,7 @@ class _ClassScanMapperConfig(_MapperConfig):
mapper_kw,
):
- super(_ClassScanMapperConfig, self).__init__(registry, cls_)
+ super(_ClassScanMapperConfig, self).__init__(registry, cls_, mapper_kw)
self.dict_ = dict(dict_) if dict_ else {}
self.persist_selectable = None
diff --git a/lib/sqlalchemy/testing/fixtures.py b/lib/sqlalchemy/testing/fixtures.py
index 01a838c56..f04056c5e 100644
--- a/lib/sqlalchemy/testing/fixtures.py
+++ b/lib/sqlalchemy/testing/fixtures.py
@@ -638,12 +638,17 @@ class MappedTest(TablesTest, assertions.AssertsExecutionResults):
@classmethod
def _setup_once_mappers(cls):
if cls.run_setup_mappers == "once":
- cls.mapper = cls._generate_mapper()
+ cls.mapper_registry, cls.mapper = cls._generate_registry()
cls._with_register_classes(cls.setup_mappers)
def _setup_each_mappers(self):
+ if self.run_setup_mappers != "once":
+ (
+ self.__class__.mapper_registry,
+ self.__class__.mapper,
+ ) = self._generate_registry()
+
if self.run_setup_mappers == "each":
- self.__class__.mapper = self._generate_mapper()
self._with_register_classes(self.setup_mappers)
def _setup_each_classes(self):
@@ -651,9 +656,9 @@ class MappedTest(TablesTest, assertions.AssertsExecutionResults):
self._with_register_classes(self.setup_classes)
@classmethod
- def _generate_mapper(cls):
- decl = registry()
- return decl.map_imperatively
+ def _generate_registry(cls):
+ decl = registry(metadata=cls._tables_metadata)
+ return decl, decl.map_imperatively
@classmethod
def _with_register_classes(cls, fn):
diff --git a/lib/sqlalchemy/testing/plugin/pytestplugin.py b/lib/sqlalchemy/testing/plugin/pytestplugin.py
index d28048f70..6c6287060 100644
--- a/lib/sqlalchemy/testing/plugin/pytestplugin.py
+++ b/lib/sqlalchemy/testing/plugin/pytestplugin.py
@@ -479,7 +479,16 @@ def setup_test_methods(request):
self = request.instance
- # 1. run outer xdist-style setup
+ # before this fixture runs:
+
+ # 1. function level "autouse" fixtures under py3k (examples: TablesTest
+ # define tables / data, MappedTest define tables / mappers / data)
+
+ # 2. run homegrown function level "autouse" fixtures under py2k
+ if py2k:
+ reinvent_fixtures_py2k.run_fn_fixture_setup(request)
+
+ # 3. run outer xdist-style setup
if hasattr(self, "setup_test"):
asyncio._maybe_async(self.setup_test)
@@ -489,15 +498,7 @@ def setup_test_methods(request):
if hasattr(self, "setUp"):
asyncio._maybe_async(self.setUp)
- # 2. run homegrown function level "autouse" fixtures under py2k
- if py2k:
- reinvent_fixtures_py2k.run_fn_fixture_setup(request)
-
# inside the yield:
-
- # 3. function level "autouse" fixtures under py3k (examples: TablesTest
- # define tables / data, MappedTest define tables / mappers / data)
-
# 4. function level fixtures defined on test functions themselves,
# e.g. "connection", "metadata" run next
@@ -509,33 +510,33 @@ def setup_test_methods(request):
# yield finishes:
- # 7. pytest hook pytest_runtest_teardown hook runs, this is associated
+ # 7. function level fixtures defined on test functions
+ # themselves, e.g. "connection" rolls back the transaction, "metadata"
+ # emits drop all
+
+ # 8. pytest hook pytest_runtest_teardown hook runs, this is associated
# with fixtures close all sessions, provisioning.stop_test_class(),
# engines.testing_reaper -> ensure all connection pool connections
# are returned, engines created by testing_engine that aren't the
# config engine are disposed
- # 8. function level fixtures defined on test functions
- # themselves, e.g. "connection" rolls back the transaction, "metadata"
- # emits drop all
-
- # 9. function level "autouse" fixtures under py3k (examples: TablesTest /
- # MappedTest delete table data, possibly drop tables and clear mappers
- # depending on the flags defined by the test class)
-
- # 10. run homegrown function-level "autouse" fixtures under py2k
- if py2k:
- reinvent_fixtures_py2k.run_fn_fixture_teardown(request)
-
asyncio._maybe_async(plugin_base.after_test_fixtures, self)
- # 11. run outer xdist-style teardown
+ # 10. run xdist-style teardown
if hasattr(self, "tearDown"):
asyncio._maybe_async(self.tearDown)
if hasattr(self, "teardown_test"):
asyncio._maybe_async(self.teardown_test)
+ # 11. run homegrown function-level "autouse" fixtures under py2k
+ if py2k:
+ reinvent_fixtures_py2k.run_fn_fixture_teardown(request)
+
+ # 12. function level "autouse" fixtures under py3k (examples: TablesTest /
+ # MappedTest delete table data, possibly drop tables and clear mappers
+ # depending on the flags defined by the test class)
+
def getargspec(fn):
if sys.version_info.major == 3:
diff --git a/lib/sqlalchemy/testing/warnings.py b/lib/sqlalchemy/testing/warnings.py
index f72d53d67..67ed452a0 100644
--- a/lib/sqlalchemy/testing/warnings.py
+++ b/lib/sqlalchemy/testing/warnings.py
@@ -66,11 +66,6 @@ def setup_filters():
# we are moving one at a time
for msg in [
#
- # ORM configuration
- #
- r"Calling the mapper\(\) function directly outside of a "
- "declarative registry",
- #
# ORM Query
#
r"The Query\.get\(\) method",