config.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. # testing/config.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 argparse import Namespace
  10. import collections
  11. import inspect
  12. import typing
  13. from typing import Any
  14. from typing import Callable
  15. from typing import Iterable
  16. from typing import NoReturn
  17. from typing import Optional
  18. from typing import Tuple
  19. from typing import TypeVar
  20. from typing import Union
  21. from . import mock
  22. from . import requirements as _requirements
  23. from .util import fail
  24. from .. import util
  25. # default requirements; this is replaced by plugin_base when pytest
  26. # is run
  27. requirements = _requirements.SuiteRequirements()
  28. db = None
  29. db_url = None
  30. db_opts = None
  31. file_config = None
  32. test_schema = None
  33. test_schema_2 = None
  34. any_async = False
  35. _current = None
  36. ident = "main"
  37. options: Namespace = None # type: ignore
  38. if typing.TYPE_CHECKING:
  39. from .plugin.plugin_base import FixtureFunctions
  40. _fixture_functions: FixtureFunctions
  41. else:
  42. class _NullFixtureFunctions:
  43. def _null_decorator(self):
  44. def go(fn):
  45. return fn
  46. return go
  47. def skip_test_exception(self, *arg, **kw):
  48. return Exception()
  49. @property
  50. def add_to_marker(self):
  51. return mock.Mock()
  52. def mark_base_test_class(self):
  53. return self._null_decorator()
  54. def combinations(self, *arg_sets, **kw):
  55. return self._null_decorator()
  56. def param_ident(self, *parameters):
  57. return self._null_decorator()
  58. def fixture(self, *arg, **kw):
  59. return self._null_decorator()
  60. def get_current_test_name(self):
  61. return None
  62. def async_test(self, fn):
  63. return fn
  64. # default fixture functions; these are replaced by plugin_base when
  65. # pytest runs
  66. _fixture_functions = _NullFixtureFunctions()
  67. _FN = TypeVar("_FN", bound=Callable[..., Any])
  68. def combinations(
  69. *comb: Union[Any, Tuple[Any, ...]],
  70. argnames: Optional[str] = None,
  71. id_: Optional[str] = None,
  72. **kw: str,
  73. ) -> Callable[[_FN], _FN]:
  74. r"""Deliver multiple versions of a test based on positional combinations.
  75. This is a facade over pytest.mark.parametrize.
  76. :param \*comb: argument combinations. These are tuples that will be passed
  77. positionally to the decorated function.
  78. :param argnames: optional list of argument names. These are the names
  79. of the arguments in the test function that correspond to the entries
  80. in each argument tuple. pytest.mark.parametrize requires this, however
  81. the combinations function will derive it automatically if not present
  82. by using ``inspect.getfullargspec(fn).args[1:]``. Note this assumes the
  83. first argument is "self" which is discarded.
  84. :param id\_: optional id template. This is a string template that
  85. describes how the "id" for each parameter set should be defined, if any.
  86. The number of characters in the template should match the number of
  87. entries in each argument tuple. Each character describes how the
  88. corresponding entry in the argument tuple should be handled, as far as
  89. whether or not it is included in the arguments passed to the function, as
  90. well as if it is included in the tokens used to create the id of the
  91. parameter set.
  92. If omitted, the argument combinations are passed to parametrize as is. If
  93. passed, each argument combination is turned into a pytest.param() object,
  94. mapping the elements of the argument tuple to produce an id based on a
  95. character value in the same position within the string template using the
  96. following scheme:
  97. .. sourcecode:: text
  98. i - the given argument is a string that is part of the id only, don't
  99. pass it as an argument
  100. n - the given argument should be passed and it should be added to the
  101. id by calling the .__name__ attribute
  102. r - the given argument should be passed and it should be added to the
  103. id by calling repr()
  104. s - the given argument should be passed and it should be added to the
  105. id by calling str()
  106. a - (argument) the given argument should be passed and it should not
  107. be used to generated the id
  108. e.g.::
  109. @testing.combinations(
  110. (operator.eq, "eq"),
  111. (operator.ne, "ne"),
  112. (operator.gt, "gt"),
  113. (operator.lt, "lt"),
  114. id_="na",
  115. )
  116. def test_operator(self, opfunc, name):
  117. pass
  118. The above combination will call ``.__name__`` on the first member of
  119. each tuple and use that as the "id" to pytest.param().
  120. """
  121. return _fixture_functions.combinations(
  122. *comb, id_=id_, argnames=argnames, **kw
  123. )
  124. def combinations_list(arg_iterable: Iterable[Tuple[Any, ...]], **kw):
  125. "As combination, but takes a single iterable"
  126. return combinations(*arg_iterable, **kw)
  127. class Variation:
  128. __slots__ = ("_name", "_argname")
  129. def __init__(self, case, argname, case_names):
  130. self._name = case
  131. self._argname = argname
  132. for casename in case_names:
  133. setattr(self, casename, casename == case)
  134. if typing.TYPE_CHECKING:
  135. def __getattr__(self, key: str) -> bool: ...
  136. @property
  137. def name(self):
  138. return self._name
  139. def __bool__(self):
  140. return self._name == self._argname
  141. def __nonzero__(self):
  142. return not self.__bool__()
  143. def __str__(self):
  144. return f"{self._argname}={self._name!r}"
  145. def __repr__(self):
  146. return str(self)
  147. def fail(self) -> NoReturn:
  148. fail(f"Unknown {self}")
  149. @classmethod
  150. def idfn(cls, variation):
  151. return variation.name
  152. @classmethod
  153. def generate_cases(cls, argname, cases):
  154. case_names = [
  155. argname if c is True else "not_" + argname if c is False else c
  156. for c in cases
  157. ]
  158. typ = type(
  159. argname,
  160. (Variation,),
  161. {
  162. "__slots__": tuple(case_names),
  163. },
  164. )
  165. return [typ(casename, argname, case_names) for casename in case_names]
  166. def variation(argname_or_fn, cases=None):
  167. """a helper around testing.combinations that provides a single namespace
  168. that can be used as a switch.
  169. e.g.::
  170. @testing.variation("querytyp", ["select", "subquery", "legacy_query"])
  171. @testing.variation("lazy", ["select", "raise", "raise_on_sql"])
  172. def test_thing(self, querytyp, lazy, decl_base):
  173. class Thing(decl_base):
  174. __tablename__ = "thing"
  175. # use name directly
  176. rel = relationship("Rel", lazy=lazy.name)
  177. # use as a switch
  178. if querytyp.select:
  179. stmt = select(Thing)
  180. elif querytyp.subquery:
  181. stmt = select(Thing).subquery()
  182. elif querytyp.legacy_query:
  183. stmt = Session.query(Thing)
  184. else:
  185. querytyp.fail()
  186. The variable provided is a slots object of boolean variables, as well
  187. as the name of the case itself under the attribute ".name"
  188. """
  189. if inspect.isfunction(argname_or_fn):
  190. argname = argname_or_fn.__name__
  191. cases = argname_or_fn(None)
  192. @variation_fixture(argname, cases)
  193. def go(self, request):
  194. yield request.param
  195. return go
  196. else:
  197. argname = argname_or_fn
  198. cases_plus_limitations = [
  199. (
  200. entry
  201. if (isinstance(entry, tuple) and len(entry) == 2)
  202. else (entry, None)
  203. )
  204. for entry in cases
  205. ]
  206. variations = Variation.generate_cases(
  207. argname, [c for c, l in cases_plus_limitations]
  208. )
  209. return combinations(
  210. *[
  211. (
  212. (variation._name, variation, limitation)
  213. if limitation is not None
  214. else (variation._name, variation)
  215. )
  216. for variation, (case, limitation) in zip(
  217. variations, cases_plus_limitations
  218. )
  219. ],
  220. id_="ia",
  221. argnames=argname,
  222. )
  223. def variation_fixture(argname, cases, scope="function"):
  224. return fixture(
  225. params=Variation.generate_cases(argname, cases),
  226. ids=Variation.idfn,
  227. scope=scope,
  228. )
  229. def fixture(*arg: Any, **kw: Any) -> Any:
  230. return _fixture_functions.fixture(*arg, **kw)
  231. def get_current_test_name() -> str:
  232. return _fixture_functions.get_current_test_name()
  233. def mark_base_test_class() -> Any:
  234. return _fixture_functions.mark_base_test_class()
  235. class _AddToMarker:
  236. def __getattr__(self, attr: str) -> Any:
  237. return getattr(_fixture_functions.add_to_marker, attr)
  238. add_to_marker = _AddToMarker()
  239. class Config:
  240. def __init__(self, db, db_opts, options, file_config):
  241. self._set_name(db)
  242. self.db = db
  243. self.db_opts = db_opts
  244. self.options = options
  245. self.file_config = file_config
  246. self.test_schema = "test_schema"
  247. self.test_schema_2 = "test_schema_2"
  248. self.is_async = db.dialect.is_async and not util.asbool(
  249. db.url.query.get("async_fallback", False)
  250. )
  251. _stack = collections.deque()
  252. _configs = set()
  253. def _set_name(self, db):
  254. suffix = "_async" if db.dialect.is_async else ""
  255. if db.dialect.server_version_info:
  256. svi = ".".join(str(tok) for tok in db.dialect.server_version_info)
  257. self.name = "%s+%s%s_[%s]" % (db.name, db.driver, suffix, svi)
  258. else:
  259. self.name = "%s+%s%s" % (db.name, db.driver, suffix)
  260. @classmethod
  261. def register(cls, db, db_opts, options, file_config):
  262. """add a config as one of the global configs.
  263. If there are no configs set up yet, this config also
  264. gets set as the "_current".
  265. """
  266. global any_async
  267. cfg = Config(db, db_opts, options, file_config)
  268. # if any backends include an async driver, then ensure
  269. # all setup/teardown and tests are wrapped in the maybe_async()
  270. # decorator that will set up a greenlet context for async drivers.
  271. any_async = any_async or cfg.is_async
  272. cls._configs.add(cfg)
  273. return cfg
  274. @classmethod
  275. def set_as_current(cls, config, namespace):
  276. global db, _current, db_url, test_schema, test_schema_2, db_opts
  277. _current = config
  278. db_url = config.db.url
  279. db_opts = config.db_opts
  280. test_schema = config.test_schema
  281. test_schema_2 = config.test_schema_2
  282. namespace.db = db = config.db
  283. @classmethod
  284. def push_engine(cls, db, namespace):
  285. assert _current, "Can't push without a default Config set up"
  286. cls.push(
  287. Config(
  288. db, _current.db_opts, _current.options, _current.file_config
  289. ),
  290. namespace,
  291. )
  292. @classmethod
  293. def push(cls, config, namespace):
  294. cls._stack.append(_current)
  295. cls.set_as_current(config, namespace)
  296. @classmethod
  297. def pop(cls, namespace):
  298. if cls._stack:
  299. # a failed test w/ -x option can call reset() ahead of time
  300. _current = cls._stack[-1]
  301. del cls._stack[-1]
  302. cls.set_as_current(_current, namespace)
  303. @classmethod
  304. def reset(cls, namespace):
  305. if cls._stack:
  306. cls.set_as_current(cls._stack[0], namespace)
  307. cls._stack.clear()
  308. @classmethod
  309. def all_configs(cls):
  310. return cls._configs
  311. @classmethod
  312. def all_dbs(cls):
  313. for cfg in cls.all_configs():
  314. yield cfg.db
  315. def skip_test(self, msg):
  316. skip_test(msg)
  317. def skip_test(msg):
  318. raise _fixture_functions.skip_test_exception(msg)
  319. def async_test(fn):
  320. return _fixture_functions.async_test(fn)