test_deprecations.py 5.2 KB

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