2021-01-11 01:22:10 +01:00
|
|
|
import unittest
|
2021-01-11 04:14:20 +01:00
|
|
|
from unittest.mock import Mock
|
2021-01-11 03:54:22 +01:00
|
|
|
|
2022-06-13 20:55:01 +02:00
|
|
|
from matrix_alertbot.config import Config
|
|
|
|
from matrix_alertbot.errors import ConfigError
|
2021-01-11 01:22:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ConfigTestCase(unittest.TestCase):
|
|
|
|
def test_get_cfg(self):
|
|
|
|
"""Test that Config._get_cfg works correctly"""
|
|
|
|
|
|
|
|
# Here's our test dictionary. Pretend that this was parsed from a YAML config file.
|
2021-01-11 03:54:22 +01:00
|
|
|
test_config_dict = {"a_key": 5, "some_key": {"some_other_key": "some_value"}}
|
2021-01-11 01:22:10 +01:00
|
|
|
|
2021-01-11 04:14:20 +01:00
|
|
|
# We create a fake config using Mock. _get_cfg will attempt to pull from self.config_dict,
|
|
|
|
# so we use a Mock to quickly create a dummy class, and set the 'config_dict' attribute to
|
|
|
|
# our test dictionary.
|
|
|
|
fake_config = Mock()
|
|
|
|
fake_config.config_dict = test_config_dict
|
2021-01-11 01:22:10 +01:00
|
|
|
|
|
|
|
# Now let's make some calls to Config._get_cfg. We provide 'fake_cfg' as the first argument
|
|
|
|
# as a substitute for 'self'. _get_cfg will then be pulling values from fake_cfg.config_dict.
|
|
|
|
|
|
|
|
# Test that we can get the value of a top-level key
|
|
|
|
self.assertEqual(
|
2021-01-11 04:14:20 +01:00
|
|
|
Config._get_cfg(fake_config, ["a_key"]),
|
2021-01-11 01:22:10 +01:00
|
|
|
5,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test that we can get the value of a nested key
|
|
|
|
self.assertEqual(
|
2021-01-11 04:14:20 +01:00
|
|
|
Config._get_cfg(fake_config, ["some_key", "some_other_key"]),
|
2021-01-11 01:22:10 +01:00
|
|
|
"some_value",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test that the value provided by the default option is used when a key does not exist
|
|
|
|
self.assertEqual(
|
2021-01-11 03:54:22 +01:00
|
|
|
Config._get_cfg(
|
2021-01-11 04:14:20 +01:00
|
|
|
fake_config,
|
2021-01-11 03:54:22 +01:00
|
|
|
["a_made_up_key", "this_does_not_exist"],
|
|
|
|
default="The default",
|
|
|
|
),
|
2021-01-11 01:22:10 +01:00
|
|
|
"The default",
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test that the value provided by the default option is *not* used when a key *does* exist
|
|
|
|
self.assertEqual(
|
2021-01-11 04:14:20 +01:00
|
|
|
Config._get_cfg(fake_config, ["a_key"], default="The default"),
|
2021-01-11 01:22:10 +01:00
|
|
|
5,
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test that keys that do not exist raise a ConfigError when the required argument is True
|
|
|
|
with self.assertRaises(ConfigError):
|
2021-01-11 03:54:22 +01:00
|
|
|
Config._get_cfg(
|
2021-01-11 04:14:20 +01:00
|
|
|
fake_config, ["a_made_up_key", "this_does_not_exist"], required=True
|
2021-01-11 03:54:22 +01:00
|
|
|
)
|
2021-01-11 01:22:10 +01:00
|
|
|
|
|
|
|
# Test that a ConfigError is not returned when a non-existent key is provided and required is False
|
|
|
|
self.assertIsNone(
|
2021-01-11 03:54:22 +01:00
|
|
|
Config._get_cfg(
|
2021-01-11 04:14:20 +01:00
|
|
|
fake_config, ["a_made_up_key", "this_does_not_exist"], required=False
|
2021-01-11 03:54:22 +01:00
|
|
|
)
|
2021-01-11 01:22:10 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
# Test that default is used for non-existent keys, even if required is True
|
|
|
|
# (Typically one shouldn't use a default with required=True anyways...)
|
|
|
|
self.assertEqual(
|
2021-01-11 03:54:22 +01:00
|
|
|
Config._get_cfg(
|
2021-01-11 04:14:20 +01:00
|
|
|
fake_config,
|
2021-01-11 03:54:22 +01:00
|
|
|
["a_made_up_key", "this_does_not_exist"],
|
|
|
|
default="something",
|
|
|
|
required=True,
|
|
|
|
),
|
2021-01-11 01:22:10 +01:00
|
|
|
"something",
|
|
|
|
)
|
|
|
|
|
|
|
|
# TODO: Test creating a test yaml file, passing the path to Config and _parse_config_values is called correctly
|
|
|
|
|
|
|
|
|
2021-01-11 03:54:22 +01:00
|
|
|
if __name__ == "__main__":
|
2021-01-11 01:22:10 +01:00
|
|
|
unittest.main()
|