Use a Mock instead of creating a fake config class
This commit is contained in:
parent
106c452ee6
commit
b28ce6d5e1
1 changed files with 13 additions and 17 deletions
|
@ -1,15 +1,10 @@
|
|||
import unittest
|
||||
from typing import Dict
|
||||
from unittest.mock import Mock
|
||||
|
||||
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"""
|
||||
|
@ -17,30 +12,31 @@ class ConfigTestCase(unittest.TestCase):
|
|||
# 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)
|
||||
# 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
|
||||
|
||||
# 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"]),
|
||||
Config._get_cfg(fake_config, ["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"]),
|
||||
Config._get_cfg(fake_config, ["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,
|
||||
fake_config,
|
||||
["a_made_up_key", "this_does_not_exist"],
|
||||
default="The default",
|
||||
),
|
||||
|
@ -49,20 +45,20 @@ class ConfigTestCase(unittest.TestCase):
|
|||
|
||||
# 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"),
|
||||
Config._get_cfg(fake_config, ["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
|
||||
fake_config, ["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
|
||||
fake_config, ["a_made_up_key", "this_does_not_exist"], required=False
|
||||
)
|
||||
)
|
||||
|
||||
|
@ -70,7 +66,7 @@ class ConfigTestCase(unittest.TestCase):
|
|||
# (Typically one shouldn't use a default with required=True anyways...)
|
||||
self.assertEqual(
|
||||
Config._get_cfg(
|
||||
fake_cfg,
|
||||
fake_config,
|
||||
["a_made_up_key", "this_does_not_exist"],
|
||||
default="something",
|
||||
required=True,
|
||||
|
|
Loading…
Reference in a new issue