diff --git a/swh/scanner/tests/conftest.py b/swh/scanner/tests/conftest.py index 5036b13..0502129 100644 --- a/swh/scanner/tests/conftest.py +++ b/swh/scanner/tests/conftest.py @@ -1,141 +1,143 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import asyncio import os from pathlib import Path import shutil import aiohttp from aioresponses import aioresponses # type: ignore import pytest from swh.model.cli import swhid_of_dir, swhid_of_file from swh.scanner.model import Tree from .flask_api import create_app @pytest.fixture def mock_aioresponse(): with aioresponses() as m: yield m @pytest.fixture def event_loop(): """Fixture that generate an asyncio event loop.""" loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) yield loop loop.close() @pytest.fixture async def aiosession(): """Fixture that generate an aiohttp Client Session.""" session = aiohttp.ClientSession() yield session session.detach() -@pytest.fixture(scope="session") -def temp_folder(tmp_path_factory): +@pytest.fixture(scope="function") +def temp_folder(tmp_path): """Fixture that generates a temporary folder with the following structure: .. code-block:: python root = { subdir: { subsubdir filesample.txt filesample2.txt } subdir2 subfile.txt } """ - root = tmp_path_factory.getbasetemp() - subdir = tmp_path_factory.mktemp("subdir") - subsubdir = subdir.joinpath("subsubdir") + root = tmp_path + subdir = root / "subdir" + subdir.mkdir() + subsubdir = subdir / "subsubdir" subsubdir.mkdir() - subdir2 = tmp_path_factory.mktemp("subdir2") + subdir2 = root / "subdir2" + subdir2.mkdir() subfile = root / "subfile.txt" subfile.touch() filesample = subdir / "filesample.txt" filesample.touch() filesample2 = subdir / "filesample2.txt" filesample2.touch() avail_path = { subdir: swhid_of_dir(bytes(subdir)), subsubdir: swhid_of_dir(bytes(subsubdir)), subdir2: swhid_of_dir(bytes(subdir2)), subfile: swhid_of_file(bytes(subfile)), filesample: swhid_of_file(bytes(filesample)), filesample2: swhid_of_file(bytes(filesample2)), } return { "root": root, "paths": avail_path, "filesample": filesample, "filesample2": filesample2, "subsubdir": subsubdir, "subdir": subdir, } @pytest.fixture(scope="function") def example_tree(temp_folder): """Fixture that generate a Tree with the root present in the session fixture "temp_folder". """ example_tree = Tree(temp_folder["root"]) assert example_tree.path == temp_folder["root"] return example_tree @pytest.fixture(scope="function") def example_dirs(example_tree, temp_folder): """ Fixture that fill the fixture example_tree with the values contained in the fixture temp_folder and returns the directories information of the filled example_tree. """ root = temp_folder["root"] filesample_path = temp_folder["filesample"] filesample2_path = temp_folder["filesample2"] subsubdir_path = temp_folder["subsubdir"] known_paths = [filesample_path, filesample2_path, subsubdir_path] for path, swhid in temp_folder["paths"].items(): if path in known_paths: example_tree.add_node(path, swhid, True) else: example_tree.add_node(path, swhid, False) return example_tree.get_directories_info(root) @pytest.fixture def test_sample_folder(datadir, tmp_path): """Location of the "data" folder """ archive_path = Path(os.path.join(datadir, "sample-folder.tgz")) assert archive_path.exists() shutil.unpack_archive(archive_path, extract_dir=tmp_path) test_sample_folder = Path(os.path.join(tmp_path, "sample-folder")) assert test_sample_folder.exists() return test_sample_folder @pytest.fixture(scope="session") def app(): """Flask backend API (used by live_server).""" app = create_app() return app diff --git a/swh/scanner/tests/test_model.py b/swh/scanner/tests/test_model.py index f089ace..dc3b444 100644 --- a/swh/scanner/tests/test_model.py +++ b/swh/scanner/tests/test_model.py @@ -1,108 +1,108 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import pytest from swh.scanner.exceptions import InvalidDirectoryPath def test_tree_add_node(example_tree, temp_folder): avail_paths = temp_folder["paths"].keys() for path, swhid in temp_folder["paths"].items(): example_tree.add_node(path, swhid, False) for path, node in example_tree.children.items(): assert path in avail_paths if node.children: for subpath, subnode in node.children.items(): assert subpath in avail_paths def test_to_json_no_one_present(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): example_tree.add_node(path, swhid, False) result = example_tree.to_dict() assert len(result) == 6 for _, node_info in result.items(): assert node_info["known"] is False def test_get_json_tree_all_present(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): example_tree.add_node(path, swhid, True) result = example_tree.to_dict() assert len(result) == 6 for _, node_info in result.items(): assert node_info["known"] is True def test_get_json_tree_only_one_present(example_tree, temp_folder): root = temp_folder["root"] filesample_path = temp_folder["filesample"] for path, swhid in temp_folder["paths"].items(): example_tree.add_node(path, swhid, True if path == filesample_path else False) result = example_tree.to_dict() assert len(result) == 6 for path, node_attr in result.items(): - if path == str(root) + "/subdir0/filesample.txt": + if path == str(root) + "/subdir/filesample.txt": assert node_attr["known"] is True else: assert node_attr["known"] is False def test_get_directories_info(example_tree, temp_folder): root_path = temp_folder["root"] filesample_path = temp_folder["filesample"] filesample2_path = temp_folder["filesample2"] subdir_path = temp_folder["subdir"].relative_to(root_path) subsubdir_path = temp_folder["subsubdir"].relative_to(root_path) for path, swhid in temp_folder["paths"].items(): if path == filesample_path or path == filesample2_path: example_tree.add_node(path, swhid, True) else: example_tree.add_node(path, swhid, False) directories = example_tree.get_directories_info(example_tree.path) assert subsubdir_path not in directories assert directories[subdir_path] == (2, 2) def test_get_files_from_dir(example_tree, temp_folder): subdir_path = temp_folder["subdir"] for path, swhid in temp_folder["paths"].items(): example_tree.add_node(path, swhid, True) files = example_tree.get_files_from_dir(subdir_path) assert len(files) == 2 def test_get_files_source_path(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): example_tree.add_node(path, swhid, True) files = example_tree.get_files_from_dir(example_tree.path) assert len(files) == 1 def test_get_files_from_dir_raise_exception(example_tree, temp_folder): for path, swhid in temp_folder["paths"].items(): example_tree.add_node(path, swhid, True) with pytest.raises(InvalidDirectoryPath): example_tree.get_files_from_dir("test/") diff --git a/swh/scanner/tests/test_plot.py b/swh/scanner/tests/test_plot.py index 5ad5548..cec801b 100644 --- a/swh/scanner/tests/test_plot.py +++ b/swh/scanner/tests/test_plot.py @@ -1,57 +1,57 @@ # Copyright (C) 2020 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information from swh.scanner.plot import ( build_hierarchical_df, compute_max_depth, generate_df_from_dirs, ) def test_max_depth(temp_folder, example_dirs): root = temp_folder["root"] max_depth = compute_max_depth(example_dirs, root) assert max_depth == 2 def test_generate_df_from_dirs(temp_folder, example_dirs): root = temp_folder["root"] max_depth = compute_max_depth(example_dirs, root) metrics_columns = ["contents", "known"] levels_columns = ["lev" + str(i) for i in range(max_depth)] df_columns = levels_columns + metrics_columns actual_df = generate_df_from_dirs(example_dirs, df_columns, root, max_depth) # assert root is empty assert actual_df["lev0"][0] == "" assert actual_df["lev1"][0] == "" # assert subdir has correct contents information assert actual_df["contents"][1] == 2 assert actual_df["known"][1] == 2 # assert subsubdir has correct level information - assert actual_df["lev0"][2] == "subdir0" - assert actual_df["lev1"][2] == "subdir0/subsubdir" + assert actual_df["lev0"][2] == "subdir" + assert actual_df["lev1"][2] == "subdir/subsubdir" def test_build_hierarchical_df(temp_folder, example_dirs): root = temp_folder["root"] max_depth = compute_max_depth(example_dirs, root) metrics_columns = ["contents", "known"] levels_columns = ["lev" + str(i) for i in range(max_depth)] df_columns = levels_columns + metrics_columns actual_df = generate_df_from_dirs(example_dirs, df_columns, root, max_depth) actual_result = build_hierarchical_df( actual_df, levels_columns, metrics_columns, root ) - assert actual_result["parent"][1] == "subdir0" + assert actual_result["parent"][1] == "subdir" assert actual_result["contents"][1] == 2 assert actual_result["id"][5] == root assert actual_result["known"][5] == 75