test_deprecations.py 5.1 KB

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