Migrate lib part 1

This commit is contained in:
Tim 2020-02-13 21:04:15 +00:00
parent a544aa11ff
commit dae7880604
No known key found for this signature in database
GPG Key ID: 3D454289AF831A6D
2 changed files with 124 additions and 137 deletions

View File

@ -21,10 +21,9 @@
""" """
Package to test the openlp.core.lib package. Package to test the openlp.core.lib package.
""" """
import shutil import pytest
from pathlib import Path from pathlib import Path
from tempfile import mkdtemp from tempfile import mkdtemp
from unittest import TestCase
from unittest.mock import MagicMock, patch from unittest.mock import MagicMock, patch
from sqlalchemy import MetaData from sqlalchemy import MetaData
@ -34,24 +33,13 @@ from sqlalchemy.pool import NullPool
from openlp.core.lib.db import delete_database, get_upgrade_op, init_db, upgrade_db from openlp.core.lib.db import delete_database, get_upgrade_op, init_db, upgrade_db
class TestDB(TestCase): @pytest.fixture(scope='module')
""" def tmp_folder():
A test case for all the tests for the :mod:`~openlp.core.lib.db` module. tmp_folder = mkdtemp(prefix='openlp_')
""" return tmp_folder
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): def test_init_db_calls_correct_functions():
""" """
Test that the init_db function makes the correct function calls Test that the init_db function makes the correct function calls
""" """
@ -81,7 +69,8 @@ class TestDB(TestCase):
assert session is mocked_scoped_session_object, 'The ``session`` object should be the mock' assert session is mocked_scoped_session_object, 'The ``session`` object should be the mock'
assert metadata is mocked_metadata, 'The ``metadata`` object should be the mock' assert metadata is mocked_metadata, 'The ``metadata`` object should be the mock'
def test_init_db_defaults(self):
def test_init_db_defaults():
""" """
Test that initialising an in-memory SQLite database via ``init_db`` uses the defaults Test that initialising an in-memory SQLite database via ``init_db`` uses the defaults
""" """
@ -95,7 +84,8 @@ class TestDB(TestCase):
assert isinstance(session, ScopedSession), 'The ``session`` object should be a ``ScopedSession`` instance' assert isinstance(session, ScopedSession), 'The ``session`` object should be a ``ScopedSession`` instance'
assert isinstance(metadata, MetaData), 'The ``metadata`` object should be a ``MetaData`` instance' assert isinstance(metadata, MetaData), 'The ``metadata`` object should be a ``MetaData`` instance'
def test_get_upgrade_op(self):
def test_get_upgrade_op():
""" """
Test that the ``get_upgrade_op`` function creates a MigrationContext and an Operations object Test that the ``get_upgrade_op`` function creates a MigrationContext and an Operations object
""" """
@ -119,7 +109,8 @@ class TestDB(TestCase):
MockedMigrationContext.configure.assert_called_with(mocked_connection) MockedMigrationContext.configure.assert_called_with(mocked_connection)
MockedOperations.assert_called_with(mocked_context) MockedOperations.assert_called_with(mocked_context)
def test_delete_database_without_db_file_name(self):
def test_delete_database_without_db_file_name():
""" """
Test that the ``delete_database`` function removes a database file, without the file name parameter Test that the ``delete_database`` function removes a database file, without the file name parameter
""" """
@ -139,7 +130,8 @@ class TestDB(TestCase):
mocked_delete_file.assert_called_with(test_location) mocked_delete_file.assert_called_with(test_location)
assert result is True, 'The result of delete_file should be True (was rigged that way)' assert result is True, 'The result of delete_file should be True (was rigged that way)'
def test_delete_database_with_db_file_name(self):
def test_delete_database_with_db_file_name():
""" """
Test that the ``delete_database`` function removes a database file, with the file name supplied Test that the ``delete_database`` function removes a database file, with the file name supplied
""" """
@ -160,15 +152,16 @@ class TestDB(TestCase):
mocked_delete_file.assert_called_with(test_location) mocked_delete_file.assert_called_with(test_location)
assert result is False, 'The result of delete_file should be False (was rigged that way)' assert result is False, 'The result of delete_file should be False (was rigged that way)'
def test_skip_db_upgrade_with_no_database(self):
def test_skip_db_upgrade_with_no_database(tmp_folder):
""" """
Test the upgrade_db function does not try to update a missing database Test the upgrade_db function does not try to update a missing database
""" """
# GIVEN: Database URL that does not (yet) exist # GIVEN: Database URL that does not (yet) exist
url = 'sqlite:///{tmp}/test_db.sqlite'.format(tmp=self.tmp_folder) url = 'sqlite:///{tmp}/test_db.sqlite'.format(tmp=tmp_folder)
mocked_upgrade = MagicMock() mocked_upgrade = MagicMock()
# WHEN: We attempt to upgrade a non-existant database # WHEN: We attempt to upgrade a non-existent database
upgrade_db(url, mocked_upgrade) upgrade_db(url, mocked_upgrade)
# THEN: upgrade should NOT have been called # THEN: upgrade should NOT have been called

View File

@ -21,16 +21,10 @@
""" """
Package to test the openlp.core.lib.exceptions package. Package to test the openlp.core.lib.exceptions package.
""" """
from unittest import TestCase
from openlp.core.lib.exceptions import ValidationError from openlp.core.lib.exceptions import ValidationError
class TestValidationError(TestCase): def test_validation_error():
"""
Test the ValidationError Class
"""
def test_validation_error(self):
""" """
Test the creation of a ValidationError Test the creation of a ValidationError
""" """