diff --git a/tests/backend/common.py b/tests/backend/common.py index 57af2674..1e298b31 100644 --- a/tests/backend/common.py +++ b/tests/backend/common.py @@ -6,18 +6,58 @@ r"""Common functions for test cases of loaders and dumpers.""" from __future__ import annotations +import importlib import pathlib import re import typing +import anyconfig.ioinfo +import pytest + from .. import common +NAMES: tuple[str, ...] = ("ipath", "opts", "exp") PATH_PATTERN: re.Pattern = re.compile( r".+[/\\:\.]test_([^_]+)_([^_]+).py" ) +def get_name(testfile: str, pattern: re.Pattern = PATH_PATTERN) -> str: + """Get the name of backend module. + + ex. tests/backend/loaders/json/test_json_stdlib.py + -> "json.stdlib" + """ + match = pattern.match(testfile) + if not match: + raise NameError( + f"Filename does not match expected pattern: {testfile}" + ) + + return ".".join(match.groups()) + + +def get_mod(testfile: str, pattern: re.Pattern = PATH_PATTERN): + """Get the module to test. + + :raises: ModuleNotFoundError: + """ + name = get_name(testfile, pattern=pattern) + mname = f"anyconfig.backend.{name}" + try: + return importlib.import_module(mname) + except ImportError: + pytest.skip( + f"Skip becuase it failed to import: {mname}", + allow_module_level=True + ) + + +def get_test_ids(*args, **opts): + return common.get_test_ids(*args, **opts) + + def get_test_resdir( testfile: str, is_loader: bool = True, @@ -28,14 +68,8 @@ def get_test_resdir( ex. tests/backend/loaders/json/test_json_stdlib.py -> tests/res/1/loaders/json.stdlib/ """ - match = pattern.match(testfile) - if not match: - raise NameError( - f"Filename does not match expected pattern: {testfile}" - ) - - name = ".".join(match.groups()) subdir = "loaders" if is_loader else "dumpers" + name = get_name(testfile, pattern=pattern) return common.RESOURCE_DIR / subdir / name @@ -49,3 +83,7 @@ def load_data_for_testfile( return common.load_data_for_testfile( testfile, datadir=datadir, **opts ) + + +def ioinfo_from_path(path: pathlib.Path) -> anyconfig.ioinfo.IOInfo: + return anyconfig.ioinfo.make(path) diff --git a/tests/backend/loaders/json/test_json_stdlib.py b/tests/backend/loaders/json/test_json_stdlib.py index 2ee6aefd..3042c293 100644 --- a/tests/backend/loaders/json/test_json_stdlib.py +++ b/tests/backend/loaders/json/test_json_stdlib.py @@ -16,6 +16,38 @@ import tests.common.tdi_base import tests.common.loader +from ... import common + + +try: + DATA_0 = common.load_data_for_testfile(__file__) +except FileNotFoundError: + pytest.skip( + f"Not found test data for: {__file__}", + allow_module_level=True + ) + +DATA_IDS_0: list[str] = common.get_test_ids(DATA_0) +Parser = getattr(common.get_mod(__file__), "Parser", None) + +assert Parser is not None + + +@pytest.mark.parametrize(common.NAMES, DATA_0, ids=DATA_IDS_0) +def test_loads(ipath: str, opts: dict, exp) -> None: + psr = Parser() + content = psr.ropen(ipath).read() + + assert psr.loads(content, **opts) == exp + + +@pytest.mark.parametrize(common.NAMES, DATA_0, ids=DATA_IDS_0) +def test_load(ipath: str, opts: dict, exp) -> None: + psr = Parser() + ioi = common.ioinfo_from_path(ipath) + + assert psr.load(ioi, **opts) == exp + class TDI(tests.common.tdi_base.TDI): _cid = tests.common.tdi_base.name_from_path(__file__)