test_migrationutils.py 4.6 KB

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