Changeset View
Changeset View
Standalone View
Standalone View
swh/indexer/tests/test_language.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 | ||||
import pytest | |||||
from swh.indexer import language | from swh.indexer import language | ||||
from swh.indexer.language import LanguageIndexer | from swh.indexer.language import LanguageIndexer | ||||
from swh.indexer.tests.utils import ( | from swh.indexer.tests.utils import ( | ||||
CommonContentIndexerTest, CommonIndexerWithErrorsTest, | CommonContentIndexerTest, | ||||
CommonIndexerNoTool, BASE_TEST_CONFIG, fill_storage, fill_obj_storage | BASE_TEST_CONFIG, fill_storage, fill_obj_storage, filter_dict, | ||||
) | ) | ||||
class LanguageTestIndexer(LanguageIndexer): | CONFIG = { | ||||
"""Specific language whose configuration is enough to satisfy the | |||||
indexing tests. | |||||
""" | |||||
def parse_config_file(self, *args, **kwargs): | |||||
return { | |||||
**BASE_TEST_CONFIG, | **BASE_TEST_CONFIG, | ||||
'tools': { | 'tools': { | ||||
'name': 'pygments', | 'name': 'pygments', | ||||
'version': '2.0.1+dfsg-1.1+deb8u1', | 'version': '2.0.1+dfsg-1.1+deb8u1', | ||||
'configuration': { | 'configuration': { | ||||
'type': 'library', | 'type': 'library', | ||||
'debian-package': 'python3-pygments', | 'debian-package': 'python3-pygments', | ||||
'max_content_size': 10240, | 'max_content_size': 10240, | ||||
}, | }, | ||||
} | } | ||||
} | } | ||||
class Language(unittest.TestCase): | class Language(unittest.TestCase): | ||||
"""Tests pygments tool for language detection | """Tests pygments tool for language detection | ||||
""" | """ | ||||
def test_compute_language_none(self): | def test_compute_language_none(self): | ||||
# given | # given | ||||
Show All 16 Lines | class TestLanguageIndexer(CommonContentIndexerTest, unittest.TestCase): | ||||
""" | """ | ||||
legacy_get_format = True | legacy_get_format = True | ||||
def get_indexer_results(self, ids): | def get_indexer_results(self, ids): | ||||
yield from self.indexer.idx_storage.content_language_get(ids) | yield from self.indexer.idx_storage.content_language_get(ids) | ||||
def setUp(self): | def setUp(self): | ||||
self.indexer = LanguageTestIndexer() | self.indexer = LanguageIndexer(config=CONFIG) | ||||
fill_storage(self.indexer.storage) | fill_storage(self.indexer.storage) | ||||
fill_obj_storage(self.indexer.objstorage) | fill_obj_storage(self.indexer.objstorage) | ||||
self.id0 = '02fb2c89e14f7fab46701478c83779c7beb7b069' | self.id0 = '02fb2c89e14f7fab46701478c83779c7beb7b069' | ||||
self.id1 = '103bc087db1d26afc3a0283f38663d081e9b01e6' | self.id1 = '103bc087db1d26afc3a0283f38663d081e9b01e6' | ||||
self.id2 = 'd4c647f0fc257591cc9ba1722484229780d1c607' | self.id2 = 'd4c647f0fc257591cc9ba1722484229780d1c607' | ||||
tool = {k.replace('tool_', ''): v | tool = {k.replace('tool_', ''): v | ||||
Show All 13 Lines | def setUp(self): | ||||
self.id2: { | self.id2: { | ||||
'id': self.id2, | 'id': self.id2, | ||||
'tool': tool, | 'tool': tool, | ||||
'lang': 'text-only' | 'lang': 'text-only' | ||||
} | } | ||||
} | } | ||||
class LanguageIndexerUnknownToolTestStorage( | def test_language_w_no_tool(): | ||||
CommonIndexerNoTool, LanguageTestIndexer): | with pytest.raises(ValueError): | ||||
"""Fossology license indexer with wrong configuration""" | LanguageIndexer(config=filter_dict(CONFIG, 'tools')) | ||||
class TestLanguageIndexersErrors( | |||||
CommonIndexerWithErrorsTest, unittest.TestCase): | |||||
"""Test the indexer raise the right errors when wrongly initialized""" | |||||
Indexer = LanguageIndexerUnknownToolTestStorage |