Changeset View
Changeset View
Standalone View
Standalone View
swh/scanner/tests/test_model.py
# Copyright (C) 2020 The Software Heritage developers | # Copyright (C) 2020 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
import pytest | import pytest | ||||
from swh.scanner.exceptions import InvalidDirectoryPath | from swh.scanner.exceptions import InvalidDirectoryPath | ||||
def test_tree_add_node(example_tree, temp_folder): | def test_tree_add_node(example_tree, temp_folder): | ||||
avail_paths = temp_folder["paths"].keys() | avail_paths = temp_folder["paths"].keys() | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
example_tree.addNode(path, swhid, False) | example_tree.add_node(path, swhid, False) | ||||
for path, node in example_tree.children.items(): | for path, node in example_tree.children.items(): | ||||
assert path in avail_paths | assert path in avail_paths | ||||
if node.children: | if node.children: | ||||
for subpath, subnode in node.children.items(): | for subpath, subnode in node.children.items(): | ||||
assert subpath in avail_paths | assert subpath in avail_paths | ||||
def test_to_json_no_one_present(example_tree, temp_folder): | def test_to_json_no_one_present(example_tree, temp_folder): | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
example_tree.addNode(path, swhid, False) | example_tree.add_node(path, swhid, False) | ||||
result = example_tree.toDict() | result = example_tree.to_dict() | ||||
assert len(result) == 6 | assert len(result) == 6 | ||||
for _, node_info in result.items(): | for _, node_info in result.items(): | ||||
assert node_info["known"] is False | assert node_info["known"] is False | ||||
def test_get_json_tree_all_present(example_tree, temp_folder): | def test_get_json_tree_all_present(example_tree, temp_folder): | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
example_tree.addNode(path, swhid, True) | example_tree.add_node(path, swhid, True) | ||||
result = example_tree.toDict() | result = example_tree.to_dict() | ||||
assert len(result) == 6 | assert len(result) == 6 | ||||
for _, node_info in result.items(): | for _, node_info in result.items(): | ||||
assert node_info["known"] is True | assert node_info["known"] is True | ||||
def test_get_json_tree_only_one_present(example_tree, temp_folder): | def test_get_json_tree_only_one_present(example_tree, temp_folder): | ||||
root = temp_folder["root"] | root = temp_folder["root"] | ||||
filesample_path = temp_folder["filesample"] | filesample_path = temp_folder["filesample"] | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
example_tree.addNode(path, swhid, True if path == filesample_path else False) | example_tree.add_node(path, swhid, True if path == filesample_path else False) | ||||
result = example_tree.toDict() | result = example_tree.to_dict() | ||||
assert len(result) == 6 | assert len(result) == 6 | ||||
for path, node_attr in result.items(): | for path, node_attr in result.items(): | ||||
if path == str(root) + "/subdir0/filesample.txt": | if path == str(root) + "/subdir0/filesample.txt": | ||||
assert node_attr["known"] is True | assert node_attr["known"] is True | ||||
else: | else: | ||||
assert node_attr["known"] is False | assert node_attr["known"] is False | ||||
def test_get_directories_info(example_tree, temp_folder): | def test_get_directories_info(example_tree, temp_folder): | ||||
root_path = temp_folder["root"] | root_path = temp_folder["root"] | ||||
filesample_path = temp_folder["filesample"] | filesample_path = temp_folder["filesample"] | ||||
filesample2_path = temp_folder["filesample2"] | filesample2_path = temp_folder["filesample2"] | ||||
subdir_path = temp_folder["subdir"].relative_to(root_path) | subdir_path = temp_folder["subdir"].relative_to(root_path) | ||||
subsubdir_path = temp_folder["subsubdir"].relative_to(root_path) | subsubdir_path = temp_folder["subsubdir"].relative_to(root_path) | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
if path == filesample_path or path == filesample2_path: | if path == filesample_path or path == filesample2_path: | ||||
example_tree.addNode(path, swhid, True) | example_tree.add_node(path, swhid, True) | ||||
else: | else: | ||||
example_tree.addNode(path, swhid, False) | example_tree.add_node(path, swhid, False) | ||||
directories = example_tree.getDirectoriesInfo(example_tree.path) | directories = example_tree.get_directories_info(example_tree.path) | ||||
assert subsubdir_path not in directories | assert subsubdir_path not in directories | ||||
assert directories[subdir_path] == (2, 2) | assert directories[subdir_path] == (2, 2) | ||||
def test_get_files_from_dir(example_tree, temp_folder): | def test_get_files_from_dir(example_tree, temp_folder): | ||||
subdir_path = temp_folder["subdir"] | subdir_path = temp_folder["subdir"] | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
example_tree.addNode(path, swhid, True) | example_tree.add_node(path, swhid, True) | ||||
files = example_tree.getFilesFromDir(subdir_path) | files = example_tree.get_files_from_dir(subdir_path) | ||||
assert len(files) == 2 | assert len(files) == 2 | ||||
def test_get_files_source_path(example_tree, temp_folder): | def test_get_files_source_path(example_tree, temp_folder): | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
example_tree.addNode(path, swhid, True) | example_tree.add_node(path, swhid, True) | ||||
files = example_tree.getFilesFromDir(example_tree.path) | files = example_tree.get_files_from_dir(example_tree.path) | ||||
assert len(files) == 1 | assert len(files) == 1 | ||||
def test_get_files_from_dir_raise_exception(example_tree, temp_folder): | def test_get_files_from_dir_raise_exception(example_tree, temp_folder): | ||||
for path, swhid in temp_folder["paths"].items(): | for path, swhid in temp_folder["paths"].items(): | ||||
example_tree.addNode(path, swhid, True) | example_tree.add_node(path, swhid, True) | ||||
with pytest.raises(InvalidDirectoryPath): | with pytest.raises(InvalidDirectoryPath): | ||||
example_tree.getFilesFromDir("test/") | example_tree.get_files_from_dir("test/") |