From 2c293cc0ac6678253c6af8101ba93866785f64c0 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 21 Jun 2020 11:12:26 -0500 Subject: WIP for refactoring tests --- tests/conftest.py | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tests/conftest.py (limited to 'tests/conftest.py') diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..bb16405 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,67 @@ +from pathlib import Path +from typing import Callable, Type + +import pytest + +from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.utils import create_config_path + + +@pytest.fixture +def tmp_file(tmp_path) -> Callable[[str, str], Path]: + def func(file_name: str, file_contents: str) -> Path: + file = tmp_path / file_name + file.touch() + file.write_text(file_contents, encoding="utf-8") + return file + + return func + + +@pytest.fixture +def template_file(tmp_file: Callable[[str, str], Path]) -> Callable[[str, str], Path]: + template_dir = Path(__file__).parent / "templates" + + def func(file_type: str, template_name: str = "default") -> Path: + file_name = f"{template_name}.{file_type}" + template = template_dir / file_type / file_name + + return tmp_file(file_name, template.read_text(encoding="utf-8")) + + return func + + +@pytest.fixture +def template_original_file() -> Callable[[Path], Path]: + def func(template_path: str): + original_path = create_config_path(template_path, original=True) + original_path.touch() + original_path.write_text(template_path.read_text(encoding="utf-8")) + + return original_path + + return func + + +@pytest.fixture +def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: + def func( + file_type: str, template_name: str = "default" + ) -> Callable[[str, str], Type[BaseParser]]: + 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) + + return func + + +@pytest.fixture +def templated_parser(template_and_parser) -> Callable[[str, str], Type[BaseParser]]: + def func(file_type: str, template_name: str = "default") -> Type[BaseParser]: + return template_and_parser(file_type, template_name)[1] + + return func -- cgit v1.2.3 From 461cb78f0a56b9293b5ade10fe90ec4aa77d23ad Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sun, 19 Jul 2020 15:02:13 -0500 Subject: Change name of BaseParser to AbstractParser --- tests/conftest.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'tests/conftest.py') diff --git a/tests/conftest.py b/tests/conftest.py index bb16405..5fdf5a9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,7 @@ from typing import Callable, Type import pytest -from config_file.parsers import BaseParser, IniParser, JsonParser +from config_file.parsers import AbstractParser, IniParser, JsonParser from config_file.utils import create_config_path @@ -47,7 +47,7 @@ def template_original_file() -> Callable[[Path], Path]: def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: def func( file_type: str, template_name: str = "default" - ) -> Callable[[str, str], Type[BaseParser]]: + ) -> Callable[[str, str], Type[AbstractParser]]: test_file = template_file(file_type, template_name) text = test_file.read_text(encoding="utf-8") @@ -60,8 +60,8 @@ def template_and_parser(template_file: Callable[[str, str], Path]) -> tuple: @pytest.fixture -def templated_parser(template_and_parser) -> Callable[[str, str], Type[BaseParser]]: - def func(file_type: str, template_name: str = "default") -> Type[BaseParser]: +def templated_parser(template_and_parser) -> Callable[[str, str], Type[AbstractParser]]: + def func(file_type: str, template_name: str = "default") -> Type[AbstractParser]: return template_and_parser(file_type, template_name)[1] return func -- cgit v1.2.3 From 3fa782f5b5e3bd4445a5db63c0fce5aaac2d4ac3 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Mon, 20 Jul 2020 22:55:40 -0500 Subject: Add work in progress tests --- tests/conftest.py | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'tests/conftest.py') 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 -- cgit v1.2.3 From 032196104a59761bd75b5d594440d1e8ef16965a Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 1 Aug 2020 17:58:32 -0500 Subject: Add ConfigFilePath to refactor out file specific code --- tests/conftest.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'tests/conftest.py') diff --git a/tests/conftest.py b/tests/conftest.py index 70e88c7..faf1630 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,7 +4,7 @@ from typing import Callable, Tuple, Type, Union import pytest from config_file import AbstractParser, ConfigFile -from config_file.utils import create_config_path +from config_file.config_file_path import ConfigFilePath @pytest.fixture @@ -37,7 +37,7 @@ def template_original_file() -> Callable[[Path], Path]: if isinstance(template_path, str): template_path = Path(template_path) - original_path = create_config_path(template_path, original=True) + original_path = ConfigFilePath(template_path).original_path original_path.touch() original_path.write_text(template_path.read_text(encoding="utf-8")) @@ -54,12 +54,7 @@ def template_and_parser( file_type: str, template_name: str = "default" ) -> Tuple[Path, Type[AbstractParser]]: test_file = template_file(file_type, template_name) - text = test_file.read_text(encoding="utf-8") - - parser = ConfigFile._ConfigFile__find_parser_by_file_type( - file_contents=text, file_type=file_type, file_path=test_file - ) - return test_file, parser + return test_file, ConfigFile(test_file)._ConfigFile__parser return func -- cgit v1.2.3 From a933dc9c424e68b1773d8af3140c7d60422b428b Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Wed, 5 Aug 2020 21:28:44 -0500 Subject: Fix AbstractParser imports for tests --- tests/conftest.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tests/conftest.py') diff --git a/tests/conftest.py b/tests/conftest.py index faf1630..3ec303e 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -3,7 +3,8 @@ from typing import Callable, Tuple, Type, Union import pytest -from config_file import AbstractParser, ConfigFile +from config_file import ConfigFile +from config_file.abstract_parser import AbstractParser from config_file.config_file_path import ConfigFilePath -- cgit v1.2.3