diff --git a/packageship/packageship/application/apps/lifecycle/view.py b/packageship/packageship/application/apps/lifecycle/view.py index d8ef03c03b136ed474965f2c44038c57de140597..efacc5aa07de3c446fa6003375a4a3b4931074db 100644 --- a/packageship/packageship/application/apps/lifecycle/view.py +++ b/packageship/packageship/application/apps/lifecycle/view.py @@ -245,6 +245,7 @@ class LifeTables(Resource): all_table_names = database_name.engine.table_names() all_table_names.remove("packages_issue") all_table_names.remove("packages_maintainer") + all_table_names.remove("databases_info") return jsonify( ResponseCode.response_json( ResponseCode.SUCCESS, data=all_table_names) diff --git a/packageship/packageship/application/apps/package/function/constants.py b/packageship/packageship/application/apps/package/function/constants.py index 083881ce31c31e5aa45fb6df7475e625f38af452..49ff91aeece23c5aa2c21761ee883e50df469678 100644 --- a/packageship/packageship/application/apps/package/function/constants.py +++ b/packageship/packageship/application/apps/package/function/constants.py @@ -44,6 +44,7 @@ class ResponseCode(): UPDATA_OR_ADD_DATA_FAILED = "4008" TABLE_NAME_NOT_EXIST = "4009" UPDATA_DATA_FAILED = "4010" + NOT_FOUND_DATABASE_INFO = "4011" # Database operation module error status code DELETE_DB_ERROR = "40051" SERVICE_ERROR = "50000" @@ -79,6 +80,7 @@ class ResponseCode(): SPECIFIED_FILE_NOT_EXIST: "The specified file does not exist", NO_PACKAGES_TABLE: "There is no packages table in the database", UPDATA_OR_ADD_DATA_FAILED: "Failed to update or add data", + NOT_FOUND_DATABASE_INFO: "Unable to get the generated database information", DATABASE_NOT_FOUND: "There is no such database in the system", TABLE_NAME_NOT_EXIST: "There is no such table in the database", UPDATA_DATA_FAILED: "Failed to update data", diff --git a/packageship/packageship/application/apps/package/function/searchdb.py b/packageship/packageship/application/apps/package/function/searchdb.py index 1624e0d0f76d2340a0f222a818b883cb4deec9aa..2f8b777a2bab12935cd6ad0d37b8ce0fab79f628 100644 --- a/packageship/packageship/application/apps/package/function/searchdb.py +++ b/packageship/packageship/application/apps/package/function/searchdb.py @@ -6,18 +6,19 @@ functions: db_priority """ from collections import namedtuple, Counter -import yaml from flask import current_app from sqlalchemy import text -from sqlalchemy.exc import SQLAlchemyError, DisconnectionError +from sqlalchemy.exc import SQLAlchemyError +from sqlalchemy.exc import DisconnectionError from sqlalchemy.sql import literal_column from sqlalchemy import exists from packageship.libs.dbutils import DBHelper +from packageship.libs.exception import Error from packageship.libs.log import Log -from packageship.application.models.package import BinPack, SrcPack -from packageship.libs.exception import ContentNoneException, Error -from packageship.system_config import DATABASE_FILE_INFO +from packageship.application.models.package import BinPack +from packageship.application.models.package import SrcPack +from packageship.application.models.package import DatabaseInfo from packageship.application.apps.package.function.constants import ResponseCode LOGGER = Log(__name__) @@ -919,26 +920,20 @@ class SearchDB(): def db_priority(): """ - Description: Read yaml file, return database name, according to priority + Description: Read the databases_info table to return the table + name according to the priority of the database Args: Returns: db_list: database name list Raises: - FileNotFoundError: file cannot be found + SQLAlchemyError: file cannot be found Error: abnormal error """ try: - with open(DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context: - - init_database_date = yaml.load( - file_context.read(), Loader=yaml.FullLoader) - if init_database_date is None: - raise ContentNoneException( - "The content of the database initialization configuration file cannot be empty") - init_database_date.sort(key=lambda x: x['priority'], reverse=False) - db_list = [item.get('database_name') - for item in init_database_date] - return db_list - except (FileNotFoundError, Error) as file_not_found: - current_app.logger.error(file_not_found) + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name).order_by(DatabaseInfo.priority).all() + return [name[0] for name in name_list] + except (SQLAlchemyError, DisconnectionError, Error) as error: + current_app.logger.error(error) return None diff --git a/packageship/packageship/application/apps/package/view.py b/packageship/packageship/application/apps/package/view.py index 24c1f183cf08025110c64d6e52f3c69c25e8b2a6..c405bd28aaba07c2123d2dd15681274d8f2c054e 100644 --- a/packageship/packageship/application/apps/package/view.py +++ b/packageship/packageship/application/apps/package/view.py @@ -4,22 +4,22 @@ description: Interface processing class: BeDepend, BuildDepend, InitSystem, InstallDepend, Packages, Repodatas, SelfDepend, SinglePack """ -import yaml +from sqlalchemy.exc import DisconnectionError +from sqlalchemy.exc import SQLAlchemyError from flask import request from flask import jsonify from flask import current_app from flask_restful import Resource -from sqlalchemy.exc import DisconnectionError from packageship import system_config from packageship.application.initsystem.data_import import InitDataBase from packageship.libs.configutils.readconfig import ReadConfig +from packageship.libs.dbutils import DBHelper from packageship.libs.exception import Error from packageship.libs.exception import ContentNoneException from packageship.libs.exception import DataMergeException from packageship.libs.exception import ConfigurationException from packageship.libs.log import Log -from packageship.system_config import DATABASE_FILE_INFO from .function.constants import ResponseCode from .function.packages import get_all_package_info from .function.packages import sing_pack @@ -37,6 +37,7 @@ from .serialize import InstallDependSchema from .serialize import BuildDependSchema from .serialize import SelfDependSchema from .serialize import have_err_db_name +from ...models.package import DatabaseInfo LOGGER = Log(__name__) @@ -201,7 +202,7 @@ class InstallDepend(Resource): if not db_pri: return jsonify( ResponseCode.response_json( - ResponseCode.FILE_NOT_FIND_ERROR + ResponseCode.NOT_FOUND_DATABASE_INFO ) ) @@ -277,7 +278,7 @@ class BuildDepend(Resource): if not db_pri: return jsonify( ResponseCode.response_json( - ResponseCode.FILE_NOT_FIND_ERROR + ResponseCode.NOT_FOUND_DATABASE_INFO ) ) @@ -356,7 +357,7 @@ class SelfDepend(Resource): if not db_pri: return jsonify( ResponseCode.response_json( - ResponseCode.FILE_NOT_FIND_ERROR + ResponseCode.NOT_FOUND_DATABASE_INFO ) ) db_list = data.get("db_list") if data.get("db_list") \ @@ -459,7 +460,7 @@ class Repodatas(Resource): def get(self): """ - get all database + get all database info Returns: for @@ -468,37 +469,32 @@ class Repodatas(Resource): "code": "", "data": [ { - "database_name": "", + "name": "", "priority": "", - "status": "" } ], "msg": "" } Raises: - FileNotFoundError: File not found exception - TypeError: Exception of wrong type - Error: abnormal Error + SQLAlchemyError: sql error + Error: Error: abnormal error + DisconnectionError: Unable to connect to the database + """ try: - with open(DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context: - init_database_date = yaml.load( - file_context.read(), Loader=yaml.FullLoader) - if init_database_date is None: - raise ContentNoneException( - "The content of the database initialization configuration " - "file cannot be empty ") - init_database_date.sort( - key=lambda x: x['priority'], reverse=False) + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name, DatabaseInfo.priority).order_by(DatabaseInfo.priority).all() + data_list = [dict(zip(ven.keys(), ven)) for ven in name_list] return jsonify( ResponseCode.response_json( ResponseCode.SUCCESS, - data=init_database_date)) - except (FileNotFoundError, TypeError, Error) as file_not_found: - current_app.logger.error(file_not_found) + data=data_list)) + except (SQLAlchemyError, Error, DisconnectionError) as data_info_error: + current_app.logger.error(data_info_error) return jsonify( - ResponseCode.response_json(ResponseCode.FILE_NOT_FOUND) - ) + ResponseCode.response_json( + ResponseCode.NOT_FOUND_DATABASE_INFO)) def delete(self): """ @@ -527,8 +523,8 @@ class Repodatas(Resource): db_list = db_priority() if db_list is None: return jsonify( - ResponseCode.response_json(ResponseCode.FILE_NOT_FOUND) - ) + ResponseCode.response_json( + ResponseCode.NOT_FOUND_DATABASE_INFO)) if db_name not in db_priority(): return jsonify( ResponseCode.response_json(ResponseCode.DB_NAME_ERROR) @@ -536,13 +532,13 @@ class Repodatas(Resource): try: drop_db = InitDataBase() del_result = drop_db.delete_db(db_name) - if del_result is False: + if not del_result: return jsonify( ResponseCode.response_json(ResponseCode.DELETE_DB_ERROR)) return jsonify( ResponseCode.response_json(ResponseCode.SUCCESS) ) - except (FileNotFoundError, TypeError, Error) as error: + except (SQLAlchemyError, TypeError, Error, DisconnectionError) as error: current_app.logger.error(error) return jsonify( ResponseCode.response_json(ResponseCode.DELETE_DB_ERROR) @@ -605,14 +601,16 @@ class InitSystem(Resource): except ConfigurationException as error: LOGGER.logger.error(error) abnormal = error - return jsonify(ResponseCode.response_json('5000', msg=abnormal.message)) + return jsonify( + ResponseCode.response_json( + '5000', msg=abnormal.message)) except DataMergeException as data_merge_error: LOGGER.logger.error(data_merge_error) abnormal = ResponseCode.DATA_MERGE_ERROR except FileNotFoundError as file_not_found_error: LOGGER.logger.error(file_not_found_error) abnormal = ResponseCode.FILE_NOT_FIND_ERROR - except Error as error: + except (Error, SQLAlchemyError) as error: LOGGER.logger.error(error) abnormal = ResponseCode.FAILED_CREATE_DATABASE_TABLE if abnormal is not None: diff --git a/packageship/packageship/application/initsystem/data_import.py b/packageship/packageship/application/initsystem/data_import.py index a5846bd89e74f66d4c56e02d52c9cc2247cf389f..34f7288a0e5ed2c0d442f0f857fd6a8f3a7d17e5 100644 --- a/packageship/packageship/application/initsystem/data_import.py +++ b/packageship/packageship/application/initsystem/data_import.py @@ -5,8 +5,8 @@ Description: Initialization of data import Class: InitDataBase,MysqlDatabaseOperations,SqliteDatabaseOperations """ import os -import pathlib import yaml +from sqlalchemy import text from sqlalchemy.exc import SQLAlchemyError, InternalError from packageship.libs.dbutils.sqlalchemy_helper import DBHelper from packageship.libs.exception import ContentNoneException @@ -15,7 +15,7 @@ from packageship.libs.exception import Error from packageship.libs.exception import ConfigurationException from packageship.libs.configutils.readconfig import ReadConfig from packageship.libs.log import Log -from packageship.application.models.package import SrcPack +from packageship.application.models.package import SrcPack, DatabaseInfo from packageship.application.models.package import BinPack from packageship.application.models.package import BinRequires from packageship.application.models.package import SrcRequires @@ -65,7 +65,8 @@ class InitDataBase(): # Create life cycle related databases and tables if not self.create_database(db_name='lifecycle', tables=['packages_issue', - 'packages_maintainer'], + 'packages_maintainer', + 'databases_info'], storage=True): raise SQLAlchemyError( 'Failed to create the specified database and table:lifecycle') @@ -129,9 +130,18 @@ class InitDataBase(): if self.__exists_repeat_database(): raise DatabaseRepeatException( 'There is a duplicate database configuration') - if not InitDataBase.delete_settings_file(): - raise IOError( - 'An error occurred while deleting the database configuration file') + + if not self.__clear_database(): + raise SQLAlchemyError( + 'Failed to clear database data in database_folder folder') + + if not InitDataBase.__clear_database_name_table(): + raise SQLAlchemyError( + 'Failed to clear data in database_name') + + if not InitDataBase.__clear_lifecycle_table(): + raise SQLAlchemyError( + 'Failed to delete table in lifecycle database') for database_config in self.config_file_datas: if not database_config.get('dbname'): @@ -215,8 +225,7 @@ class InitDataBase(): 'database_name': _db_name, 'priority': database_config.get('priority'), } - InitDataBase.__updata_settings_file( - database_content=database_content) + InitDataBase.__update_data_name(database_content) except (SQLAlchemyError, ContentNoneException, TypeError, Error, FileNotFoundError) as error_msg: @@ -498,53 +507,82 @@ class InitDataBase(): return False @staticmethod - def __updata_settings_file(**Kwargs): + def __update_data_name(database_content): """ - update some configuration files related to the database in the system + Update the database_name table Args: - **Kwargs: data related to configuration file nodes - database_name: Name database + database_content: + Dictionary of database names and priorities Returns: - Raises: - FileNotFoundError: The specified file was not found - IOError: File or network operation io abnormal """ try: - if not os.path.exists(system_config.DATABASE_FILE_INFO): - pathlib.Path(system_config.DATABASE_FILE_INFO).touch() - with open(system_config.DATABASE_FILE_INFO, 'a+', encoding='utf8') as file_context: - setting_content = [] - if 'database_content' in Kwargs.keys(): - content = Kwargs.get('database_content') - if content: - setting_content.append(content) - yaml.dump(setting_content, file_context) - - except FileNotFoundError as not_found: - LOGGER.logger.error(not_found) - except IOError as exception_msg: - LOGGER.logger.error(exception_msg) + with DBHelper(db_name="lifecycle") as database_name: + name = database_content.get("database_name") + priority = database_content.get("priority") + database_name.add(DatabaseInfo( + name=name, priority=priority + )) + database_name.session.commit() + except (SQLAlchemyError, Error, AttributeError) as error: + LOGGER.logger.error(error) + @staticmethod - def delete_settings_file(): + def __clear_database_name_table(): """ - Delete the configuration file of the database - - Args: + Clear the data in the databases_info table + Returns: + True or False + """ + try: + with DBHelper(db_name="lifecycle") as database_name: + clear_sql = """delete from databases_info;""" + database_name.session.execute(text(clear_sql)) + database_name.session.commit() + except SQLAlchemyError as error: + LOGGER.logger.error(error) + return False + else: + return True + @staticmethod + def __clear_lifecycle_table(): + """ + Delete the tables in the lifecycle except for the specific three tables Returns: - True if the deletion is successful, otherwise false - Raises: - IOError: File or network operation io abnormal + + """ + try: + with DBHelper(db_name="lifecycle") as database_name: + table_list = database_name.engine.table_names() + for item in table_list: + if item not in ['packages_maintainer', 'databases_info', 'packages_issue']: + drop_sql = '''DROP TABLE if exists `{table_name}`'''.format(table_name=item) + database_name.session.execute(text(drop_sql)) + database_name.session.commit() + except SQLAlchemyError as sql_error: + LOGGER.logger.error(sql_error) + return False + else: + return True + + def __clear_database(self): """ + Delete database + Returns: + """ try: - if os.path.exists(system_config.DATABASE_FILE_INFO): - os.remove(system_config.DATABASE_FILE_INFO) - except (IOError, Error) as exception_msg: - LOGGER.logger.error(exception_msg) + with DBHelper(db_name='lifecycle') as data_name: + name_data_list = data_name.session.query( + DatabaseInfo.name).order_by(DatabaseInfo.priority).all() + name_list = [name[0] for name in name_data_list] + for item in name_list: + self.__del_database(item) + except (SQLAlchemyError, Error, IOError) as error: + LOGGER.logger.error(error) return False else: return True @@ -562,25 +600,18 @@ class InitDataBase(): """ try: del_result = True - file_read = open( - system_config.DATABASE_FILE_INFO, 'r', encoding='utf-8') - _databases = yaml.load( - file_read.read(), Loader=yaml.FullLoader) - for database in _databases: - if database.get('database_name') == db_name: - _databases.remove(database) - # Delete the successfully imported database configuration node - with open(system_config.DATABASE_FILE_INFO, 'w+', encoding='utf-8') as file_context: - yaml.safe_dump(_databases, file_context) - except (IOError, Error) as del_config_error: - LOGGER.logger.error(del_config_error) + with DBHelper(db_name='lifecycle') as database_name: + database_name.session.query(DatabaseInfo).filter \ + (DatabaseInfo.name == db_name).delete() + drop_sql = '''DROP TABLE if exists `{table_name}`''' \ + .format(table_name=db_name) + database_name.session.execute(text(drop_sql)) + database_name.session.commit() + except SQLAlchemyError as sql_error: + LOGGER.logger.error(sql_error) del_result = False - finally: - file_read.close() - if del_result: del_result = self.__del_database(db_name) - return del_result def create_database(self, db_name, tables=None, storage=True): diff --git a/packageship/packageship/application/models/package.py b/packageship/packageship/application/models/package.py index f95bfcc242c5a2b939a5c2ec123c617bdf9ea24d..3fbd671873824540e7c53fb834feb7161dabdc32 100644 --- a/packageship/packageship/application/models/package.py +++ b/packageship/packageship/application/models/package.py @@ -202,3 +202,13 @@ class PackagesMaintainer(DBHelper.BASE): name = Column(String(200), nullable=True) maintainer = Column(String(200), nullable=True) maintainlevel = Column(Integer, nullable=True) + + +class DatabaseInfo(DBHelper.BASE): + """ + Save the name and priority of the database + """ + __tablename__ = 'databases_info' + id = Column(Integer, primary_key=True) + name = Column(String(200), nullable=True) + priority = Column(Integer, nullable=True) \ No newline at end of file diff --git a/packageship/test/common_files/correct_test_result_json/get_repodatas.json b/packageship/test/common_files/correct_test_result_json/get_repodatas.json index 8b8d0124342ca5c50f09f072f488f914fb42665e..c977baa482b87e8d8d400469974ff312ec65f5bb 100644 --- a/packageship/test/common_files/correct_test_result_json/get_repodatas.json +++ b/packageship/test/common_files/correct_test_result_json/get_repodatas.json @@ -1,10 +1,10 @@ [ { - "database_name": "mainline", + "name": "mainline", "priority": 1 }, { - "database_name": "fedora30", + "name": "fedora30", "priority": 2 } ] \ No newline at end of file diff --git a/packageship/test/common_files/dbs/lifecycle.db b/packageship/test/common_files/dbs/lifecycle.db index 264f168a630bc3e8485bdf9e464d8a56e409b808..457902d611737b640179d876a3a9e5c67b68c3fa 100644 Binary files a/packageship/test/common_files/dbs/lifecycle.db and b/packageship/test/common_files/dbs/lifecycle.db differ diff --git a/packageship/test/common_files/operate_dbs/lifecycle.db b/packageship/test/common_files/operate_dbs/lifecycle.db index 9053816339c706c36913a234b7e928c439529b21..2979e19f647503f5c425f40dc178bec09b925993 100644 Binary files a/packageship/test/common_files/operate_dbs/lifecycle.db and b/packageship/test/common_files/operate_dbs/lifecycle.db differ diff --git a/packageship/test/test_module/init_system_tests/test_importdata.py b/packageship/test/test_module/init_system_tests/test_importdata.py index 681be9c8c95e79176ab9bf35017b5e92288baf75..c1c16533c25fc51a212b4c49d2bb447e746d4b8a 100644 --- a/packageship/test/test_module/init_system_tests/test_importdata.py +++ b/packageship/test/test_module/init_system_tests/test_importdata.py @@ -9,8 +9,10 @@ import unittest import warnings from configparser import ConfigParser import yaml + from packageship import system_config -from packageship.libs.exception import Error,ConfigurationException +from packageship.libs.exception import Error +from packageship.libs.exception import ConfigurationException try: @@ -36,11 +38,13 @@ try: except Error: raise Error +from sqlalchemy.exc import SQLAlchemyError from packageship.application.initsystem.data_import import InitDataBase from packageship.libs.exception import ContentNoneException from packageship.libs.exception import DatabaseRepeatException from packageship.libs.configutils.readconfig import ReadConfig - +from packageship.application.models.package import DatabaseInfo +from packageship.libs.dbutils import DBHelper class ImportData(unittest.TestCase): """ @@ -64,7 +68,8 @@ class ImportData(unittest.TestCase): # Yaml file exists but the content is empty try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'w', encoding='utf-8') as w_f: @@ -83,7 +88,8 @@ class ImportData(unittest.TestCase): # Yaml file exists but DB exists_ The same with name try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -118,7 +124,8 @@ class ImportData(unittest.TestCase): config.set("SYSTEM", "init_conf_path", "D:\\Users\\conf.yaml") config.write(open(system_config.SYS_CONFIG_PATH, "w")) - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') InitDataBase(config_file_path=_config_path).init_data() except FileNotFoundError as error: self.assertEqual( @@ -135,7 +142,8 @@ class ImportData(unittest.TestCase): def test_dbname(self): """test dbname""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -159,7 +167,8 @@ class ImportData(unittest.TestCase): def test_src_db_file(self): """test src db file""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -183,7 +192,8 @@ class ImportData(unittest.TestCase): def test_priority(self): """test priority""" try: - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') shutil.copyfile(_config_path, _config_path + '.bak') with open(_config_path, 'r', encoding='utf-8') as file: origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) @@ -192,12 +202,15 @@ class ImportData(unittest.TestCase): with open(_config_path, 'w', encoding='utf-8') as w_f: yaml.dump(origin_yaml, w_f) InitDataBase(config_file_path=_config_path).init_data() - with open(system_config.DATABASE_FILE_INFO, 'r', encoding='utf-8') as file_context: - init_database_date = yaml.load( - file_context.read(), Loader=yaml.FullLoader) + + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name).order_by( + DatabaseInfo.priority).all() + init_database_date = [name[0] for name in name_list] self.assertEqual( init_database_date, - None, + [], msg=" Priority must be a positive integer between 0 and 100 ") except FileNotFoundError: return @@ -211,43 +224,30 @@ class ImportData(unittest.TestCase): Initialization of system data """ # Normal configuration - _config_path = ReadConfig(system_config.SYS_CONFIG_PATH).get_system('init_conf_path') - InitDataBase(config_file_path=_config_path).init_data() - - # In the correct case, an import will be generated under the initsystem - # directory_ success_ databse.yaml - path = system_config.DATABASE_FILE_INFO - - self.assertTrue( - os.path.exists(path), - msg="Import was not generated successfully " - "after initialization_ success_ databse.yaml file") - - # And there is data in this file, and it comes from the yaml file of - # conf - with open(_config_path, 'r', encoding='utf-8') as file: - yaml_config = yaml.load(file.read(), Loader=yaml.FullLoader) - - with open(path, 'r', encoding='utf-8') as files: - yaml_success = yaml.load(files.read(), Loader=yaml.FullLoader) - - self.assertEqual( - len(yaml_config), - len(yaml_success), - msg="The success file is inconsistent with the original yaml file") - - # Compare name and priority - success_name_priority = dict() - config_name_priority = dict() - len_con = len(yaml_config) - for i in range(len_con): - success_name_priority[yaml_success[i]["database_name"]] = \ - yaml_success[i]["priority"] - config_name_priority[yaml_config[i]["dbname"]] = \ - yaml_config[i]["priority"] - - self.assertEqual( - success_name_priority, - config_name_priority, - msg="The database and priority after initialization are" - "inconsistent with the original file") + try: + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + InitDataBase(config_file_path=_config_path).init_data() + with DBHelper(db_name='lifecycle') as data_name: + name_list = data_name.session.query( + DatabaseInfo.name, DatabaseInfo.priority).order_by( + DatabaseInfo.priority).all() + data_list = [dict(zip(ven.keys(), ven)) for ven in name_list] + _config_path = ReadConfig( + system_config.SYS_CONFIG_PATH).get_system('init_conf_path') + with open(_config_path, 'r', encoding='utf-8') as file: + origin_yaml = yaml.load(file.read(), Loader=yaml.FullLoader) + origin_list = list() + for item in origin_yaml: + data_dict = dict() + data_dict['name'] = item.get("dbname") + data_dict['priority'] = item.get("priority") + origin_list.append(data_dict) + + self.assertEqual( + data_list, + origin_list, + msg="The name and priority of the data generated by the initialization are correct") + + except (Error, SQLAlchemyError, FileNotFoundError, yaml.YAMLError): + return None diff --git a/packageship/test/test_module/repodatas_test/test_delete_repodatas.py b/packageship/test/test_module/repodatas_test/test_delete_repodatas.py index 0cd640aed9ecac82615f6297a2df137c2e6c04fe..6476fea386a9ecc57fc9f08231f5f55ad2755159 100644 --- a/packageship/test/test_module/repodatas_test/test_delete_repodatas.py +++ b/packageship/test/test_module/repodatas_test/test_delete_repodatas.py @@ -6,6 +6,8 @@ test delete repodatas import os import shutil +from sqlalchemy.exc import SQLAlchemyError + from test.base_code.operate_data_base import OperateTestBase import unittest import json @@ -70,7 +72,6 @@ class TestDeleteRepodatas(OperateTestBase): Returns: """ try: - shutil.copyfile(system_config.DATABASE_FILE_INFO, system_config.DATABASE_FILE_INFO + '.bak') shutil.copytree(system_config.DATABASE_FOLDER_PATH, system_config.DATABASE_FOLDER_PATH + '.bak') resp = self.client.delete("/repodatas?dbName=fedora30") resp_dict = json.loads(resp.data) @@ -90,10 +91,8 @@ class TestDeleteRepodatas(OperateTestBase): self.assertIsNone( resp_dict.get("data"), msg="Error in data information return") - except Error: + except (SQLAlchemyError, FileExistsError, Error): return None finally: - os.remove(system_config.DATABASE_FILE_INFO) - os.rename(system_config.DATABASE_FILE_INFO + '.bak', system_config.DATABASE_FILE_INFO) shutil.rmtree(system_config.DATABASE_FOLDER_PATH) os.rename(system_config.DATABASE_FOLDER_PATH + '.bak', system_config.DATABASE_FOLDER_PATH)