config.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. # testing/config.py
  2. # Copyright (C) 2005-2017 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. requirements = None
  9. db = None
  10. db_url = None
  11. db_opts = None
  12. file_config = None
  13. test_schema = None
  14. test_schema_2 = None
  15. _current = None
  16. try:
  17. from unittest import SkipTest as _skip_test_exception
  18. except ImportError:
  19. _skip_test_exception = None
  20. class Config(object):
  21. def __init__(self, db, db_opts, options, file_config):
  22. self.db = db
  23. self.db_opts = db_opts
  24. self.options = options
  25. self.file_config = file_config
  26. self.test_schema = "test_schema"
  27. self.test_schema_2 = "test_schema_2"
  28. _stack = collections.deque()
  29. _configs = {}
  30. @classmethod
  31. def register(cls, db, db_opts, options, file_config):
  32. """add a config as one of the global configs.
  33. If there are no configs set up yet, this config also
  34. gets set as the "_current".
  35. """
  36. cfg = Config(db, db_opts, options, file_config)
  37. cls._configs[cfg.db.name] = cfg
  38. cls._configs[(cfg.db.name, cfg.db.dialect)] = cfg
  39. cls._configs[cfg.db] = cfg
  40. return cfg
  41. @classmethod
  42. def set_as_current(cls, config, namespace):
  43. global db, _current, db_url, test_schema, test_schema_2, db_opts
  44. _current = config
  45. db_url = config.db.url
  46. db_opts = config.db_opts
  47. test_schema = config.test_schema
  48. test_schema_2 = config.test_schema_2
  49. namespace.db = db = config.db
  50. @classmethod
  51. def push_engine(cls, db, namespace):
  52. assert _current, "Can't push without a default Config set up"
  53. cls.push(
  54. Config(
  55. db, _current.db_opts, _current.options, _current.file_config),
  56. namespace
  57. )
  58. @classmethod
  59. def push(cls, config, namespace):
  60. cls._stack.append(_current)
  61. cls.set_as_current(config, namespace)
  62. @classmethod
  63. def reset(cls, namespace):
  64. if cls._stack:
  65. cls.set_as_current(cls._stack[0], namespace)
  66. cls._stack.clear()
  67. @classmethod
  68. def all_configs(cls):
  69. for cfg in set(cls._configs.values()):
  70. yield cfg
  71. @classmethod
  72. def all_dbs(cls):
  73. for cfg in cls.all_configs():
  74. yield cfg.db
  75. def skip_test(self, msg):
  76. skip_test(msg)
  77. def skip_test(msg):
  78. raise _skip_test_exception(msg)