Add support for setting migrations

Signed-off-by: Roberto Rosario <roberto.rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2019-11-18 00:27:33 -04:00
parent c75033f676
commit fa8fddb349
8 changed files with 224 additions and 30 deletions
@@ -5,3 +5,8 @@ ENVIRONMENT_TEST_VALUE = '999999'
TEST_NAMESPACE_LABEL = 'test namespace label'
TEST_NAMESPACE_NAME = 'test_namespace_name'
TEST_SETTING_GLOBAL_NAME = 'SMART_SETTINGS_TEST_SETTING'
TEST_SETTING_INITIAL_VALUE = 'test_setting_initial_value'
TEST_SETTING_DEFAULT_VALUE = 'test_setting_default_value'
TEST_SETTING_VALUE = 'test_setting_value'
+16 -3
View File
@@ -2,7 +2,10 @@ from __future__ import absolute_import, unicode_literals
from ..classes import Namespace
from .literals import TEST_NAMESPACE_LABEL, TEST_NAMESPACE_NAME
from .literals import (
TEST_NAMESPACE_LABEL, TEST_NAMESPACE_NAME, TEST_SETTING_DEFAULT_VALUE,
TEST_SETTING_GLOBAL_NAME
)
class SmartSettingsTestCaseMixin(object):
@@ -12,16 +15,26 @@ class SmartSettingsTestCaseMixin(object):
class SmartSettingTestMixin(object):
def _create_test_settings_namespace(self):
def _create_test_settings_namespace(self, **kwargs):
try:
self.test_settings_namespace = Namespace.get(
name=TEST_NAMESPACE_NAME
)
self.test_settings_namespace.migration_class = None
self.test_settings_namespace.version = None
self.test_settings_namespace.__dict__.update(kwargs)
except KeyError:
self.test_settings_namespace = Namespace(
label=TEST_NAMESPACE_LABEL, name=TEST_NAMESPACE_NAME
label=TEST_NAMESPACE_LABEL, name=TEST_NAMESPACE_NAME,
**kwargs
)
def _create_test_setting(self):
self.test_setting = self.test_settings_namespace.add_setting(
global_name=TEST_SETTING_GLOBAL_NAME,
default=TEST_SETTING_DEFAULT_VALUE
)
class SmartSettingViewTestMixin(object):
def _request_namespace_list_view(self):
+16
View File
@@ -0,0 +1,16 @@
from __future__ import absolute_import, unicode_literals
from ..classes import NamespaceMigration
class TestNamespaceMigrationOne(NamespaceMigration):
def smart_settings_test_setting_0001(self, value):
return '{}_0001'.format(value)
class TestNamespaceMigrationTwo(NamespaceMigration):
def smart_settings_test_setting_0001(self, value):
return '{}_0001'.format(value)
def smart_settings_test_setting_0002(self, value):
return '{}_0002'.format(value)
@@ -1,27 +1,32 @@
from __future__ import absolute_import, unicode_literals
import os
from pathlib2 import Path
from django.conf import settings
from django.utils.encoding import force_text
from django.utils.encoding import force_bytes, force_text
from mayan.apps.common.mixins import EnvironmentTestCaseMixin
from mayan.apps.common.settings import setting_paginate_by
from mayan.apps.common.tests.base import BaseTestCase
from mayan.apps.storage.utils import fs_cleanup
from mayan.apps.storage.utils import fs_cleanup, NamedTemporaryFile
from ..classes import Setting
from .literals import ENVIRONMENT_TEST_NAME, ENVIRONMENT_TEST_VALUE
from .literals import (
ENVIRONMENT_TEST_NAME, ENVIRONMENT_TEST_VALUE, TEST_SETTING_GLOBAL_NAME,
TEST_SETTING_INITIAL_VALUE, TEST_SETTING_VALUE
)
from .mixins import SmartSettingTestMixin
from .mocks import TestNamespaceMigrationOne, TestNamespaceMigrationTwo
class ClassesTestCase(SmartSettingTestMixin, BaseTestCase):
class ClassesTestCase(EnvironmentTestCaseMixin, SmartSettingTestMixin, BaseTestCase):
def test_environment_variable(self):
os.environ[
'MAYAN_{}'.format(ENVIRONMENT_TEST_NAME)
] = ENVIRONMENT_TEST_VALUE
self._set_environment_variable(
name='MAYAN_{}'.format(ENVIRONMENT_TEST_NAME),
value=ENVIRONMENT_TEST_VALUE
)
self.assertTrue(setting_paginate_by.value, ENVIRONMENT_TEST_VALUE)
def test_config_backup_creation(self):
@@ -53,3 +58,59 @@ class ClassesTestCase(SmartSettingTestMixin, BaseTestCase):
self.assertFalse(Setting.check_changed())
test_setting.value = 'test value edited'
self.assertTrue(Setting.check_changed())
class NamespaceMigrationTestCase(
EnvironmentTestCaseMixin, SmartSettingTestMixin, BaseTestCase
):
def test_environment_migration(self):
self._set_environment_variable(
name='MAYAN_{}'.format(TEST_SETTING_GLOBAL_NAME),
value=TEST_SETTING_INITIAL_VALUE
)
self._create_test_settings_namespace(
migration_class=TestNamespaceMigrationOne, version='0002'
)
self._create_test_setting()
self.assertEqual(
self.test_setting.value, TEST_SETTING_INITIAL_VALUE
)
def test_migration_0001_to_0002(self):
self._create_test_settings_namespace(
migration_class=TestNamespaceMigrationTwo, version='0002'
)
self._create_test_setting()
with NamedTemporaryFile() as file_object:
settings.CONFIGURATION_FILEPATH = file_object.name
file_object.write(
force_bytes(
'{}: {}'.format(TEST_SETTING_GLOBAL_NAME, TEST_SETTING_VALUE)
)
)
file_object.seek(0)
self.assertEqual(
self.test_setting.value, '{}_0001'.format(TEST_SETTING_VALUE)
)
def test_migration_0001_to_0003(self):
self._create_test_settings_namespace(
migration_class=TestNamespaceMigrationTwo, version='0003'
)
self._create_test_setting()
with NamedTemporaryFile() as file_object:
settings.CONFIGURATION_FILEPATH = file_object.name
file_object.write(
force_bytes(
'{}: {}'.format(TEST_SETTING_GLOBAL_NAME, TEST_SETTING_VALUE)
)
)
file_object.seek(0)
self.assertEqual(
self.test_setting.value, '{}_0001_0002'.format(TEST_SETTING_VALUE)
)