tests.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. from unittest import mock
  2. from django.conf import settings
  3. from sentry.testutils import TestCase
  4. from sentry.utils.settings import ConfigurationError, import_string, validate_settings
  5. DEPENDENCY_TEST_DATA = {
  6. "postgresql": (
  7. "DATABASES",
  8. "psycopg2.extensions",
  9. "database engine",
  10. "django.db.backends.postgresql_psycopg2",
  11. {
  12. "default": {
  13. "ENGINE": "django.db.backends.postgresql_psycopg2",
  14. "NAME": "test",
  15. "USER": "root",
  16. "PASSWORD": "",
  17. "HOST": "127.0.0.1",
  18. "PORT": "",
  19. }
  20. },
  21. ),
  22. "memcache": (
  23. "CACHES",
  24. "memcache",
  25. "caching backend",
  26. "django.core.cache.backends.memcached.MemcachedCache",
  27. {
  28. "default": {
  29. "BACKEND": "django.core.cache.backends.memcached.MemcachedCache",
  30. "LOCATION": "127.0.0.1:11211",
  31. }
  32. },
  33. ),
  34. "pylibmc": (
  35. "CACHES",
  36. "pylibmc",
  37. "caching backend",
  38. "django.core.cache.backends.memcached.PyLibMCCache",
  39. {
  40. "default": {
  41. "BACKEND": "django.core.cache.backends.memcached.PyLibMCCache",
  42. "LOCATION": "127.0.0.1:11211",
  43. }
  44. },
  45. ),
  46. }
  47. class DependencyTest(TestCase):
  48. def raise_import_error(self, package):
  49. def callable(package_name):
  50. if package_name != package:
  51. return import_string(package_name)
  52. raise ImportError(f"No module named {package}")
  53. return callable
  54. @mock.patch("django.conf.settings", mock.Mock())
  55. @mock.patch("sentry.utils.settings.import_string")
  56. def validate_dependency(
  57. self, key, package, dependency_type, dependency, setting_value, import_string
  58. ):
  59. import_string.side_effect = self.raise_import_error(package)
  60. with self.settings(**{key: setting_value}):
  61. with self.assertRaises(ConfigurationError):
  62. validate_settings(settings)
  63. def test_validate_fails_on_postgres(self):
  64. self.validate_dependency(*DEPENDENCY_TEST_DATA["postgresql"])
  65. def test_validate_fails_on_memcache(self):
  66. self.validate_dependency(*DEPENDENCY_TEST_DATA["memcache"])
  67. def test_validate_fails_on_pylibmc(self):
  68. self.validate_dependency(*DEPENDENCY_TEST_DATA["pylibmc"])