test_settings.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. from django.apps import apps
  2. from django.conf import settings as dj_settings
  3. from django.test import TestCase, override_settings
  4. from misago.core import threadstore
  5. from misago.core.cache import cache
  6. from misago.conf import defaults
  7. from misago.conf.dbsettings import db_settings
  8. from misago.conf.gateway import settings as gateway
  9. from misago.conf.migrationutils import migrate_settings_group
  10. class DBSettingsTests(TestCase):
  11. def test_get_existing_setting(self):
  12. """forum_name is defined"""
  13. self.assertEqual(db_settings.forum_name, 'Misago')
  14. with self.assertRaises(AttributeError):
  15. db_settings.MISAGO_THREADS_PER_PAGE
  16. class GatewaySettingsTests(TestCase):
  17. def tearDown(self):
  18. cache.clear()
  19. threadstore.clear()
  20. def test_get_existing_setting(self):
  21. """forum_name is defined"""
  22. self.assertEqual(gateway.forum_name, db_settings.forum_name)
  23. self.assertEqual(gateway.INSTALLED_APPS,
  24. dj_settings.INSTALLED_APPS)
  25. self.assertEqual(gateway.MISAGO_THREADS_PER_PAGE,
  26. defaults.MISAGO_THREADS_PER_PAGE)
  27. with self.assertRaises(AttributeError):
  28. gateway.LoremIpsum
  29. @override_settings(MISAGO_THREADS_PER_PAGE=1234)
  30. def test_override_file_setting(self):
  31. """file settings are overrideable"""
  32. self.assertEqual(gateway.MISAGO_THREADS_PER_PAGE, 1234)
  33. def test_setting_public(self):
  34. """get_public_settings returns public settings"""
  35. test_group = {
  36. 'key': 'test_group',
  37. 'name': "Test settings",
  38. 'description': "Those are test settings.",
  39. 'settings': (
  40. {
  41. 'setting': 'fish_name',
  42. 'name': "Fish's name",
  43. 'value': "Public Eric",
  44. 'field_extra': {
  45. 'min_length': 2,
  46. 'max_length': 255
  47. },
  48. 'is_public': True
  49. },
  50. {
  51. 'setting': 'private_fish_name',
  52. 'name': "Fish's name",
  53. 'value': "Private Eric",
  54. 'field_extra': {
  55. 'min_length': 2,
  56. 'max_length': 255
  57. },
  58. 'is_public': False
  59. },
  60. )
  61. }
  62. migrate_settings_group(apps, test_group)
  63. self.assertEqual(gateway.fish_name, 'Public Eric')
  64. self.assertEqual(gateway.private_fish_name, 'Private Eric')
  65. public_settings = gateway.get_public_settings().keys()
  66. self.assertIn('fish_name', public_settings)
  67. self.assertNotIn('private_fish_name', public_settings)
  68. def test_setting_lazy(self):
  69. """lazy settings work"""
  70. test_group = {
  71. 'key': 'test_group',
  72. 'name': "Test settings",
  73. 'description': "Those are test settings.",
  74. 'settings': (
  75. {
  76. 'setting': 'fish_name',
  77. 'name': "Fish's name",
  78. 'value': "Greedy Eric",
  79. 'field_extra': {
  80. 'min_length': 2,
  81. 'max_length': 255
  82. },
  83. 'is_lazy': False
  84. },
  85. {
  86. 'setting': 'lazy_fish_name',
  87. 'name': "Fish's name",
  88. 'value': "Lazy Eric",
  89. 'field_extra': {
  90. 'min_length': 2,
  91. 'max_length': 255
  92. },
  93. 'is_lazy': True
  94. },
  95. {
  96. 'setting': 'lazy_empty_setting',
  97. 'name': "Fish's name",
  98. 'field_extra': {
  99. 'min_length': 2,
  100. 'max_length': 255
  101. },
  102. 'is_lazy': True
  103. },
  104. )
  105. }
  106. migrate_settings_group(apps, test_group)
  107. self.assertTrue(gateway.lazy_fish_name)
  108. self.assertTrue(db_settings.lazy_fish_name)
  109. self.assertTrue(gateway.lazy_fish_name)
  110. self.assertEqual(
  111. gateway.get_lazy_setting('lazy_fish_name'), 'Lazy Eric')
  112. self.assertTrue(db_settings.lazy_fish_name)
  113. self.assertEqual(
  114. db_settings.get_lazy_setting('lazy_fish_name'), 'Lazy Eric')
  115. self.assertTrue(gateway.lazy_empty_setting is None)
  116. self.assertTrue(db_settings.lazy_empty_setting is None)
  117. with self.assertRaises(ValueError):
  118. db_settings.get_lazy_setting('fish_name')