--- a/test/coverage_count.py +++ b/test/coverage_count.py @@ -56,22 +56,24 @@ if __name__ == "__main__": os.path.join(TEST_CASE_PATH, "test_module/test_pkgship_version/"), os.path.join(TEST_CASE_PATH, "test_module/test_selfbuild/"), os.path.join(TEST_CASE_PATH, "test_module/test_install/"), - os.path.join(TEST_CASE_PATH, "test_module/test_build/"),] - # os.path.join(TEST_CASE_PATH, "test_module/test_bedepend/test_database_query/")] + os.path.join(TEST_CASE_PATH, "test_module/test_build/"), + os.path.join(TEST_CASE_PATH, "test_module/test_graph/") + ] + errors = [] failures = [] for file in test_case_files: runner_result = runner.run(specify_case(file)) errors.extend(runner_result.errors) failures.extend(runner_result.failures) - + if any([errors, failures]): sys.exit(1) - + cov.stop() try: cov.report(show_missing=True) # cov.html_report() except CoverageException: print("No data to report") - sys.exit(1) \ No newline at end of file + sys.exit(1) --- /dev/null +++ b/test/test_module/test_database/data/__init__.py @@ -0,0 +1,12 @@ +#!/usr/bin/python3 +# ****************************************************************************** +# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved. +# licensed under the Mulan PSL v2. +# You can use this software according to the terms and conditions of the Mulan PSL v2. +# You may obtain a copy of Mulan PSL v2 at: +# http://license.coscl.org.cn/MulanPSL2 +# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR +# PURPOSE. +# See the Mulan PSL v2 for more details. +# ******************************************************************************/ --- /dev/null +++ b/test/test_module/test_database/data/mapping.json @@ -0,0 +1,5 @@ +{ +"name": "test_name", +"version": "1.0.1", +"release": 2 +} \ No newline at end of file --- a/test/test_module/test_database/test_es_query.py +++ b/test/test_module/test_database/test_es_query.py @@ -10,13 +10,19 @@ # PURPOSE. # See the Mulan PSL v2 for more details. # ******************************************************************************/ +import os from unittest import TestCase, mock +from unittest.mock import MagicMock from elasticsearch import Elasticsearch, helpers +from elasticsearch.client.indices import IndicesClient +from elasticsearch.exceptions import ElasticsearchException, TransportError from packageship.application.common.exc import ElasticSearchQueryException, DatabaseConfigException from packageship.application.database.engines.elastic import ElasticSearch +MOCK_DATA_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/mapping.json") + class TestEsQuery(TestCase): @@ -112,6 +118,85 @@ class TestEsQuery(TestCase): es2 = ElasticSearch(host="127.0.0.1") self.assertIs(es1, es2) + def test_create_index_success(self): + """ + Test create indices success + Returns: + """ + IndicesClient.exists = MagicMock(side_effect=[False, False]) + IndicesClient.create = MagicMock(side_effect=[True, True]) + + es_instance = self._es_init() + indices = [dict(file=MOCK_DATA_FILE, name="test1"), dict(file=MOCK_DATA_FILE, name="test2")] + result = es_instance.create_index(indices) + self.assertEqual(result, []) + + def test_create_index_fail(self): + """ + Test create indices failed + Returns: + """ + IndicesClient.exists = MagicMock(side_effect=[False]) + IndicesClient.create = MagicMock(side_effect=[ElasticsearchException]) + + es_instance = self._es_init() + indices = [dict(file=MOCK_DATA_FILE, name="test1")] + result = es_instance.create_index(indices) + self.assertEqual(result, ["test1"]) + + def test_delete_index_fail(self): + """ + Test delete indices success + Returns: + """ + IndicesClient.exists = MagicMock(side_effect=[True]) + IndicesClient.delete = MagicMock(side_effect=[TransportError]) + + es_instance = self._es_init() + indices = [dict(file=MOCK_DATA_FILE, name="test1")] + result = es_instance.create_index(indices) + self.assertEqual(result, ["test1"]) + + def test_load_mapping_fail(self): + """ + Test load mapping success + Returns: + """ + es_instance = self._es_init() + indices = dict(file=MOCK_DATA_FILE + "1", name="test1") + result = es_instance.create_index(indices) + self.assertEqual(result, ["test1"]) + + def test_insert_fail(self): + """ + Test insert indices success + Returns: + """ + es_instance = self._es_init() + with self.assertRaises(ElasticSearchQueryException): + es_instance.insert(index="test", body={}) + + def test_delete_index_none(self): + """ + Test delete indices is none + Returns: + """ + es_instance = self._es_init() + result = es_instance.delete_index(index="") + self.assertIsNone(result) + + def test_delete_many_indices_fail(self): + """ + Test delete indices failed + Returns: + """ + IndicesClient.delete = MagicMock(side_effect=[TransportError]) + + es_instance = self._es_init() + indices = ['test1', 'test2'] + result = es_instance.delete_index(indices) + self.assertEqual(result, "test1,test2") + @staticmethod def _es_init(): return ElasticSearch(host="127.0.0.1", port="9200")