You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

210 lines
6.4KB

  1. # testing/config.py
  2. # Copyright (C) 2005-2021 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: http://www.opensource.org/licenses/mit-license.php
  7. import collections
  8. from .. import util
  9. requirements = None
  10. db = None
  11. db_url = None
  12. db_opts = None
  13. file_config = None
  14. test_schema = None
  15. test_schema_2 = None
  16. any_async = False
  17. _current = None
  18. ident = "main"
  19. _fixture_functions = None # installed by plugin_base
  20. def combinations(*comb, **kw):
  21. r"""Deliver multiple versions of a test based on positional combinations.
  22. This is a facade over pytest.mark.parametrize.
  23. :param \*comb: argument combinations. These are tuples that will be passed
  24. positionally to the decorated function.
  25. :param argnames: optional list of argument names. These are the names
  26. of the arguments in the test function that correspond to the entries
  27. in each argument tuple. pytest.mark.parametrize requires this, however
  28. the combinations function will derive it automatically if not present
  29. by using ``inspect.getfullargspec(fn).args[1:]``. Note this assumes the
  30. first argument is "self" which is discarded.
  31. :param id\_: optional id template. This is a string template that
  32. describes how the "id" for each parameter set should be defined, if any.
  33. The number of characters in the template should match the number of
  34. entries in each argument tuple. Each character describes how the
  35. corresponding entry in the argument tuple should be handled, as far as
  36. whether or not it is included in the arguments passed to the function, as
  37. well as if it is included in the tokens used to create the id of the
  38. parameter set.
  39. If omitted, the argument combinations are passed to parametrize as is. If
  40. passed, each argument combination is turned into a pytest.param() object,
  41. mapping the elements of the argument tuple to produce an id based on a
  42. character value in the same position within the string template using the
  43. following scheme::
  44. i - the given argument is a string that is part of the id only, don't
  45. pass it as an argument
  46. n - the given argument should be passed and it should be added to the
  47. id by calling the .__name__ attribute
  48. r - the given argument should be passed and it should be added to the
  49. id by calling repr()
  50. s - the given argument should be passed and it should be added to the
  51. id by calling str()
  52. a - (argument) the given argument should be passed and it should not
  53. be used to generated the id
  54. e.g.::
  55. @testing.combinations(
  56. (operator.eq, "eq"),
  57. (operator.ne, "ne"),
  58. (operator.gt, "gt"),
  59. (operator.lt, "lt"),
  60. id_="na"
  61. )
  62. def test_operator(self, opfunc, name):
  63. pass
  64. The above combination will call ``.__name__`` on the first member of
  65. each tuple and use that as the "id" to pytest.param().
  66. """
  67. return _fixture_functions.combinations(*comb, **kw)
  68. def combinations_list(arg_iterable, **kw):
  69. "As combination, but takes a single iterable"
  70. return combinations(*arg_iterable, **kw)
  71. def fixture(*arg, **kw):
  72. return _fixture_functions.fixture(*arg, **kw)
  73. def get_current_test_name():
  74. return _fixture_functions.get_current_test_name()
  75. def mark_base_test_class():
  76. return _fixture_functions.mark_base_test_class()
  77. class Config(object):
  78. def __init__(self, db, db_opts, options, file_config):
  79. self._set_name(db)
  80. self.db = db
  81. self.db_opts = db_opts
  82. self.options = options
  83. self.file_config = file_config
  84. self.test_schema = "test_schema"
  85. self.test_schema_2 = "test_schema_2"
  86. self.is_async = db.dialect.is_async and not util.asbool(
  87. db.url.query.get("async_fallback", False)
  88. )
  89. _stack = collections.deque()
  90. _configs = set()
  91. def _set_name(self, db):
  92. if db.dialect.server_version_info:
  93. svi = ".".join(str(tok) for tok in db.dialect.server_version_info)
  94. self.name = "%s+%s_[%s]" % (db.name, db.driver, svi)
  95. else:
  96. self.name = "%s+%s" % (db.name, db.driver)
  97. @classmethod
  98. def register(cls, db, db_opts, options, file_config):
  99. """add a config as one of the global configs.
  100. If there are no configs set up yet, this config also
  101. gets set as the "_current".
  102. """
  103. global any_async
  104. cfg = Config(db, db_opts, options, file_config)
  105. # if any backends include an async driver, then ensure
  106. # all setup/teardown and tests are wrapped in the maybe_async()
  107. # decorator that will set up a greenlet context for async drivers.
  108. any_async = any_async or cfg.is_async
  109. cls._configs.add(cfg)
  110. return cfg
  111. @classmethod
  112. def set_as_current(cls, config, namespace):
  113. global db, _current, db_url, test_schema, test_schema_2, db_opts
  114. _current = config
  115. db_url = config.db.url
  116. db_opts = config.db_opts
  117. test_schema = config.test_schema
  118. test_schema_2 = config.test_schema_2
  119. namespace.db = db = config.db
  120. @classmethod
  121. def push_engine(cls, db, namespace):
  122. assert _current, "Can't push without a default Config set up"
  123. cls.push(
  124. Config(
  125. db, _current.db_opts, _current.options, _current.file_config
  126. ),
  127. namespace,
  128. )
  129. @classmethod
  130. def push(cls, config, namespace):
  131. cls._stack.append(_current)
  132. cls.set_as_current(config, namespace)
  133. @classmethod
  134. def pop(cls, namespace):
  135. if cls._stack:
  136. # a failed test w/ -x option can call reset() ahead of time
  137. _current = cls._stack[-1]
  138. del cls._stack[-1]
  139. cls.set_as_current(_current, namespace)
  140. @classmethod
  141. def reset(cls, namespace):
  142. if cls._stack:
  143. cls.set_as_current(cls._stack[0], namespace)
  144. cls._stack.clear()
  145. @classmethod
  146. def all_configs(cls):
  147. return cls._configs
  148. @classmethod
  149. def all_dbs(cls):
  150. for cfg in cls.all_configs():
  151. yield cfg.db
  152. def skip_test(self, msg):
  153. skip_test(msg)
  154. def skip_test(msg):
  155. raise _fixture_functions.skip_test_exception(msg)
  156. def async_test(fn):
  157. return _fixture_functions.async_test(fn)