summaryrefslogtreecommitdiff
path: root/test/orm
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2015-01-13 17:04:35 -0500
committerMike Bayer <mike_mp@zzzcomputing.com>2015-01-13 17:04:35 -0500
commitb63aae2c232f980a47aa2a635c35dfa45390f451 (patch)
tree61d23fbb2884155a30e3117198668e7c59117002 /test/orm
parent268bb4d5f6a8c8a23d6f53014980bba58698b4b4 (diff)
downloadsqlalchemy-b63aae2c232f980a47aa2a635c35dfa45390f451.tar.gz
- The "wildcard" loader options, in particular the one set up by
the :func:`.orm.load_only` option to cover all attributes not explicitly mentioned, now takes into account the superclasses of a given entity, if that entity is mapped with inheritance mapping, so that attribute names within the superclasses are also omitted from the load. Additionally, the polymorphic discriminator column is unconditionally included in the list, just in the same way that primary key columns are, so that even with load_only() set up, polymorphic loading of subtypes continues to function correctly. fixes #3287
Diffstat (limited to 'test/orm')
-rw-r--r--test/orm/test_deferred.py133
1 files changed, 131 insertions, 2 deletions
diff --git a/test/orm/test_deferred.py b/test/orm/test_deferred.py
index 1457852d8..1b777b527 100644
--- a/test/orm/test_deferred.py
+++ b/test/orm/test_deferred.py
@@ -2,10 +2,14 @@ import sqlalchemy as sa
from sqlalchemy import testing, util
from sqlalchemy.orm import mapper, deferred, defer, undefer, Load, \
load_only, undefer_group, create_session, synonym, relationship, Session,\
- joinedload, defaultload
+ joinedload, defaultload, aliased, contains_eager, with_polymorphic
from sqlalchemy.testing import eq_, AssertsCompiledSQL, assert_raises_message
from test.orm import _fixtures
-from sqlalchemy.orm import strategies
+
+
+from .inheritance._poly_fixtures import Company, Person, Engineer, Manager, \
+ Boss, Machine, Paperwork, _Polymorphic
+
class DeferredTest(AssertsCompiledSQL, _fixtures.FixtureTest):
@@ -595,3 +599,128 @@ class DeferredOptionsTest(AssertsCompiledSQL, _fixtures.FixtureTest):
)
+class InheritanceTest(_Polymorphic):
+ __dialect__ = 'default'
+
+ def test_load_only_subclass(self):
+ s = Session()
+ q = s.query(Manager).options(load_only("status", "manager_name"))
+ self.assert_compile(
+ q,
+ "SELECT managers.person_id AS managers_person_id, "
+ "people.person_id AS people_person_id, "
+ "people.type AS people_type, "
+ "managers.status AS managers_status, "
+ "managers.manager_name AS managers_manager_name "
+ "FROM people JOIN managers "
+ "ON people.person_id = managers.person_id "
+ "ORDER BY people.person_id"
+ )
+
+ def test_load_only_subclass_and_superclass(self):
+ s = Session()
+ q = s.query(Boss).options(load_only("status", "manager_name"))
+ self.assert_compile(
+ q,
+ "SELECT managers.person_id AS managers_person_id, "
+ "people.person_id AS people_person_id, "
+ "people.type AS people_type, "
+ "managers.status AS managers_status, "
+ "managers.manager_name AS managers_manager_name "
+ "FROM people JOIN managers "
+ "ON people.person_id = managers.person_id JOIN boss "
+ "ON managers.person_id = boss.boss_id ORDER BY people.person_id"
+ )
+
+ def test_load_only_alias_subclass(self):
+ s = Session()
+ m1 = aliased(Manager, flat=True)
+ q = s.query(m1).options(load_only("status", "manager_name"))
+ self.assert_compile(
+ q,
+ "SELECT managers_1.person_id AS managers_1_person_id, "
+ "people_1.person_id AS people_1_person_id, "
+ "people_1.type AS people_1_type, "
+ "managers_1.status AS managers_1_status, "
+ "managers_1.manager_name AS managers_1_manager_name "
+ "FROM people AS people_1 JOIN managers AS "
+ "managers_1 ON people_1.person_id = managers_1.person_id "
+ "ORDER BY people_1.person_id"
+ )
+
+ def test_load_only_subclass_from_relationship_polymorphic(self):
+ s = Session()
+ wp = with_polymorphic(Person, [Manager], flat=True)
+ q = s.query(Company).join(Company.employees.of_type(wp)).options(
+ contains_eager(Company.employees.of_type(wp)).
+ load_only(wp.Manager.status, wp.Manager.manager_name)
+ )
+ self.assert_compile(
+ q,
+ "SELECT people_1.person_id AS people_1_person_id, "
+ "people_1.type AS people_1_type, "
+ "managers_1.person_id AS managers_1_person_id, "
+ "managers_1.status AS managers_1_status, "
+ "managers_1.manager_name AS managers_1_manager_name, "
+ "companies.company_id AS companies_company_id, "
+ "companies.name AS companies_name "
+ "FROM companies JOIN (people AS people_1 LEFT OUTER JOIN "
+ "managers AS managers_1 ON people_1.person_id = "
+ "managers_1.person_id) ON companies.company_id = "
+ "people_1.company_id"
+ )
+
+ def test_load_only_subclass_from_relationship(self):
+ s = Session()
+ from sqlalchemy import inspect
+ inspect(Company).add_property("managers", relationship(Manager))
+ q = s.query(Company).join(Company.managers).options(
+ contains_eager(Company.managers).
+ load_only("status", "manager_name")
+ )
+ self.assert_compile(
+ q,
+ "SELECT companies.company_id AS companies_company_id, "
+ "companies.name AS companies_name, "
+ "managers.person_id AS managers_person_id, "
+ "people.person_id AS people_person_id, "
+ "people.type AS people_type, "
+ "managers.status AS managers_status, "
+ "managers.manager_name AS managers_manager_name "
+ "FROM companies JOIN (people JOIN managers ON people.person_id = "
+ "managers.person_id) ON companies.company_id = people.company_id"
+ )
+
+
+ def test_defer_on_wildcard_subclass(self):
+ # pretty much the same as load_only except doesn't
+ # exclude the primary key
+
+ s = Session()
+ q = s.query(Manager).options(
+ defer(".*"), undefer("status"))
+ self.assert_compile(
+ q,
+ "SELECT managers.status AS managers_status "
+ "FROM people JOIN managers ON "
+ "people.person_id = managers.person_id ORDER BY people.person_id"
+ )
+
+ def test_defer_super_name_on_subclass(self):
+ s = Session()
+ q = s.query(Manager).options(defer("name"))
+ self.assert_compile(
+ q,
+ "SELECT managers.person_id AS managers_person_id, "
+ "people.person_id AS people_person_id, "
+ "people.company_id AS people_company_id, "
+ "people.type AS people_type, managers.status AS managers_status, "
+ "managers.manager_name AS managers_manager_name "
+ "FROM people JOIN managers "
+ "ON people.person_id = managers.person_id "
+ "ORDER BY people.person_id"
+ )
+
+
+
+