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