Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/test_fossology_license.py
# Copyright (C) 2017-2018 The Software Heritage developers | # Copyright (C) 2017-2022 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 | ||||
from typing import Any, Dict | from typing import Any, Dict | ||||
import unittest | import unittest | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
import pytest | import pytest | ||||
from swh.indexer import fossology_license | from swh.indexer import fossology_license | ||||
from swh.indexer.fossology_license import ( | from swh.indexer.fossology_license import ( | ||||
FossologyLicenseIndexer, | FossologyLicenseIndexer, | ||||
FossologyLicensePartitionIndexer, | FossologyLicensePartitionIndexer, | ||||
compute_license, | compute_license, | ||||
) | ) | ||||
from swh.indexer.storage.model import ContentLicenseRow | from swh.indexer.storage.model import ContentLicenseRow | ||||
from swh.indexer.tests.utils import ( | from swh.indexer.tests.utils import ( | ||||
BASE_TEST_CONFIG, | BASE_TEST_CONFIG, | ||||
RAW_CONTENT_IDS, | |||||
SHA1_TO_LICENSES, | SHA1_TO_LICENSES, | ||||
CommonContentIndexerPartitionTest, | CommonContentIndexerPartitionTest, | ||||
CommonContentIndexerTest, | CommonContentIndexerTest, | ||||
fill_obj_storage, | fill_obj_storage, | ||||
fill_storage, | fill_storage, | ||||
filter_dict, | filter_dict, | ||||
) | ) | ||||
from swh.model.hashutil import hash_to_bytes | from swh.model.hashutil import hash_to_bytes | ||||
Show All 21 Lines | def test_compute_license(self, mock_subprocess): | ||||
) | ) | ||||
def mock_compute_license(path): | def mock_compute_license(path): | ||||
"""path is the content identifier""" | """path is the content identifier""" | ||||
if isinstance(id, bytes): | if isinstance(id, bytes): | ||||
path = path.decode("utf-8") | path = path.decode("utf-8") | ||||
# path is something like /tmp/tmpXXX/<sha1> so we keep only the sha1 part | # path is something like /tmp/tmpXXX/<sha1> so we keep only the sha1 part | ||||
path = path.split("/")[-1] | id_ = path.split("/")[-1] | ||||
return {"licenses": SHA1_TO_LICENSES.get(path, [])} | return {"licenses": SHA1_TO_LICENSES.get(hash_to_bytes(id_), [])} | ||||
CONFIG = { | CONFIG = { | ||||
**BASE_TEST_CONFIG, | **BASE_TEST_CONFIG, | ||||
"workdir": "/tmp", | "workdir": "/tmp", | ||||
"tools": { | "tools": { | ||||
"name": "nomos", | "name": "nomos", | ||||
"version": "3.1.0rc2-31-ga2cbb8c", | "version": "3.1.0rc2-31-ga2cbb8c", | ||||
Show All 24 Lines | def setUp(self): | ||||
fossology_license.compute_license = mock_compute_license | fossology_license.compute_license = mock_compute_license | ||||
self.indexer = FossologyLicenseIndexer(CONFIG) | self.indexer = FossologyLicenseIndexer(CONFIG) | ||||
self.indexer.catch_exceptions = False | self.indexer.catch_exceptions = False | ||||
self.idx_storage = self.indexer.idx_storage | self.idx_storage = self.indexer.idx_storage | ||||
fill_storage(self.indexer.storage) | fill_storage(self.indexer.storage) | ||||
fill_obj_storage(self.indexer.objstorage) | fill_obj_storage(self.indexer.objstorage) | ||||
self.id0 = "01c9379dfc33803963d07c1ccc748d3fe4c96bb5" | self.id0, self.id1, self.id2 = RAW_CONTENT_IDS | ||||
self.id1 = "688a5ef812c53907562fe379d4b3851e69c7cb15" | |||||
self.id2 = "da39a3ee5e6b4b0d3255bfef95601890afd80709" # empty content | |||||
tool = {k.replace("tool_", ""): v for (k, v) in self.indexer.tool.items()} | tool = {k.replace("tool_", ""): v for (k, v) in self.indexer.tool.items()} | ||||
# then | # then | ||||
self.expected_results = [ | self.expected_results = [ | ||||
*[ | *[ | ||||
ContentLicenseRow( | ContentLicenseRow(id=self.id0, tool=tool, license=license) | ||||
id=hash_to_bytes(self.id0), tool=tool, license=license | |||||
) | |||||
for license in SHA1_TO_LICENSES[self.id0] | for license in SHA1_TO_LICENSES[self.id0] | ||||
], | ], | ||||
*[ | *[ | ||||
ContentLicenseRow( | ContentLicenseRow(id=self.id1, tool=tool, license=license) | ||||
id=hash_to_bytes(self.id1), tool=tool, license=license | |||||
) | |||||
for license in SHA1_TO_LICENSES[self.id1] | for license in SHA1_TO_LICENSES[self.id1] | ||||
], | ], | ||||
*[], # self.id2 | *[], # self.id2 | ||||
] | ] | ||||
def tearDown(self): | def tearDown(self): | ||||
super().tearDown() | super().tearDown() | ||||
fossology_license.compute_license = self.orig_compute_license | fossology_license.compute_license = self.orig_compute_license | ||||
Show All 39 Lines |