Add some unit testing, starting with the config

This commit is contained in:
Andrew Morgan 2021-01-10 19:22:10 -05:00
parent 015ef12782
commit 0e37bc724d
5 changed files with 88 additions and 6 deletions

View file

@ -15,18 +15,22 @@ logging.getLogger("peewee").setLevel(
class Config:
"""Creates a Config object from a YAML-encoded config file from a given filepath"""
def __init__(self, filepath: str):
"""
Args:
filepath: Path to a config file.
"""
self.filepath = filepath
if not os.path.isfile(filepath):
raise ConfigError(f"Config file '{filepath}' does not exist")
# Load in the config file at the given filepath
with open(filepath) as file_stream:
self.config = yaml.safe_load(file_stream.read())
self.config_dict = yaml.safe_load(file_stream.read())
# Parse and validate config options
self._parse_config_values()
def _parse_config_values(self):
"""Read and validate each config option"""
# Logging setup
formatter = logging.Formatter(
"%(asctime)s | %(name)s [%(levelname)s] %(message)s"
@ -115,7 +119,7 @@ class Config:
no default value provided), a ConfigError will be raised.
"""
# Sift through the the config until we reach our option
config = self.config
config = self.config_dict
for name in path:
config = config.get(name)

View file

@ -32,6 +32,8 @@ async def main():
config_path = sys.argv[1]
else:
config_path = "config.yaml"
# Read the parsed config file and create a Config object
config = Config(config_path)
# Configure the database

0
tests/__init__.py Normal file
View file

0
tests/config/__init__.py Normal file
View file

View file

@ -0,0 +1,76 @@
from typing import Dict
import unittest
from my_project_name.config import Config
from my_project_name.errors import ConfigError
class FakeConfigClass:
def __init__(self, my_test_config_dict: Dict):
self.config_dict = my_test_config_dict
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.
test_config_dict = {
"a_key": 5,
"some_key": {
"some_other_key": "some_value"
}
}
# We create a fake config class. This class is needed as _get_cfg pulls config options
# from 'self.config_dict'. So, we make a class that has a self.config_dict, and fill it
# with our test config options.
fake_cfg = FakeConfigClass(my_test_config_dict=test_config_dict)
# 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(
Config._get_cfg(fake_cfg, ["a_key"]),
5,
)
# Test that we can get the value of a nested key
self.assertEqual(
Config._get_cfg(fake_cfg, ["some_key", "some_other_key"]),
"some_value",
)
# Test that the value provided by the default option is used when a key does not exist
self.assertEqual(
Config._get_cfg(fake_cfg, ["a_made_up_key", "this_does_not_exist"], default="The default"),
"The default",
)
# Test that the value provided by the default option is *not* used when a key *does* exist
self.assertEqual(
Config._get_cfg(fake_cfg, ["a_key"], default="The default"),
5,
)
# Test that keys that do not exist raise a ConfigError when the required argument is True
with self.assertRaises(ConfigError):
Config._get_cfg(fake_cfg, ["a_made_up_key", "this_does_not_exist"], required=True)
# Test that a ConfigError is not returned when a non-existent key is provided and required is False
self.assertIsNone(
Config._get_cfg(fake_cfg, ["a_made_up_key", "this_does_not_exist"], required=False)
)
# 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(
Config._get_cfg(fake_cfg, ["a_made_up_key", "this_does_not_exist"], default="something", required=True),
"something",
)
# TODO: Test creating a test yaml file, passing the path to Config and _parse_config_values is called correctly
if __name__ == '__main__':
unittest.main()