Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 62 additions & 25 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,98 @@
import zipfile

import pytest
import yaml

from cihai.data.unihan.constants import UNIHAN_FILES

if t.TYPE_CHECKING:
from cihai.types import UntypedDict as UnihanOptions
from cihai.types import UntypedDict


@pytest.fixture()
def tests_path() -> pathlib.Path:
"""Return tests/ directory."""
return pathlib.Path(__file__).parent
@pytest.fixture(scope="session")
def project_path() -> pathlib.Path:
return pathlib.Path(__file__).parent.parent


@pytest.fixture()
def fixture_path(tests_path: pathlib.Path) -> pathlib.Path:
"""Return tests/fixtures/ directory."""
return tests_path / "fixtures"
@pytest.fixture(scope="session")
def cache_path(project_path: pathlib.Path) -> pathlib.Path:
return project_path / ".cihai_cache"


@pytest.fixture()
def test_config_file(fixture_path: pathlib.Path) -> pathlib.Path:
"""Return test_config.yml file."""
return fixture_path / "test_config.yml"
@pytest.fixture(scope="session", autouse=True)
def ensure_cache_path(cache_path: pathlib.Path) -> None:
cache_path.mkdir(parents=True, exist_ok=True)


@pytest.fixture()
def zip_path(tmp_path: pathlib.Path) -> pathlib.Path:
"""Return Unihan.zip in temporary path."""
return tmp_path / "Unihan.zip"
@pytest.fixture(scope="session")
def tests_path(project_path: pathlib.Path) -> pathlib.Path:
return project_path / "tests"


@pytest.fixture()
@pytest.fixture(scope="session")
def fixture_path(tests_path: pathlib.Path) -> pathlib.Path:
"""Return tests/fixtures/ directory."""
return tests_path / "fixtures"


@pytest.fixture(scope="session")
def test_config_file_path(cache_path: pathlib.Path) -> pathlib.Path:
return cache_path / "test_config.yml"


@pytest.fixture(scope="session")
def test_config_file(
test_config_file_path: pathlib.Path,
unihan_options: "UntypedDict",
) -> pathlib.Path:
with test_config_file_path.open("w") as file:
config = yaml.dump(
{
"database": {"url": "sqlite:///:memory:"},
"unihan_options": {
"source": str(unihan_options["source"]),
"work_dir": str(unihan_options["work_dir"]),
"zip_path": str(unihan_options["zip_path"]),
},
},
Dumper=yaml.SafeDumper,
indent=True,
default_flow_style=False,
allow_unicode=True,
)
file.write(config)
return test_config_file_path


@pytest.fixture(scope="session")
def zip_path(cache_path: pathlib.Path) -> pathlib.Path:
return cache_path / "Unihan.zip"


@pytest.fixture(scope="session")
def zip_file(zip_path: pathlib.Path, fixture_path: pathlib.Path) -> zipfile.ZipFile:
"""Create and return ZipFile."""
_files = []
for f in UNIHAN_FILES:
_files += [fixture_path / f]
zf = zipfile.ZipFile(zip_path, "a")
for _f in _files:
zf.write(_f, _f.name)
if _f.name not in zf.namelist():
zf.write(_f, _f.name)
zf.close()
return zf


@pytest.fixture()
@pytest.fixture(scope="session")
def unihan_options(
zip_file: zipfile.ZipFile,
zip_path: pathlib.Path,
tmp_path: pathlib.Path,
) -> "UnihanOptions":
"""Return UnihanOptions for fixture."""
cache_path: pathlib.Path,
) -> "UntypedDict":
return {
"source": zip_path,
"work_dir": tmp_path,
"zip_path": tmp_path / "downloads" / "Moo.zip",
"work_dir": cache_path / "work_dir",
"zip_path": cache_path / "downloads" / "Moo.zip",
}


Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/test_config.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
database:
url: 'sqlite:///:memory:'
unihan_options:
source: /home/t/work/cihai/cihai-cli/.cihai_cache/Unihan.zip
work_dir: /home/t/work/cihai/cihai-cli/.cihai_cache
zip_path: /home/t/work/cihai/cihai-cli/.cihai_cache/downloads/Moo.zip
6 changes: 4 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from cihai_cli.cli import cli

if t.TYPE_CHECKING:
from cihai.types import UntypedDict as UnihanOptions
from cihai.types import UntypedDict


def test_cli(
Expand All @@ -29,6 +29,8 @@ def test_cli(

with contextlib.suppress(SystemExit):
cli(["info"])
captured = capsys.readouterr()
assert "usage" in captured.out

with contextlib.suppress(SystemExit):
cli(["reverse"])
Expand All @@ -40,7 +42,7 @@ def test_cli_reflects_after_bootstrap(
caplog: pytest.LogCaptureFixture,
monkeypatch: pytest.MonkeyPatch,
tmpdb_file: pathlib.Path,
unihan_options: "UnihanOptions",
unihan_options: "UntypedDict",
) -> None:
"""High-level, integrative CLI-based test."""
config = {
Expand Down