aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2020-07-20 22:55:40 -0500
committerEugene Triguba <eugenetriguba@gmail.com>2020-07-20 22:55:40 -0500
commit3fa782f5b5e3bd4445a5db63c0fce5aaac2d4ac3 (patch)
tree2fb11ecde1046ac202808f0a8f0b0b41b51e1aae /tests
parentRemove `all`, start convert to google docstring, add other parsers (diff)
downloadconfig-file-3fa782f5b5e3bd4445a5db63c0fce5aaac2d4ac3.zip
Add work in progress tests
Diffstat (limited to 'tests')
-rw-r--r--tests/conftest.py23
-rw-r--r--tests/templates/toml/default.toml6
-rw-r--r--tests/templates/yaml/default.yaml6
-rw-r--r--tests/test_abstract_parser.py0
-rw-r--r--tests/test_base_parser.py114
-rw-r--r--tests/test_config_file.py70
-rw-r--r--tests/test_ini_parser.py26
-rw-r--r--tests/test_json_parser.py68
-rw-r--r--tests/test_lookup_api.py13
-rw-r--r--tests/test_nested_lookup.py44
-rw-r--r--tests/test_parse_value.py2
-rw-r--r--tests/test_toml_parser.py0
-rw-r--r--tests/test_yaml_parser.py0
13 files changed, 161 insertions, 211 deletions
diff --git a/tests/conftest.py b/tests/conftest.py
index 5fdf5a9..70e88c7 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -1,9 +1,9 @@
from pathlib import Path
-from typing import Callable, Type
+from typing import Callable, Tuple, Type, Union
import pytest
-from config_file.parsers import AbstractParser, IniParser, JsonParser
+from config_file import AbstractParser, ConfigFile
from config_file.utils import create_config_path
@@ -33,7 +33,10 @@ def template_file(tmp_file: Callable[[str, str], Path]) -> Callable[[str, str],
@pytest.fixture
def template_original_file() -> Callable[[Path], Path]:
- def func(template_path: str):
+ def func(template_path: Union[str, Path]):
+ if isinstance(template_path, str):
+ template_path = Path(template_path)
+
original_path = create_config_path(template_path, original=True)
original_path.touch()
original_path.write_text(template_path.read_text(encoding="utf-8"))
@@ -44,17 +47,19 @@ def template_original_file() -> Callable[[Path], Path]:
@pytest.fixture
-def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple:
+def template_and_parser(
+ template_file: Callable[[str, str], Path]
+) -> Callable[[str, str], Tuple[Path, Type[AbstractParser]]]:
def func(
file_type: str, template_name: str = "default"
- ) -> Callable[[str, str], Type[AbstractParser]]:
+ ) -> Tuple[Path, Type[AbstractParser]]:
test_file = template_file(file_type, template_name)
text = test_file.read_text(encoding="utf-8")
- if file_type == "ini":
- return test_file, IniParser(text)
- elif file_type == "json":
- return test_file, JsonParser(text)
+ parser = ConfigFile._ConfigFile__find_parser_by_file_type(
+ file_contents=text, file_type=file_type, file_path=test_file
+ )
+ return test_file, parser
return func
diff --git a/tests/templates/toml/default.toml b/tests/templates/toml/default.toml
new file mode 100644
index 0000000..326de33
--- /dev/null
+++ b/tests/templates/toml/default.toml
@@ -0,0 +1,6 @@
+[header_one]
+number_key = 0
+
+[header_two]
+str_key = "string"
+list_key = [1, 2, 3]
diff --git a/tests/templates/yaml/default.yaml b/tests/templates/yaml/default.yaml
new file mode 100644
index 0000000..095ada8
--- /dev/null
+++ b/tests/templates/yaml/default.yaml
@@ -0,0 +1,6 @@
+header_one:
+ number_key: 0
+
+header_two:
+ str_key: "string"
+ list_key: [1, 2, 3] \ No newline at end of file
diff --git a/tests/test_abstract_parser.py b/tests/test_abstract_parser.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/test_abstract_parser.py
diff --git a/tests/test_base_parser.py b/tests/test_base_parser.py
index 4eaa92f..90d45f4 100644
--- a/tests/test_base_parser.py
+++ b/tests/test_base_parser.py
@@ -1,57 +1,57 @@
-import pytest
-
-from config_file.parsers import AbstractParser
-
-
-class ConcreteParser(AbstractParser):
- def __init__(self, file_contents):
- super().__init__(file_contents)
-
- def get(self, key, parse_types=True):
- super().get(key, parse_types=parse_types)
-
- def set(self, key, value):
- super().set(key, value)
-
- def delete(self, section_key):
- super().delete(section_key)
-
- def has(self, section_key: str) -> bool:
- super().has(section_key)
-
- def stringify(self) -> str:
- super().stringify()
-
- def parse(self, file_contents: str, call_super=False):
- if call_super:
- super().parse(file_contents)
-
- return file_contents
-
-
-def test_that_base_parser_can_not_be_instantiated():
- with pytest.raises(TypeError):
- AbstractParser("")
-
-
-@pytest.mark.parametrize(
- "function, args",
- [
- (ConcreteParser("").get, ""),
- (ConcreteParser("").set, ("", "")),
- (ConcreteParser("").delete, ""),
- (ConcreteParser("").stringify, None),
- (ConcreteParser("").has, ""),
- (ConcreteParser("").parse, "call super"),
- ],
-)
-def test_that_base_parser_raises_not_implemented_errors(function, args):
- with pytest.raises(NotImplementedError):
- if args is None:
- function()
- elif type(args) is tuple:
- function(*args)
- elif args == "call super":
- function(args, call_super=True)
- else:
- function(args)
+# import pytest
+#
+# from config_file.parsers import BaseParser
+#
+
+# class ConcreteParser(AbstractParser):
+# def __init__(self, file_contents):
+# super().__init__(file_contents)
+#
+# def get(self, key, parse_types=True):
+# super().get(key, parse_types=parse_types)
+#
+# def set(self, key, value):
+# super().set(key, value)
+#
+# def delete(self, section_key):
+# super().delete(section_key)
+#
+# def has(self, section_key: str) -> bool:
+# super().has(section_key)
+#
+# def stringify(self) -> str:
+# super().stringify()
+#
+# def parse(self, file_contents: str, call_super=False):
+# if call_super:
+# super().parse(file_contents)
+#
+# return file_contents
+#
+#
+# def test_that_base_parser_can_not_be_instantiated():
+# with pytest.raises(TypeError):
+# AbstractParser("")
+#
+#
+# @pytest.mark.parametrize(
+# "function, args",
+# [
+# (ConcreteParser("").get, ""),
+# (ConcreteParser("").set, ("", "")),
+# (ConcreteParser("").delete, ""),
+# (ConcreteParser("").stringify, None),
+# (ConcreteParser("").has, ""),
+# (ConcreteParser("").parse, "call super"),
+# ],
+# )
+# def test_that_base_parser_raises_not_implemented_errors(function, args):
+# with pytest.raises(NotImplementedError):
+# if args is None:
+# function()
+# elif type(args) is tuple:
+# function(*args)
+# elif args == "call super":
+# function(args, call_super=True)
+# else:
+# function(args)
diff --git a/tests/test_config_file.py b/tests/test_config_file.py
index 5430fb8..13d7271 100644
--- a/tests/test_config_file.py
+++ b/tests/test_config_file.py
@@ -4,21 +4,21 @@ from pathlib import Path
import pytest
from config_file.config_file import ConfigFile
-from config_file.parsers import AbstractParser
+from config_file.parsers.abstract_parser import AbstractParser
-SUPPORTED_FILE_TYPES = ["ini", "json"]
+SUPPORTED_FILE_TYPES = ["ini", "json", "yaml", "toml"]
@pytest.fixture(params=SUPPORTED_FILE_TYPES)
def template_and_config_file(request, template_file):
def func(template_name: str = "default", parser: AbstractParser = None):
template = template_file(request.param, template_name)
- return (template, ConfigFile(template, parser=parser))
+ return template, ConfigFile(template, parser=parser)
return func
-@pytest.fixture
+@pytest.fixture(params=SUPPORTED_FILE_TYPES)
def templated_config_file(template_and_config_file):
def func(template_name: str = "default", parser: AbstractParser = None):
return template_and_config_file(template_name, parser)[1]
@@ -179,34 +179,34 @@ def test_that_config_file_can_delete(templated_config_file):
assert config.has("header_one.number_key") is False
-def test_that_custom_parser_can_be_used(template_and_config_file):
- class CustomParser(AbstractParser):
- def __init__(self, file_contents):
- super().__init__(file_contents)
-
- def parse(self, file_contents: str):
- return file_contents
-
- def get(self, key, parse_types=True):
- return key
-
- def set(self, key, value):
- return key, value
-
- def delete(self, section_key):
- return section_key
-
- def has(self, section_key: str) -> bool:
- return True
-
- def stringify(self) -> str:
- return str(self.parsed_content)
-
- template_file, config = template_and_config_file(parser=CustomParser)
-
- assert isinstance(config._ConfigFile__parser, CustomParser)
- assert config.get("key") == "key"
- assert config.set("key", "value") == ("key", "value")
- assert config.delete("section_key") == "section_key"
- assert config.has("blah")
- assert config.stringify() == template_file.read_text()
+# def test_that_custom_parser_can_be_used(template_and_config_file):
+# class CustomParser(AbstractParser):
+# def __init__(self, file_contents):
+# super().__init__(file_contents)
+#
+# def parse(self, file_contents: str):
+# return file_contents
+#
+# def get(self, key, parse_types=True):
+# return key
+#
+# def set(self, key, value):
+# return key, value
+#
+# def delete(self, section_key):
+# return section_key
+#
+# def has(self, section_key: str) -> bool:
+# return True
+#
+# def stringify(self) -> str:
+# return str(self.parsed_content)
+#
+# template_file, config = template_and_config_file(parser=CustomParser)
+#
+# assert isinstance(config._ConfigFile__parser, CustomParser)
+# assert config.get("key") == "key"
+# assert config.set("key", "value") == ("key", "value")
+# assert config.delete("section_key") == "section_key"
+# assert config.has("blah")
+# assert config.stringify() == template_file.read_text()
diff --git a/tests/test_ini_parser.py b/tests/test_ini_parser.py
index d39758e..6577291 100644
--- a/tests/test_ini_parser.py
+++ b/tests/test_ini_parser.py
@@ -1,7 +1,7 @@
import pytest
from config_file.exceptions import ParsingError
-from config_file.parsers import IniParser
+from config_file.parsers.ini_parser import IniParser
@pytest.mark.parametrize("ini_file", ["blahblahblah", "{ 'key1': 5 }"])
@@ -11,29 +11,19 @@ def test_incorrect_ini_formats(ini_file):
@pytest.mark.parametrize(
- "section_key,expected_value,parse_type",
+ "section_key,expected_value",
[
- ("test_section.intkey", 5, True),
- ("test_section.strkey", "blah", True),
- ("test_section.boolkey", False, True),
- ("test_section.floatkey", 5.3, True),
- ("test_section.intkey", "5", False),
- ("test_section.strkey", "blah", False),
- ("test_section.boolkey", "false", False),
- ("test_section.floatkey", "5.3", False),
+ ("test_section.intkey", "5"),
+ ("test_section.strkey", "blah"),
+ ("test_section.boolkey", "false"),
+ ("test_section.floatkey", "5.3"),
(
"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):
+def test_that_ini_parser_can_get_values(section_key, expected_value):
ini_file = """[test_section]
intkey = 5
strkey = blah
@@ -42,7 +32,7 @@ floatkey = 5.3
"""
parser = IniParser(ini_file)
- assert parser.get(section_key, parse_types=parse_type) == expected_value
+ assert parser.get(section_key) == expected_value
@pytest.mark.parametrize(
diff --git a/tests/test_json_parser.py b/tests/test_json_parser.py
index b622f01..aa29ec6 100644
--- a/tests/test_json_parser.py
+++ b/tests/test_json_parser.py
@@ -1,9 +1,6 @@
-import json
-
import pytest
from config_file.exceptions import ParsingError
-from config_file.parsers import JsonParser
def test_invalid_json(templated_parser):
@@ -73,25 +70,6 @@ def test_that_json_parser_can_get_keys_without_parsing_types(
@pytest.mark.parametrize(
- "key, value",
- [
- ("glossary.title", "str_value"),
- ("glossary.dict.str_number", 5),
- (
- "glossary.dict_within_dict",
- {
- "str_number": 16,
- "another_dict": {"str_bool": False, "list_of_values": [5, False, True]},
- },
- ),
- ],
-)
-def test_that_json_parser_can_get_keys_with_parsing_types(templated_parser, key, value):
- parser = templated_parser("json", "glossary")
- assert parser.get(key, parse_types=True) == value
-
-
-@pytest.mark.parametrize(
"key, value", [("zip", "zipzip"), ("foo.bar.foobar", 5), ("foo.baz", "bar")]
)
def test_that_json_parser_can_set_keys(templated_parser, key, value):
@@ -116,53 +94,9 @@ def test_that_json_parser_can_set_keys(templated_parser, key, value):
"key",
["glossary", "dict_within_dict", "glossary.GlossDiv.GlossList.GlossEntry.Abbrev"],
)
-def test_that_json_parser_can_delete_keys(
- template_and_parser, key, test_json, expected_json
-):
+def test_that_json_parser_can_delete_keys(template_and_parser, key):
template, parser = template_and_parser("json", "glossary")
parser.delete(key)
assert parser.stringify() == template.read_text(encoding="utf-8")
-
-
-@pytest.mark.parametrize(
- "test_json, key, expected_output",
- [({"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}}, "bar", [5, 10, 15])],
-)
-def test_that_json_parser_can_retrieve_all_keys(test_json, key, expected_output):
- parser = JsonParser(json.dumps(test_json))
- assert parser.get(key, all=True) == expected_output
-
-
-@pytest.mark.parametrize(
- "test_json, key, value, expected_output",
- [
- (
- {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}},
- "bar",
- "new_value",
- ["new_value", "new_value", "new_value"],
- )
- ],
-)
-def test_that_json_parser_can_set_all_keys(test_json, key, value, expected_output):
- parser = JsonParser(json.dumps(test_json))
- parser.set(key, value, all=True)
- assert parser.get(key, all=True) == expected_output
-
-
-@pytest.mark.parametrize(
- "test_json, key, expected_output",
- [
- (
- {"foo": {"bar": 5, "baz": {"bar": 10, "bam": {"bar": 15}}}},
- "bar",
- {"foo": {"baz": {"bam": {}}}},
- )
- ],
-)
-def test_that_json_parser_can_delete_all_keys(test_json, key, expected_output):
- parser = JsonParser(json.dumps(test_json))
- parser.delete(key, all=True)
- assert parser.stringify() == json.dumps(expected_output)
diff --git a/tests/test_lookup_api.py b/tests/test_lookup_api.py
index f7b7cc3..8b8ca9e 100644
--- a/tests/test_lookup_api.py
+++ b/tests/test_lookup_api.py
@@ -1,7 +1,11 @@
from unittest import TestCase
-from config_file.nested_lookup import nested_lookup, nested_update
-from config_file.nested_lookup import nested_delete, nested_alter
+from config_file.nested_lookup import (
+ nested_alter,
+ nested_delete,
+ nested_lookup,
+ nested_update,
+)
class BaseLookUpApi(TestCase):
@@ -553,17 +557,16 @@ class TestNestedAlter(BaseLookUpApi):
self.assertEqual(out[1]["salsa"][0]["burrito"]["taco"], 79)
def test_nested_alter_work_with_right_order(self):
- document = {"taco": 42, "salsa": [{"burrito":{"key":20}}], "key":50}
+ document = {"taco": 42, "salsa": [{"burrito": {"key": 20}}], "key": 50}
def callback(data):
return data + 100
altered_document = nested_alter(document, "key", callback, in_place=True)
-
+
self.assertEqual(altered_document["salsa"][0]["burrito"]["key"], 120)
self.assertEqual(altered_document["key"], 150)
-
def test_sample_data4(self):
result = {
diff --git a/tests/test_nested_lookup.py b/tests/test_nested_lookup.py
index 6c1cc83..591edda 100644
--- a/tests/test_nested_lookup.py
+++ b/tests/test_nested_lookup.py
@@ -1,11 +1,11 @@
from unittest import TestCase
from config_file.nested_lookup import (
- nested_lookup,
get_all_keys,
get_occurrence_of_key,
get_occurrence_of_value,
- get_occurrences_and_values
+ get_occurrences_and_values,
+ nested_lookup,
)
@@ -39,7 +39,13 @@ class TestNestedLookup(TestCase):
"name": "Test",
"date": "YYYY-MM-DD HH:MM:SS",
}
- self.subject_dict4 = {1: "a", 2: {"b": 44, "C": 55}, 3: "d", 4: "e", "6776": "works"}
+ self.subject_dict4 = {
+ 1: "a",
+ 2: {"b": 44, "C": 55},
+ 3: "d",
+ 4: "e",
+ "6776": "works",
+ }
def test_nested_lookup(self):
results = nested_lookup("d", self.subject_dict)
@@ -357,7 +363,7 @@ class TestGetOccurrence(TestCase):
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
- }
+ },
}
]
@@ -368,7 +374,7 @@ class TestGetOccurrence(TestCase):
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
- }
+ },
},
{
"processor_name": "4",
@@ -376,8 +382,8 @@ class TestGetOccurrence(TestCase):
"core_details": {
"total_numberof_cores": "4",
"l2_cache(per_core)": "256 KB",
- }
- }
+ },
+ },
]
def test_sample_data1(self):
@@ -417,28 +423,28 @@ class TestGetOccurrence(TestCase):
self.assertEqual(2, get_occurrence_of_key(self.sample5, "memory"))
def test_sample_data6(self):
- value = '4'
+ value = "4"
result = get_occurrences_and_values(self.sample6, value)
- self.assertEqual(2, result[value]['occurrences'])
- self.assertEqual(2, len(result[value]['values']))
+ self.assertEqual(2, result[value]["occurrences"])
+ self.assertEqual(2, len(result[value]["values"]))
def test_sample_data7(self):
- value = '2.7 GHz'
+ value = "2.7 GHz"
result = get_occurrences_and_values(self.sample6, value)
- self.assertEqual(1, result[value]['occurrences'])
- self.assertEqual(1, len(result[value]['values']))
+ self.assertEqual(1, result[value]["occurrences"])
+ self.assertEqual(1, len(result[value]["values"]))
def test_sample_data8(self):
- value = '4'
+ value = "4"
result = get_occurrences_and_values(self.sample7, value)
- self.assertEqual(4, result[value]['occurrences'])
- self.assertEqual(4, len(result[value]['values']))
+ self.assertEqual(4, result[value]["occurrences"])
+ self.assertEqual(4, len(result[value]["values"]))
def test_sample_data9(self):
- value = '5'
+ value = "5"
result = get_occurrences_and_values(self.sample7, value)
- self.assertEqual(0, result[value]['occurrences'])
- self.assertEqual(0, len(result[value]['values']))
+ self.assertEqual(0, result[value]["occurrences"])
+ self.assertEqual(0, len(result[value]["values"]))
if __name__ == "__main__":
diff --git a/tests/test_parse_value.py b/tests/test_parse_value.py
index 37c68b5..81db472 100644
--- a/tests/test_parse_value.py
+++ b/tests/test_parse_value.py
@@ -1,6 +1,6 @@
import pytest
-from config_file.parsers import can_be_parsed_as_bool, parse_value
+from config_file.parsers.parse_value import can_be_parsed_as_bool, parse_value
@pytest.mark.parametrize(
diff --git a/tests/test_toml_parser.py b/tests/test_toml_parser.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/test_toml_parser.py
diff --git a/tests/test_yaml_parser.py b/tests/test_yaml_parser.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/tests/test_yaml_parser.py