assertions.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. # testing/assertions.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. from __future__ import annotations
  9. from collections import defaultdict
  10. import contextlib
  11. from copy import copy
  12. from itertools import filterfalse
  13. import re
  14. import sys
  15. import warnings
  16. from . import assertsql
  17. from . import config
  18. from . import engines
  19. from . import mock
  20. from .exclusions import db_spec
  21. from .util import fail
  22. from .. import exc as sa_exc
  23. from .. import schema
  24. from .. import sql
  25. from .. import types as sqltypes
  26. from .. import util
  27. from ..engine import default
  28. from ..engine import url
  29. from ..sql.selectable import LABEL_STYLE_TABLENAME_PLUS_COL
  30. from ..util import decorator
  31. def expect_warnings(*messages, **kw):
  32. """Context manager which expects one or more warnings.
  33. With no arguments, squelches all SAWarning emitted via
  34. sqlalchemy.util.warn and sqlalchemy.util.warn_limited. Otherwise
  35. pass string expressions that will match selected warnings via regex;
  36. all non-matching warnings are sent through.
  37. The expect version **asserts** that the warnings were in fact seen.
  38. Note that the test suite sets SAWarning warnings to raise exceptions.
  39. """ # noqa
  40. return _expect_warnings_sqla_only(sa_exc.SAWarning, messages, **kw)
  41. @contextlib.contextmanager
  42. def expect_warnings_on(db, *messages, **kw):
  43. """Context manager which expects one or more warnings on specific
  44. dialects.
  45. The expect version **asserts** that the warnings were in fact seen.
  46. """
  47. spec = db_spec(db)
  48. if isinstance(db, str) and not spec(config._current):
  49. yield
  50. else:
  51. with expect_warnings(*messages, **kw):
  52. yield
  53. def emits_warning(*messages):
  54. """Decorator form of expect_warnings().
  55. Note that emits_warning does **not** assert that the warnings
  56. were in fact seen.
  57. """
  58. @decorator
  59. def decorate(fn, *args, **kw):
  60. with expect_warnings(assert_=False, *messages):
  61. return fn(*args, **kw)
  62. return decorate
  63. def expect_deprecated(*messages, **kw):
  64. return _expect_warnings_sqla_only(
  65. sa_exc.SADeprecationWarning, messages, **kw
  66. )
  67. def expect_deprecated_20(*messages, **kw):
  68. return _expect_warnings_sqla_only(
  69. sa_exc.Base20DeprecationWarning, messages, **kw
  70. )
  71. def emits_warning_on(db, *messages):
  72. """Mark a test as emitting a warning on a specific dialect.
  73. With no arguments, squelches all SAWarning failures. Or pass one or more
  74. strings; these will be matched to the root of the warning description by
  75. warnings.filterwarnings().
  76. Note that emits_warning_on does **not** assert that the warnings
  77. were in fact seen.
  78. """
  79. @decorator
  80. def decorate(fn, *args, **kw):
  81. with expect_warnings_on(db, assert_=False, *messages):
  82. return fn(*args, **kw)
  83. return decorate
  84. def uses_deprecated(*messages):
  85. """Mark a test as immune from fatal deprecation warnings.
  86. With no arguments, squelches all SADeprecationWarning failures.
  87. Or pass one or more strings; these will be matched to the root
  88. of the warning description by warnings.filterwarnings().
  89. As a special case, you may pass a function name prefixed with //
  90. and it will be re-written as needed to match the standard warning
  91. verbiage emitted by the sqlalchemy.util.deprecated decorator.
  92. Note that uses_deprecated does **not** assert that the warnings
  93. were in fact seen.
  94. """
  95. @decorator
  96. def decorate(fn, *args, **kw):
  97. with expect_deprecated(*messages, assert_=False):
  98. return fn(*args, **kw)
  99. return decorate
  100. _FILTERS = None
  101. _SEEN = None
  102. _EXC_CLS = None
  103. def _expect_warnings_sqla_only(
  104. exc_cls,
  105. messages,
  106. regex=True,
  107. search_msg=False,
  108. assert_=True,
  109. ):
  110. """SQLAlchemy internal use only _expect_warnings().
  111. Alembic is using _expect_warnings() directly, and should be updated
  112. to use this new interface.
  113. """
  114. return _expect_warnings(
  115. exc_cls,
  116. messages,
  117. regex=regex,
  118. search_msg=search_msg,
  119. assert_=assert_,
  120. raise_on_any_unexpected=True,
  121. )
  122. @contextlib.contextmanager
  123. def _expect_warnings(
  124. exc_cls,
  125. messages,
  126. regex=True,
  127. search_msg=False,
  128. assert_=True,
  129. raise_on_any_unexpected=False,
  130. squelch_other_warnings=False,
  131. ):
  132. global _FILTERS, _SEEN, _EXC_CLS
  133. if regex or search_msg:
  134. filters = [re.compile(msg, re.I | re.S) for msg in messages]
  135. else:
  136. filters = list(messages)
  137. if _FILTERS is not None:
  138. # nested call; update _FILTERS and _SEEN, return. outer
  139. # block will assert our messages
  140. assert _SEEN is not None
  141. assert _EXC_CLS is not None
  142. _FILTERS.extend(filters)
  143. _SEEN.update(filters)
  144. _EXC_CLS += (exc_cls,)
  145. yield
  146. else:
  147. seen = _SEEN = set(filters)
  148. _FILTERS = filters
  149. _EXC_CLS = (exc_cls,)
  150. if raise_on_any_unexpected:
  151. def real_warn(msg, *arg, **kw):
  152. raise AssertionError("Got unexpected warning: %r" % msg)
  153. else:
  154. real_warn = warnings.warn
  155. def our_warn(msg, *arg, **kw):
  156. if isinstance(msg, _EXC_CLS):
  157. exception = type(msg)
  158. msg = str(msg)
  159. elif arg:
  160. exception = arg[0]
  161. else:
  162. exception = None
  163. if not exception or not issubclass(exception, _EXC_CLS):
  164. if not squelch_other_warnings:
  165. return real_warn(msg, *arg, **kw)
  166. else:
  167. return
  168. if not filters and not raise_on_any_unexpected:
  169. return
  170. for filter_ in filters:
  171. if (
  172. (search_msg and filter_.search(msg))
  173. or (regex and filter_.match(msg))
  174. or (not regex and filter_ == msg)
  175. ):
  176. seen.discard(filter_)
  177. break
  178. else:
  179. if not squelch_other_warnings:
  180. real_warn(msg, *arg, **kw)
  181. with mock.patch("warnings.warn", our_warn):
  182. try:
  183. yield
  184. finally:
  185. _SEEN = _FILTERS = _EXC_CLS = None
  186. if assert_:
  187. assert not seen, "Warnings were not seen: %s" % ", ".join(
  188. "%r" % (s.pattern if regex else s) for s in seen
  189. )
  190. def global_cleanup_assertions():
  191. """Check things that have to be finalized at the end of a test suite.
  192. Hardcoded at the moment, a modular system can be built here
  193. to support things like PG prepared transactions, tables all
  194. dropped, etc.
  195. """
  196. _assert_no_stray_pool_connections()
  197. def _assert_no_stray_pool_connections():
  198. engines.testing_reaper.assert_all_closed()
  199. def int_within_variance(expected, received, variance):
  200. deviance = int(expected * variance)
  201. assert (
  202. abs(received - expected) < deviance
  203. ), "Given int value %s is not within %d%% of expected value %s" % (
  204. received,
  205. variance * 100,
  206. expected,
  207. )
  208. def eq_regex(a, b, msg=None, flags=0):
  209. assert re.match(b, a, flags), msg or "%r !~ %r" % (a, b)
  210. def eq_(a, b, msg=None):
  211. """Assert a == b, with repr messaging on failure."""
  212. assert a == b, msg or "%r != %r" % (a, b)
  213. def ne_(a, b, msg=None):
  214. """Assert a != b, with repr messaging on failure."""
  215. assert a != b, msg or "%r == %r" % (a, b)
  216. def le_(a, b, msg=None):
  217. """Assert a <= b, with repr messaging on failure."""
  218. assert a <= b, msg or "%r != %r" % (a, b)
  219. def is_instance_of(a, b, msg=None):
  220. assert isinstance(a, b), msg or "%r is not an instance of %r" % (a, b)
  221. def is_none(a, msg=None):
  222. is_(a, None, msg=msg)
  223. def is_not_none(a, msg=None):
  224. is_not(a, None, msg=msg)
  225. def is_true(a, msg=None):
  226. is_(bool(a), True, msg=msg)
  227. def is_false(a, msg=None):
  228. is_(bool(a), False, msg=msg)
  229. def is_(a, b, msg=None):
  230. """Assert a is b, with repr messaging on failure."""
  231. assert a is b, msg or "%r is not %r" % (a, b)
  232. def is_not(a, b, msg=None):
  233. """Assert a is not b, with repr messaging on failure."""
  234. assert a is not b, msg or "%r is %r" % (a, b)
  235. # deprecated. See #5429
  236. is_not_ = is_not
  237. def in_(a, b, msg=None):
  238. """Assert a in b, with repr messaging on failure."""
  239. assert a in b, msg or "%r not in %r" % (a, b)
  240. def not_in(a, b, msg=None):
  241. """Assert a in not b, with repr messaging on failure."""
  242. assert a not in b, msg or "%r is in %r" % (a, b)
  243. # deprecated. See #5429
  244. not_in_ = not_in
  245. def startswith_(a, fragment, msg=None):
  246. """Assert a.startswith(fragment), with repr messaging on failure."""
  247. assert a.startswith(fragment), msg or "%r does not start with %r" % (
  248. a,
  249. fragment,
  250. )
  251. def eq_ignore_whitespace(a, b, msg=None):
  252. a = re.sub(r"^\s+?|\n", "", a)
  253. a = re.sub(r" {2,}", " ", a)
  254. a = re.sub(r"\t", "", a)
  255. b = re.sub(r"^\s+?|\n", "", b)
  256. b = re.sub(r" {2,}", " ", b)
  257. b = re.sub(r"\t", "", b)
  258. assert a == b, msg or "%r != %r" % (a, b)
  259. def _assert_proper_exception_context(exception):
  260. """assert that any exception we're catching does not have a __context__
  261. without a __cause__, and that __suppress_context__ is never set.
  262. Python 3 will report nested as exceptions as "during the handling of
  263. error X, error Y occurred". That's not what we want to do. we want
  264. these exceptions in a cause chain.
  265. """
  266. if (
  267. exception.__context__ is not exception.__cause__
  268. and not exception.__suppress_context__
  269. ):
  270. assert False, (
  271. "Exception %r was correctly raised but did not set a cause, "
  272. "within context %r as its cause."
  273. % (exception, exception.__context__)
  274. )
  275. def assert_raises(except_cls, callable_, *args, **kw):
  276. return _assert_raises(except_cls, callable_, args, kw, check_context=True)
  277. def assert_raises_context_ok(except_cls, callable_, *args, **kw):
  278. return _assert_raises(except_cls, callable_, args, kw)
  279. def assert_raises_message(except_cls, msg, callable_, *args, **kwargs):
  280. return _assert_raises(
  281. except_cls, callable_, args, kwargs, msg=msg, check_context=True
  282. )
  283. def assert_warns(except_cls, callable_, *args, **kwargs):
  284. """legacy adapter function for functions that were previously using
  285. assert_raises with SAWarning or similar.
  286. has some workarounds to accommodate the fact that the callable completes
  287. with this approach rather than stopping at the exception raise.
  288. """
  289. with _expect_warnings_sqla_only(except_cls, [".*"]):
  290. return callable_(*args, **kwargs)
  291. def assert_warns_message(except_cls, msg, callable_, *args, **kwargs):
  292. """legacy adapter function for functions that were previously using
  293. assert_raises with SAWarning or similar.
  294. has some workarounds to accommodate the fact that the callable completes
  295. with this approach rather than stopping at the exception raise.
  296. Also uses regex.search() to match the given message to the error string
  297. rather than regex.match().
  298. """
  299. with _expect_warnings_sqla_only(
  300. except_cls,
  301. [msg],
  302. search_msg=True,
  303. regex=False,
  304. ):
  305. return callable_(*args, **kwargs)
  306. def assert_raises_message_context_ok(
  307. except_cls, msg, callable_, *args, **kwargs
  308. ):
  309. return _assert_raises(except_cls, callable_, args, kwargs, msg=msg)
  310. def _assert_raises(
  311. except_cls, callable_, args, kwargs, msg=None, check_context=False
  312. ):
  313. with _expect_raises(except_cls, msg, check_context) as ec:
  314. callable_(*args, **kwargs)
  315. return ec.error
  316. class _ErrorContainer:
  317. error = None
  318. @contextlib.contextmanager
  319. def _expect_raises(except_cls, msg=None, check_context=False):
  320. if (
  321. isinstance(except_cls, type)
  322. and issubclass(except_cls, Warning)
  323. or isinstance(except_cls, Warning)
  324. ):
  325. raise TypeError(
  326. "Use expect_warnings for warnings, not "
  327. "expect_raises / assert_raises"
  328. )
  329. ec = _ErrorContainer()
  330. if check_context:
  331. are_we_already_in_a_traceback = sys.exc_info()[0]
  332. try:
  333. yield ec
  334. success = False
  335. except except_cls as err:
  336. ec.error = err
  337. success = True
  338. if msg is not None:
  339. # I'm often pdbing here, and "err" above isn't
  340. # in scope, so assign the string explicitly
  341. error_as_string = str(err)
  342. assert re.search(msg, error_as_string, re.UNICODE), "%r !~ %s" % (
  343. msg,
  344. error_as_string,
  345. )
  346. if check_context and not are_we_already_in_a_traceback:
  347. _assert_proper_exception_context(err)
  348. print(str(err).encode("utf-8"))
  349. # it's generally a good idea to not carry traceback objects outside
  350. # of the except: block, but in this case especially we seem to have
  351. # hit some bug in either python 3.10.0b2 or greenlet or both which
  352. # this seems to fix:
  353. # https://github.com/python-greenlet/greenlet/issues/242
  354. del ec
  355. # assert outside the block so it works for AssertionError too !
  356. assert success, "Callable did not raise an exception"
  357. def expect_raises(except_cls, check_context=True):
  358. return _expect_raises(except_cls, check_context=check_context)
  359. def expect_raises_message(except_cls, msg, check_context=True):
  360. return _expect_raises(except_cls, msg=msg, check_context=check_context)
  361. class AssertsCompiledSQL:
  362. def assert_compile(
  363. self,
  364. clause,
  365. result,
  366. params=None,
  367. checkparams=None,
  368. for_executemany=False,
  369. check_literal_execute=None,
  370. check_post_param=None,
  371. dialect=None,
  372. checkpositional=None,
  373. check_prefetch=None,
  374. use_default_dialect=False,
  375. allow_dialect_select=False,
  376. supports_default_values=True,
  377. supports_native_boolean=False,
  378. supports_default_metavalue=True,
  379. literal_binds=False,
  380. render_postcompile=False,
  381. schema_translate_map=None,
  382. render_schema_translate=False,
  383. default_schema_name=None,
  384. from_linting=False,
  385. check_param_order=True,
  386. use_literal_execute_for_simple_int=False,
  387. ):
  388. if use_default_dialect:
  389. dialect = default.DefaultDialect()
  390. dialect.supports_default_values = supports_default_values
  391. dialect.supports_default_metavalue = supports_default_metavalue
  392. dialect.supports_native_boolean = supports_native_boolean
  393. elif allow_dialect_select:
  394. dialect = None
  395. else:
  396. if dialect is None:
  397. dialect = getattr(self, "__dialect__", None)
  398. if dialect is None:
  399. dialect = config.db.dialect
  400. elif dialect == "default" or dialect == "default_qmark":
  401. if dialect == "default":
  402. dialect = default.DefaultDialect()
  403. else:
  404. dialect = default.DefaultDialect("qmark")
  405. dialect.supports_default_values = supports_default_values
  406. dialect.supports_default_metavalue = supports_default_metavalue
  407. elif dialect == "default_enhanced":
  408. dialect = default.StrCompileDialect()
  409. elif isinstance(dialect, str):
  410. dialect = url.URL.create(dialect).get_dialect()()
  411. if default_schema_name:
  412. dialect.default_schema_name = default_schema_name
  413. kw = {}
  414. compile_kwargs = {}
  415. if schema_translate_map:
  416. kw["schema_translate_map"] = schema_translate_map
  417. if params is not None:
  418. kw["column_keys"] = list(params)
  419. if literal_binds:
  420. compile_kwargs["literal_binds"] = True
  421. if render_postcompile:
  422. compile_kwargs["render_postcompile"] = True
  423. if use_literal_execute_for_simple_int:
  424. compile_kwargs["use_literal_execute_for_simple_int"] = True
  425. if for_executemany:
  426. kw["for_executemany"] = True
  427. if render_schema_translate:
  428. kw["render_schema_translate"] = True
  429. if from_linting or getattr(self, "assert_from_linting", False):
  430. kw["linting"] = sql.FROM_LINTING
  431. from sqlalchemy import orm
  432. if isinstance(clause, orm.Query):
  433. stmt = clause._statement_20()
  434. stmt._label_style = LABEL_STYLE_TABLENAME_PLUS_COL
  435. clause = stmt
  436. if compile_kwargs:
  437. kw["compile_kwargs"] = compile_kwargs
  438. class DontAccess:
  439. def __getattribute__(self, key):
  440. raise NotImplementedError(
  441. "compiler accessed .statement; use "
  442. "compiler.current_executable"
  443. )
  444. class CheckCompilerAccess:
  445. def __init__(self, test_statement):
  446. self.test_statement = test_statement
  447. self._annotations = {}
  448. self.supports_execution = getattr(
  449. test_statement, "supports_execution", False
  450. )
  451. if self.supports_execution:
  452. self._execution_options = test_statement._execution_options
  453. if hasattr(test_statement, "_returning"):
  454. self._returning = test_statement._returning
  455. if hasattr(test_statement, "_inline"):
  456. self._inline = test_statement._inline
  457. if hasattr(test_statement, "_return_defaults"):
  458. self._return_defaults = test_statement._return_defaults
  459. @property
  460. def _variant_mapping(self):
  461. return self.test_statement._variant_mapping
  462. def _default_dialect(self):
  463. return self.test_statement._default_dialect()
  464. def compile(self, dialect, **kw):
  465. return self.test_statement.compile.__func__(
  466. self, dialect=dialect, **kw
  467. )
  468. def _compiler(self, dialect, **kw):
  469. return self.test_statement._compiler.__func__(
  470. self, dialect, **kw
  471. )
  472. def _compiler_dispatch(self, compiler, **kwargs):
  473. if hasattr(compiler, "statement"):
  474. with mock.patch.object(
  475. compiler, "statement", DontAccess()
  476. ):
  477. return self.test_statement._compiler_dispatch(
  478. compiler, **kwargs
  479. )
  480. else:
  481. return self.test_statement._compiler_dispatch(
  482. compiler, **kwargs
  483. )
  484. # no construct can assume it's the "top level" construct in all cases
  485. # as anything can be nested. ensure constructs don't assume they
  486. # are the "self.statement" element
  487. c = CheckCompilerAccess(clause).compile(dialect=dialect, **kw)
  488. if isinstance(clause, sqltypes.TypeEngine):
  489. cache_key_no_warnings = clause._static_cache_key
  490. if cache_key_no_warnings:
  491. hash(cache_key_no_warnings)
  492. else:
  493. cache_key_no_warnings = clause._generate_cache_key()
  494. if cache_key_no_warnings:
  495. hash(cache_key_no_warnings[0])
  496. param_str = repr(getattr(c, "params", {}))
  497. param_str = param_str.encode("utf-8").decode("ascii", "ignore")
  498. print(("\nSQL String:\n" + str(c) + param_str).encode("utf-8"))
  499. cc = re.sub(r"[\n\t]", "", str(c))
  500. eq_(cc, result, "%r != %r on dialect %r" % (cc, result, dialect))
  501. if checkparams is not None:
  502. if render_postcompile:
  503. expanded_state = c.construct_expanded_state(
  504. params, escape_names=False
  505. )
  506. eq_(expanded_state.parameters, checkparams)
  507. else:
  508. eq_(c.construct_params(params), checkparams)
  509. if checkpositional is not None:
  510. if render_postcompile:
  511. expanded_state = c.construct_expanded_state(
  512. params, escape_names=False
  513. )
  514. eq_(
  515. tuple(
  516. [
  517. expanded_state.parameters[x]
  518. for x in expanded_state.positiontup
  519. ]
  520. ),
  521. checkpositional,
  522. )
  523. else:
  524. p = c.construct_params(params, escape_names=False)
  525. eq_(tuple([p[x] for x in c.positiontup]), checkpositional)
  526. if check_prefetch is not None:
  527. eq_(c.prefetch, check_prefetch)
  528. if check_literal_execute is not None:
  529. eq_(
  530. {
  531. c.bind_names[b]: b.effective_value
  532. for b in c.literal_execute_params
  533. },
  534. check_literal_execute,
  535. )
  536. if check_post_param is not None:
  537. eq_(
  538. {
  539. c.bind_names[b]: b.effective_value
  540. for b in c.post_compile_params
  541. },
  542. check_post_param,
  543. )
  544. if check_param_order and getattr(c, "params", None):
  545. def get_dialect(paramstyle, positional):
  546. cp = copy(dialect)
  547. cp.paramstyle = paramstyle
  548. cp.positional = positional
  549. return cp
  550. pyformat_dialect = get_dialect("pyformat", False)
  551. pyformat_c = clause.compile(dialect=pyformat_dialect, **kw)
  552. stmt = re.sub(r"[\n\t]", "", str(pyformat_c))
  553. qmark_dialect = get_dialect("qmark", True)
  554. qmark_c = clause.compile(dialect=qmark_dialect, **kw)
  555. values = list(qmark_c.positiontup)
  556. escaped = qmark_c.escaped_bind_names
  557. for post_param in (
  558. qmark_c.post_compile_params | qmark_c.literal_execute_params
  559. ):
  560. name = qmark_c.bind_names[post_param]
  561. if name in values:
  562. values = [v for v in values if v != name]
  563. positions = []
  564. pos_by_value = defaultdict(list)
  565. for v in values:
  566. try:
  567. if v in pos_by_value:
  568. start = pos_by_value[v][-1]
  569. else:
  570. start = 0
  571. esc = escaped.get(v, v)
  572. pos = stmt.index("%%(%s)s" % (esc,), start) + 2
  573. positions.append(pos)
  574. pos_by_value[v].append(pos)
  575. except ValueError:
  576. msg = "Expected to find bindparam %r in %r" % (v, stmt)
  577. assert False, msg
  578. ordered = all(
  579. positions[i - 1] < positions[i]
  580. for i in range(1, len(positions))
  581. )
  582. expected = [v for _, v in sorted(zip(positions, values))]
  583. msg = (
  584. "Order of parameters %s does not match the order "
  585. "in the statement %s. Statement %r" % (values, expected, stmt)
  586. )
  587. is_true(ordered, msg)
  588. class ComparesTables:
  589. def assert_tables_equal(
  590. self,
  591. table,
  592. reflected_table,
  593. strict_types=False,
  594. strict_constraints=True,
  595. ):
  596. assert len(table.c) == len(reflected_table.c)
  597. for c, reflected_c in zip(table.c, reflected_table.c):
  598. eq_(c.name, reflected_c.name)
  599. assert reflected_c is reflected_table.c[c.name]
  600. if strict_constraints:
  601. eq_(c.primary_key, reflected_c.primary_key)
  602. eq_(c.nullable, reflected_c.nullable)
  603. if strict_types:
  604. msg = "Type '%s' doesn't correspond to type '%s'"
  605. assert isinstance(reflected_c.type, type(c.type)), msg % (
  606. reflected_c.type,
  607. c.type,
  608. )
  609. else:
  610. self.assert_types_base(reflected_c, c)
  611. if isinstance(c.type, sqltypes.String):
  612. eq_(c.type.length, reflected_c.type.length)
  613. if strict_constraints:
  614. eq_(
  615. {f.column.name for f in c.foreign_keys},
  616. {f.column.name for f in reflected_c.foreign_keys},
  617. )
  618. if c.server_default:
  619. assert isinstance(
  620. reflected_c.server_default, schema.FetchedValue
  621. )
  622. if strict_constraints:
  623. assert len(table.primary_key) == len(reflected_table.primary_key)
  624. for c in table.primary_key:
  625. assert reflected_table.primary_key.columns[c.name] is not None
  626. def assert_types_base(self, c1, c2):
  627. assert c1.type._compare_type_affinity(
  628. c2.type
  629. ), "On column %r, type '%s' doesn't correspond to type '%s'" % (
  630. c1.name,
  631. c1.type,
  632. c2.type,
  633. )
  634. class AssertsExecutionResults:
  635. def assert_result(self, result, class_, *objects):
  636. result = list(result)
  637. print(repr(result))
  638. self.assert_list(result, class_, objects)
  639. def assert_list(self, result, class_, list_):
  640. self.assert_(
  641. len(result) == len(list_),
  642. "result list is not the same size as test list, "
  643. + "for class "
  644. + class_.__name__,
  645. )
  646. for i in range(0, len(list_)):
  647. self.assert_row(class_, result[i], list_[i])
  648. def assert_row(self, class_, rowobj, desc):
  649. self.assert_(
  650. rowobj.__class__ is class_, "item class is not " + repr(class_)
  651. )
  652. for key, value in desc.items():
  653. if isinstance(value, tuple):
  654. if isinstance(value[1], list):
  655. self.assert_list(getattr(rowobj, key), value[0], value[1])
  656. else:
  657. self.assert_row(value[0], getattr(rowobj, key), value[1])
  658. else:
  659. self.assert_(
  660. getattr(rowobj, key) == value,
  661. "attribute %s value %s does not match %s"
  662. % (key, getattr(rowobj, key), value),
  663. )
  664. def assert_unordered_result(self, result, cls, *expected):
  665. """As assert_result, but the order of objects is not considered.
  666. The algorithm is very expensive but not a big deal for the small
  667. numbers of rows that the test suite manipulates.
  668. """
  669. class immutabledict(dict):
  670. def __hash__(self):
  671. return id(self)
  672. found = util.IdentitySet(result)
  673. expected = {immutabledict(e) for e in expected}
  674. for wrong in filterfalse(lambda o: isinstance(o, cls), found):
  675. fail(
  676. 'Unexpected type "%s", expected "%s"'
  677. % (type(wrong).__name__, cls.__name__)
  678. )
  679. if len(found) != len(expected):
  680. fail(
  681. 'Unexpected object count "%s", expected "%s"'
  682. % (len(found), len(expected))
  683. )
  684. NOVALUE = object()
  685. def _compare_item(obj, spec):
  686. for key, value in spec.items():
  687. if isinstance(value, tuple):
  688. try:
  689. self.assert_unordered_result(
  690. getattr(obj, key), value[0], *value[1]
  691. )
  692. except AssertionError:
  693. return False
  694. else:
  695. if getattr(obj, key, NOVALUE) != value:
  696. return False
  697. return True
  698. for expected_item in expected:
  699. for found_item in found:
  700. if _compare_item(found_item, expected_item):
  701. found.remove(found_item)
  702. break
  703. else:
  704. fail(
  705. "Expected %s instance with attributes %s not found."
  706. % (cls.__name__, repr(expected_item))
  707. )
  708. return True
  709. def sql_execution_asserter(self, db=None):
  710. if db is None:
  711. from . import db as db
  712. return assertsql.assert_engine(db)
  713. def assert_sql_execution(self, db, callable_, *rules):
  714. with self.sql_execution_asserter(db) as asserter:
  715. result = callable_()
  716. asserter.assert_(*rules)
  717. return result
  718. def assert_sql(self, db, callable_, rules):
  719. newrules = []
  720. for rule in rules:
  721. if isinstance(rule, dict):
  722. newrule = assertsql.AllOf(
  723. *[assertsql.CompiledSQL(k, v) for k, v in rule.items()]
  724. )
  725. else:
  726. newrule = assertsql.CompiledSQL(*rule)
  727. newrules.append(newrule)
  728. return self.assert_sql_execution(db, callable_, *newrules)
  729. def assert_sql_count(self, db, callable_, count):
  730. return self.assert_sql_execution(
  731. db, callable_, assertsql.CountStatements(count)
  732. )
  733. @contextlib.contextmanager
  734. def assert_execution(self, db, *rules):
  735. with self.sql_execution_asserter(db) as asserter:
  736. yield
  737. asserter.assert_(*rules)
  738. def assert_statement_count(self, db, count):
  739. return self.assert_execution(db, assertsql.CountStatements(count))
  740. @contextlib.contextmanager
  741. def assert_statement_count_multi_db(self, dbs, counts):
  742. recs = [
  743. (self.sql_execution_asserter(db), db, count)
  744. for (db, count) in zip(dbs, counts)
  745. ]
  746. asserters = []
  747. for ctx, db, count in recs:
  748. asserters.append(ctx.__enter__())
  749. try:
  750. yield
  751. finally:
  752. for asserter, (ctx, db, count) in zip(asserters, recs):
  753. ctx.__exit__(None, None, None)
  754. asserter.assert_(assertsql.CountStatements(count))
  755. class ComparesIndexes:
  756. def compare_table_index_with_expected(
  757. self, table: schema.Table, expected: list, dialect_name: str
  758. ):
  759. eq_(len(table.indexes), len(expected))
  760. idx_dict = {idx.name: idx for idx in table.indexes}
  761. for exp in expected:
  762. idx = idx_dict[exp["name"]]
  763. eq_(idx.unique, exp["unique"])
  764. cols = [c for c in exp["column_names"] if c is not None]
  765. eq_(len(idx.columns), len(cols))
  766. for c in cols:
  767. is_true(c in idx.columns)
  768. exprs = exp.get("expressions")
  769. if exprs:
  770. eq_(len(idx.expressions), len(exprs))
  771. for idx_exp, expr, col in zip(
  772. idx.expressions, exprs, exp["column_names"]
  773. ):
  774. if col is None:
  775. eq_(idx_exp.text, expr)
  776. if (
  777. exp.get("dialect_options")
  778. and f"{dialect_name}_include" in exp["dialect_options"]
  779. ):
  780. eq_(
  781. idx.dialect_options[dialect_name]["include"],
  782. exp["dialect_options"][f"{dialect_name}_include"],
  783. )