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-2018 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 unittest | import unittest | ||||
from unittest.mock import patch | from unittest.mock import patch | ||||
import pytest | import pytest | ||||
from swh.model.hashutil import hash_to_bytes | |||||
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, FossologyLicenseRangeIndexer, | FossologyLicenseIndexer, FossologyLicenseRangeIndexer, | ||||
compute_license | compute_license | ||||
) | ) | ||||
from swh.indexer.tests.utils import ( | from swh.indexer.tests.utils import ( | ||||
SHA1_TO_LICENSES, CommonContentIndexerTest, CommonContentIndexerRangeTest, | SHA1_TO_LICENSES, CommonContentIndexerTest, CommonContentIndexerRangeTest, | ||||
▲ Show 20 Lines • Show All 56 Lines • ▼ Show 20 Lines | class TestFossologyLicenseIndexer(CommonContentIndexerTest, unittest.TestCase): | ||||
"""Language indexer test scenarios: | """Language indexer test scenarios: | ||||
- Known sha1s in the input list have their data indexed | - Known sha1s in the input list have their data indexed | ||||
- Unknown sha1 in the input list are not indexed | - Unknown sha1 in the input list are not indexed | ||||
""" | """ | ||||
def get_indexer_results(self, ids): | def get_indexer_results(self, ids): | ||||
yield from self.idx_storage.content_fossology_license_get(ids) | return self.idx_storage.content_fossology_license_get(ids) | ||||
def setUp(self): | def setUp(self): | ||||
super().setUp() | super().setUp() | ||||
# replace actual license computation with a mock | # replace actual license computation with a mock | ||||
self.orig_compute_license = fossology_license.compute_license | self.orig_compute_license = fossology_license.compute_license | ||||
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 = '01c9379dfc33803963d07c1ccc748d3fe4c96bb5' | ||||
self.id1 = '688a5ef812c53907562fe379d4b3851e69c7cb15' | self.id1 = '688a5ef812c53907562fe379d4b3851e69c7cb15' | ||||
self.id2 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' # empty content | self.id2 = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' # empty content | ||||
tool = {k.replace('tool_', ''): v | tool = {k.replace('tool_', ''): v | ||||
for (k, v) in self.indexer.tool.items()} | for (k, v) in self.indexer.tool.items()} | ||||
# then | # then | ||||
self.expected_results = { | self.expected_results = { | ||||
self.id0: { | self.id0: [{ | ||||
'tool': tool, | 'tool': tool, | ||||
'licenses': SHA1_TO_LICENSES[self.id0], | 'licenses': SHA1_TO_LICENSES[self.id0], | ||||
}, | }], | ||||
self.id1: { | self.id1: [{ | ||||
'tool': tool, | 'tool': tool, | ||||
'licenses': SHA1_TO_LICENSES[self.id1], | 'licenses': SHA1_TO_LICENSES[self.id1], | ||||
}, | }], | ||||
self.id2: { | self.id2: [{ | ||||
'tool': tool, | 'tool': tool, | ||||
'licenses': SHA1_TO_LICENSES[self.id2], | 'licenses': SHA1_TO_LICENSES[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 | ||||
def assert_results_ok(self, sha1s, expected_results=None): | |||||
if self.legacy_get_format: | |||||
self.assert_legacy_results_ok(sha1s, expected_results) | |||||
return | |||||
vlorentz: This conditional is not needed | |||||
sha1s = [sha1 if isinstance(sha1, bytes) else hash_to_bytes(sha1) | |||||
for sha1 in sha1s] | |||||
vlorentzAuthorUnsubmitted Done Inline ActionsNeither is this one vlorentz: Neither is this one | |||||
actual_results = self.get_indexer_results(sha1s) | |||||
if expected_results is None: | |||||
expected_results = self.expected_results | |||||
vlorentzAuthorUnsubmitted Done Inline ActionsI'm not sure this one is either vlorentz: I'm not sure this one is either | |||||
for _id, indexed_data in expected_results.items(): | |||||
actual_data = actual_results[hash_to_bytes(_id)] | |||||
self.assertEqual(indexed_data, actual_data) | |||||
vlorentzAuthorUnsubmitted Done Inline ActionsYou can replace this for loop with simply: self.assertEqual(expected_results, actual_results). vlorentz: You can replace this for loop with simply: `self.assertEqual(expected_results, actual_results)`. | |||||
class TestFossologyLicenseRangeIndexer( | class TestFossologyLicenseRangeIndexer( | ||||
CommonContentIndexerRangeTest, unittest.TestCase): | CommonContentIndexerRangeTest, unittest.TestCase): | ||||
"""Range Fossology License Indexer tests. | """Range Fossology License Indexer tests. | ||||
- new data within range are indexed | - new data within range are indexed | ||||
- no data outside a range are indexed | - no data outside a range are indexed | ||||
- with filtering existing indexed data prior to compute new index | - with filtering existing indexed data prior to compute new index | ||||
▲ Show 20 Lines • Show All 50 Lines • Show Last 20 Lines |
This conditional is not needed