aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-08-07 10:25:55 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-08-07 10:25:55 -0500
commit577193b1c025e18599ef1cdb91841a30b631b103 (patch)
tree084b0567d58cb100029904a0f17f0a3e43dfef0d
parentFix invalid reference to MissingKeyError (diff)
downloadconfig-file-577193b1c025e18599ef1cdb91841a30b631b103.zip
Add docstrings to config file test
-rw-r--r--tests/test_config_file.py153
1 files changed, 134 insertions, 19 deletions
diff --git a/tests/test_config_file.py b/tests/test_config_file.py
index 023b6ae..33f1945 100644
--- a/tests/test_config_file.py
+++ b/tests/test_config_file.py
@@ -1,4 +1,6 @@
+import json
from pathlib import Path
+from typing import Callable, Tuple
import pytest
@@ -10,7 +12,21 @@ SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"]
@pytest.fixture(params=SUPPORTED_FILE_TYPES)
-def template_and_config_file(request, template_file):
+def template_and_config_file(
+ request, template_file
+) -> Callable[[str], Tuple[Path, ConfigFile]]:
+ """
+ Fixture that is a wrapper around the template_file
+ fixture. Using the function returned, we can retrieve
+ a temporary file that has the contents of the specified
+ template file as well as a ConfigFile with the template
+ already passed in.
+
+ This fixture will make the test function run once for every
+ supported file type, each with the corresponding template
+ in the different file type.
+ """
+
def func(template_name: str = "default"):
template = template_file(request.param, template_name)
return template, ConfigFile(template)
@@ -19,7 +35,12 @@ def template_and_config_file(request, template_file):
@pytest.fixture(params=SUPPORTED_FILE_TYPES)
-def templated_config_file(template_and_config_file):
+def templated_config_file(template_and_config_file) -> Callable[[str], ConfigFile]:
+ """
+ Wrapper around template_and_config_file for when we
+ only need the ConfigFile and not the template.
+ """
+
def func(template_name: str = "default"):
return template_and_config_file(template_name)[1]
@@ -27,14 +48,25 @@ def templated_config_file(template_and_config_file):
def test_that_config_file_is_initialized_correctly(template_and_config_file):
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure that an initialized ConfigFile has the
+ correct file path and has a parser that is a AbstractParser.
+ """
template_file, config = template_and_config_file()
assert config.file_path == template_file
- assert config.stringify() == template_file.read_text()
assert isinstance(config._ConfigFile__parser, AbstractParser)
def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path():
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure that a home tilde (~) in a file path passed
+ to ConfigFile is expanded to the full path.
+ """
config_name = "temp.ini"
config_path = "~/" + config_name
@@ -47,17 +79,34 @@ def test_that_a_tidle_in_the_config_path_expands_to_the_absolute_path():
@pytest.mark.parametrize("path", ["invalid", "invalid.conf"])
def test_invalid_path_raises_file_not_found_error(path):
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure that when a path to a file that does not exist
+ that is passed into the constructor, it raises a FileNotFoundError.
+ """
with pytest.raises(FileNotFoundError):
ConfigFile(path)
-@pytest.mark.parametrize("path", [""])
-def test_directory_raises_value_error(path):
+def test_directory_raises_value_error():
+ """
+ config_file.config_file.ConfigFile.__init__
+
+ Ensure when a path to a directory is passed into
+ the constructor, it raises a ValueError.
+ """
with pytest.raises(ValueError):
- ConfigFile(path)
+ ConfigFile(".")
def test_that_config_file_can_save(template_and_config_file):
+ """
+ config_file.config_file.ConfigFile.save
+
+ Ensure that altering the configuration file and saving it
+ writes those changes out to the filesystem.
+ """
template_file, config = template_and_config_file()
config.set("header_one.number_key", 25)
@@ -69,6 +118,14 @@ def test_that_config_file_can_save(template_and_config_file):
def test_that_config_file_can_restore_the_original(
templated_config_file, template_original_file
):
+ """
+ config_file.config_file.ConfigFile.restore_original
+
+ Ensure that we can restore the "original" configuration
+ file, using the calculated path and after altering the
+ internal config contents. Those internal contents and file
+ should be reset to the original file.
+ """
config = templated_config_file()
original_file = template_original_file(config.file_path)
@@ -76,9 +133,18 @@ def test_that_config_file_can_restore_the_original(
config.restore_original()
assert config.stringify() == original_file.read_text()
+ assert config.file_path.read_text() == original_file.read_text()
def test_missing_config_file_during_restore(templated_config_file):
+ """
+ config_file.config_file.ConfigFile.restore_original
+
+ Ensure that if we try to restore_original without using
+ the optional original_file_path argument but we do not
+ have a file at the calculated default path, a FileNotFoundError
+ is raised.
+ """
config = templated_config_file()
with pytest.raises(FileNotFoundError) as error:
@@ -88,35 +154,76 @@ def test_missing_config_file_during_restore(templated_config_file):
assert "does not exist" in str(error)
-@pytest.mark.parametrize(
- "key, expected_result",
- [
- ("header_one", True),
- ("header_one.number_key", True),
- ("header_one.blah", False),
- ],
-)
-def test_that_config_file_can_find_sections_and_keys(
- templated_config_file, key, expected_result
+@pytest.mark.parametrize("key", ["header_one", "header_one.number_key"])
+def test_that_config_file_has_can_find_sections_and_keys_that_exist(
+ templated_config_file, key
):
+ """
+ config_file.config_file.ConfigFile.has
+
+ Ensure that when a section or key that exists
+ is passed to has(), it can find it and returns True.
+ """
config = templated_config_file()
- assert config.has(key) == expected_result
+ assert config.has(key)
+
+
+def test_that_config_file_has_returns_false_for_sections_and_keys_that_do_not_exist(
+ templated_config_file,
+):
+ """
+ config_file.config_file.ConfigFile.has
+
+ Ensure that when a section or key that does not exist
+ is passed to has(), it can't find it and returns False.
+ """
+ config = templated_config_file()
+ assert not config.has("header_one.blah")
@pytest.mark.parametrize(
- "key, value", [("header_one.number_key", 0), ("header_two.list_key", [1, 2, 3])],
+ "key, value",
+ [
+ ("header_one.number_key", 0),
+ ("header_two.list_key", [1, 2, 3]),
+ ("header_one", {"number_key": 0}),
+ ],
)
def test_that_config_file_can_get_existent_keys(templated_config_file, key, value):
+ """
+ config_file.config_file.ConfigFile.get
+
+ Ensure that we can retrieve a section or key
+ and that it is in its native type (the type that
+ it was set to).
+
+ The one caveat here is the IniParser. The IniParser
+ uses configparser under the hood which simply always
+ returns back a string. However, that is not the case
+ for JsonParser, YamlParser, or TomlParser.
+ """
config = templated_config_file()
if isinstance(config._ConfigFile__parser, IniParser):
- value = str(value)
+ if type(value) is not dict:
+ # not a section being retrieved
+ value = str(value)
+ else:
+ # section is being retrieved so it should be a dict
+ # but the values inside should be strings.
+ value = json.loads(json.dumps(value), parse_int=str)
assert config.get(key) == value
@pytest.mark.parametrize("key", ["header_one.number_key", "header_one"])
def test_that_config_file_can_delete(templated_config_file, key):
+ """
+ config_file.config_file.ConfigFile.delete
+
+ Ensure that we can delete keys and that they are removed
+ from the internal contents of the ConfigFile.
+ """
config = templated_config_file()
config.delete(key)
@@ -128,6 +235,14 @@ def test_that_config_file_can_delete(templated_config_file, key):
def test_that_config_file_raises_key_error_on_invalid_input_or_missing_key(
templated_config_file, key
):
+ """
+ config_file.config_file.ConfigFile.delete
+
+ Ensure that a KeyError is raised if we
+ tried to provide a value that is not valid
+ (like the wrong type so it is an invalid key)
+ or one that does not exist.
+ """
config = templated_config_file()
with pytest.raises(KeyError):