test_results.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. import datetime
  2. from .. import engines
  3. from .. import fixtures
  4. from ..assertions import eq_
  5. from ..config import requirements
  6. from ..schema import Column
  7. from ..schema import Table
  8. from ... import DateTime
  9. from ... import func
  10. from ... import Integer
  11. from ... import select
  12. from ... import sql
  13. from ... import String
  14. from ... import testing
  15. from ... import text
  16. from ... import util
  17. class RowFetchTest(fixtures.TablesTest):
  18. __backend__ = True
  19. @classmethod
  20. def define_tables(cls, metadata):
  21. Table(
  22. "plain_pk",
  23. metadata,
  24. Column("id", Integer, primary_key=True),
  25. Column("data", String(50)),
  26. )
  27. Table(
  28. "has_dates",
  29. metadata,
  30. Column("id", Integer, primary_key=True),
  31. Column("today", DateTime),
  32. )
  33. @classmethod
  34. def insert_data(cls, connection):
  35. connection.execute(
  36. cls.tables.plain_pk.insert(),
  37. [
  38. {"id": 1, "data": "d1"},
  39. {"id": 2, "data": "d2"},
  40. {"id": 3, "data": "d3"},
  41. ],
  42. )
  43. connection.execute(
  44. cls.tables.has_dates.insert(),
  45. [{"id": 1, "today": datetime.datetime(2006, 5, 12, 12, 0, 0)}],
  46. )
  47. def test_via_attr(self, connection):
  48. row = connection.execute(
  49. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  50. ).first()
  51. eq_(row.id, 1)
  52. eq_(row.data, "d1")
  53. def test_via_string(self, connection):
  54. row = connection.execute(
  55. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  56. ).first()
  57. eq_(row._mapping["id"], 1)
  58. eq_(row._mapping["data"], "d1")
  59. def test_via_int(self, connection):
  60. row = connection.execute(
  61. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  62. ).first()
  63. eq_(row[0], 1)
  64. eq_(row[1], "d1")
  65. def test_via_col_object(self, connection):
  66. row = connection.execute(
  67. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  68. ).first()
  69. eq_(row._mapping[self.tables.plain_pk.c.id], 1)
  70. eq_(row._mapping[self.tables.plain_pk.c.data], "d1")
  71. @requirements.duplicate_names_in_cursor_description
  72. def test_row_with_dupe_names(self, connection):
  73. result = connection.execute(
  74. select(
  75. self.tables.plain_pk.c.data,
  76. self.tables.plain_pk.c.data.label("data"),
  77. ).order_by(self.tables.plain_pk.c.id)
  78. )
  79. row = result.first()
  80. eq_(result.keys(), ["data", "data"])
  81. eq_(row, ("d1", "d1"))
  82. def test_row_w_scalar_select(self, connection):
  83. """test that a scalar select as a column is returned as such
  84. and that type conversion works OK.
  85. (this is half a SQLAlchemy Core test and half to catch database
  86. backends that may have unusual behavior with scalar selects.)
  87. """
  88. datetable = self.tables.has_dates
  89. s = select(datetable.alias("x").c.today).scalar_subquery()
  90. s2 = select(datetable.c.id, s.label("somelabel"))
  91. row = connection.execute(s2).first()
  92. eq_(row.somelabel, datetime.datetime(2006, 5, 12, 12, 0, 0))
  93. class PercentSchemaNamesTest(fixtures.TablesTest):
  94. """tests using percent signs, spaces in table and column names.
  95. This didn't work for PostgreSQL / MySQL drivers for a long time
  96. but is now supported.
  97. """
  98. __requires__ = ("percent_schema_names",)
  99. __backend__ = True
  100. @classmethod
  101. def define_tables(cls, metadata):
  102. cls.tables.percent_table = Table(
  103. "percent%table",
  104. metadata,
  105. Column("percent%", Integer),
  106. Column("spaces % more spaces", Integer),
  107. )
  108. cls.tables.lightweight_percent_table = sql.table(
  109. "percent%table",
  110. sql.column("percent%"),
  111. sql.column("spaces % more spaces"),
  112. )
  113. def test_single_roundtrip(self, connection):
  114. percent_table = self.tables.percent_table
  115. for params in [
  116. {"percent%": 5, "spaces % more spaces": 12},
  117. {"percent%": 7, "spaces % more spaces": 11},
  118. {"percent%": 9, "spaces % more spaces": 10},
  119. {"percent%": 11, "spaces % more spaces": 9},
  120. ]:
  121. connection.execute(percent_table.insert(), params)
  122. self._assert_table(connection)
  123. def test_executemany_roundtrip(self, connection):
  124. percent_table = self.tables.percent_table
  125. connection.execute(
  126. percent_table.insert(), {"percent%": 5, "spaces % more spaces": 12}
  127. )
  128. connection.execute(
  129. percent_table.insert(),
  130. [
  131. {"percent%": 7, "spaces % more spaces": 11},
  132. {"percent%": 9, "spaces % more spaces": 10},
  133. {"percent%": 11, "spaces % more spaces": 9},
  134. ],
  135. )
  136. self._assert_table(connection)
  137. def _assert_table(self, conn):
  138. percent_table = self.tables.percent_table
  139. lightweight_percent_table = self.tables.lightweight_percent_table
  140. for table in (
  141. percent_table,
  142. percent_table.alias(),
  143. lightweight_percent_table,
  144. lightweight_percent_table.alias(),
  145. ):
  146. eq_(
  147. list(
  148. conn.execute(table.select().order_by(table.c["percent%"]))
  149. ),
  150. [(5, 12), (7, 11), (9, 10), (11, 9)],
  151. )
  152. eq_(
  153. list(
  154. conn.execute(
  155. table.select()
  156. .where(table.c["spaces % more spaces"].in_([9, 10]))
  157. .order_by(table.c["percent%"])
  158. )
  159. ),
  160. [(9, 10), (11, 9)],
  161. )
  162. row = conn.execute(
  163. table.select().order_by(table.c["percent%"])
  164. ).first()
  165. eq_(row._mapping["percent%"], 5)
  166. eq_(row._mapping["spaces % more spaces"], 12)
  167. eq_(row._mapping[table.c["percent%"]], 5)
  168. eq_(row._mapping[table.c["spaces % more spaces"]], 12)
  169. conn.execute(
  170. percent_table.update().values(
  171. {percent_table.c["spaces % more spaces"]: 15}
  172. )
  173. )
  174. eq_(
  175. list(
  176. conn.execute(
  177. percent_table.select().order_by(
  178. percent_table.c["percent%"]
  179. )
  180. )
  181. ),
  182. [(5, 15), (7, 15), (9, 15), (11, 15)],
  183. )
  184. class ServerSideCursorsTest(
  185. fixtures.TestBase, testing.AssertsExecutionResults
  186. ):
  187. __requires__ = ("server_side_cursors",)
  188. __backend__ = True
  189. def _is_server_side(self, cursor):
  190. # TODO: this is a huge issue as it prevents these tests from being
  191. # usable by third party dialects.
  192. if self.engine.dialect.driver == "psycopg2":
  193. return bool(cursor.name)
  194. elif self.engine.dialect.driver == "pymysql":
  195. sscursor = __import__("pymysql.cursors").cursors.SSCursor
  196. return isinstance(cursor, sscursor)
  197. elif self.engine.dialect.driver in ("aiomysql", "asyncmy"):
  198. return cursor.server_side
  199. elif self.engine.dialect.driver == "mysqldb":
  200. sscursor = __import__("MySQLdb.cursors").cursors.SSCursor
  201. return isinstance(cursor, sscursor)
  202. elif self.engine.dialect.driver == "mariadbconnector":
  203. return not cursor.buffered
  204. elif self.engine.dialect.driver in ("asyncpg", "aiosqlite"):
  205. return cursor.server_side
  206. elif self.engine.dialect.driver == "pg8000":
  207. return getattr(cursor, "server_side", False)
  208. else:
  209. return False
  210. def _fixture(self, server_side_cursors):
  211. if server_side_cursors:
  212. with testing.expect_deprecated(
  213. "The create_engine.server_side_cursors parameter is "
  214. "deprecated and will be removed in a future release. "
  215. "Please use the Connection.execution_options.stream_results "
  216. "parameter."
  217. ):
  218. self.engine = engines.testing_engine(
  219. options={"server_side_cursors": server_side_cursors}
  220. )
  221. else:
  222. self.engine = engines.testing_engine(
  223. options={"server_side_cursors": server_side_cursors}
  224. )
  225. return self.engine
  226. @testing.combinations(
  227. ("global_string", True, "select 1", True),
  228. ("global_text", True, text("select 1"), True),
  229. ("global_expr", True, select(1), True),
  230. ("global_off_explicit", False, text("select 1"), False),
  231. (
  232. "stmt_option",
  233. False,
  234. select(1).execution_options(stream_results=True),
  235. True,
  236. ),
  237. (
  238. "stmt_option_disabled",
  239. True,
  240. select(1).execution_options(stream_results=False),
  241. False,
  242. ),
  243. ("for_update_expr", True, select(1).with_for_update(), True),
  244. # TODO: need a real requirement for this, or dont use this test
  245. (
  246. "for_update_string",
  247. True,
  248. "SELECT 1 FOR UPDATE",
  249. True,
  250. testing.skip_if("sqlite"),
  251. ),
  252. ("text_no_ss", False, text("select 42"), False),
  253. (
  254. "text_ss_option",
  255. False,
  256. text("select 42").execution_options(stream_results=True),
  257. True,
  258. ),
  259. id_="iaaa",
  260. argnames="engine_ss_arg, statement, cursor_ss_status",
  261. )
  262. def test_ss_cursor_status(
  263. self, engine_ss_arg, statement, cursor_ss_status
  264. ):
  265. engine = self._fixture(engine_ss_arg)
  266. with engine.begin() as conn:
  267. if isinstance(statement, util.string_types):
  268. result = conn.exec_driver_sql(statement)
  269. else:
  270. result = conn.execute(statement)
  271. eq_(self._is_server_side(result.cursor), cursor_ss_status)
  272. result.close()
  273. def test_conn_option(self):
  274. engine = self._fixture(False)
  275. with engine.connect() as conn:
  276. # should be enabled for this one
  277. result = conn.execution_options(
  278. stream_results=True
  279. ).exec_driver_sql("select 1")
  280. assert self._is_server_side(result.cursor)
  281. def test_stmt_enabled_conn_option_disabled(self):
  282. engine = self._fixture(False)
  283. s = select(1).execution_options(stream_results=True)
  284. with engine.connect() as conn:
  285. # not this one
  286. result = conn.execution_options(stream_results=False).execute(s)
  287. assert not self._is_server_side(result.cursor)
  288. def test_aliases_and_ss(self):
  289. engine = self._fixture(False)
  290. s1 = (
  291. select(sql.literal_column("1").label("x"))
  292. .execution_options(stream_results=True)
  293. .subquery()
  294. )
  295. # options don't propagate out when subquery is used as a FROM clause
  296. with engine.begin() as conn:
  297. result = conn.execute(s1.select())
  298. assert not self._is_server_side(result.cursor)
  299. result.close()
  300. s2 = select(1).select_from(s1)
  301. with engine.begin() as conn:
  302. result = conn.execute(s2)
  303. assert not self._is_server_side(result.cursor)
  304. result.close()
  305. def test_roundtrip_fetchall(self, metadata):
  306. md = self.metadata
  307. engine = self._fixture(True)
  308. test_table = Table(
  309. "test_table",
  310. md,
  311. Column("id", Integer, primary_key=True),
  312. Column("data", String(50)),
  313. )
  314. with engine.begin() as connection:
  315. test_table.create(connection, checkfirst=True)
  316. connection.execute(test_table.insert(), dict(data="data1"))
  317. connection.execute(test_table.insert(), dict(data="data2"))
  318. eq_(
  319. connection.execute(
  320. test_table.select().order_by(test_table.c.id)
  321. ).fetchall(),
  322. [(1, "data1"), (2, "data2")],
  323. )
  324. connection.execute(
  325. test_table.update()
  326. .where(test_table.c.id == 2)
  327. .values(data=test_table.c.data + " updated")
  328. )
  329. eq_(
  330. connection.execute(
  331. test_table.select().order_by(test_table.c.id)
  332. ).fetchall(),
  333. [(1, "data1"), (2, "data2 updated")],
  334. )
  335. connection.execute(test_table.delete())
  336. eq_(
  337. connection.scalar(
  338. select(func.count("*")).select_from(test_table)
  339. ),
  340. 0,
  341. )
  342. def test_roundtrip_fetchmany(self, metadata):
  343. md = self.metadata
  344. engine = self._fixture(True)
  345. test_table = Table(
  346. "test_table",
  347. md,
  348. Column("id", Integer, primary_key=True),
  349. Column("data", String(50)),
  350. )
  351. with engine.begin() as connection:
  352. test_table.create(connection, checkfirst=True)
  353. connection.execute(
  354. test_table.insert(),
  355. [dict(data="data%d" % i) for i in range(1, 20)],
  356. )
  357. result = connection.execute(
  358. test_table.select().order_by(test_table.c.id)
  359. )
  360. eq_(
  361. result.fetchmany(5),
  362. [(i, "data%d" % i) for i in range(1, 6)],
  363. )
  364. eq_(
  365. result.fetchmany(10),
  366. [(i, "data%d" % i) for i in range(6, 16)],
  367. )
  368. eq_(result.fetchall(), [(i, "data%d" % i) for i in range(16, 20)])