test_migrationutils.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from django.test import TestCase
  2. from misago.core import threadstore
  3. from misago.conf import migrationutils
  4. from misago.conf.models import SettingsGroup, Setting
  5. class MigrationUtilsTests(TestCase):
  6. serialized_rollback = True
  7. def test_with_conf_models(self):
  8. """with_conf_models builds correct dict of models"""
  9. models = {
  10. u'core.cacheversion': {
  11. 'Meta': {'object_name': 'CacheVersion'},
  12. 'cache': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
  13. u'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
  14. 'version': ('django.db.models.fields.PositiveIntegerField', [], {'default': '0'})
  15. }
  16. }
  17. final_models = migrationutils.with_conf_models('0001_initial')
  18. self.assertTrue('conf.settingsgroup' in final_models),
  19. self.assertTrue('conf.setting' in final_models),
  20. final_models = migrationutils.with_conf_models('0001_initial', models)
  21. self.assertTrue('conf.settingsgroup' in final_models),
  22. self.assertTrue('conf.setting' in final_models),
  23. self.assertTrue('core.cacheversion' in final_models),
  24. class DBConfMigrationUtilsTests(TestCase):
  25. serialized_rollback = True
  26. def setUp(self):
  27. self.orm = {
  28. 'conf.SettingsGroup': SettingsGroup,
  29. 'conf.Setting': Setting,
  30. }
  31. self.test_group = {
  32. 'key': 'test_group',
  33. 'name': "Test settings",
  34. 'description': "Those are test settings.",
  35. 'settings': (
  36. {
  37. 'setting': 'fish_name',
  38. 'name': "Fish's name",
  39. 'value': "Eric",
  40. 'field_extra': {
  41. 'min_length': 2,
  42. 'max_length': 255
  43. },
  44. },
  45. {
  46. 'setting': 'fish_license_no',
  47. 'name': "Fish's license number",
  48. 'default_value': '123-456',
  49. 'field_extra': {
  50. 'max_length': 255
  51. },
  52. },
  53. )
  54. }
  55. migrationutils.migrate_settings_group(self.orm, self.test_group)
  56. self.groups_count = SettingsGroup.objects.count()
  57. def tearDown(self):
  58. threadstore.clear()
  59. def test_get_custom_group_and_settings(self):
  60. """tests setup created settings group"""
  61. custom_group = migrationutils.get_group(self.orm,
  62. self.test_group['key'])
  63. self.assertEqual(custom_group.key, self.test_group['key'])
  64. self.assertEqual(custom_group.name, self.test_group['name'])
  65. self.assertEqual(custom_group.description,
  66. self.test_group['description'])
  67. custom_settings = migrationutils.get_custom_settings_values(
  68. self.orm, custom_group)
  69. self.assertEqual(custom_settings['fish_name'], 'Eric')
  70. self.assertTrue('fish_license_no' not in custom_settings)
  71. def test_change_group_key(self):
  72. """migrate_settings_group changed group key"""
  73. new_group = {
  74. 'key': 'new_test_group',
  75. 'name': "New test settings",
  76. 'description': "Those are updated test settings.",
  77. 'settings': (
  78. {
  79. 'setting': 'fish_new_name',
  80. 'name': "Fish's new name",
  81. 'value': "Eric",
  82. 'field_extra': {
  83. 'min_length': 2,
  84. 'max_length': 255
  85. },
  86. },
  87. {
  88. 'setting': 'fish_new_license_no',
  89. 'name': "Fish's changed license number",
  90. 'default_value': '123-456',
  91. 'field_extra': {
  92. 'max_length': 255
  93. },
  94. },
  95. )
  96. }
  97. migrationutils.migrate_settings_group(
  98. self.orm, new_group, old_group_key=self.test_group['key'])
  99. db_group = migrationutils.get_group(self.orm, new_group['key'])
  100. self.assertEqual(SettingsGroup.objects.count(), self.groups_count)
  101. self.assertEqual(db_group.key, new_group['key'])
  102. self.assertEqual(db_group.name, new_group['name'])
  103. self.assertEqual(db_group.description,
  104. new_group['description'])
  105. for setting in new_group['settings']:
  106. db_setting = db_group.setting_set.get(setting=setting['setting'])
  107. self.assertEqual(db_setting.name, setting['name'])