From 288233e05923c862ac57c6a9a9220e6c0e2171b2 Mon Sep 17 00:00:00 2001 From: thatmattlove Date: Mon, 26 Dec 2022 09:57:30 -0500 Subject: [PATCH] add file utility tests --- hyperglass/util/tests/test_files.py | 110 ++++++++++++++++++++++++++-- poetry.lock | 21 +++++- pyproject.toml | 1 + 3 files changed, 123 insertions(+), 9 deletions(-) diff --git a/hyperglass/util/tests/test_files.py b/hyperglass/util/tests/test_files.py index ed557ad..acc2706 100644 --- a/hyperglass/util/tests/test_files.py +++ b/hyperglass/util/tests/test_files.py @@ -1,14 +1,15 @@ """Test file-related utilities.""" # Standard Library -import tempfile +import random +import string from pathlib import Path # Third Party import pytest # Local -from ..files import dotenv_to_dict +from ..files import copyfiles, check_path, move_files, dotenv_to_dict ENV_TEST = """KEY1=VALUE1 KEY2=VALUE2 @@ -23,16 +24,15 @@ def test_dotenv_to_dict_string(): assert result.get("KEY3") == "VALUE3" -def test_dotenv_to_dict_file(): - _, filename = tempfile.mkstemp() - file = Path(filename) - with file.open("w+") as f: +def test_dotenv_to_dict_file(tmp_path_factory: pytest.TempPathFactory): + dirname = tmp_path_factory.mktemp("dotenv") + file_ = dirname / "test_dotenv_to_dict_file.env" + with file_.open("w+") as f: f.write(ENV_TEST) - result = dotenv_to_dict(file) + result = dotenv_to_dict(file_) assert result.get("KEY1") == "VALUE1" assert result.get("KEY2") == "VALUE2" assert result.get("KEY3") == "VALUE3" - file.unlink() def test_dotenv_to_dict_raises_type_error(): @@ -48,3 +48,97 @@ def test_dotenv_to_dict_raises_filenotfounderror(): def test_dotenv_invalid_format(): with pytest.raises(TypeError): dotenv_to_dict("this should raise an error") + + +def test_check_path_file(tmp_path_factory: pytest.TempPathFactory): + dir_ = tmp_path_factory.mktemp("test") + file_ = dir_ / "file.txt" + file_.touch() + result = check_path(file_) + assert result == file_ + + +def test_check_path_dir(tmp_path_factory: pytest.TempPathFactory): + dir_ = tmp_path_factory.mktemp("test") + child = dir_ / "child_dir" + child.mkdir() + result = check_path(child) + assert child.exists() + assert result == child + + +def test_check_path_create_file(tmp_path_factory: pytest.TempPathFactory): + dir_ = tmp_path_factory.mktemp("test") + file_ = dir_ / "file.txt" + result = check_path(file_, create=True) + assert file_.exists() + assert result == file_ + + +def test_check_path_create_dir(tmp_path_factory: pytest.TempPathFactory): + dir_ = tmp_path_factory.mktemp("test") + child = dir_ / "child_dir" + result = check_path(child, create=True) + assert child.exists() + assert result == child + + +def test_check_path_raises(tmp_path_factory: pytest.TempPathFactory): + dir_ = tmp_path_factory.mktemp("test") + file_ = dir_ / "file.txt" + with pytest.raises(FileNotFoundError): + check_path(file_, create=False) + + +@pytest.mark.asyncio +async def test_move_files(tmp_path_factory: pytest.TempPathFactory): + src = tmp_path_factory.mktemp("src") + dst = tmp_path_factory.mktemp("dst") + filenames = ( + "".join(random.choice(string.ascii_lowercase) for _ in range(8)) for _ in range(10) + ) + files = [src / name for name in filenames] + [f.touch() for f in files] + result = await move_files(src, dst, files) + dst_files = sorted([str(c) for c in dst.iterdir()]) + result_files = sorted(result) + assert result_files == dst_files + + +@pytest.mark.asyncio +async def test_move_files_raise(tmp_path_factory: pytest.TempPathFactory): + src = tmp_path_factory.mktemp("src") + dst = tmp_path_factory.mktemp("dst") + filenames = ( + "".join(random.choice(string.ascii_lowercase) for _ in range(8)) for _ in range(10) + ) + files = [src / name for name in filenames] + with pytest.raises(RuntimeError): + await move_files(src, dst, files) + + +def test_copyfiles(tmp_path_factory: pytest.TempPathFactory): + src = tmp_path_factory.mktemp("src") + dst = tmp_path_factory.mktemp("dst") + filenames = [ + "".join(random.choice(string.ascii_lowercase) for _ in range(8)) for _ in range(10) + ] + src_files = [src / name for name in filenames] + dst_files = [dst / name for name in filenames] + [f.touch() for f in src_files] + result = copyfiles(src_files, dst_files) + assert result + + +def test_copyfiles_wrong_length(tmp_path_factory: pytest.TempPathFactory): + src = tmp_path_factory.mktemp("src") + dst = tmp_path_factory.mktemp("dst") + filenames = [ + "".join(random.choice(string.ascii_lowercase) for _ in range(8)) for _ in range(10) + ] + dst_filenames = filenames[1:8] + src_files = [src / name for name in filenames] + dst_files = [dst / name for name in dst_filenames] + [f.touch() for f in src_files] + with pytest.raises(ValueError): + copyfiles(src_files, dst_files) diff --git a/poetry.lock b/poetry.lock index 69fdddc..5e2ed1d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -755,6 +755,21 @@ tomli = {version = ">=1.0.0", markers = "python_version < \"3.11\""} [package.extras] testing = ["argcomplete", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] +[[package]] +name = "pytest-asyncio" +version = "0.20.3" +description = "Pytest support for asyncio" +category = "dev" +optional = false +python-versions = ">=3.7" + +[package.dependencies] +pytest = ">=6.1.0" + +[package.extras] +docs = ["sphinx (>=5.3)", "sphinx-rtd-theme (>=1.0)"] +testing = ["coverage (>=6.2)", "flaky (>=3.5.0)", "hypothesis (>=5.7.1)", "mypy (>=0.931)", "pytest-trio (>=0.7.0)"] + [[package]] name = "pytest-dependency" version = "0.5.1" @@ -1138,7 +1153,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" [metadata] lock-version = "1.1" python-versions = ">=3.8.1,<4.0" -content-hash = "03ec17db8c6644b1b6e4e4fc53177107c75f23179c491e5c722a379b3bdb765e" +content-hash = "9ad50b50b99cb22ece06d5e56c58f9f1f9518fcb352acf8c072626be5c682837" [metadata.files] aiofiles = [ @@ -1765,6 +1780,10 @@ pytest = [ {file = "pytest-7.2.0-py3-none-any.whl", hash = "sha256:892f933d339f068883b6fd5a459f03d85bfcb355e4981e146d2c7616c21fef71"}, {file = "pytest-7.2.0.tar.gz", hash = "sha256:c4014eb40e10f11f355ad4e3c2fb2c6c6d1919c73f3b5a433de4708202cade59"}, ] +pytest-asyncio = [ + {file = "pytest-asyncio-0.20.3.tar.gz", hash = "sha256:83cbf01169ce3e8eb71c6c278ccb0574d1a7a3bb8eaaf5e50e0ad342afb33b36"}, + {file = "pytest_asyncio-0.20.3-py3-none-any.whl", hash = "sha256:f129998b209d04fcc65c96fc85c11e5316738358909a8399e93be553d7656442"}, +] pytest-dependency = [ {file = "pytest-dependency-0.5.1.tar.gz", hash = "sha256:c2a892906192663f85030a6ab91304e508e546cddfe557d692d61ec57a1d946b"}, ] diff --git a/pyproject.toml b/pyproject.toml index e1fc5e7..0259935 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -65,6 +65,7 @@ pytest-dependency = "^0.5.1" ruff = "^0.0.192" stackprinter = "^0.2.10" taskipy = "^1.10.3" +pytest-asyncio = "^0.20.3" [tool.black] line-length = 100