OpenSlides/tests/old/config/test_config.py

155 lines
5.1 KiB
Python
Raw Normal View History

from openslides.core.config import ConfigVariable, config
2018-09-01 08:00:00 +02:00
from openslides.core.exceptions import ConfigError
from openslides.utils.test import TestCase
class TTestConfigException(Exception):
pass
class HandleConfigTest(TestCase):
def setUp(self):
# Save the old value of the config object and add the test values
# TODO: Can be changed to setUpClass when Django 1.8 is no longer supported
self._config_values = config.config_variables.copy()
2016-06-08 22:18:35 +02:00
config.update_config_variables(set_grouped_config_view())
config.update_config_variables(set_simple_config_view())
config.update_config_variables(set_simple_config_view_multiple_vars())
config.update_config_variables(set_simple_config_collection_disabled_view())
config.update_config_variables(set_simple_config_collection_with_callback())
2017-08-22 14:17:20 +02:00
config.save_default_values()
def tearDown(self):
# Reset the config variables
config.config_variables = self._config_values
2017-08-22 14:17:20 +02:00
super().tearDown()
def get_config_var(self, key):
return config[key]
def set_config_var(self, key, value):
config[key] = value
def test_get_multiple_config_var_error(self):
with self.assertRaisesMessage(
2019-01-06 16:22:33 +01:00
ConfigError,
"Too many values for config variables {'multiple_config_var'} found.",
):
2016-06-08 22:18:35 +02:00
config.update_config_variables(set_simple_config_view_multiple_vars())
def test_setup_config_var(self):
self.assertRaises(TypeError, ConfigVariable)
2019-01-06 16:22:33 +01:00
self.assertRaises(TypeError, ConfigVariable, name="foo")
self.assertRaises(TypeError, ConfigVariable, default_value="foo")
2017-08-22 14:17:20 +02:00
def test_config_exists(self):
2019-01-06 16:22:33 +01:00
self.assertTrue(config.exists("string_var"))
self.assertFalse(config.exists("unknown_config_var"))
def test_set_value_before_getting_it(self):
"""
2015-06-16 14:03:42 +02:00
Try to call __setitem__ before __getitem__.
"""
2019-01-06 16:22:33 +01:00
config["additional_config_var"] = "value"
def test_on_change(self):
"""
Tests that the special callback is called and raises a special
message.
"""
with self.assertRaisesMessage(
2019-01-06 16:22:33 +01:00
TTestConfigException,
"Change callback dhcnfg34dlg06kdg successfully called.",
):
self.set_config_var(
2019-01-06 16:22:33 +01:00
key="var_with_callback_ghvnfjd5768gdfkwg0hm2",
value="new_string_kbmbnfhdgibkdjshg452bc",
)
self.assertEqual(
2019-01-06 16:22:33 +01:00
config["var_with_callback_ghvnfjd5768gdfkwg0hm2"],
"new_string_kbmbnfhdgibkdjshg452bc",
)
def set_grouped_config_view():
"""
Sets a grouped config collection. There are some variables, one variable
with a string as default value, one with a boolean as default value,
one with an integer as default value, one with choices and one hidden
variable. These variables are grouped in two subgroups.
"""
yield ConfigVariable(
2019-01-06 16:22:33 +01:00
name="string_var",
default_value="default_string_rien4ooCZieng6ah",
group="Config vars for testing 1",
subgroup="Group 1 aiYeix2mCieQuae3",
)
yield ConfigVariable(
2019-01-06 16:22:33 +01:00
name="bool_var",
default_value=True,
2019-01-06 16:22:33 +01:00
input_type="boolean",
group="Config vars for testing 1",
subgroup="Group 1 aiYeix2mCieQuae3",
)
yield ConfigVariable(
2019-01-06 16:22:33 +01:00
name="integer_var",
default_value=3,
2019-01-06 16:22:33 +01:00
input_type="integer",
group="Config vars for testing 1",
subgroup="Group 1 aiYeix2mCieQuae3",
)
yield ConfigVariable(
2019-01-06 16:22:33 +01:00
name="hidden_var",
default_value="hidden_value",
group="Config vars for testing 1",
subgroup="Group 2 Toongai7ahyahy7B",
)
yield ConfigVariable(
2019-01-06 16:22:33 +01:00
name="choices_var",
default_value="1",
input_type="choice",
choices=(
2019-01-06 16:22:33 +01:00
{"value": "1", "display_name": "Choice One Ughoch4ocoche6Ee"},
{"value": "2", "display_name": "Choice Two Vahnoh5yalohv5Eb"},
),
group="Config vars for testing 1",
subgroup="Group 2 Toongai7ahyahy7B",
)
def set_simple_config_view():
"""
Sets a simple config view with some config variables but without
grouping.
"""
2019-01-06 16:22:33 +01:00
yield ConfigVariable(name="additional_config_var", default_value="BaeB0ahcMae3feem")
yield ConfigVariable(name="additional_config_var_2", default_value="")
yield ConfigVariable(name="none_config_var", default_value=None)
def set_simple_config_view_multiple_vars():
"""
Sets a bad config view with some multiple config vars.
"""
2019-01-06 16:22:33 +01:00
yield ConfigVariable(name="multiple_config_var", default_value="foobar1")
yield ConfigVariable(name="multiple_config_var", default_value="foobar2")
def set_simple_config_collection_disabled_view():
2019-01-06 16:22:33 +01:00
yield ConfigVariable(name="hidden_config_var_2", default_value="")
def set_simple_config_collection_with_callback():
def callback():
2019-01-06 16:22:33 +01:00
raise TTestConfigException(
"Change callback dhcnfg34dlg06kdg successfully called."
)
yield ConfigVariable(
2019-01-06 16:22:33 +01:00
name="var_with_callback_ghvnfjd5768gdfkwg0hm2",
default_value="",
on_change=callback,
)