From d0d0bb08f96e443bd2b3801de2528ffb51c5c22c Mon Sep 17 00:00:00 2001 From: Raoul Snyman Date: Fri, 18 Aug 2023 13:25:17 -0700 Subject: [PATCH] Fix issue #1624 --- openlp/core/db/manager.py | 4 ++-- tests/openlp_core/db/test_manager.py | 11 +++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/openlp/core/db/manager.py b/openlp/core/db/manager.py index e0725f39f..4d49f6a41 100644 --- a/openlp/core/db/manager.py +++ b/openlp/core/db/manager.py @@ -26,7 +26,7 @@ from pathlib import Path from types import FunctionType, ModuleType from typing import List, Optional, Type, Union -from sqlalchemy import create_engine, func +from sqlalchemy import create_engine, func, text from sqlalchemy.exc import DBAPIError, InvalidRequestError, OperationalError, SQLAlchemyError from sqlalchemy.orm import Session from sqlalchemy.orm.decl_api import DeclarativeMeta @@ -318,7 +318,7 @@ class DBManager(object): if self.is_dirty and self.db_url.startswith('sqlite'): try: engine = create_engine(self.db_url) - engine.connect().execute("vacuum") + engine.connect().execute(text('vacuum')) except OperationalError: # Just ignore the operational error pass diff --git a/tests/openlp_core/db/test_manager.py b/tests/openlp_core/db/test_manager.py index 5208c48d8..1ca262428 100644 --- a/tests/openlp_core/db/test_manager.py +++ b/tests/openlp_core/db/test_manager.py @@ -26,16 +26,20 @@ from unittest.mock import MagicMock, patch from sqlalchemy.exc import OperationalError as SQLAlchemyOperationalError +from openlp.core.common.settings import Settings from openlp.core.db.manager import DBManager +@patch('openlp.core.db.manager.text') @patch('openlp.core.db.manager.init_url') @patch('openlp.core.db.manager.create_engine') -def test_manager_finalise_exception(mocked_create_engine, mocked_init_url, temp_folder, settings): +def test_manager_finalise_exception(mocked_create_engine: MagicMock, mocked_init_url: MagicMock, + mocked_text: MagicMock, temp_folder: str, settings: Settings): """Test that the finalise method silently fails on an exception""" # GIVEN: A db Manager object mocked_init_url.return_value = f'sqlite:///{temp_folder}/test_db.sqlite' mocked_session = MagicMock() + mocked_text.side_effect = lambda t: t def init_schema(url): return mocked_session @@ -55,13 +59,16 @@ def test_manager_finalise_exception(mocked_create_engine, mocked_init_url, temp_ mocked_create_engine.return_value.connect.return_value.execute.assert_called_once_with('vacuum') +@patch('openlp.core.db.manager.text') @patch('openlp.core.db.manager.init_url') @patch('openlp.core.db.manager.create_engine') -def test_manager_finalise(mocked_create_engine, mocked_init_url, temp_folder, settings): +def test_manager_finalise(mocked_create_engine: MagicMock, mocked_init_url: MagicMock, mocked_text: MagicMock, + temp_folder: str, settings: Settings): """Test that the finalise method works correctly""" # GIVEN: A db Manager object mocked_init_url.return_value = f'sqlite:///{temp_folder}/test_db.sqlite' mocked_session = MagicMock() + mocked_text.side_effect = lambda t: t def init_schema(url): return mocked_session