test_migrationutils.py 4.6 KB

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