fixtures.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import os
  2. from mako.cache import CacheImpl
  3. from mako.cache import register_plugin
  4. from mako.template import Template
  5. from .assertions import eq_
  6. from .config import config
  7. class TemplateTest:
  8. def _file_template(self, filename, **kw):
  9. filepath = self._file_path(filename)
  10. return Template(
  11. uri=filename,
  12. filename=filepath,
  13. module_directory=config.module_base,
  14. **kw,
  15. )
  16. def _file_path(self, filename):
  17. name, ext = os.path.splitext(filename)
  18. py3k_path = os.path.join(config.template_base, name + "_py3k" + ext)
  19. if os.path.exists(py3k_path):
  20. return py3k_path
  21. return os.path.join(config.template_base, filename)
  22. def _do_file_test(
  23. self,
  24. filename,
  25. expected,
  26. filters=None,
  27. unicode_=True,
  28. template_args=None,
  29. **kw,
  30. ):
  31. t1 = self._file_template(filename, **kw)
  32. self._do_test(
  33. t1,
  34. expected,
  35. filters=filters,
  36. unicode_=unicode_,
  37. template_args=template_args,
  38. )
  39. def _do_memory_test(
  40. self,
  41. source,
  42. expected,
  43. filters=None,
  44. unicode_=True,
  45. template_args=None,
  46. **kw,
  47. ):
  48. t1 = Template(text=source, **kw)
  49. self._do_test(
  50. t1,
  51. expected,
  52. filters=filters,
  53. unicode_=unicode_,
  54. template_args=template_args,
  55. )
  56. def _do_test(
  57. self,
  58. template,
  59. expected,
  60. filters=None,
  61. template_args=None,
  62. unicode_=True,
  63. ):
  64. if template_args is None:
  65. template_args = {}
  66. if unicode_:
  67. output = template.render_unicode(**template_args)
  68. else:
  69. output = template.render(**template_args)
  70. if filters:
  71. output = filters(output)
  72. eq_(output, expected)
  73. def indicates_unbound_local_error(self, rendered_output, unbound_var):
  74. var = f"'{unbound_var}'"
  75. error_msgs = (
  76. # < 3.11
  77. f"local variable {var} referenced before assignment",
  78. # >= 3.11
  79. f"cannot access local variable {var} where it is not associated",
  80. )
  81. return any((msg in rendered_output) for msg in error_msgs)
  82. class PlainCacheImpl(CacheImpl):
  83. """Simple memory cache impl so that tests which
  84. use caching can run without beaker."""
  85. def __init__(self, cache):
  86. self.cache = cache
  87. self.data = {}
  88. def get_or_create(self, key, creation_function, **kw):
  89. if key in self.data:
  90. return self.data[key]
  91. else:
  92. self.data[key] = data = creation_function(**kw)
  93. return data
  94. def put(self, key, value, **kw):
  95. self.data[key] = value
  96. def get(self, key, **kw):
  97. return self.data[key]
  98. def invalidate(self, key, **kw):
  99. del self.data[key]
  100. register_plugin("plain", __name__, "PlainCacheImpl")