pytest
plugin¶
Download and reuse UNIHAN.zip on the fly in pytest.
Usage¶
Install unihan-etl
via the python package manager of your choosing, e.g.
$ pip install unihan-etl
The pytest plugin will automatically be detected via pytest, and the fixtures will be added.
Fixtures¶
pytest-unihan
works through providing pytest fixtures - so read up on
those!
The plugin’s fixtures guarantee downloading, and then reusing UNIHAN.zip every test without needing to redownload.
Recommended fixtures¶
These fixtures are automatically used when the plugin is enabled and pytest
is run.
Creating temporary, test directories for:
/home/
(home_path()
)/home/${user}
(user_path()
)
Setting your home directory
Patch
$HOME
to point touser_path()
(set_home()
)
Bootstrapping pytest in your conftest.py
¶
The most common scenario is you will want to configure the above fixtures with autouse
.
Why doesn’t the plugin automatically add them? It’s part of being a decent pytest plugin and python package: explicitness.
Setting a temporary home directory¶
import pytest
@pytest.fixture(autouse=True)
def setup(
set_home: None,
):
pass
See examples¶
View unihan-etl’s own tests/
API reference¶
pytest plugin for unihan-etl.
- unihan_etl.pytest_plugin.unihan_user_cache_path()[source]¶
unihan-etl cache directory, overridable.
- Return type:
Path
- unihan_etl.pytest_plugin.unihan_project_cache_path()[source]¶
Return unihan_etl project-based cache path. Override to path of your choice.
- Return type:
Path
- unihan_etl.pytest_plugin.unihan_cache_path(unihan_project_cache_path)[source]¶
Return unihan_etl cache path, override this to destination of your choice.
- Return type:
Path
- Parameters:
unihan_project_cache_path (Path)
- unihan_etl.pytest_plugin.unihan_fixture_root(unihan_cache_path)[source]¶
Return pytest cached directory fixture root.
- Return type:
Path
- Parameters:
unihan_cache_path (Path)
- unihan_etl.pytest_plugin.unihan_full_path(unihan_fixture_root)[source]¶
Return directory path for “full” UNIHAN dataset.
- Return type:
Path
- Parameters:
unihan_fixture_root (Path)
- unihan_etl.pytest_plugin.unihan_full_options(unihan_full_path)[source]¶
Return UnihanOptions for “full” UNIHAN dataset.
- Return type:
- Parameters:
unihan_full_path (Path)
- unihan_etl.pytest_plugin.unihan_full_packager(unihan_full_path, unihan_full_options)[source]¶
Return Packager for “full” portion of UNIHAN, return a UnihanOptions.
- unihan_etl.pytest_plugin.unihan_ensure_full(unihan_full_path, unihan_full_options, unihan_full_packager)[source]¶
Download and extract “full” UNIHAN, return UnihanOptions.
- Return type:
- Parameters:
>>> import pathlib
>>> from unihan_etl.core import Packager >>> from unihan_etl.options import Options as UnihanOptions
>>> def test_unihan_ensure_full( ... unihan_full_path: pathlib.Path, ... unihan_full_options: "UnihanOptions", ... unihan_full_packager: "Packager", ... ) -> None: ... unihan_full_destination = unihan_full_options.destination ... assert unihan_full_destination.exists() ... assert unihan_full_destination.stat().st_size > 20_000_000 ... ... assert unihan_full_options.work_dir.exists() ... unihan_readings = unihan_full_options.work_dir / 'Unihan_Readings.txt' ... assert unihan_readings.stat().st_size > 6_200_000
Extending fixtures:
>>> import pathlib
>>> import pytest
>>> from unihan_etl.core import Packager >>> from unihan_etl.options import Options as UnihanOptions
>>> @pytest.fixture ... def my_unihan( ... unihan_full_path: pathlib.Path, ... unihan_full_options: "UnihanOptions", ... unihan_full_packager: "Packager", ... ) -> "Packager": ... return unihan_full_packager
>>> def test_my_extended_unihan_Fixture(my_unihan: "Packager") -> None: ... my_unihan.download() ... my_unihan_destination = my_unihan.options.destination ... if not my_unihan_destination.exists(): ... my_unihan.export() ... assert my_unihan_destination.exists() ... assert my_unihan_destination.stat().st_size > 20_000_000 ... ... assert my_unihan.options.work_dir.exists() ... unihan_readings = my_unihan.options.work_dir / 'Unihan_Readings.txt' ... assert unihan_readings.stat().st_size > 6_200_000
- unihan_etl.pytest_plugin.unihan_quick_path(unihan_fixture_root)[source]¶
Return directory path for “quick” test data set.
- Return type:
Path
- Parameters:
unihan_fixture_root (Path)
- unihan_etl.pytest_plugin.unihan_quick_zip_path(unihan_quick_path)[source]¶
Return zip file path for “quick” test data set.
- Return type:
Path
- Parameters:
unihan_quick_path (Path)
- unihan_etl.pytest_plugin.unihan_quick_zip(unihan_quick_path, unihan_quick_zip_path, unihan_quick_fixture_files)[source]¶
Return zip file for “quick” test data set.
- unihan_etl.pytest_plugin.unihan_quick_options(unihan_quick_path, unihan_quick_zip, unihan_quick_zip_path)[source]¶
Return UnihanOptions for “quick” test data set.
- unihan_etl.pytest_plugin.unihan_quick_packager(unihan_quick_path, unihan_quick_options)[source]¶
Bootstrap a small, but effective portion of UNIHAN, return a UnihanOptions.
- unihan_etl.pytest_plugin.unihan_ensure_quick(unihan_quick_path, unihan_quick_options, unihan_quick_packager)[source]¶
Return a small, but effective portion of UNIHAN, return a UnihanOptions.
- Return type:
- Parameters:
>>> import pathlib
>>> from unihan_etl.core import Packager >>> from unihan_etl.options import Options as UnihanOptions
>>> def test_unihan_ensure_quick( ... unihan_quick_path: pathlib.Path, ... unihan_quick_options: "UnihanOptions", ... unihan_quick_packager: "Packager", ... ) -> None: ... unihan_quick_destination = unihan_quick_options.destination ... assert unihan_quick_destination.exists() ... assert unihan_quick_destination.stat().st_size >= 140_000 ... assert unihan_quick_destination.stat().st_size < 200_000 ... ... assert unihan_quick_options.work_dir.exists() ... unihan_readings = unihan_quick_options.work_dir / 'Unihan_Readings.txt' ... assert unihan_readings.stat().st_size >= 21_631 ... assert unihan_readings.stat().st_size < 30_000
Extending fixtures:
>>> import pathlib
>>> import pytest
>>> from unihan_etl.core import Packager >>> from unihan_etl.options import Options as UnihanOptions
>>> @pytest.fixture ... def my_unihan( ... unihan_quick_path: pathlib.Path, ... unihan_quick_options: "UnihanOptions", ... unihan_quick_packager: "Packager", ... ) -> "Packager": ... return unihan_quick_packager
>>> def test_my_extended_unihan_Fixture(my_unihan: "Packager") -> None: ... my_unihan.download() ... my_unihan_destination = my_unihan.options.destination ... if not my_unihan_destination.exists(): ... my_unihan.export() ... assert my_unihan_destination.exists() ... assert my_unihan_destination.stat().st_size >= 140_000 ... assert my_unihan_destination.stat().st_size < 200_000 ... ... assert my_unihan.options.work_dir.exists() ... unihan_readings = my_unihan.options.work_dir / 'Unihan_Readings.txt' ... assert unihan_readings.stat().st_size >= 21_000 ... assert unihan_readings.stat().st_size < 30_000
- unihan_etl.pytest_plugin.unihan_bootstrap_all(unihan_ensure_full, unihan_ensure_quick)[source]¶
Noop that bootstraps all unihan_etl pytest datasets (“full” and “quick”).
This should be used like so in your project’s conftest.py:
- Return type:
- Parameters:
unihan_ensure_full (None)
unihan_ensure_quick (None)
>>> import pytest >>> @pytest.fixture(scope="session", autouse=True) ... def bootstrap(unihan_bootstrap_all) -> None: ... return None
- unihan_etl.pytest_plugin.unihan_home_path(tmp_path_factory)[source]¶
Return temporary /home/ path for use by unihan_etl pytest fixtures.
- Return type:
Path
- Parameters:
tmp_path_factory (TempPathFactory)
- unihan_etl.pytest_plugin.unihan_home_user_name()[source]¶
Return username to set for
unihan_user_path()
fixture.- Return type:
- unihan_etl.pytest_plugin.unihan_user_path(unihan_home_path, unihan_home_user_name)[source]¶
Return temporary user directory.
Used by:
unihan_zshrc()
Note: You will need to set the home directory, see Setting a temporary home directory.
- Return type:
Path
- Parameters:
unihan_home_path (Path)
unihan_home_user_name (str)
- unihan_etl.pytest_plugin.unihan_zshrc(unihan_user_path)[source]¶
Suppress ZSH default message.
Needs a startup file .zshenv, .zprofile, .unihan_zshrc, .zlogin.
- Return type:
Path
- Parameters:
unihan_user_path (Path)
- unihan_etl.pytest_plugin.unihan_mock_zip_pathname()[source]¶
Return zip file name in “quick” test data set.
- Return type:
- unihan_etl.pytest_plugin.unihan_quick_fixture_files()[source]¶
Return files used in “quick” test data set.
- Return type:
list
[Path
]
- unihan_etl.pytest_plugin.unihan_mock_test_dir(tmp_path_factory)[source]¶
Return temporary directory for unihan_etl py.test fixtures.
- Return type:
Path
- Parameters:
tmp_path_factory (TempPathFactory)
- unihan_etl.pytest_plugin.unihan_mock_zip_path(unihan_mock_test_dir, unihan_mock_zip_pathname)[source]¶
Return path to Unihan zipfile.
- Return type:
Path
- Parameters:
unihan_mock_test_dir (Path)
unihan_mock_zip_pathname (str)
- unihan_etl.pytest_plugin.unihan_mock_zip(unihan_mock_zip_path, unihan_quick_data)[source]¶
Return Unihan zipfile.
- unihan_etl.pytest_plugin.unihan_quick_columns()[source]¶
Return columns used in “quick” test data set.
- Return type:
ColumnData
- unihan_etl.pytest_plugin.unihan_quick_normalized_data(unihan_quick_columns, unihan_quick_fixture_files)[source]¶
Return normalized test data from “quick” test data set.
- Return type:
UntypedNormalizedData
- Parameters:
unihan_quick_columns (ColumnData)
unihan_quick_fixture_files (list[Path])
- unihan_etl.pytest_plugin.unihan_quick_expanded_data(unihan_quick_normalized_data)[source]¶
Return a list of expanded fields from “quick” test data.
- unihan_etl.pytest_plugin.unihan_quick_data()[source]¶
Raw snippet excerpted from UNIHAN corpus from “quick” test data.
- Return type:
>>> def test_unihan_quick_data( ... unihan_quick_data: str, ... ) -> None: ... assert isinstance(unihan_quick_data, str) ... ... assert isinstance(unihan_quick_data.splitlines()[1], str) ...