Skip to content

Commit

Permalink
change: introduce new test data loader based on tests.common.tdc to s…
Browse files Browse the repository at this point in the history
…implify test cases for loader backends
  • Loading branch information
ssato committed May 20, 2024
1 parent 78a469b commit a559cec
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
52 changes: 45 additions & 7 deletions tests/backend/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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

Expand All @@ -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)
32 changes: 32 additions & 0 deletions tests/backend/loaders/json/test_json_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)
Expand Down

0 comments on commit a559cec

Please sign in to comment.