codekingpro/portable-devtools
114k
1# testing/suite/test_deprecations.py
2# Copyright (C) 2005-2026 the SQLAlchemy authors and contributors
3# <see AUTHORS file>
4#
5# This module is part of SQLAlchemy and is released under
6# the MIT License: https://www.opensource.org/licenses/mit-license.php
7# mypy: ignore-errors
8
9from .. import fixtures
10from ..assertions import eq_
11from ..schema import Column
12from ..schema import Table
13from ... import Integer
14from ... import select
15from ... import testing
16from ... import union
17
18
19class DeprecatedCompoundSelectTest(fixtures.TablesTest):
20 __backend__ = True
21
22 @classmethod
23 def define_tables(cls, metadata):
24 Table(
25 "some_table",
26 metadata,
27 Column("id", Integer, primary_key=True),
28 Column("x", Integer),
29 Column("y", Integer),
30 )
31
32 @classmethod
33 def insert_data(cls, connection):
34 connection.execute(
35 cls.tables.some_table.insert(),
36 [
37 {"id": 1, "x": 1, "y": 2},
38 {"id": 2, "x": 2, "y": 3},
39 {"id": 3, "x": 3, "y": 4},
40 {"id": 4, "x": 4, "y": 5},
41 ],
42 )
43
44 def _assert_result(self, conn, select, result, params=()):
45 eq_(conn.execute(select, params).fetchall(), result)
46
47 def test_plain_union(self, connection):
48 table = self.tables.some_table
49 s1 = select(table).where(table.c.id == 2)
50 s2 = select(table).where(table.c.id == 3)
51
52 u1 = union(s1, s2)
53 with testing.expect_deprecated(
54 "The SelectBase.c and SelectBase.columns "
55 "attributes are deprecated"
56 ):
57 self._assert_result(
58 connection, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)]
59 )
60
61 # note we've had to remove one use case entirely, which is this
62 # one. the Select gets its FROMS from the WHERE clause and the
63 # columns clause, but not the ORDER BY, which means the old ".c" system
64 # allowed you to "order_by(s.c.foo)" to get an unnamed column in the
65 # ORDER BY without adding the SELECT into the FROM and breaking the
66 # query. Users will have to adjust for this use case if they were doing
67 # it before.
68 def _dont_test_select_from_plain_union(self, connection):
69 table = self.tables.some_table
70 s1 = select(table).where(table.c.id == 2)
71 s2 = select(table).where(table.c.id == 3)
72
73 u1 = union(s1, s2).alias().select()
74 with testing.expect_deprecated(
75 "The SelectBase.c and SelectBase.columns "
76 "attributes are deprecated"
77 ):
78 self._assert_result(
79 connection, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)]
80 )
81
82 @testing.requires.order_by_col_from_union
83 @testing.requires.parens_in_union_contained_select_w_limit_offset
84 def test_limit_offset_selectable_in_unions(self, connection):
85 table = self.tables.some_table
86 s1 = select(table).where(table.c.id == 2).limit(1).order_by(table.c.id)
87 s2 = select(table).where(table.c.id == 3).limit(1).order_by(table.c.id)
88
89 u1 = union(s1, s2).limit(2)
90 with testing.expect_deprecated(
91 "The SelectBase.c and SelectBase.columns "
92 "attributes are deprecated"
93 ):
94 self._assert_result(
95 connection, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)]
96 )
97
98 @testing.requires.parens_in_union_contained_select_wo_limit_offset
99 def test_order_by_selectable_in_unions(self, connection):
100 table = self.tables.some_table
101 s1 = select(table).where(table.c.id == 2).order_by(table.c.id)
102 s2 = select(table).where(table.c.id == 3).order_by(table.c.id)
103
104 u1 = union(s1, s2).limit(2)
105 with testing.expect_deprecated(
106 "The SelectBase.c and SelectBase.columns "
107 "attributes are deprecated"
108 ):
109 self._assert_result(
110 connection, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)]
111 )
112
113 def test_distinct_selectable_in_unions(self, connection):
114 table = self.tables.some_table
115 s1 = select(table).where(table.c.id == 2).distinct()
116 s2 = select(table).where(table.c.id == 3).distinct()
117
118 u1 = union(s1, s2).limit(2)
119 with testing.expect_deprecated(
120 "The SelectBase.c and SelectBase.columns "
121 "attributes are deprecated"
122 ):
123 self._assert_result(
124 connection, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)]
125 )
126
127 def test_limit_offset_aliased_selectable_in_unions(self, connection):
128 table = self.tables.some_table
129 s1 = (
130 select(table)
131 .where(table.c.id == 2)
132 .limit(1)
133 .order_by(table.c.id)
134 .alias()
135 .select()
136 )
137 s2 = (
138 select(table)
139 .where(table.c.id == 3)
140 .limit(1)
141 .order_by(table.c.id)
142 .alias()
143 .select()
144 )
145
146 u1 = union(s1, s2).limit(2)
147 with testing.expect_deprecated(
148 "The SelectBase.c and SelectBase.columns "
149 "attributes are deprecated"
150 ):
151 self._assert_result(
152 connection, u1.order_by(u1.c.id), [(2, 2, 3), (3, 3, 4)]
153 )
154 