aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-01-19 14:57:37 -0600
committerEugene Triguba <eugenetriguba@gmail.com>2020-01-19 14:57:37 -0600
commit7f32dc49460875aedec9e555d0e5d27f23a3f2df (patch)
treef70688edfcf512e3e29fdd7f706391a0f99fb018 /tests
parentAdd start of json_parser implementaiton (diff)
parentAdd spacing (diff)
downloadconfig-file-7f32dc49460875aedec9e555d0e5d27f23a3f2df.zip
Merge branch 'master' of github.com:eugenetriguba/config_file into json
Diffstat (limited to 'tests')
-rw-r--r--tests/test_config_file.py63
-rw-r--r--tests/test_ini_parser.py25
-rw-r--r--tests/test_parse_value.py11
-rw-r--r--tests/test_utils.py19
4 files changed, 114 insertions, 4 deletions
diff --git a/tests/test_config_file.py b/tests/test_config_file.py
new file mode 100644
index 0000000..1daba6f
--- /dev/null
+++ b/tests/test_config_file.py
@@ -0,0 +1,63 @@
+from pathlib import Path
+
+import pytest
+
+from config_file.config_file import BaseParser, ConfigFile
+
+
+@pytest.mark.parametrize(
+ "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
+)
+def test_that_config_file_initializes_correctly(tmp_path, file_contents, file_name):
+ temp = tmp_path / file_name
+ temp.write_text(file_contents)
+
+ config = ConfigFile(temp)
+ assert config.path == str(temp)
+ assert config.contents == temp.read_text()
+ assert isinstance(config.parser, BaseParser)
+
+
+@pytest.mark.parametrize(
+ "file_contents, file_name, original_file_name",
+ [("[calendar]\nsunday_index = 0\n\n", "config.ini", "config.original.ini")],
+)
+def test_that_config_file_can_reset(
+ tmpdir, file_contents, file_name, original_file_name
+):
+ config_path = tmpdir / file_name
+ config_path.write_text(file_contents, encoding="utf-8")
+
+ original_config_path = tmpdir / original_file_name
+ original_config_path.write_text(file_contents, encoding="utf-8")
+
+ config = ConfigFile(str(config_path))
+ config.set("calendar.sunday_index", 0)
+ config.reset()
+ assert config.stringify() == ConfigFile(str(original_config_path)).stringify()
+ assert config_path.read_text(encoding="utf-8") == original_config_path.read_text(
+ encoding="utf-8"
+ )
+
+
+@pytest.mark.parametrize(
+ "file_contents, file_name", [("[calendar]\nsunday_index = 0\n\n", "config.ini")]
+)
+def test_that_config_file_can_save(tmpdir, file_contents, file_name):
+ config_path = tmpdir / file_name
+ config_path.write_text(file_contents, encoding="utf-8")
+
+ config = ConfigFile(str(config_path))
+ config.set("calendar.sunday_index", 1)
+ config.save()
+ assert config.stringify() == Path(config_path).read_text(encoding="utf-8")
+
+
+@pytest.mark.parametrize(
+ "path, is_dir", [("invalid", False), ("invalid", True), ("invalid.conf", False)]
+)
+def test_invalid_config_paths(tmpdir, path, is_dir):
+ with pytest.raises(ValueError):
+ temp_path = tmpdir / path
+ temp_path.write("")
+ ConfigFile(str(tmpdir)) if is_dir else ConfigFile(str(temp_path))
diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py
index 5c26b23..7af3ea4 100644
--- a/tests/test_ini_parser.py
+++ b/tests/test_ini_parser.py
@@ -20,6 +20,16 @@ def test_incorrect_ini_formats(ini_file):
("test_section.strkey", "blah", False),
("test_section.boolkey", "false", False),
("test_section.floatkey", "5.3", False),
+ (
+ "test_section",
+ {"intkey": "5", "strkey": "blah", "boolkey": "false", "floatkey": "5.3"},
+ False,
+ ),
+ (
+ "test_section",
+ {"intkey": 5, "strkey": "blah", "boolkey": False, "floatkey": 5.3},
+ True,
+ ),
],
)
def test_that_ini_parser_can_get_values(section_key, expected_value, parse_type):
@@ -35,17 +45,26 @@ floatkey = 5.3
@pytest.mark.parametrize(
+ "test_input", ["invalid.invalid_key"],
+)
+def test_that_getting_unknown_sections_or_keys_throws_errors(test_input):
+ with pytest.raises(ParsingError):
+ IniParser("").get(test_input)
+
+
+@pytest.mark.parametrize(
"section_key,value,expected_value",
[
("test.key1", "different value", "[test]\nkey1 = different value\n\n"),
- ("test.key2", 5, "[test]\nkey1 = different value\nkey2 = 5\n\n"),
- ("test2.key", False, "[test]\nkey1 = value1\n[test2]\nkey = False\n\n"),
+ ("test.key2", 5, "[test]\nkey1 = value1\nkey2 = 5\n\n"),
+ ("test2.key", False, "[test]\nkey1 = value1\n\n[test2]\nkey = False\n\n"),
],
)
def test_that_ini_parser_can_set_values(section_key, value, expected_value):
ini_file = """[test]\nkey1 = value1\n\n"""
parser = IniParser(ini_file)
- assert parser.stringify() == ini_file
+ parser.set(section_key, value)
+ assert parser.stringify() == expected_value
@pytest.mark.parametrize(
diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py
index 54e056b..2123337 100644
--- a/tests/test_parse_value.py
+++ b/tests/test_parse_value.py
@@ -1,6 +1,6 @@
import pytest
-from config_file.parsers.parse_value import parse_value
+from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value
@pytest.mark.parametrize(
@@ -21,3 +21,12 @@ from config_file.parsers.parse_value import parse_value
)
def test_parse_value(test_input, expected):
assert parse_value(test_input) == expected
+
+
+def test_can_be_parsed_as_bool():
+ """
+ This is the section not being covered in the parse_value.py
+ file from test_parse_value, but I don't think it's reachable
+ from using it.
+ """
+ assert can_be_parsed_as_bool(5) is False
diff --git a/tests/test_utils.py b/tests/test_utils.py
new file mode 100644
index 0000000..6779d18
--- /dev/null
+++ b/tests/test_utils.py
@@ -0,0 +1,19 @@
+import pytest
+
+from config_file.utils import split_on_dot
+
+
+@pytest.mark.parametrize(
+ "test_input, only_last_dot, expected_result",
+ [("section.key.blah", False, ["section", "key", "blah"])],
+)
+def test_that_split_on_dot_splits_the_line_correctly(
+ test_input, only_last_dot, expected_result
+):
+ assert split_on_dot(test_input, only_last_dot=only_last_dot) == expected_result
+
+
+@pytest.mark.parametrize("test_input", ["this line has no dots in it"])
+def test_that_split_on_dot_raises_error_when_there_is_no_dot(test_input):
+ with pytest.raises(ValueError):
+ split_on_dot(test_input)