summaryrefslogtreecommitdiff
path: root/test/sql/test_query.py
diff options
context:
space:
mode:
authorGord Thompson <gord@gordthompson.com>2021-01-09 14:56:38 -0700
committerMike Bayer <mike_mp@zzzcomputing.com>2021-01-26 16:52:30 -0500
commit22f65156bbe846dea2fb9f87fe4187abe0ed790a (patch)
treef49338a10dd2800d4d754b14d2e7fd549b4b833f /test/sql/test_query.py
parent7bdb1f30f66aaea16efbcf96e314491058493e6c (diff)
downloadsqlalchemy-22f65156bbe846dea2fb9f87fe4187abe0ed790a.tar.gz
Replace with_labels() and apply_labels() in ORM/Core
Replace :meth:`_orm.Query.with_labels` and :meth:`_sql.GenerativeSelect.apply_labels` with explicit getters and setters ``get_label_style`` and ``set_label_style`` to accommodate the three supported label styles: ``LABEL_STYLE_DISAMBIGUATE_ONLY`` (default), ``LABEL_STYLE_TABLENAME_PLUS_COL``, and ``LABEL_STYLE_NONE``. In addition, for Core and "future style" ORM queries, ``LABEL_STYLE_DISAMBIGUATE_ONLY`` is now the default label style. This style differs from the existing "no labels" style in that labeling is applied in the case of column name conflicts; with ``LABEL_STYLE_NONE``, a duplicate column name is not accessible via name in any case. For legacy ORM queries using :class:`_query.Query`, the table-plus-column names labeling style applied by ``LABEL_STYLE_TABLENAME_PLUS_COL`` continues to be used so that existing test suites and logging facilities see no change in behavior by default, however this style of labeling is no longer required for SQLAlchemy queries to function, as result sets are commonly matched to columns using a positional approach since SQLAlchemy 1.0. Within test suites, all use of apply_labels() / use_labels now uses the new methods. New tests added to test/sql/test_deprecations.py nad test/orm/test_deprecations.py to cover just the old apply_labels() method call. Tests in ORM that made explicit use apply_labels()/ etc. where it isn't needed for the ORM to work correctly use default label style now. Co-authored-by: Mike Bayer <mike_mp@zzzcomputing.com> Fixes: #4757 Change-Id: I5fdcd2ed4ae8c7fe62f8be2b6d0e8f66409b6a54
Diffstat (limited to 'test/sql/test_query.py')
-rw-r--r--test/sql/test_query.py57
1 files changed, 29 insertions, 28 deletions
diff --git a/test/sql/test_query.py b/test/sql/test_query.py
index 903a6f23e..913b7f4d1 100644
--- a/test/sql/test_query.py
+++ b/test/sql/test_query.py
@@ -26,6 +26,8 @@ from sqlalchemy import union
from sqlalchemy import union_all
from sqlalchemy import VARCHAR
from sqlalchemy.engine import default
+from sqlalchemy.sql import LABEL_STYLE_TABLENAME_PLUS_COL
+from sqlalchemy.sql.selectable import LABEL_STYLE_NONE
from sqlalchemy.testing import assert_raises_message
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
@@ -383,22 +385,28 @@ class QueryTest(fixtures.TablesTest):
eq_(got, wanted)
for labels in False, True:
+ label_style = (
+ LABEL_STYLE_NONE
+ if labels is False
+ else LABEL_STYLE_TABLENAME_PLUS_COL
+ )
def go(stmt):
if labels:
- stmt = stmt.apply_labels()
+ stmt = stmt.set_label_style(label_style)
return stmt
a_eq(
- users.select(order_by=[users.c.user_id], use_labels=labels),
+ users.select(order_by=[users.c.user_id]).set_label_style(
+ label_style
+ ),
[(1, "c"), (2, "b"), (3, "a")],
)
a_eq(
users.select(
order_by=[users.c.user_name, users.c.user_id],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(3, "a"), (2, "b"), (1, "c")],
)
@@ -423,9 +431,8 @@ class QueryTest(fixtures.TablesTest):
a_eq(
users.select(
distinct=True,
- use_labels=labels,
order_by=[users.c.user_id],
- ),
+ ).set_label_style(label_style),
[(1, "c"), (2, "b"), (3, "a")],
)
@@ -452,9 +459,8 @@ class QueryTest(fixtures.TablesTest):
a_eq(
users.select(
distinct=True,
- use_labels=labels,
order_by=[desc(users.c.user_id)],
- ),
+ ).set_label_style(label_style),
[(3, "a"), (2, "b"), (1, "c")],
)
@@ -485,67 +491,64 @@ class QueryTest(fixtures.TablesTest):
eq_(got, wanted)
for labels in False, True:
+ label_style = (
+ LABEL_STYLE_NONE
+ if labels is False
+ else LABEL_STYLE_TABLENAME_PLUS_COL
+ )
a_eq(
users.select(
order_by=[users.c.user_name.nulls_first()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(1, None), (3, "a"), (2, "b")],
)
a_eq(
users.select(
order_by=[users.c.user_name.nulls_last()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(3, "a"), (2, "b"), (1, None)],
)
a_eq(
users.select(
order_by=[asc(users.c.user_name).nulls_first()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(1, None), (3, "a"), (2, "b")],
)
a_eq(
users.select(
order_by=[asc(users.c.user_name).nulls_last()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(3, "a"), (2, "b"), (1, None)],
)
a_eq(
users.select(
order_by=[users.c.user_name.desc().nulls_first()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(1, None), (2, "b"), (3, "a")],
)
a_eq(
users.select(
order_by=[users.c.user_name.desc().nulls_last()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(2, "b"), (3, "a"), (1, None)],
)
a_eq(
users.select(
order_by=[desc(users.c.user_name).nulls_first()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(1, None), (2, "b"), (3, "a")],
)
a_eq(
users.select(
order_by=[desc(users.c.user_name).nulls_last()],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(2, "b"), (3, "a"), (1, None)],
)
@@ -555,16 +558,14 @@ class QueryTest(fixtures.TablesTest):
users.c.user_name.nulls_first(),
users.c.user_id,
],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(1, None), (3, "a"), (2, "b")],
)
a_eq(
users.select(
order_by=[users.c.user_name.nulls_last(), users.c.user_id],
- use_labels=labels,
- ),
+ ).set_label_style(label_style),
[(3, "a"), (2, "b"), (1, None)],
)