SQLAlchemy 2.0.47__cp313-cp313t-win32.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (274) hide show
  1. sqlalchemy/__init__.py +283 -0
  2. sqlalchemy/connectors/__init__.py +18 -0
  3. sqlalchemy/connectors/aioodbc.py +184 -0
  4. sqlalchemy/connectors/asyncio.py +429 -0
  5. sqlalchemy/connectors/pyodbc.py +250 -0
  6. sqlalchemy/cyextension/__init__.py +6 -0
  7. sqlalchemy/cyextension/collections.cp313t-win32.pyd +0 -0
  8. sqlalchemy/cyextension/collections.pyx +409 -0
  9. sqlalchemy/cyextension/immutabledict.cp313t-win32.pyd +0 -0
  10. sqlalchemy/cyextension/immutabledict.pxd +8 -0
  11. sqlalchemy/cyextension/immutabledict.pyx +133 -0
  12. sqlalchemy/cyextension/processors.cp313t-win32.pyd +0 -0
  13. sqlalchemy/cyextension/processors.pyx +68 -0
  14. sqlalchemy/cyextension/resultproxy.cp313t-win32.pyd +0 -0
  15. sqlalchemy/cyextension/resultproxy.pyx +102 -0
  16. sqlalchemy/cyextension/util.cp313t-win32.pyd +0 -0
  17. sqlalchemy/cyextension/util.pyx +90 -0
  18. sqlalchemy/dialects/__init__.py +62 -0
  19. sqlalchemy/dialects/_typing.py +30 -0
  20. sqlalchemy/dialects/mssql/__init__.py +88 -0
  21. sqlalchemy/dialects/mssql/aioodbc.py +63 -0
  22. sqlalchemy/dialects/mssql/base.py +4093 -0
  23. sqlalchemy/dialects/mssql/information_schema.py +285 -0
  24. sqlalchemy/dialects/mssql/json.py +129 -0
  25. sqlalchemy/dialects/mssql/provision.py +185 -0
  26. sqlalchemy/dialects/mssql/pymssql.py +126 -0
  27. sqlalchemy/dialects/mssql/pyodbc.py +760 -0
  28. sqlalchemy/dialects/mysql/__init__.py +104 -0
  29. sqlalchemy/dialects/mysql/aiomysql.py +250 -0
  30. sqlalchemy/dialects/mysql/asyncmy.py +231 -0
  31. sqlalchemy/dialects/mysql/base.py +3949 -0
  32. sqlalchemy/dialects/mysql/cymysql.py +106 -0
  33. sqlalchemy/dialects/mysql/dml.py +225 -0
  34. sqlalchemy/dialects/mysql/enumerated.py +282 -0
  35. sqlalchemy/dialects/mysql/expression.py +146 -0
  36. sqlalchemy/dialects/mysql/json.py +91 -0
  37. sqlalchemy/dialects/mysql/mariadb.py +72 -0
  38. sqlalchemy/dialects/mysql/mariadbconnector.py +322 -0
  39. sqlalchemy/dialects/mysql/mysqlconnector.py +302 -0
  40. sqlalchemy/dialects/mysql/mysqldb.py +314 -0
  41. sqlalchemy/dialects/mysql/provision.py +153 -0
  42. sqlalchemy/dialects/mysql/pymysql.py +158 -0
  43. sqlalchemy/dialects/mysql/pyodbc.py +157 -0
  44. sqlalchemy/dialects/mysql/reflection.py +727 -0
  45. sqlalchemy/dialects/mysql/reserved_words.py +570 -0
  46. sqlalchemy/dialects/mysql/types.py +835 -0
  47. sqlalchemy/dialects/oracle/__init__.py +81 -0
  48. sqlalchemy/dialects/oracle/base.py +3802 -0
  49. sqlalchemy/dialects/oracle/cx_oracle.py +1555 -0
  50. sqlalchemy/dialects/oracle/dictionary.py +507 -0
  51. sqlalchemy/dialects/oracle/oracledb.py +941 -0
  52. sqlalchemy/dialects/oracle/provision.py +297 -0
  53. sqlalchemy/dialects/oracle/types.py +316 -0
  54. sqlalchemy/dialects/oracle/vector.py +365 -0
  55. sqlalchemy/dialects/postgresql/__init__.py +167 -0
  56. sqlalchemy/dialects/postgresql/_psycopg_common.py +189 -0
  57. sqlalchemy/dialects/postgresql/array.py +519 -0
  58. sqlalchemy/dialects/postgresql/asyncpg.py +1284 -0
  59. sqlalchemy/dialects/postgresql/base.py +5378 -0
  60. sqlalchemy/dialects/postgresql/dml.py +339 -0
  61. sqlalchemy/dialects/postgresql/ext.py +540 -0
  62. sqlalchemy/dialects/postgresql/hstore.py +406 -0
  63. sqlalchemy/dialects/postgresql/json.py +404 -0
  64. sqlalchemy/dialects/postgresql/named_types.py +524 -0
  65. sqlalchemy/dialects/postgresql/operators.py +129 -0
  66. sqlalchemy/dialects/postgresql/pg8000.py +669 -0
  67. sqlalchemy/dialects/postgresql/pg_catalog.py +326 -0
  68. sqlalchemy/dialects/postgresql/provision.py +183 -0
  69. sqlalchemy/dialects/postgresql/psycopg.py +862 -0
  70. sqlalchemy/dialects/postgresql/psycopg2.py +892 -0
  71. sqlalchemy/dialects/postgresql/psycopg2cffi.py +61 -0
  72. sqlalchemy/dialects/postgresql/ranges.py +1031 -0
  73. sqlalchemy/dialects/postgresql/types.py +313 -0
  74. sqlalchemy/dialects/sqlite/__init__.py +57 -0
  75. sqlalchemy/dialects/sqlite/aiosqlite.py +482 -0
  76. sqlalchemy/dialects/sqlite/base.py +3056 -0
  77. sqlalchemy/dialects/sqlite/dml.py +263 -0
  78. sqlalchemy/dialects/sqlite/json.py +92 -0
  79. sqlalchemy/dialects/sqlite/provision.py +229 -0
  80. sqlalchemy/dialects/sqlite/pysqlcipher.py +157 -0
  81. sqlalchemy/dialects/sqlite/pysqlite.py +756 -0
  82. sqlalchemy/dialects/type_migration_guidelines.txt +145 -0
  83. sqlalchemy/engine/__init__.py +62 -0
  84. sqlalchemy/engine/_py_processors.py +136 -0
  85. sqlalchemy/engine/_py_row.py +128 -0
  86. sqlalchemy/engine/_py_util.py +74 -0
  87. sqlalchemy/engine/base.py +3390 -0
  88. sqlalchemy/engine/characteristics.py +155 -0
  89. sqlalchemy/engine/create.py +893 -0
  90. sqlalchemy/engine/cursor.py +2298 -0
  91. sqlalchemy/engine/default.py +2394 -0
  92. sqlalchemy/engine/events.py +965 -0
  93. sqlalchemy/engine/interfaces.py +3471 -0
  94. sqlalchemy/engine/mock.py +134 -0
  95. sqlalchemy/engine/processors.py +61 -0
  96. sqlalchemy/engine/reflection.py +2102 -0
  97. sqlalchemy/engine/result.py +2399 -0
  98. sqlalchemy/engine/row.py +400 -0
  99. sqlalchemy/engine/strategies.py +16 -0
  100. sqlalchemy/engine/url.py +924 -0
  101. sqlalchemy/engine/util.py +167 -0
  102. sqlalchemy/event/__init__.py +26 -0
  103. sqlalchemy/event/api.py +220 -0
  104. sqlalchemy/event/attr.py +676 -0
  105. sqlalchemy/event/base.py +472 -0
  106. sqlalchemy/event/legacy.py +258 -0
  107. sqlalchemy/event/registry.py +390 -0
  108. sqlalchemy/events.py +17 -0
  109. sqlalchemy/exc.py +832 -0
  110. sqlalchemy/ext/__init__.py +11 -0
  111. sqlalchemy/ext/associationproxy.py +2027 -0
  112. sqlalchemy/ext/asyncio/__init__.py +25 -0
  113. sqlalchemy/ext/asyncio/base.py +281 -0
  114. sqlalchemy/ext/asyncio/engine.py +1471 -0
  115. sqlalchemy/ext/asyncio/exc.py +21 -0
  116. sqlalchemy/ext/asyncio/result.py +965 -0
  117. sqlalchemy/ext/asyncio/scoping.py +1599 -0
  118. sqlalchemy/ext/asyncio/session.py +1947 -0
  119. sqlalchemy/ext/automap.py +1701 -0
  120. sqlalchemy/ext/baked.py +570 -0
  121. sqlalchemy/ext/compiler.py +600 -0
  122. sqlalchemy/ext/declarative/__init__.py +65 -0
  123. sqlalchemy/ext/declarative/extensions.py +564 -0
  124. sqlalchemy/ext/horizontal_shard.py +478 -0
  125. sqlalchemy/ext/hybrid.py +1535 -0
  126. sqlalchemy/ext/indexable.py +364 -0
  127. sqlalchemy/ext/instrumentation.py +450 -0
  128. sqlalchemy/ext/mutable.py +1085 -0
  129. sqlalchemy/ext/mypy/__init__.py +6 -0
  130. sqlalchemy/ext/mypy/apply.py +324 -0
  131. sqlalchemy/ext/mypy/decl_class.py +515 -0
  132. sqlalchemy/ext/mypy/infer.py +590 -0
  133. sqlalchemy/ext/mypy/names.py +335 -0
  134. sqlalchemy/ext/mypy/plugin.py +303 -0
  135. sqlalchemy/ext/mypy/util.py +357 -0
  136. sqlalchemy/ext/orderinglist.py +439 -0
  137. sqlalchemy/ext/serializer.py +185 -0
  138. sqlalchemy/future/__init__.py +16 -0
  139. sqlalchemy/future/engine.py +15 -0
  140. sqlalchemy/inspection.py +174 -0
  141. sqlalchemy/log.py +288 -0
  142. sqlalchemy/orm/__init__.py +171 -0
  143. sqlalchemy/orm/_orm_constructors.py +2661 -0
  144. sqlalchemy/orm/_typing.py +179 -0
  145. sqlalchemy/orm/attributes.py +2845 -0
  146. sqlalchemy/orm/base.py +971 -0
  147. sqlalchemy/orm/bulk_persistence.py +2135 -0
  148. sqlalchemy/orm/clsregistry.py +571 -0
  149. sqlalchemy/orm/collections.py +1627 -0
  150. sqlalchemy/orm/context.py +3334 -0
  151. sqlalchemy/orm/decl_api.py +2004 -0
  152. sqlalchemy/orm/decl_base.py +2192 -0
  153. sqlalchemy/orm/dependency.py +1302 -0
  154. sqlalchemy/orm/descriptor_props.py +1092 -0
  155. sqlalchemy/orm/dynamic.py +300 -0
  156. sqlalchemy/orm/evaluator.py +379 -0
  157. sqlalchemy/orm/events.py +3252 -0
  158. sqlalchemy/orm/exc.py +237 -0
  159. sqlalchemy/orm/identity.py +302 -0
  160. sqlalchemy/orm/instrumentation.py +754 -0
  161. sqlalchemy/orm/interfaces.py +1496 -0
  162. sqlalchemy/orm/loading.py +1686 -0
  163. sqlalchemy/orm/mapped_collection.py +557 -0
  164. sqlalchemy/orm/mapper.py +4444 -0
  165. sqlalchemy/orm/path_registry.py +809 -0
  166. sqlalchemy/orm/persistence.py +1788 -0
  167. sqlalchemy/orm/properties.py +935 -0
  168. sqlalchemy/orm/query.py +3459 -0
  169. sqlalchemy/orm/relationships.py +3508 -0
  170. sqlalchemy/orm/scoping.py +2148 -0
  171. sqlalchemy/orm/session.py +5280 -0
  172. sqlalchemy/orm/state.py +1168 -0
  173. sqlalchemy/orm/state_changes.py +196 -0
  174. sqlalchemy/orm/strategies.py +3470 -0
  175. sqlalchemy/orm/strategy_options.py +2568 -0
  176. sqlalchemy/orm/sync.py +164 -0
  177. sqlalchemy/orm/unitofwork.py +796 -0
  178. sqlalchemy/orm/util.py +2403 -0
  179. sqlalchemy/orm/writeonly.py +674 -0
  180. sqlalchemy/pool/__init__.py +44 -0
  181. sqlalchemy/pool/base.py +1524 -0
  182. sqlalchemy/pool/events.py +375 -0
  183. sqlalchemy/pool/impl.py +588 -0
  184. sqlalchemy/py.typed +0 -0
  185. sqlalchemy/schema.py +69 -0
  186. sqlalchemy/sql/__init__.py +145 -0
  187. sqlalchemy/sql/_dml_constructors.py +132 -0
  188. sqlalchemy/sql/_elements_constructors.py +1872 -0
  189. sqlalchemy/sql/_orm_types.py +20 -0
  190. sqlalchemy/sql/_py_util.py +75 -0
  191. sqlalchemy/sql/_selectable_constructors.py +763 -0
  192. sqlalchemy/sql/_typing.py +482 -0
  193. sqlalchemy/sql/annotation.py +587 -0
  194. sqlalchemy/sql/base.py +2293 -0
  195. sqlalchemy/sql/cache_key.py +1057 -0
  196. sqlalchemy/sql/coercions.py +1404 -0
  197. sqlalchemy/sql/compiler.py +8081 -0
  198. sqlalchemy/sql/crud.py +1752 -0
  199. sqlalchemy/sql/ddl.py +1444 -0
  200. sqlalchemy/sql/default_comparator.py +551 -0
  201. sqlalchemy/sql/dml.py +1850 -0
  202. sqlalchemy/sql/elements.py +5589 -0
  203. sqlalchemy/sql/events.py +458 -0
  204. sqlalchemy/sql/expression.py +159 -0
  205. sqlalchemy/sql/functions.py +2158 -0
  206. sqlalchemy/sql/lambdas.py +1442 -0
  207. sqlalchemy/sql/naming.py +209 -0
  208. sqlalchemy/sql/operators.py +2623 -0
  209. sqlalchemy/sql/roles.py +323 -0
  210. sqlalchemy/sql/schema.py +6222 -0
  211. sqlalchemy/sql/selectable.py +7265 -0
  212. sqlalchemy/sql/sqltypes.py +3930 -0
  213. sqlalchemy/sql/traversals.py +1024 -0
  214. sqlalchemy/sql/type_api.py +2368 -0
  215. sqlalchemy/sql/util.py +1485 -0
  216. sqlalchemy/sql/visitors.py +1164 -0
  217. sqlalchemy/testing/__init__.py +96 -0
  218. sqlalchemy/testing/assertions.py +994 -0
  219. sqlalchemy/testing/assertsql.py +520 -0
  220. sqlalchemy/testing/asyncio.py +135 -0
  221. sqlalchemy/testing/config.py +434 -0
  222. sqlalchemy/testing/engines.py +483 -0
  223. sqlalchemy/testing/entities.py +117 -0
  224. sqlalchemy/testing/exclusions.py +476 -0
  225. sqlalchemy/testing/fixtures/__init__.py +28 -0
  226. sqlalchemy/testing/fixtures/base.py +384 -0
  227. sqlalchemy/testing/fixtures/mypy.py +332 -0
  228. sqlalchemy/testing/fixtures/orm.py +227 -0
  229. sqlalchemy/testing/fixtures/sql.py +482 -0
  230. sqlalchemy/testing/pickleable.py +155 -0
  231. sqlalchemy/testing/plugin/__init__.py +6 -0
  232. sqlalchemy/testing/plugin/bootstrap.py +51 -0
  233. sqlalchemy/testing/plugin/plugin_base.py +828 -0
  234. sqlalchemy/testing/plugin/pytestplugin.py +892 -0
  235. sqlalchemy/testing/profiling.py +329 -0
  236. sqlalchemy/testing/provision.py +603 -0
  237. sqlalchemy/testing/requirements.py +1945 -0
  238. sqlalchemy/testing/schema.py +198 -0
  239. sqlalchemy/testing/suite/__init__.py +19 -0
  240. sqlalchemy/testing/suite/test_cte.py +237 -0
  241. sqlalchemy/testing/suite/test_ddl.py +389 -0
  242. sqlalchemy/testing/suite/test_deprecations.py +153 -0
  243. sqlalchemy/testing/suite/test_dialect.py +776 -0
  244. sqlalchemy/testing/suite/test_insert.py +630 -0
  245. sqlalchemy/testing/suite/test_reflection.py +3557 -0
  246. sqlalchemy/testing/suite/test_results.py +504 -0
  247. sqlalchemy/testing/suite/test_rowcount.py +258 -0
  248. sqlalchemy/testing/suite/test_select.py +2010 -0
  249. sqlalchemy/testing/suite/test_sequence.py +317 -0
  250. sqlalchemy/testing/suite/test_types.py +2147 -0
  251. sqlalchemy/testing/suite/test_unicode_ddl.py +189 -0
  252. sqlalchemy/testing/suite/test_update_delete.py +139 -0
  253. sqlalchemy/testing/util.py +535 -0
  254. sqlalchemy/testing/warnings.py +52 -0
  255. sqlalchemy/types.py +74 -0
  256. sqlalchemy/util/__init__.py +162 -0
  257. sqlalchemy/util/_collections.py +712 -0
  258. sqlalchemy/util/_concurrency_py3k.py +288 -0
  259. sqlalchemy/util/_has_cy.py +40 -0
  260. sqlalchemy/util/_py_collections.py +541 -0
  261. sqlalchemy/util/compat.py +421 -0
  262. sqlalchemy/util/concurrency.py +110 -0
  263. sqlalchemy/util/deprecations.py +401 -0
  264. sqlalchemy/util/langhelpers.py +2203 -0
  265. sqlalchemy/util/preloaded.py +150 -0
  266. sqlalchemy/util/queue.py +322 -0
  267. sqlalchemy/util/tool_support.py +201 -0
  268. sqlalchemy/util/topological.py +120 -0
  269. sqlalchemy/util/typing.py +734 -0
  270. sqlalchemy-2.0.47.dist-info/METADATA +243 -0
  271. sqlalchemy-2.0.47.dist-info/RECORD +274 -0
  272. sqlalchemy-2.0.47.dist-info/WHEEL +5 -0
  273. sqlalchemy-2.0.47.dist-info/licenses/LICENSE +19 -0
  274. sqlalchemy-2.0.47.dist-info/top_level.txt +1 -0
@@ -0,0 +1,483 @@
1
+ # testing/engines.py
2
+ # Copyright (C) 2005-2026 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
+
9
+
10
+ from __future__ import annotations
11
+
12
+ import collections
13
+ import re
14
+ import typing
15
+ from typing import Any
16
+ from typing import Dict
17
+ from typing import Optional
18
+ from typing import Union
19
+ import warnings
20
+ import weakref
21
+
22
+ from . import config
23
+ from .util import decorator
24
+ from .util import gc_collect
25
+ from .. import event
26
+ from .. import pool
27
+ from ..util import await_only
28
+ from ..util.typing import Literal
29
+
30
+
31
+ if typing.TYPE_CHECKING:
32
+ from ..engine import Engine
33
+ from ..engine.url import URL
34
+ from ..ext.asyncio import AsyncEngine
35
+
36
+
37
+ class ConnectionKiller:
38
+ def __init__(self):
39
+ self.proxy_refs = weakref.WeakKeyDictionary()
40
+ self.testing_engines = collections.defaultdict(set)
41
+ self.dbapi_connections = set()
42
+
43
+ def add_pool(self, pool):
44
+ event.listen(pool, "checkout", self._add_conn)
45
+ event.listen(pool, "checkin", self._remove_conn)
46
+ event.listen(pool, "close", self._remove_conn)
47
+ event.listen(pool, "close_detached", self._remove_conn)
48
+ # note we are keeping "invalidated" here, as those are still
49
+ # opened connections we would like to roll back
50
+
51
+ def _add_conn(self, dbapi_con, con_record, con_proxy):
52
+ self.dbapi_connections.add(dbapi_con)
53
+ self.proxy_refs[con_proxy] = True
54
+
55
+ def _remove_conn(self, dbapi_conn, *arg):
56
+ self.dbapi_connections.discard(dbapi_conn)
57
+
58
+ def add_engine(self, engine, scope):
59
+ self.add_pool(engine.pool)
60
+
61
+ assert scope in ("class", "global", "function", "fixture")
62
+ self.testing_engines[scope].add(engine)
63
+
64
+ def _safe(self, fn):
65
+ try:
66
+ fn()
67
+ except Exception as e:
68
+ warnings.warn(
69
+ "testing_reaper couldn't rollback/close connection: %s" % e
70
+ )
71
+
72
+ def rollback_all(self):
73
+ for rec in list(self.proxy_refs):
74
+ if rec is not None and rec.is_valid:
75
+ self._safe(rec.rollback)
76
+
77
+ def checkin_all(self):
78
+ # run pool.checkin() for all ConnectionFairy instances we have
79
+ # tracked.
80
+
81
+ for rec in list(self.proxy_refs):
82
+ if rec is not None and rec.is_valid:
83
+ self.dbapi_connections.discard(rec.dbapi_connection)
84
+ self._safe(rec._checkin)
85
+
86
+ # for fairy refs that were GCed and could not close the connection,
87
+ # such as asyncio, roll back those remaining connections
88
+ for con in self.dbapi_connections:
89
+ self._safe(con.rollback)
90
+ self.dbapi_connections.clear()
91
+
92
+ def close_all(self):
93
+ self.checkin_all()
94
+
95
+ def prepare_for_drop_tables(self, connection):
96
+ # don't do aggressive checks for third party test suites
97
+ if not config.bootstrapped_as_sqlalchemy:
98
+ return
99
+
100
+ from . import provision
101
+
102
+ provision.prepare_for_drop_tables(connection.engine.url, connection)
103
+
104
+ def _drop_testing_engines(self, scope):
105
+ eng = self.testing_engines[scope]
106
+ for rec in list(eng):
107
+ for proxy_ref in list(self.proxy_refs):
108
+ if proxy_ref is not None and proxy_ref.is_valid:
109
+ if (
110
+ proxy_ref._pool is not None
111
+ and proxy_ref._pool is rec.pool
112
+ ):
113
+ self._safe(proxy_ref._checkin)
114
+
115
+ if hasattr(rec, "sync_engine"):
116
+ await_only(rec.dispose())
117
+ else:
118
+ rec.dispose()
119
+
120
+ eng.clear()
121
+
122
+ def _dispose_testing_engines(self, scope):
123
+ eng = self.testing_engines[scope]
124
+ for rec in list(eng):
125
+ if hasattr(rec, "sync_engine"):
126
+ await_only(rec.dispose())
127
+ else:
128
+ rec.dispose()
129
+
130
+ def after_test(self):
131
+ self._drop_testing_engines("function")
132
+
133
+ def after_test_outside_fixtures(self, test):
134
+ # don't do aggressive checks for third party test suites
135
+ if not config.bootstrapped_as_sqlalchemy:
136
+ return
137
+
138
+ if test.__class__.__leave_connections_for_teardown__:
139
+ return
140
+
141
+ self.checkin_all()
142
+
143
+ # on PostgreSQL, this will test for any "idle in transaction"
144
+ # connections. useful to identify tests with unusual patterns
145
+ # that can't be cleaned up correctly.
146
+ from . import provision
147
+
148
+ with config.db.connect() as conn:
149
+ provision.prepare_for_drop_tables(conn.engine.url, conn)
150
+
151
+ def stop_test_class_inside_fixtures(self):
152
+ self.checkin_all()
153
+ self._drop_testing_engines("function")
154
+ self._drop_testing_engines("class")
155
+
156
+ def stop_test_class_outside_fixtures(self):
157
+ # ensure no refs to checked out connections at all.
158
+
159
+ if pool.base._strong_ref_connection_records:
160
+ gc_collect()
161
+
162
+ if pool.base._strong_ref_connection_records:
163
+ ln = len(pool.base._strong_ref_connection_records)
164
+ pool.base._strong_ref_connection_records.clear()
165
+
166
+ if ln > 2:
167
+ # allow two connections to linger, as on loaded down
168
+ # CI hardware there seem to be occasional GC lapses that
169
+ # are not easily preventable
170
+ assert (
171
+ False
172
+ ), "%d connection recs not cleared after test suite" % (ln)
173
+ if config.options and config.options.low_connections:
174
+ # for suites running with --low-connections, dispose the "global"
175
+ # engines to disconnect everything before making a testing engine
176
+ self._dispose_testing_engines("global")
177
+
178
+ def final_cleanup(self):
179
+ self.checkin_all()
180
+ for scope in self.testing_engines:
181
+ self._drop_testing_engines(scope)
182
+
183
+ def assert_all_closed(self):
184
+ for rec in self.proxy_refs:
185
+ if rec.is_valid:
186
+ assert False
187
+
188
+
189
+ testing_reaper = ConnectionKiller()
190
+
191
+
192
+ @decorator
193
+ def assert_conns_closed(fn, *args, **kw):
194
+ try:
195
+ fn(*args, **kw)
196
+ finally:
197
+ testing_reaper.assert_all_closed()
198
+
199
+
200
+ @decorator
201
+ def rollback_open_connections(fn, *args, **kw):
202
+ """Decorator that rolls back all open connections after fn execution."""
203
+
204
+ try:
205
+ fn(*args, **kw)
206
+ finally:
207
+ testing_reaper.rollback_all()
208
+
209
+
210
+ @decorator
211
+ def close_first(fn, *args, **kw):
212
+ """Decorator that closes all connections before fn execution."""
213
+
214
+ testing_reaper.checkin_all()
215
+ fn(*args, **kw)
216
+
217
+
218
+ @decorator
219
+ def close_open_connections(fn, *args, **kw):
220
+ """Decorator that closes all connections after fn execution."""
221
+ try:
222
+ fn(*args, **kw)
223
+ finally:
224
+ testing_reaper.checkin_all()
225
+
226
+
227
+ def all_dialects(exclude=None):
228
+ import sqlalchemy.dialects as d
229
+
230
+ for name in d.__all__:
231
+ # TEMPORARY
232
+ if exclude and name in exclude:
233
+ continue
234
+ mod = getattr(d, name, None)
235
+ if not mod:
236
+ mod = getattr(
237
+ __import__("sqlalchemy.dialects.%s" % name).dialects, name
238
+ )
239
+ yield mod.dialect()
240
+
241
+
242
+ class ReconnectFixture:
243
+ def __init__(self, dbapi):
244
+ self.dbapi = dbapi
245
+ self.connections = []
246
+ self.is_stopped = False
247
+
248
+ def __getattr__(self, key):
249
+ return getattr(self.dbapi, key)
250
+
251
+ def connect(self, *args, **kwargs):
252
+ conn = self.dbapi.connect(*args, **kwargs)
253
+ if self.is_stopped:
254
+ self._safe(conn.close)
255
+ curs = conn.cursor() # should fail on Oracle etc.
256
+ # should fail for everything that didn't fail
257
+ # above, connection is closed
258
+ curs.execute("select 1")
259
+ assert False, "simulated connect failure didn't work"
260
+ else:
261
+ self.connections.append(conn)
262
+ return conn
263
+
264
+ def _safe(self, fn):
265
+ try:
266
+ fn()
267
+ except Exception as e:
268
+ warnings.warn("ReconnectFixture couldn't close connection: %s" % e)
269
+
270
+ def shutdown(self, stop=False):
271
+ # TODO: this doesn't cover all cases
272
+ # as nicely as we'd like, namely MySQLdb.
273
+ # would need to implement R. Brewer's
274
+ # proxy server idea to get better
275
+ # coverage.
276
+ self.is_stopped = stop
277
+ for c in list(self.connections):
278
+ self._safe(c.close)
279
+ self.connections = []
280
+
281
+ def restart(self):
282
+ self.is_stopped = False
283
+
284
+
285
+ def reconnecting_engine(url=None, options=None):
286
+ url = url or config.db.url
287
+ dbapi = config.db.dialect.dbapi
288
+ if not options:
289
+ options = {}
290
+ options["module"] = ReconnectFixture(dbapi)
291
+ engine = testing_engine(url, options)
292
+ _dispose = engine.dispose
293
+
294
+ def dispose():
295
+ engine.dialect.dbapi.shutdown()
296
+ engine.dialect.dbapi.is_stopped = False
297
+ _dispose()
298
+
299
+ engine.test_shutdown = engine.dialect.dbapi.shutdown
300
+ engine.test_restart = engine.dialect.dbapi.restart
301
+ engine.dispose = dispose
302
+ return engine
303
+
304
+
305
+ @typing.overload
306
+ def testing_engine(
307
+ url: Optional[URL] = ...,
308
+ options: Optional[Dict[str, Any]] = ...,
309
+ *,
310
+ asyncio: Literal[False],
311
+ ) -> Engine: ...
312
+
313
+
314
+ @typing.overload
315
+ def testing_engine(
316
+ url: Optional[URL] = ...,
317
+ options: Optional[Dict[str, Any]] = ...,
318
+ *,
319
+ asyncio: Literal[True],
320
+ ) -> AsyncEngine: ...
321
+
322
+
323
+ def testing_engine(
324
+ url: Optional[URL] = None,
325
+ options: Optional[Dict[str, Any]] = None,
326
+ *,
327
+ asyncio: bool = False,
328
+ ) -> Union[Engine, AsyncEngine]:
329
+
330
+ if asyncio:
331
+ from sqlalchemy.ext.asyncio import (
332
+ create_async_engine as create_engine,
333
+ )
334
+ else:
335
+ from sqlalchemy import create_engine
336
+ from sqlalchemy.engine.url import make_url
337
+
338
+ url = make_url(url if url else config.db.url)
339
+
340
+ if not options:
341
+ options = {}
342
+
343
+ use_options = {}
344
+
345
+ for opt_dict in (config.db_opts, options):
346
+ if not opt_dict:
347
+ continue
348
+ use_options.update(
349
+ {
350
+ opt: value
351
+ for opt, value in opt_dict.items()
352
+ if opt not in ("scope", "use_reaper")
353
+ and not opt.startswith("sqlite_")
354
+ }
355
+ )
356
+
357
+ engine = create_engine(url, **use_options)
358
+
359
+ if config.options and config.options.low_connections:
360
+ # for suites running with --low-connections, dispose the "global"
361
+ # engines to disconnect everything before making a testing engine
362
+ testing_reaper._dispose_testing_engines("global")
363
+
364
+ scope = options.get("scope", "function")
365
+ if scope == "global":
366
+ if asyncio:
367
+ engine.sync_engine._has_events = True
368
+ else:
369
+ engine._has_events = (
370
+ True # enable event blocks, helps with profiling
371
+ )
372
+
373
+ from . import provision
374
+
375
+ provision.post_configure_testing_engine(engine.url, engine, options, scope)
376
+
377
+ # post_configure_testing_engine may have modified the options dictionary
378
+ # in place; consume additional post arguments afterwards
379
+
380
+ use_reaper = options.get("use_reaper", True)
381
+ if use_reaper:
382
+ testing_reaper.add_engine(engine, scope)
383
+
384
+ if (
385
+ isinstance(engine.pool, pool.QueuePool)
386
+ and "pool" not in options
387
+ and "pool_timeout" not in options
388
+ and "max_overflow" not in options
389
+ ):
390
+ engine.pool._timeout = 0
391
+ engine.pool._max_overflow = 0
392
+
393
+ return engine
394
+
395
+
396
+ def mock_engine(dialect_name=None):
397
+ """Provides a mocking engine based on the current testing.db.
398
+
399
+ This is normally used to test DDL generation flow as emitted
400
+ by an Engine.
401
+
402
+ It should not be used in other cases, as assert_compile() and
403
+ assert_sql_execution() are much better choices with fewer
404
+ moving parts.
405
+
406
+ """
407
+
408
+ from sqlalchemy import create_mock_engine
409
+
410
+ if not dialect_name:
411
+ dialect_name = config.db.name
412
+
413
+ buffer = []
414
+
415
+ def executor(sql, *a, **kw):
416
+ buffer.append(sql)
417
+
418
+ def assert_sql(stmts):
419
+ recv = [re.sub(r"[\n\t]", "", str(s)) for s in buffer]
420
+ assert recv == stmts, recv
421
+
422
+ def print_sql():
423
+ d = engine.dialect
424
+ return "\n".join(str(s.compile(dialect=d)) for s in engine.mock)
425
+
426
+ engine = create_mock_engine(dialect_name + "://", executor)
427
+ assert not hasattr(engine, "mock")
428
+ engine.mock = buffer
429
+ engine.assert_sql = assert_sql
430
+ engine.print_sql = print_sql
431
+ return engine
432
+
433
+
434
+ class DBAPIProxyCursor:
435
+ """Proxy a DBAPI cursor.
436
+
437
+ Tests can provide subclasses of this to intercept
438
+ DBAPI-level cursor operations.
439
+
440
+ """
441
+
442
+ def __init__(self, engine, conn, *args, **kwargs):
443
+ self.engine = engine
444
+ self.connection = conn
445
+ self.cursor = conn.cursor(*args, **kwargs)
446
+
447
+ def execute(self, stmt, parameters=None, **kw):
448
+ if parameters:
449
+ return self.cursor.execute(stmt, parameters, **kw)
450
+ else:
451
+ return self.cursor.execute(stmt, **kw)
452
+
453
+ def executemany(self, stmt, params, **kw):
454
+ return self.cursor.executemany(stmt, params, **kw)
455
+
456
+ def __iter__(self):
457
+ return iter(self.cursor)
458
+
459
+ def __getattr__(self, key):
460
+ return getattr(self.cursor, key)
461
+
462
+
463
+ class DBAPIProxyConnection:
464
+ """Proxy a DBAPI connection.
465
+
466
+ Tests can provide subclasses of this to intercept
467
+ DBAPI-level connection operations.
468
+
469
+ """
470
+
471
+ def __init__(self, engine, conn, cursor_cls):
472
+ self.conn = conn
473
+ self.engine = engine
474
+ self.cursor_cls = cursor_cls
475
+
476
+ def cursor(self, *args, **kwargs):
477
+ return self.cursor_cls(self.engine, self.conn, *args, **kwargs)
478
+
479
+ def close(self):
480
+ self.conn.close()
481
+
482
+ def __getattr__(self, key):
483
+ return getattr(self.conn, key)
@@ -0,0 +1,117 @@
1
+ # testing/entities.py
2
+ # Copyright (C) 2005-2026 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
+
9
+
10
+ from __future__ import annotations
11
+
12
+ import sqlalchemy as sa
13
+ from .. import exc as sa_exc
14
+ from ..orm.writeonly import WriteOnlyCollection
15
+
16
+ _repr_stack = set()
17
+
18
+
19
+ class BasicEntity:
20
+ def __init__(self, **kw):
21
+ for key, value in kw.items():
22
+ setattr(self, key, value)
23
+
24
+ def __repr__(self):
25
+ if id(self) in _repr_stack:
26
+ return object.__repr__(self)
27
+ _repr_stack.add(id(self))
28
+ try:
29
+ return "%s(%s)" % (
30
+ (self.__class__.__name__),
31
+ ", ".join(
32
+ [
33
+ "%s=%r" % (key, getattr(self, key))
34
+ for key in sorted(self.__dict__.keys())
35
+ if not key.startswith("_")
36
+ ]
37
+ ),
38
+ )
39
+ finally:
40
+ _repr_stack.remove(id(self))
41
+
42
+
43
+ _recursion_stack = set()
44
+
45
+
46
+ class ComparableMixin:
47
+ def __ne__(self, other):
48
+ return not self.__eq__(other)
49
+
50
+ def __eq__(self, other):
51
+ """'Deep, sparse compare.
52
+
53
+ Deeply compare two entities, following the non-None attributes of the
54
+ non-persisted object, if possible.
55
+
56
+ """
57
+ if other is self:
58
+ return True
59
+ elif not self.__class__ == other.__class__:
60
+ return False
61
+
62
+ if id(self) in _recursion_stack:
63
+ return True
64
+ _recursion_stack.add(id(self))
65
+
66
+ try:
67
+ # pick the entity that's not SA persisted as the source
68
+ try:
69
+ self_key = sa.orm.attributes.instance_state(self).key
70
+ except sa.orm.exc.NO_STATE:
71
+ self_key = None
72
+
73
+ if other is None:
74
+ a = self
75
+ b = other
76
+ elif self_key is not None:
77
+ a = other
78
+ b = self
79
+ else:
80
+ a = self
81
+ b = other
82
+
83
+ for attr in list(a.__dict__):
84
+ if attr.startswith("_"):
85
+ continue
86
+
87
+ value = getattr(a, attr)
88
+
89
+ if isinstance(value, WriteOnlyCollection):
90
+ continue
91
+
92
+ try:
93
+ # handle lazy loader errors
94
+ battr = getattr(b, attr)
95
+ except (AttributeError, sa_exc.UnboundExecutionError):
96
+ return False
97
+
98
+ if hasattr(value, "__iter__") and not isinstance(value, str):
99
+ if hasattr(value, "__getitem__") and not hasattr(
100
+ value, "keys"
101
+ ):
102
+ if list(value) != list(battr):
103
+ return False
104
+ else:
105
+ if set(value) != set(battr):
106
+ return False
107
+ else:
108
+ if value is not None and value != battr:
109
+ return False
110
+ return True
111
+ finally:
112
+ _recursion_stack.remove(id(self))
113
+
114
+
115
+ class ComparableEntity(ComparableMixin, BasicEntity):
116
+ def __hash__(self):
117
+ return hash(self.__class__)