diff --git a/swh/indexer/tests/test_ctags.py b/swh/indexer/tests/test_ctags.py --- a/swh/indexer/tests/test_ctags.py +++ b/swh/indexer/tests/test_ctags.py @@ -7,6 +7,8 @@ import unittest from unittest.mock import patch +import pytest + import swh.indexer.ctags from swh.indexer.ctags import ( CtagsIndexer, run_ctags @@ -14,9 +16,9 @@ from swh.indexer.tests.utils import ( CommonContentIndexerTest, - CommonIndexerWithErrorsTest, CommonIndexerNoTool, SHA1_TO_CTAGS, BASE_TEST_CONFIG, - OBJ_STORAGE_DATA, fill_storage, fill_obj_storage + OBJ_STORAGE_DATA, fill_storage, fill_obj_storage, + filter_dict, ) @@ -78,29 +80,24 @@ } -class CtagsIndexerTest(InjectCtagsIndexer, CtagsIndexer): - """Specific language whose configuration is enough to satisfy the - indexing tests. - """ - def parse_config_file(self, *args, **kwargs): - return { - **BASE_TEST_CONFIG, - 'tools': { - 'name': 'universal-ctags', - 'version': '~git7859817b', - 'configuration': { - 'command_line': '''ctags --fields=+lnz --sort=no ''' - ''' --links=no ''', - 'max_content_size': 1000, - }, - }, - 'languages': { - 'python': 'python', - 'haskell': 'haskell', - 'bar': 'bar', - }, - 'workdir': '/tmp', - } +CONFIG = { + **BASE_TEST_CONFIG, + 'tools': { + 'name': 'universal-ctags', + 'version': '~git7859817b', + 'configuration': { + 'command_line': '''ctags --fields=+lnz --sort=no ''' + ''' --links=no ''', + 'max_content_size': 1000, + }, + }, + 'languages': { + 'python': 'python', + 'haskell': 'haskell', + 'bar': 'bar', + }, + 'workdir': '/tmp', +} class TestCtagsIndexer(CommonContentIndexerTest, unittest.TestCase): @@ -118,7 +115,7 @@ def setUp(self): super().setUp() - self.indexer = CtagsIndexerTest() + self.indexer = CtagsIndexer(config=CONFIG) self.idx_storage = self.indexer.idx_storage fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) @@ -181,12 +178,6 @@ super().tearDown() -class CtagsIndexerUnknownToolTestStorage( - CommonIndexerNoTool, CtagsIndexerTest): - """Fossology license indexer with wrong configuration""" - - -class TestCtagsIndexersErrors( - CommonIndexerWithErrorsTest, unittest.TestCase): - """Test the indexer raise the right errors when wrongly initialized""" - Indexer = CtagsIndexerUnknownToolTestStorage +def test_ctags_w_no_tool(): + with pytest.raises(ValueError): + CtagsIndexer(config=filter_dict(CONFIG, 'tools')) diff --git a/swh/indexer/tests/test_fossology_license.py b/swh/indexer/tests/test_fossology_license.py --- a/swh/indexer/tests/test_fossology_license.py +++ b/swh/indexer/tests/test_fossology_license.py @@ -4,9 +4,10 @@ # See top-level LICENSE file for more information import unittest - from unittest.mock import patch +import pytest + from swh.indexer import fossology_license from swh.indexer.fossology_license import ( FossologyLicenseIndexer, FossologyLicenseRangeIndexer, @@ -15,8 +16,7 @@ from swh.indexer.tests.utils import ( SHA1_TO_LICENSES, CommonContentIndexerTest, CommonContentIndexerRangeTest, - CommonIndexerWithErrorsTest, CommonIndexerNoTool, - BASE_TEST_CONFIG, fill_storage, fill_obj_storage + BASE_TEST_CONFIG, fill_storage, fill_obj_storage, filter_dict, ) @@ -56,23 +56,19 @@ } -class FossologyLicenseTestIndexer(FossologyLicenseIndexer): - """Specific fossology license whose configuration is enough to satisfy - the indexing checks. +CONFIG = { + **BASE_TEST_CONFIG, + 'workdir': '/tmp', + 'tools': { + 'name': 'nomos', + 'version': '3.1.0rc2-31-ga2cbb8c', + 'configuration': { + 'command_line': 'nomossa ', + }, + }, +} - """ - def parse_config_file(self, *args, **kwargs): - return { - **BASE_TEST_CONFIG, - 'workdir': '/tmp', - 'tools': { - 'name': 'nomos', - 'version': '3.1.0rc2-31-ga2cbb8c', - 'configuration': { - 'command_line': 'nomossa ', - }, - }, - } +RANGE_CONFIG = dict(list(CONFIG.items()) + [('write_batch_size', 100)]) class TestFossologyLicenseIndexer(CommonContentIndexerTest, unittest.TestCase): @@ -92,7 +88,7 @@ self.orig_compute_license = fossology_license.compute_license fossology_license.compute_license = mock_compute_license - self.indexer = FossologyLicenseTestIndexer() + self.indexer = FossologyLicenseIndexer(CONFIG) self.idx_storage = self.indexer.idx_storage fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) @@ -124,25 +120,6 @@ fossology_license.compute_license = self.orig_compute_license -class FossologyLicenseRangeIndexerTest(FossologyLicenseRangeIndexer): - """Testing the range indexer on fossology license. - - """ - def parse_config_file(self, *args, **kwargs): - return { - **BASE_TEST_CONFIG, - 'workdir': '/tmp', - 'tools': { - 'name': 'nomos', - 'version': '3.1.0rc2-31-ga2cbb8c', - 'configuration': { - 'command_line': 'nomossa ', - }, - }, - 'write_batch_size': 100, - } - - class TestFossologyLicenseRangeIndexer( CommonContentIndexerRangeTest, unittest.TestCase): """Range Fossology License Indexer tests. @@ -160,7 +137,7 @@ self.orig_compute_license = fossology_license.compute_license fossology_license.compute_license = mock_compute_license - self.indexer = FossologyLicenseRangeIndexerTest() + self.indexer = FossologyLicenseRangeIndexer(config=RANGE_CONFIG) fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) @@ -191,18 +168,11 @@ fossology_license.compute_license = self.orig_compute_license -class FossologyLicenseIndexerUnknownToolTestStorage( - CommonIndexerNoTool, FossologyLicenseTestIndexer): - """Fossology license indexer with wrong configuration""" - - -class FossologyLicenseRangeIndexerUnknownToolTestStorage( - CommonIndexerNoTool, FossologyLicenseRangeIndexerTest): - """Fossology license range indexer with wrong configuration""" +def test_fossology_w_no_tool(): + with pytest.raises(ValueError): + FossologyLicenseIndexer(config=filter_dict(CONFIG, 'tools')) -class TestFossologyLicenseIndexersErrors( - CommonIndexerWithErrorsTest, unittest.TestCase): - """Test the indexer raise the right errors when wrongly initialized""" - Indexer = FossologyLicenseIndexerUnknownToolTestStorage - RangeIndexer = FossologyLicenseRangeIndexerUnknownToolTestStorage +def test_fossology_range_w_no_tool(): + with pytest.raises(ValueError): + FossologyLicenseRangeIndexer(config=filter_dict(RANGE_CONFIG, 'tools')) diff --git a/swh/indexer/tests/test_language.py b/swh/indexer/tests/test_language.py --- a/swh/indexer/tests/test_language.py +++ b/swh/indexer/tests/test_language.py @@ -4,31 +4,28 @@ # See top-level LICENSE file for more information import unittest +import pytest + from swh.indexer import language from swh.indexer.language import LanguageIndexer from swh.indexer.tests.utils import ( - CommonContentIndexerTest, CommonIndexerWithErrorsTest, - CommonIndexerNoTool, BASE_TEST_CONFIG, fill_storage, fill_obj_storage + CommonContentIndexerTest, + BASE_TEST_CONFIG, fill_storage, fill_obj_storage, filter_dict, ) -class LanguageTestIndexer(LanguageIndexer): - """Specific language whose configuration is enough to satisfy the - indexing tests. - """ - def parse_config_file(self, *args, **kwargs): - return { - **BASE_TEST_CONFIG, - 'tools': { - 'name': 'pygments', - 'version': '2.0.1+dfsg-1.1+deb8u1', - 'configuration': { - 'type': 'library', - 'debian-package': 'python3-pygments', - 'max_content_size': 10240, - }, - } - } +CONFIG = { + **BASE_TEST_CONFIG, + 'tools': { + 'name': 'pygments', + 'version': '2.0.1+dfsg-1.1+deb8u1', + 'configuration': { + 'type': 'library', + 'debian-package': 'python3-pygments', + 'max_content_size': 10240, + }, + } +} class Language(unittest.TestCase): @@ -61,7 +58,7 @@ yield from self.indexer.idx_storage.content_language_get(ids) def setUp(self): - self.indexer = LanguageTestIndexer() + self.indexer = LanguageIndexer(config=CONFIG) fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) @@ -91,12 +88,6 @@ } -class LanguageIndexerUnknownToolTestStorage( - CommonIndexerNoTool, LanguageTestIndexer): - """Fossology license indexer with wrong configuration""" - - -class TestLanguageIndexersErrors( - CommonIndexerWithErrorsTest, unittest.TestCase): - """Test the indexer raise the right errors when wrongly initialized""" - Indexer = LanguageIndexerUnknownToolTestStorage +def test_language_w_no_tool(): + with pytest.raises(ValueError): + LanguageIndexer(config=filter_dict(CONFIG, 'tools')) diff --git a/swh/indexer/tests/test_mimetype.py b/swh/indexer/tests/test_mimetype.py --- a/swh/indexer/tests/test_mimetype.py +++ b/swh/indexer/tests/test_mimetype.py @@ -3,6 +3,7 @@ # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information +import pytest import unittest from swh.indexer.mimetype import ( @@ -11,8 +12,7 @@ from swh.indexer.tests.utils import ( CommonContentIndexerTest, CommonContentIndexerRangeTest, - CommonIndexerWithErrorsTest, CommonIndexerNoTool, - BASE_TEST_CONFIG, fill_storage, fill_obj_storage + BASE_TEST_CONFIG, fill_storage, fill_obj_storage, filter_dict, ) @@ -30,23 +30,17 @@ }) -class MimetypeTestIndexer(MimetypeIndexer): - """Specific mimetype indexer instance whose configuration is enough to - satisfy the indexing tests. - - """ - def parse_config_file(self, *args, **kwargs): - return { - **BASE_TEST_CONFIG, - 'tools': { - 'name': 'file', - 'version': '1:5.30-1+deb9u1', - 'configuration': { - "type": "library", - "debian-package": "python3-magic" - }, - }, - } +CONFIG = { + **BASE_TEST_CONFIG, + 'tools': { + 'name': 'file', + 'version': '1:5.30-1+deb9u1', + 'configuration': { + "type": "library", + "debian-package": "python3-magic" + }, + }, +} class TestMimetypeIndexer(CommonContentIndexerTest, unittest.TestCase): @@ -62,7 +56,7 @@ yield from self.idx_storage.content_mimetype_get(ids) def setUp(self): - self.indexer = MimetypeTestIndexer() + self.indexer = MimetypeIndexer(config=CONFIG) self.idx_storage = self.indexer.idx_storage fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) @@ -96,24 +90,7 @@ } -class MimetypeRangeIndexerTest(MimetypeRangeIndexer): - """Specific mimetype whose configuration is enough to satisfy the - indexing tests. - - """ - def parse_config_file(self, *args, **kwargs): - return { - **BASE_TEST_CONFIG, - 'tools': { - 'name': 'file', - 'version': '1:5.30-1+deb9u1', - 'configuration': { - "type": "library", - "debian-package": "python3-magic" - }, - }, - 'write_batch_size': 100, - } +RANGE_CONFIG = dict(list(CONFIG.items()) + [('write_batch_size', 100)]) class TestMimetypeRangeIndexer( @@ -128,7 +105,7 @@ """ def setUp(self): super().setUp() - self.indexer = MimetypeRangeIndexerTest() + self.indexer = MimetypeRangeIndexer(config=RANGE_CONFIG) fill_storage(self.indexer.storage) fill_obj_storage(self.indexer.objstorage) @@ -156,18 +133,11 @@ } -class MimetypeIndexerUnknownToolTestStorage( - CommonIndexerNoTool, MimetypeTestIndexer): - """Mimetype indexer with wrong configuration""" - - -class MimetypeRangeIndexerUnknownToolTestStorage( - CommonIndexerNoTool, MimetypeRangeIndexerTest): - """Mimetype range indexer with wrong configuration""" +def test_mimetype_w_no_tool(): + with pytest.raises(ValueError): + MimetypeIndexer(config=filter_dict(CONFIG, 'tools')) -class TestMimetypeIndexersErrors( - CommonIndexerWithErrorsTest, unittest.TestCase): - """Test the indexer raise the right errors when wrongly initialized""" - Indexer = MimetypeIndexerUnknownToolTestStorage - RangeIndexer = MimetypeRangeIndexerUnknownToolTestStorage +def test_mimetype_range_w_no_tool(): + with pytest.raises(ValueError): + MimetypeRangeIndexer(config=filter_dict(CONFIG, 'tools')) diff --git a/swh/indexer/tests/utils.py b/swh/indexer/tests/utils.py --- a/swh/indexer/tests/utils.py +++ b/swh/indexer/tests/utils.py @@ -405,6 +405,13 @@ }] +def filter_dict(d, keys): + 'return a copy of the dict with keys deleted' + if not isinstance(keys, (list, tuple)): + keys = (keys, ) + return dict((k, v) for (k, v) in d.items() if k not in keys) + + def fill_obj_storage(obj_storage): """Add some content in an object storage.""" for (obj_id, content) in OBJ_STORAGE_DATA.items(): @@ -454,33 +461,6 @@ }]) -class CommonIndexerNoTool: - """Mixin to wronly initialize content indexer""" - def prepare(self): - super().prepare() - self.tools = None - - -class CommonIndexerWithErrorsTest: - """Test indexer configuration checks. - - """ - Indexer = None - RangeIndexer = None - - def test_wrong_unknown_configuration_tool(self): - """Indexer with unknown configuration tool fails check""" - with self.assertRaisesRegex(ValueError, 'Tools None is unknown'): - print('indexer: %s' % self.Indexer) - self.Indexer() - - def test_wrong_unknown_configuration_tool_range(self): - """Range Indexer with unknown configuration tool fails check""" - if self.RangeIndexer is not None: - with self.assertRaisesRegex(ValueError, 'Tools None is unknown'): - self.RangeIndexer() - - class CommonContentIndexerTest(metaclass=abc.ABCMeta): legacy_get_format = False """True iff the tested indexer uses the legacy format.