config.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. # testing/config.py
  2. # Copyright (C) 2005-2024 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. i - the given argument is a string that is part of the id only, don't
  98. pass it as an argument
  99. n - the given argument should be passed and it should be added to the
  100. id by calling the .__name__ attribute
  101. r - the given argument should be passed and it should be added to the
  102. id by calling repr()
  103. s - the given argument should be passed and it should be added to the
  104. id by calling str()
  105. a - (argument) the given argument should be passed and it should not
  106. be used to generated the id
  107. e.g.::
  108. @testing.combinations(
  109. (operator.eq, "eq"),
  110. (operator.ne, "ne"),
  111. (operator.gt, "gt"),
  112. (operator.lt, "lt"),
  113. id_="na"
  114. )
  115. def test_operator(self, opfunc, name):
  116. pass
  117. The above combination will call ``.__name__`` on the first member of
  118. each tuple and use that as the "id" to pytest.param().
  119. """
  120. return _fixture_functions.combinations(
  121. *comb, id_=id_, argnames=argnames, **kw
  122. )
  123. def combinations_list(arg_iterable: Iterable[Tuple[Any, ...]], **kw):
  124. "As combination, but takes a single iterable"
  125. return combinations(*arg_iterable, **kw)
  126. class Variation:
  127. __slots__ = ("_name", "_argname")
  128. def __init__(self, case, argname, case_names):
  129. self._name = case
  130. self._argname = argname
  131. for casename in case_names:
  132. setattr(self, casename, casename == case)
  133. if typing.TYPE_CHECKING:
  134. def __getattr__(self, key: str) -> bool:
  135. ...
  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(
  173. self,
  174. querytyp,
  175. lazy,
  176. decl_base
  177. ):
  178. class Thing(decl_base):
  179. __tablename__ = 'thing'
  180. # use name directly
  181. rel = relationship("Rel", lazy=lazy.name)
  182. # use as a switch
  183. if querytyp.select:
  184. stmt = select(Thing)
  185. elif querytyp.subquery:
  186. stmt = select(Thing).subquery()
  187. elif querytyp.legacy_query:
  188. stmt = Session.query(Thing)
  189. else:
  190. querytyp.fail()
  191. The variable provided is a slots object of boolean variables, as well
  192. as the name of the case itself under the attribute ".name"
  193. """
  194. if inspect.isfunction(argname_or_fn):
  195. argname = argname_or_fn.__name__
  196. cases = argname_or_fn(None)
  197. @variation_fixture(argname, cases)
  198. def go(self, request):
  199. yield request.param
  200. return go
  201. else:
  202. argname = argname_or_fn
  203. cases_plus_limitations = [
  204. entry
  205. if (isinstance(entry, tuple) and len(entry) == 2)
  206. else (entry, None)
  207. for entry in cases
  208. ]
  209. variations = Variation.generate_cases(
  210. argname, [c for c, l in cases_plus_limitations]
  211. )
  212. return combinations(
  213. *[
  214. (variation._name, variation, limitation)
  215. if limitation is not None
  216. else (variation._name, variation)
  217. for variation, (case, limitation) in zip(
  218. variations, cases_plus_limitations
  219. )
  220. ],
  221. id_="ia",
  222. argnames=argname,
  223. )
  224. def variation_fixture(argname, cases, scope="function"):
  225. return fixture(
  226. params=Variation.generate_cases(argname, cases),
  227. ids=Variation.idfn,
  228. scope=scope,
  229. )
  230. def fixture(*arg: Any, **kw: Any) -> Any:
  231. return _fixture_functions.fixture(*arg, **kw)
  232. def get_current_test_name() -> str:
  233. return _fixture_functions.get_current_test_name()
  234. def mark_base_test_class() -> Any:
  235. return _fixture_functions.mark_base_test_class()
  236. class _AddToMarker:
  237. def __getattr__(self, attr: str) -> Any:
  238. return getattr(_fixture_functions.add_to_marker, attr)
  239. add_to_marker = _AddToMarker()
  240. class Config:
  241. def __init__(self, db, db_opts, options, file_config):
  242. self._set_name(db)
  243. self.db = db
  244. self.db_opts = db_opts
  245. self.options = options
  246. self.file_config = file_config
  247. self.test_schema = "test_schema"
  248. self.test_schema_2 = "test_schema_2"
  249. self.is_async = db.dialect.is_async and not util.asbool(
  250. db.url.query.get("async_fallback", False)
  251. )
  252. _stack = collections.deque()
  253. _configs = set()
  254. def _set_name(self, db):
  255. suffix = "_async" if db.dialect.is_async else ""
  256. if db.dialect.server_version_info:
  257. svi = ".".join(str(tok) for tok in db.dialect.server_version_info)
  258. self.name = "%s+%s%s_[%s]" % (db.name, db.driver, suffix, svi)
  259. else:
  260. self.name = "%s+%s%s" % (db.name, db.driver, suffix)
  261. @classmethod
  262. def register(cls, db, db_opts, options, file_config):
  263. """add a config as one of the global configs.
  264. If there are no configs set up yet, this config also
  265. gets set as the "_current".
  266. """
  267. global any_async
  268. cfg = Config(db, db_opts, options, file_config)
  269. # if any backends include an async driver, then ensure
  270. # all setup/teardown and tests are wrapped in the maybe_async()
  271. # decorator that will set up a greenlet context for async drivers.
  272. any_async = any_async or cfg.is_async
  273. cls._configs.add(cfg)
  274. return cfg
  275. @classmethod
  276. def set_as_current(cls, config, namespace):
  277. global db, _current, db_url, test_schema, test_schema_2, db_opts
  278. _current = config
  279. db_url = config.db.url
  280. db_opts = config.db_opts
  281. test_schema = config.test_schema
  282. test_schema_2 = config.test_schema_2
  283. namespace.db = db = config.db
  284. @classmethod
  285. def push_engine(cls, db, namespace):
  286. assert _current, "Can't push without a default Config set up"
  287. cls.push(
  288. Config(
  289. db, _current.db_opts, _current.options, _current.file_config
  290. ),
  291. namespace,
  292. )
  293. @classmethod
  294. def push(cls, config, namespace):
  295. cls._stack.append(_current)
  296. cls.set_as_current(config, namespace)
  297. @classmethod
  298. def pop(cls, namespace):
  299. if cls._stack:
  300. # a failed test w/ -x option can call reset() ahead of time
  301. _current = cls._stack[-1]
  302. del cls._stack[-1]
  303. cls.set_as_current(_current, namespace)
  304. @classmethod
  305. def reset(cls, namespace):
  306. if cls._stack:
  307. cls.set_as_current(cls._stack[0], namespace)
  308. cls._stack.clear()
  309. @classmethod
  310. def all_configs(cls):
  311. return cls._configs
  312. @classmethod
  313. def all_dbs(cls):
  314. for cfg in cls.all_configs():
  315. yield cfg.db
  316. def skip_test(self, msg):
  317. skip_test(msg)
  318. def skip_test(msg):
  319. raise _fixture_functions.skip_test_exception(msg)
  320. def async_test(fn):
  321. return _fixture_functions.async_test(fn)