diff options
| author | mike bayer <mike_mp@zzzcomputing.com> | 2021-07-09 21:29:40 +0000 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@ci3.zzzcomputing.com> | 2021-07-09 21:29:40 +0000 |
| commit | ca52e87268fec966f6005b1e4aa30206ae895e9e (patch) | |
| tree | 179bd2b61fbb50c114e0617e06913b8c5917357b /test | |
| parent | a457e555e0b1cc8aca79270f2330b5fdef78eafb (diff) | |
| parent | 4e1b2ee4cb345f499bdfbff6c393efbebd8e1e72 (diff) | |
| download | sqlalchemy-ca52e87268fec966f6005b1e4aa30206ae895e9e.tar.gz | |
Merge "Add additional support to honor _proxy_key in Core selects"
Diffstat (limited to 'test')
| -rw-r--r-- | test/ext/test_hybrid.py | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/test/ext/test_hybrid.py b/test/ext/test_hybrid.py index 3345d872d..08face22b 100644 --- a/test/ext/test_hybrid.py +++ b/test/ext/test_hybrid.py @@ -5,6 +5,7 @@ from sqlalchemy import ForeignKey from sqlalchemy import func from sqlalchemy import inspect from sqlalchemy import Integer +from sqlalchemy import LABEL_STYLE_TABLENAME_PLUS_COL from sqlalchemy import literal_column from sqlalchemy import Numeric from sqlalchemy import select @@ -244,6 +245,93 @@ class PropertyExpressionTest(fixtures.TestBase, AssertsCompiledSQL): return A, B + @testing.fixture + def _unnamed_expr_fixture(self): + Base = declarative_base() + + class A(Base): + __tablename__ = "a" + id = Column(Integer, primary_key=True) + firstname = Column(String) + lastname = Column(String) + + @hybrid.hybrid_property + def name(self): + return self.firstname + " " + self.lastname + + return A + + def test_labeling_for_unnamed(self, _unnamed_expr_fixture): + A = _unnamed_expr_fixture + + stmt = select(A.id, A.name) + self.assert_compile( + stmt, + "SELECT a.id, a.firstname || :firstname_1 || a.lastname AS name " + "FROM a", + ) + + eq_(stmt.subquery().c.keys(), ["id", "name"]) + + self.assert_compile( + select(stmt.subquery()), + "SELECT anon_1.id, anon_1.name " + "FROM (SELECT a.id AS id, a.firstname || :firstname_1 || " + "a.lastname AS name FROM a) AS anon_1", + ) + + def test_labeling_for_unnamed_tablename_plus_col( + self, _unnamed_expr_fixture + ): + A = _unnamed_expr_fixture + + stmt = select(A.id, A.name).set_label_style( + LABEL_STYLE_TABLENAME_PLUS_COL + ) + # looks like legacy query + self.assert_compile( + stmt, + "SELECT a.id AS a_id, a.firstname || :firstname_1 || " + "a.lastname AS anon_1 FROM a", + ) + + # but no ORM translate... + eq_(stmt.subquery().c.keys(), ["a_id", "name"]) + + # then it comes out like this, not really sure if this is useful + self.assert_compile( + select(stmt.subquery()), + "SELECT anon_1.a_id, anon_1.anon_2 FROM (SELECT a.id AS a_id, " + "a.firstname || :firstname_1 || a.lastname AS anon_2 FROM a) " + "AS anon_1", + ) + + def test_labeling_for_unnamed_legacy(self, _unnamed_expr_fixture): + A = _unnamed_expr_fixture + + sess = fixture_session() + + stmt = sess.query(A.id, A.name) + + # TABLENAME_PLUS_COL uses anon label right now, this is a little + # awkward looking, but loading.py translates + self.assert_compile( + stmt, + "SELECT a.id AS a_id, a.firstname || " + ":firstname_1 || a.lastname AS anon_1 FROM a", + ) + + # for the subquery, we lose the "ORM-ness" from the subquery + # so we have to carry it over using _proxy_key + eq_(stmt.subquery().c.keys(), ["id", "name"]) + + self.assert_compile( + sess.query(stmt.subquery()), + "SELECT anon_1.id AS anon_1_id, anon_1.name AS anon_1_name " + "FROM (SELECT a.id AS id, a.firstname || :firstname_1 || " + "a.lastname AS name FROM a) AS anon_1", + ) + def test_info(self): A = self._fixture() inspect(A).all_orm_descriptors.value.info["some key"] = "some value" |
