Skip to content

Commit

Permalink
enhancement: add new test loader and its test cases, for test cases o…
Browse files Browse the repository at this point in the history
…f backends
  • Loading branch information
ssato committed May 20, 2024
1 parent 87bbf34 commit 102e8c0
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/backend/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#
# Copyright (C) 2023, 2024 Satoru SATOH <satoru.satoh gmail.com>
# SPDX-License-Identifier: MIT
#
# pylint: disable=missing-docstring,too-few-public-methods
r"""Common functions for test cases of loaders and dumpers."""
from __future__ import annotations

import pathlib
import re
import typing

from .. import common


PATH_PATTERN: re.Pattern = re.compile(
r".+[/\\:\.]test_([^_]+)_([^_]+).py"
)


def get_test_resdir(
testfile: str,
is_loader: bool = True,
pattern: re.Pattern = PATH_PATTERN
) -> pathlib.Path:
"""Get test resource dir for given test file path.
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"

return common.RESOURCE_DIR / subdir / name


def load_data_for_testfile(
testfile: str,
is_loader: bool = True,
**opts
) -> list[tuple[pathlib.Path, dict[str, typing.Any], ...]]:
datadir = get_test_resdir(testfile, is_loader=is_loader)
return common.load_data_for_testfile(
testfile, datadir=datadir, **opts
)
28 changes: 28 additions & 0 deletions tests/backend/test_common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#
# Copyright (C) 2024 Satoru SATOH <satoru.satoh gmail.com>
# SPDX-License-Identifier: MIT
#
# pylint: disable=missing-docstring,too-few-public-methods
r"""Test cases for Test Data Collecor."""
from __future__ import annotations

import pathlib

import pytest

from . import common as TT


CURDIR: pathlib.Path = pathlib.Path(__file__).parent
TESTFILE = CURDIR / "loaders" / "json" / "test_json_stdlib.py"

TEST_DATADIR = TT.common.RESOURCE_DIR / "loaders" / "json.stdlib"


@pytest.mark.parametrize(
("path", "is_loader", "exp"),
((str(TESTFILE), True, TEST_DATADIR),
),
)
def test_get_test_resdir(path, is_loader, exp):
assert TT.get_test_resdir(path, is_loader) == exp

0 comments on commit 102e8c0

Please sign in to comment.