test_unicode_ddl.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # testing/suite/test_unicode_ddl.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 sqlalchemy import desc
  9. from sqlalchemy import ForeignKey
  10. from sqlalchemy import Integer
  11. from sqlalchemy import MetaData
  12. from sqlalchemy import testing
  13. from sqlalchemy.testing import eq_
  14. from sqlalchemy.testing import fixtures
  15. from sqlalchemy.testing.schema import Column
  16. from sqlalchemy.testing.schema import Table
  17. class UnicodeSchemaTest(fixtures.TablesTest):
  18. __requires__ = ("unicode_ddl",)
  19. __backend__ = True
  20. @classmethod
  21. def define_tables(cls, metadata):
  22. global t1, t2, t3
  23. t1 = Table(
  24. "unitable1",
  25. metadata,
  26. Column("méil", Integer, primary_key=True),
  27. Column("\u6e2c\u8a66", Integer),
  28. test_needs_fk=True,
  29. )
  30. t2 = Table(
  31. "Unitéble2",
  32. metadata,
  33. Column("méil", Integer, primary_key=True, key="a"),
  34. Column(
  35. "\u6e2c\u8a66",
  36. Integer,
  37. ForeignKey("unitable1.méil"),
  38. key="b",
  39. ),
  40. test_needs_fk=True,
  41. )
  42. # Few DBs support Unicode foreign keys
  43. if testing.against("sqlite"):
  44. t3 = Table(
  45. "\u6e2c\u8a66",
  46. metadata,
  47. Column(
  48. "\u6e2c\u8a66_id",
  49. Integer,
  50. primary_key=True,
  51. autoincrement=False,
  52. ),
  53. Column(
  54. "unitable1_\u6e2c\u8a66",
  55. Integer,
  56. ForeignKey("unitable1.\u6e2c\u8a66"),
  57. ),
  58. Column("Unitéble2_b", Integer, ForeignKey("Unitéble2.b")),
  59. Column(
  60. "\u6e2c\u8a66_self",
  61. Integer,
  62. ForeignKey("\u6e2c\u8a66.\u6e2c\u8a66_id"),
  63. ),
  64. test_needs_fk=True,
  65. )
  66. else:
  67. t3 = Table(
  68. "\u6e2c\u8a66",
  69. metadata,
  70. Column(
  71. "\u6e2c\u8a66_id",
  72. Integer,
  73. primary_key=True,
  74. autoincrement=False,
  75. ),
  76. Column("unitable1_\u6e2c\u8a66", Integer),
  77. Column("Unitéble2_b", Integer),
  78. Column("\u6e2c\u8a66_self", Integer),
  79. test_needs_fk=True,
  80. )
  81. def test_insert(self, connection):
  82. connection.execute(t1.insert(), {"méil": 1, "\u6e2c\u8a66": 5})
  83. connection.execute(t2.insert(), {"a": 1, "b": 1})
  84. connection.execute(
  85. t3.insert(),
  86. {
  87. "\u6e2c\u8a66_id": 1,
  88. "unitable1_\u6e2c\u8a66": 5,
  89. "Unitéble2_b": 1,
  90. "\u6e2c\u8a66_self": 1,
  91. },
  92. )
  93. eq_(connection.execute(t1.select()).fetchall(), [(1, 5)])
  94. eq_(connection.execute(t2.select()).fetchall(), [(1, 1)])
  95. eq_(connection.execute(t3.select()).fetchall(), [(1, 5, 1, 1)])
  96. def test_col_targeting(self, connection):
  97. connection.execute(t1.insert(), {"méil": 1, "\u6e2c\u8a66": 5})
  98. connection.execute(t2.insert(), {"a": 1, "b": 1})
  99. connection.execute(
  100. t3.insert(),
  101. {
  102. "\u6e2c\u8a66_id": 1,
  103. "unitable1_\u6e2c\u8a66": 5,
  104. "Unitéble2_b": 1,
  105. "\u6e2c\u8a66_self": 1,
  106. },
  107. )
  108. row = connection.execute(t1.select()).first()
  109. eq_(row._mapping[t1.c["méil"]], 1)
  110. eq_(row._mapping[t1.c["\u6e2c\u8a66"]], 5)
  111. row = connection.execute(t2.select()).first()
  112. eq_(row._mapping[t2.c["a"]], 1)
  113. eq_(row._mapping[t2.c["b"]], 1)
  114. row = connection.execute(t3.select()).first()
  115. eq_(row._mapping[t3.c["\u6e2c\u8a66_id"]], 1)
  116. eq_(row._mapping[t3.c["unitable1_\u6e2c\u8a66"]], 5)
  117. eq_(row._mapping[t3.c["Unitéble2_b"]], 1)
  118. eq_(row._mapping[t3.c["\u6e2c\u8a66_self"]], 1)
  119. def test_reflect(self, connection):
  120. connection.execute(t1.insert(), {"méil": 2, "\u6e2c\u8a66": 7})
  121. connection.execute(t2.insert(), {"a": 2, "b": 2})
  122. connection.execute(
  123. t3.insert(),
  124. {
  125. "\u6e2c\u8a66_id": 2,
  126. "unitable1_\u6e2c\u8a66": 7,
  127. "Unitéble2_b": 2,
  128. "\u6e2c\u8a66_self": 2,
  129. },
  130. )
  131. meta = MetaData()
  132. tt1 = Table(t1.name, meta, autoload_with=connection)
  133. tt2 = Table(t2.name, meta, autoload_with=connection)
  134. tt3 = Table(t3.name, meta, autoload_with=connection)
  135. connection.execute(tt1.insert(), {"méil": 1, "\u6e2c\u8a66": 5})
  136. connection.execute(tt2.insert(), {"méil": 1, "\u6e2c\u8a66": 1})
  137. connection.execute(
  138. tt3.insert(),
  139. {
  140. "\u6e2c\u8a66_id": 1,
  141. "unitable1_\u6e2c\u8a66": 5,
  142. "Unitéble2_b": 1,
  143. "\u6e2c\u8a66_self": 1,
  144. },
  145. )
  146. eq_(
  147. connection.execute(tt1.select().order_by(desc("méil"))).fetchall(),
  148. [(2, 7), (1, 5)],
  149. )
  150. eq_(
  151. connection.execute(tt2.select().order_by(desc("méil"))).fetchall(),
  152. [(2, 2), (1, 1)],
  153. )
  154. eq_(
  155. connection.execute(
  156. tt3.select().order_by(desc("\u6e2c\u8a66_id"))
  157. ).fetchall(),
  158. [(2, 7, 2, 2), (1, 5, 1, 1)],
  159. )
  160. def test_repr(self):
  161. meta = MetaData()
  162. t = Table("\u6e2c\u8a66", meta, Column("\u6e2c\u8a66_id", Integer))
  163. eq_(
  164. repr(t),
  165. (
  166. "Table('測試', MetaData(), "
  167. "Column('測試_id', Integer(), "
  168. "table=<測試>), "
  169. "schema=None)"
  170. ),
  171. )