From 208c1b022f641dad205aa1442fc2314c193fe2f1 Mon Sep 17 00:00:00 2001 From: Ken Roberts Date: Fri, 9 Jun 2017 06:45:18 -0700 Subject: [PATCH] Test for db upgrade skip --- openlp/core/lib/db.py | 2 +- tests/functional/openlp_core_lib/test_db.py | 33 ++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/openlp/core/lib/db.py b/openlp/core/lib/db.py index 674204925..4c20a9d59 100644 --- a/openlp/core/lib/db.py +++ b/openlp/core/lib/db.py @@ -62,7 +62,7 @@ def database_exists(url): create_database(engine.url) database_exists(engine.url) #=> True - Borrowed from SQLAlchemy_Utils since we only need this one function. + Borrowed from SQLAlchemy_Utils (v0.32.14 )since we only need this one function. """ url = copy(make_url(url)) diff --git a/tests/functional/openlp_core_lib/test_db.py b/tests/functional/openlp_core_lib/test_db.py index 70c6be9ae..1db1d5188 100644 --- a/tests/functional/openlp_core_lib/test_db.py +++ b/tests/functional/openlp_core_lib/test_db.py @@ -23,6 +23,9 @@ Package to test the openlp.core.lib package. """ import os +import shutil + +from tempfile import mkdtemp from unittest import TestCase from unittest.mock import patch, MagicMock @@ -30,13 +33,27 @@ 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, delete_database +from openlp.core.lib.db import init_db, get_upgrade_op, delete_database, upgrade_db +from openlp.core.lib.projector import upgrade as pjlink_upgrade class TestDB(TestCase): """ A test case for all the tests for the :mod:`~openlp.core.lib.db` module. """ + def setUp(self): + """ + Set up anything necessary for all tests + """ + self.tmp_folder = mkdtemp(prefix='openlp_') + + def tearDown(self): + """ + Clean up + """ + # Ignore errors since windows can have problems with locked files + shutil.rmtree(self.tmp_folder, ignore_errors=True) + def test_init_db_calls_correct_functions(self): """ Test that the init_db function makes the correct function calls @@ -145,3 +162,17 @@ class TestDB(TestCase): 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)') + + @patch('tests.functional.openlp_core_lib.test_db.pjlink_upgrade') + def test_skip_db_upgrade_with_no_database(self, mocked_upgrade): + """ + Test the upgrade_db function does not try to update a missing database + """ + # GIVEN: Database URL that does not (yet) exist + url = 'sqlite:///{tmp}/test_db.sqlite'.format(tmp=self.tmp_folder) + + # WHEN: We attempt to upgrade a non-existant database + upgrade_db(url, pjlink_upgrade) + + # THEN: upgrade should NOT have been called + self.assertFalse(mocked_upgrade.called, 'Database upgrade function should NOT have been called')