test_deprecations.py 5.2 KB

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