test_migrationutils.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from django.test import TestCase
  2. from misago.conf import migrationutils
  3. from misago.conf.models import SettingsGroup, Setting
  4. class MigrationUtilsTests(TestCase):
  5. def test_with_conf_models(self):
  6. """with_conf_models builds correct dict of models"""
  7. models = {
  8. u'core.cacheversion': {
  9. 'Meta': {'object_name': 'CacheVersion'},
  10. 'cache': ('django.db.models.fields.CharField',
  11. [], {'max_length': '128'}),
  12. u'id': ('django.db.models.fields.AutoField',
  13. [], {'primary_key': 'True'}),
  14. 'version': ('django.db.models.fields.PositiveIntegerField',
  15. [], {'default': '0'})
  16. }
  17. }
  18. final_models = migrationutils.with_conf_models('0001_initial')
  19. self.assertTrue('conf.settingsgroup' in final_models),
  20. self.assertTrue('conf.setting' in final_models),
  21. final_models = migrationutils.with_conf_models('0001_initial', models)
  22. self.assertTrue('conf.settingsgroup' in final_models),
  23. self.assertTrue('conf.setting' in final_models),
  24. self.assertTrue('core.cacheversion' in final_models),
  25. class DBConfMigrationUtilsTests(TestCase):
  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 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'])