test_unicode_ddl.py 6.8 KB

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