test_results.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. # testing/suite/test_results.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. import datetime
  9. import re
  10. from .. import engines
  11. from .. import fixtures
  12. from ..assertions import eq_
  13. from ..config import requirements
  14. from ..schema import Column
  15. from ..schema import Table
  16. from ... import DateTime
  17. from ... import func
  18. from ... import Integer
  19. from ... import select
  20. from ... import sql
  21. from ... import String
  22. from ... import testing
  23. from ... import text
  24. class RowFetchTest(fixtures.TablesTest):
  25. __backend__ = True
  26. @classmethod
  27. def define_tables(cls, metadata):
  28. Table(
  29. "plain_pk",
  30. metadata,
  31. Column("id", Integer, primary_key=True),
  32. Column("data", String(50)),
  33. )
  34. Table(
  35. "has_dates",
  36. metadata,
  37. Column("id", Integer, primary_key=True),
  38. Column("today", DateTime),
  39. )
  40. @classmethod
  41. def insert_data(cls, connection):
  42. connection.execute(
  43. cls.tables.plain_pk.insert(),
  44. [
  45. {"id": 1, "data": "d1"},
  46. {"id": 2, "data": "d2"},
  47. {"id": 3, "data": "d3"},
  48. ],
  49. )
  50. connection.execute(
  51. cls.tables.has_dates.insert(),
  52. [{"id": 1, "today": datetime.datetime(2006, 5, 12, 12, 0, 0)}],
  53. )
  54. def test_via_attr(self, connection):
  55. row = connection.execute(
  56. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  57. ).first()
  58. eq_(row.id, 1)
  59. eq_(row.data, "d1")
  60. def test_via_string(self, connection):
  61. row = connection.execute(
  62. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  63. ).first()
  64. eq_(row._mapping["id"], 1)
  65. eq_(row._mapping["data"], "d1")
  66. def test_via_int(self, connection):
  67. row = connection.execute(
  68. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  69. ).first()
  70. eq_(row[0], 1)
  71. eq_(row[1], "d1")
  72. def test_via_col_object(self, connection):
  73. row = connection.execute(
  74. self.tables.plain_pk.select().order_by(self.tables.plain_pk.c.id)
  75. ).first()
  76. eq_(row._mapping[self.tables.plain_pk.c.id], 1)
  77. eq_(row._mapping[self.tables.plain_pk.c.data], "d1")
  78. @requirements.duplicate_names_in_cursor_description
  79. def test_row_with_dupe_names(self, connection):
  80. result = connection.execute(
  81. select(
  82. self.tables.plain_pk.c.data,
  83. self.tables.plain_pk.c.data.label("data"),
  84. ).order_by(self.tables.plain_pk.c.id)
  85. )
  86. row = result.first()
  87. eq_(result.keys(), ["data", "data"])
  88. eq_(row, ("d1", "d1"))
  89. def test_row_w_scalar_select(self, connection):
  90. """test that a scalar select as a column is returned as such
  91. and that type conversion works OK.
  92. (this is half a SQLAlchemy Core test and half to catch database
  93. backends that may have unusual behavior with scalar selects.)
  94. """
  95. datetable = self.tables.has_dates
  96. s = select(datetable.alias("x").c.today).scalar_subquery()
  97. s2 = select(datetable.c.id, s.label("somelabel"))
  98. row = connection.execute(s2).first()
  99. eq_(row.somelabel, datetime.datetime(2006, 5, 12, 12, 0, 0))
  100. class PercentSchemaNamesTest(fixtures.TablesTest):
  101. """tests using percent signs, spaces in table and column names.
  102. This didn't work for PostgreSQL / MySQL drivers for a long time
  103. but is now supported.
  104. """
  105. __requires__ = ("percent_schema_names",)
  106. __backend__ = True
  107. @classmethod
  108. def define_tables(cls, metadata):
  109. cls.tables.percent_table = Table(
  110. "percent%table",
  111. metadata,
  112. Column("percent%", Integer),
  113. Column("spaces % more spaces", Integer),
  114. )
  115. cls.tables.lightweight_percent_table = sql.table(
  116. "percent%table",
  117. sql.column("percent%"),
  118. sql.column("spaces % more spaces"),
  119. )
  120. def test_single_roundtrip(self, connection):
  121. percent_table = self.tables.percent_table
  122. for params in [
  123. {"percent%": 5, "spaces % more spaces": 12},
  124. {"percent%": 7, "spaces % more spaces": 11},
  125. {"percent%": 9, "spaces % more spaces": 10},
  126. {"percent%": 11, "spaces % more spaces": 9},
  127. ]:
  128. connection.execute(percent_table.insert(), params)
  129. self._assert_table(connection)
  130. def test_executemany_roundtrip(self, connection):
  131. percent_table = self.tables.percent_table
  132. connection.execute(
  133. percent_table.insert(), {"percent%": 5, "spaces % more spaces": 12}
  134. )
  135. connection.execute(
  136. percent_table.insert(),
  137. [
  138. {"percent%": 7, "spaces % more spaces": 11},
  139. {"percent%": 9, "spaces % more spaces": 10},
  140. {"percent%": 11, "spaces % more spaces": 9},
  141. ],
  142. )
  143. self._assert_table(connection)
  144. @requirements.insert_executemany_returning
  145. def test_executemany_returning_roundtrip(self, connection):
  146. percent_table = self.tables.percent_table
  147. connection.execute(
  148. percent_table.insert(), {"percent%": 5, "spaces % more spaces": 12}
  149. )
  150. result = connection.execute(
  151. percent_table.insert().returning(
  152. percent_table.c["percent%"],
  153. percent_table.c["spaces % more spaces"],
  154. ),
  155. [
  156. {"percent%": 7, "spaces % more spaces": 11},
  157. {"percent%": 9, "spaces % more spaces": 10},
  158. {"percent%": 11, "spaces % more spaces": 9},
  159. ],
  160. )
  161. eq_(result.all(), [(7, 11), (9, 10), (11, 9)])
  162. self._assert_table(connection)
  163. def _assert_table(self, conn):
  164. percent_table = self.tables.percent_table
  165. lightweight_percent_table = self.tables.lightweight_percent_table
  166. for table in (
  167. percent_table,
  168. percent_table.alias(),
  169. lightweight_percent_table,
  170. lightweight_percent_table.alias(),
  171. ):
  172. eq_(
  173. list(
  174. conn.execute(table.select().order_by(table.c["percent%"]))
  175. ),
  176. [(5, 12), (7, 11), (9, 10), (11, 9)],
  177. )
  178. eq_(
  179. list(
  180. conn.execute(
  181. table.select()
  182. .where(table.c["spaces % more spaces"].in_([9, 10]))
  183. .order_by(table.c["percent%"])
  184. )
  185. ),
  186. [(9, 10), (11, 9)],
  187. )
  188. row = conn.execute(
  189. table.select().order_by(table.c["percent%"])
  190. ).first()
  191. eq_(row._mapping["percent%"], 5)
  192. eq_(row._mapping["spaces % more spaces"], 12)
  193. eq_(row._mapping[table.c["percent%"]], 5)
  194. eq_(row._mapping[table.c["spaces % more spaces"]], 12)
  195. conn.execute(
  196. percent_table.update().values(
  197. {percent_table.c["spaces % more spaces"]: 15}
  198. )
  199. )
  200. eq_(
  201. list(
  202. conn.execute(
  203. percent_table.select().order_by(
  204. percent_table.c["percent%"]
  205. )
  206. )
  207. ),
  208. [(5, 15), (7, 15), (9, 15), (11, 15)],
  209. )
  210. class ServerSideCursorsTest(
  211. fixtures.TestBase, testing.AssertsExecutionResults
  212. ):
  213. __requires__ = ("server_side_cursors",)
  214. __backend__ = True
  215. def _is_server_side(self, cursor):
  216. # TODO: this is a huge issue as it prevents these tests from being
  217. # usable by third party dialects.
  218. if self.engine.dialect.driver == "psycopg2":
  219. return bool(cursor.name)
  220. elif self.engine.dialect.driver == "pymysql":
  221. sscursor = __import__("pymysql.cursors").cursors.SSCursor
  222. return isinstance(cursor, sscursor)
  223. elif self.engine.dialect.driver in ("aiomysql", "asyncmy", "aioodbc"):
  224. return cursor.server_side
  225. elif self.engine.dialect.driver == "mysqldb":
  226. sscursor = __import__("MySQLdb.cursors").cursors.SSCursor
  227. return isinstance(cursor, sscursor)
  228. elif self.engine.dialect.driver == "mariadbconnector":
  229. return not cursor.buffered
  230. elif self.engine.dialect.driver == "mysqlconnector":
  231. return "buffered" not in type(cursor).__name__.lower()
  232. elif self.engine.dialect.driver in ("asyncpg", "aiosqlite"):
  233. return cursor.server_side
  234. elif self.engine.dialect.driver == "pg8000":
  235. return getattr(cursor, "server_side", False)
  236. elif self.engine.dialect.driver == "psycopg":
  237. return bool(getattr(cursor, "name", False))
  238. elif self.engine.dialect.driver == "oracledb":
  239. return getattr(cursor, "server_side", False)
  240. else:
  241. return False
  242. def _fixture(self, server_side_cursors):
  243. if server_side_cursors:
  244. with testing.expect_deprecated(
  245. "The create_engine.server_side_cursors parameter is "
  246. "deprecated and will be removed in a future release. "
  247. "Please use the Connection.execution_options.stream_results "
  248. "parameter."
  249. ):
  250. self.engine = engines.testing_engine(
  251. options={"server_side_cursors": server_side_cursors}
  252. )
  253. else:
  254. self.engine = engines.testing_engine(
  255. options={"server_side_cursors": server_side_cursors}
  256. )
  257. return self.engine
  258. def stringify(self, str_):
  259. return re.compile(r"SELECT (\d+)", re.I).sub(
  260. lambda m: str(select(int(m.group(1))).compile(testing.db)), str_
  261. )
  262. @testing.combinations(
  263. ("global_string", True, lambda stringify: stringify("select 1"), True),
  264. (
  265. "global_text",
  266. True,
  267. lambda stringify: text(stringify("select 1")),
  268. True,
  269. ),
  270. ("global_expr", True, select(1), True),
  271. (
  272. "global_off_explicit",
  273. False,
  274. lambda stringify: text(stringify("select 1")),
  275. False,
  276. ),
  277. (
  278. "stmt_option",
  279. False,
  280. select(1).execution_options(stream_results=True),
  281. True,
  282. ),
  283. (
  284. "stmt_option_disabled",
  285. True,
  286. select(1).execution_options(stream_results=False),
  287. False,
  288. ),
  289. ("for_update_expr", True, select(1).with_for_update(), True),
  290. # TODO: need a real requirement for this, or dont use this test
  291. (
  292. "for_update_string",
  293. True,
  294. lambda stringify: stringify("SELECT 1 FOR UPDATE"),
  295. True,
  296. testing.skip_if(["sqlite", "mssql"]),
  297. ),
  298. (
  299. "text_no_ss",
  300. False,
  301. lambda stringify: text(stringify("select 42")),
  302. False,
  303. ),
  304. (
  305. "text_ss_option",
  306. False,
  307. lambda stringify: text(stringify("select 42")).execution_options(
  308. stream_results=True
  309. ),
  310. True,
  311. ),
  312. id_="iaaa",
  313. argnames="engine_ss_arg, statement, cursor_ss_status",
  314. )
  315. def test_ss_cursor_status(
  316. self, engine_ss_arg, statement, cursor_ss_status
  317. ):
  318. engine = self._fixture(engine_ss_arg)
  319. with engine.begin() as conn:
  320. if callable(statement):
  321. statement = testing.resolve_lambda(
  322. statement, stringify=self.stringify
  323. )
  324. if isinstance(statement, str):
  325. result = conn.exec_driver_sql(statement)
  326. else:
  327. result = conn.execute(statement)
  328. eq_(self._is_server_side(result.cursor), cursor_ss_status)
  329. result.close()
  330. def test_conn_option(self):
  331. engine = self._fixture(False)
  332. with engine.connect() as conn:
  333. # should be enabled for this one
  334. result = conn.execution_options(
  335. stream_results=True
  336. ).exec_driver_sql(self.stringify("select 1"))
  337. assert self._is_server_side(result.cursor)
  338. # the connection has autobegun, which means at the end of the
  339. # block, we will roll back, which on MySQL at least will fail
  340. # with "Commands out of sync" if the result set
  341. # is not closed, so we close it first.
  342. #
  343. # fun fact! why did we not have this result.close() in this test
  344. # before 2.0? don't we roll back in the connection pool
  345. # unconditionally? yes! and in fact if you run this test in 1.4
  346. # with stdout shown, there is in fact "Exception during reset or
  347. # similar" with "Commands out sync" emitted a warning! 2.0's
  348. # architecture finds and fixes what was previously an expensive
  349. # silent error condition.
  350. result.close()
  351. def test_stmt_enabled_conn_option_disabled(self):
  352. engine = self._fixture(False)
  353. s = select(1).execution_options(stream_results=True)
  354. with engine.connect() as conn:
  355. # not this one
  356. result = conn.execution_options(stream_results=False).execute(s)
  357. assert not self._is_server_side(result.cursor)
  358. def test_aliases_and_ss(self):
  359. engine = self._fixture(False)
  360. s1 = (
  361. select(sql.literal_column("1").label("x"))
  362. .execution_options(stream_results=True)
  363. .subquery()
  364. )
  365. # options don't propagate out when subquery is used as a FROM clause
  366. with engine.begin() as conn:
  367. result = conn.execute(s1.select())
  368. assert not self._is_server_side(result.cursor)
  369. result.close()
  370. s2 = select(1).select_from(s1)
  371. with engine.begin() as conn:
  372. result = conn.execute(s2)
  373. assert not self._is_server_side(result.cursor)
  374. result.close()
  375. def test_roundtrip_fetchall(self, metadata):
  376. md = self.metadata
  377. engine = self._fixture(True)
  378. test_table = Table(
  379. "test_table",
  380. md,
  381. Column(
  382. "id", Integer, primary_key=True, test_needs_autoincrement=True
  383. ),
  384. Column("data", String(50)),
  385. )
  386. with engine.begin() as connection:
  387. test_table.create(connection, checkfirst=True)
  388. connection.execute(test_table.insert(), dict(data="data1"))
  389. connection.execute(test_table.insert(), dict(data="data2"))
  390. eq_(
  391. connection.execute(
  392. test_table.select().order_by(test_table.c.id)
  393. ).fetchall(),
  394. [(1, "data1"), (2, "data2")],
  395. )
  396. connection.execute(
  397. test_table.update()
  398. .where(test_table.c.id == 2)
  399. .values(data=test_table.c.data + " updated")
  400. )
  401. eq_(
  402. connection.execute(
  403. test_table.select().order_by(test_table.c.id)
  404. ).fetchall(),
  405. [(1, "data1"), (2, "data2 updated")],
  406. )
  407. connection.execute(test_table.delete())
  408. eq_(
  409. connection.scalar(
  410. select(func.count("*")).select_from(test_table)
  411. ),
  412. 0,
  413. )
  414. def test_roundtrip_fetchmany(self, metadata):
  415. md = self.metadata
  416. engine = self._fixture(True)
  417. test_table = Table(
  418. "test_table",
  419. md,
  420. Column(
  421. "id", Integer, primary_key=True, test_needs_autoincrement=True
  422. ),
  423. Column("data", String(50)),
  424. )
  425. with engine.begin() as connection:
  426. test_table.create(connection, checkfirst=True)
  427. connection.execute(
  428. test_table.insert(),
  429. [dict(data="data%d" % i) for i in range(1, 20)],
  430. )
  431. result = connection.execute(
  432. test_table.select().order_by(test_table.c.id)
  433. )
  434. eq_(
  435. result.fetchmany(5),
  436. [(i, "data%d" % i) for i in range(1, 6)],
  437. )
  438. eq_(
  439. result.fetchmany(10),
  440. [(i, "data%d" % i) for i in range(6, 16)],
  441. )
  442. eq_(result.fetchall(), [(i, "data%d" % i) for i in range(16, 20)])