/lib/sqlalchemy/testing/suite/test_results.py

https://github.com/nosmokingbandit/Watcher3 · Python · 220 lines · 191 code · 18 blank · 11 comment · 2 complexity · 4b79a9a7d683bfba90a4608a107822fc MD5 · raw file

  1. from .. import fixtures, config
  2. from ..config import requirements
  3. from .. import exclusions
  4. from ..assertions import eq_
  5. from .. import engines
  6. from sqlalchemy import Integer, String, select, util, sql, DateTime
  7. import datetime
  8. from ..schema import Table, Column
  9. class RowFetchTest(fixtures.TablesTest):
  10. __backend__ = True
  11. @classmethod
  12. def define_tables(cls, metadata):
  13. Table('plain_pk', metadata,
  14. Column('id', Integer, primary_key=True),
  15. Column('data', String(50))
  16. )
  17. Table('has_dates', metadata,
  18. Column('id', Integer, primary_key=True),
  19. Column('today', DateTime)
  20. )
  21. @classmethod
  22. def insert_data(cls):
  23. config.db.execute(
  24. cls.tables.plain_pk.insert(),
  25. [
  26. {"id": 1, "data": "d1"},
  27. {"id": 2, "data": "d2"},
  28. {"id": 3, "data": "d3"},
  29. ]
  30. )
  31. config.db.execute(
  32. cls.tables.has_dates.insert(),
  33. [
  34. {"id": 1, "today": datetime.datetime(2006, 5, 12, 12, 0, 0)}
  35. ]
  36. )
  37. def test_via_string(self):
  38. row = config.db.execute(
  39. self.tables.plain_pk.select().
  40. order_by(self.tables.plain_pk.c.id)
  41. ).first()
  42. eq_(
  43. row['id'], 1
  44. )
  45. eq_(
  46. row['data'], "d1"
  47. )
  48. def test_via_int(self):
  49. row = config.db.execute(
  50. self.tables.plain_pk.select().
  51. order_by(self.tables.plain_pk.c.id)
  52. ).first()
  53. eq_(
  54. row[0], 1
  55. )
  56. eq_(
  57. row[1], "d1"
  58. )
  59. def test_via_col_object(self):
  60. row = config.db.execute(
  61. self.tables.plain_pk.select().
  62. order_by(self.tables.plain_pk.c.id)
  63. ).first()
  64. eq_(
  65. row[self.tables.plain_pk.c.id], 1
  66. )
  67. eq_(
  68. row[self.tables.plain_pk.c.data], "d1"
  69. )
  70. @requirements.duplicate_names_in_cursor_description
  71. def test_row_with_dupe_names(self):
  72. result = config.db.execute(
  73. select([self.tables.plain_pk.c.data,
  74. self.tables.plain_pk.c.data.label('data')]).
  75. order_by(self.tables.plain_pk.c.id)
  76. )
  77. row = result.first()
  78. eq_(result.keys(), ['data', 'data'])
  79. eq_(row, ('d1', 'd1'))
  80. def test_row_w_scalar_select(self):
  81. """test that a scalar select as a column is returned as such
  82. and that type conversion works OK.
  83. (this is half a SQLAlchemy Core test and half to catch database
  84. backends that may have unusual behavior with scalar selects.)
  85. """
  86. datetable = self.tables.has_dates
  87. s = select([datetable.alias('x').c.today]).as_scalar()
  88. s2 = select([datetable.c.id, s.label('somelabel')])
  89. row = config.db.execute(s2).first()
  90. eq_(row['somelabel'], datetime.datetime(2006, 5, 12, 12, 0, 0))
  91. class PercentSchemaNamesTest(fixtures.TablesTest):
  92. """tests using percent signs, spaces in table and column names.
  93. This is a very fringe use case, doesn't work for MySQL
  94. or PostgreSQL. the requirement, "percent_schema_names",
  95. is marked "skip" by default.
  96. """
  97. __requires__ = ('percent_schema_names', )
  98. __backend__ = True
  99. @classmethod
  100. def define_tables(cls, metadata):
  101. cls.tables.percent_table = Table('percent%table', metadata,
  102. Column("percent%", Integer),
  103. Column(
  104. "spaces % more spaces", Integer),
  105. )
  106. cls.tables.lightweight_percent_table = sql.table(
  107. 'percent%table', sql.column("percent%"),
  108. sql.column("spaces % more spaces")
  109. )
  110. def test_single_roundtrip(self):
  111. percent_table = self.tables.percent_table
  112. for params in [
  113. {'percent%': 5, 'spaces % more spaces': 12},
  114. {'percent%': 7, 'spaces % more spaces': 11},
  115. {'percent%': 9, 'spaces % more spaces': 10},
  116. {'percent%': 11, 'spaces % more spaces': 9}
  117. ]:
  118. config.db.execute(percent_table.insert(), params)
  119. self._assert_table()
  120. def test_executemany_roundtrip(self):
  121. percent_table = self.tables.percent_table
  122. config.db.execute(
  123. percent_table.insert(),
  124. {'percent%': 5, 'spaces % more spaces': 12}
  125. )
  126. config.db.execute(
  127. percent_table.insert(),
  128. [{'percent%': 7, 'spaces % more spaces': 11},
  129. {'percent%': 9, 'spaces % more spaces': 10},
  130. {'percent%': 11, 'spaces % more spaces': 9}]
  131. )
  132. self._assert_table()
  133. def _assert_table(self):
  134. percent_table = self.tables.percent_table
  135. lightweight_percent_table = self.tables.lightweight_percent_table
  136. for table in (
  137. percent_table,
  138. percent_table.alias(),
  139. lightweight_percent_table,
  140. lightweight_percent_table.alias()):
  141. eq_(
  142. list(
  143. config.db.execute(
  144. table.select().order_by(table.c['percent%'])
  145. )
  146. ),
  147. [
  148. (5, 12),
  149. (7, 11),
  150. (9, 10),
  151. (11, 9)
  152. ]
  153. )
  154. eq_(
  155. list(
  156. config.db.execute(
  157. table.select().
  158. where(table.c['spaces % more spaces'].in_([9, 10])).
  159. order_by(table.c['percent%']),
  160. )
  161. ),
  162. [
  163. (9, 10),
  164. (11, 9)
  165. ]
  166. )
  167. row = config.db.execute(table.select().
  168. order_by(table.c['percent%'])).first()
  169. eq_(row['percent%'], 5)
  170. eq_(row['spaces % more spaces'], 12)
  171. eq_(row[table.c['percent%']], 5)
  172. eq_(row[table.c['spaces % more spaces']], 12)
  173. config.db.execute(
  174. percent_table.update().values(
  175. {percent_table.c['spaces % more spaces']: 15}
  176. )
  177. )
  178. eq_(
  179. list(
  180. config.db.execute(
  181. percent_table.
  182. select().
  183. order_by(percent_table.c['percent%'])
  184. )
  185. ),
  186. [(5, 15), (7, 15), (9, 15), (11, 15)]
  187. )