diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index 36bfa24ef..d43938afe 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -93,10 +93,11 @@ def upgrade_db(url, upgrade): """ pass - metadata_table = Table('metadata', metadata, - Column('key', types.Unicode(64), primary_key=True), - Column('value', types.UnicodeText(), default=None) - ) + metadata_table = Table( + 'metadata', metadata, + Column('key', types.Unicode(64), primary_key=True), + Column('value', types.UnicodeText(), default=None) + ) metadata_table.create(checkfirst=True) mapper(Metadata, metadata_table) version_meta = session.query(Metadata).get('version') @@ -137,7 +138,6 @@ def delete_database(plugin_name, db_file_name=None): :param plugin_name: The name of the plugin to remove the database for :param db_file_name: The database file name. Defaults to None resulting in the plugin_name being used. """ - db_file_path = None if db_file_name: db_file_path = os.path.join(AppLocation.get_section_data_path(plugin_name), db_file_name) else: diff --git a/tests/functional/openlp_core_lib/test_db.py b/tests/functional/openlp_core_lib/test_db.py index 8a2f21ec3..470bd0636 100644 --- a/tests/functional/openlp_core_lib/test_db.py +++ b/tests/functional/openlp_core_lib/test_db.py @@ -29,13 +29,14 @@ """ Package to test the openlp.core.lib package. """ +import os from unittest import TestCase from sqlalchemy.pool import NullPool from sqlalchemy.orm.scoping import ScopedSession from sqlalchemy import MetaData -from openlp.core.lib.db import init_db, get_upgrade_op +from openlp.core.lib.db import init_db, get_upgrade_op, delete_database from tests.functional import patch, MagicMock @@ -110,3 +111,44 @@ class TestDB(TestCase): mocked_session.bind.connect.assert_called_with() MockedMigrationContext.configure.assert_called_with(mocked_connection) MockedOperations.assert_called_with(mocked_context) + + def delete_database_without_db_file_name_test(self): + """ + Test that the ``delete_database`` function removes a database file, without the file name parameter + """ + # GIVEN: Mocked out AppLocation class and delete_file method, a test plugin name and a db location + with patch('openlp.core.lib.db.AppLocation') as MockedAppLocation, \ + patch('openlp.core.lib.db.delete_file') as mocked_delete_file: + MockedAppLocation.get_section_data_path.return_value = 'test-dir' + mocked_delete_file.return_value = True + test_plugin = 'test' + test_location = os.path.join('test-dir', test_plugin) + + # WHEN: delete_database is run without a database file + result = delete_database(test_plugin) + + # THEN: The AppLocation.get_section_data_path and delete_file methods should have been called + MockedAppLocation.get_section_data_path.assert_called_with(test_plugin) + mocked_delete_file.assert_called_with(test_location) + self.assertTrue(result, 'The result of delete_file should be True (was rigged that way)') + + def delete_database_with_db_file_name_test(self): + """ + Test that the ``delete_database`` function removes a database file, with the file name supplied + """ + # GIVEN: Mocked out AppLocation class and delete_file method, a test plugin name and a db location + with patch('openlp.core.lib.db.AppLocation') as MockedAppLocation, \ + patch('openlp.core.lib.db.delete_file') as mocked_delete_file: + MockedAppLocation.get_section_data_path.return_value = 'test-dir' + mocked_delete_file.return_value = False + test_plugin = 'test' + test_db_file = 'mydb.sqlite' + test_location = os.path.join('test-dir', test_db_file) + + # WHEN: delete_database is run without a database file + result = delete_database(test_plugin, test_db_file) + + # THEN: The AppLocation.get_section_data_path and delete_file methods should have been called + MockedAppLocation.get_section_data_path.assert_called_with(test_plugin) + mocked_delete_file.assert_called_with(test_location) + self.assertFalse(result, 'The result of delete_file should be False (was rigged that way)')