diff --git a/swh/scanner/tests/conftest.py b/swh/scanner/tests/conftest.py --- a/swh/scanner/tests/conftest.py +++ b/swh/scanner/tests/conftest.py @@ -38,19 +38,38 @@ @pytest.fixture(scope='session') -def temp_paths(tmp_path_factory): - """Fixture to generate temporary paths""" - subpath = tmp_path_factory.getbasetemp() +def temp_folder(tmp_path_factory): + """Fixture that generate a temporary folder with the following + structure: + + root: { + subdir: { + filesample.txt + } + subdir2 + subfile.txt + } + """ + root = tmp_path_factory.getbasetemp() subdir = tmp_path_factory.mktemp('subdir') subdir2 = tmp_path_factory.mktemp('subdir2') - subfile = subpath.joinpath(PosixPath('./subfile.txt')) + subfile = root.joinpath(PosixPath('./subfile.txt')) subfile.touch() + filesample = subdir.joinpath(PosixPath('./filesample.txt')) + filesample.touch() - avail_path = [subdir, subdir2, subfile] - avail_pid = [pid_of_dir(bytes(subdir)), pid_of_dir(bytes(subdir2)), - pid_of_file(bytes(subfile))] + avail_path = { + subdir: pid_of_dir(bytes(subdir)), + subdir2: pid_of_dir(bytes(subdir2)), + subfile: pid_of_file(bytes(subfile)), + filesample: pid_of_file(bytes(filesample)) + } - return {'paths': avail_path, 'pids': avail_pid} + return { + 'root': root, + 'paths': avail_path, + 'filesample': filesample + } @pytest.fixture(scope='session') @@ -62,6 +81,7 @@ @pytest.fixture def test_folder(): + """Location of the "data" folder """ tests_path = PosixPath(os.path.abspath(__file__)).parent tests_data_folder = tests_path.joinpath('data') assert tests_data_folder.exists() diff --git a/swh/scanner/tests/test_model.py b/swh/scanner/tests/test_model.py --- a/swh/scanner/tests/test_model.py +++ b/swh/scanner/tests/test_model.py @@ -2,3 +2,66 @@ # 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.model import Tree + + +@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 + + +def test_tree_add_node(example_tree, temp_folder): + avail_paths = temp_folder['paths'].keys() + + for path, pid in temp_folder['paths'].items(): + example_tree.addNode(path, pid) + + 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_get_json_tree_all_not_present(example_tree, temp_folder): + for path, pid in temp_folder['paths'].items(): + example_tree.addNode(path) + + json_tree = example_tree.getJsonTree() + + assert len(json_tree) == 0 + + +def test_get_json_tree_all_present(example_tree, temp_folder): + for path, pid in temp_folder['paths'].items(): + example_tree.addNode(path, pid) + + tree_dict = example_tree.getJsonTree() + + assert len(tree_dict) == 3 + # since subdir have a pid, it can't have a children path + assert tree_dict['subdir0'] is not dict + + +def test_get_json_tree_only_one_present(example_tree, temp_folder): + filesample_path = temp_folder['filesample'] + + for path, pid in temp_folder['paths'].items(): + if path == filesample_path: + example_tree.addNode(path, pid) + else: + example_tree.addNode(path) + + tree_dict = example_tree.getJsonTree() + + assert len(tree_dict) == 1 + assert tree_dict['subdir0']['filesample.txt'] diff --git a/swh/scanner/tests/test_scanner.py b/swh/scanner/tests/test_scanner.py --- a/swh/scanner/tests/test_scanner.py +++ b/swh/scanner/tests/test_scanner.py @@ -47,10 +47,13 @@ pids_discovery(request, aiosession, api_url)) -def test_scanner_get_subpaths(tmp_path, temp_paths): +def test_scanner_get_subpaths(temp_folder, tmp_path): + paths = temp_folder['paths'].keys() + pids = temp_folder['paths'].values() + for subpath, pid in get_subpaths(tmp_path): - assert subpath in temp_paths['paths'] - assert pid in temp_paths['pids'] + assert subpath in paths + assert pid in pids @pytest.mark.options(debug=False)