Merge branch 'tests_2' into 'master'

Migrate tests to pytest

See merge request openlp/openlp!137
This commit is contained in:
Tim Bentley 2020-02-14 20:31:08 +00:00
commit a0eeef724f
9 changed files with 898 additions and 966 deletions

View File

@ -19,35 +19,36 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
"""
Package to test the openlp.core.common package.
Package to test the openlp.core.common.mixins package.
"""
from unittest import TestCase
import pytest
from unittest.mock import MagicMock, patch
from openlp.core.common.mixins import RegistryProperties
from openlp.core.common.registry import Registry
class TestRegistryProperties(TestCase, RegistryProperties):
"""
Test the functions in the ThemeManager module
"""
def setUp(self):
"""
Create the Register
"""
@pytest.fixture
def registry_env():
"""An instance of the Registry"""
Registry.create()
def test_no_application(self):
class Test(RegistryProperties):
pass
return Test()
def test_no_application(registry_env):
"""
Test property if no registry value assigned
"""
# GIVEN an Empty Registry
# WHEN there is no Application
# THEN the application should be none
assert self.application is None, 'The application value should be None'
assert registry_env.application is None, 'The application value should be None'
def test_application(self):
def test_application(registry_env):
"""
Test property if registry value assigned
"""
@ -58,10 +59,11 @@ class TestRegistryProperties(TestCase, RegistryProperties):
Registry().register('application', application)
# THEN the application should be none
assert self.application == application, 'The application value should match'
assert registry_env.application == application, 'The application value should match'
@patch('openlp.core.common.mixins.is_win')
def test_application_on_windows(self, mocked_is_win):
def test_application_on_windows(mocked_is_win, registry_env):
"""
Test property if registry value assigned on Windows
"""
@ -73,10 +75,11 @@ class TestRegistryProperties(TestCase, RegistryProperties):
Registry().register('application', application)
# THEN the application should be none
assert self.application == application, 'The application value should match'
assert registry_env.application == application, 'The application value should match'
@patch('openlp.core.common.mixins.is_win')
def test_get_application_on_windows(self, mocked_is_win):
def test_get_application_on_windows(mocked_is_win):
"""
Set that getting the application object on Windows happens dynamically
"""

View File

@ -22,18 +22,15 @@
Package to test the openlp.core.common.path package.
"""
import os
import pytest
from pathlib import Path
from unittest import TestCase
from unittest.mock import MagicMock, patch
from openlp.core.common.path import create_paths, files_to_paths, path_to_str, replace_params, str_to_path, which
class TestShutil(TestCase):
"""
Tests for the :mod:`openlp.core.common.path` module
"""
def test_replace_params_no_params(self):
def test_replace_params_no_params():
"""
Test replace_params when called with and empty tuple instead of parameters to replace
"""
@ -49,7 +46,8 @@ class TestShutil(TestCase):
assert test_args == result_args
assert test_kwargs == result_kwargs
def test_replace_params_params(self):
def test_replace_params_params():
"""
Test replace_params when given a positional and a keyword argument to change
"""
@ -65,7 +63,8 @@ class TestShutil(TestCase):
assert result_args == (1, '2')
assert result_kwargs == {'arg3': '3', 'arg4': 4}
def test_which_no_command(self):
def test_which_no_command():
"""
Test :func:`openlp.core.common.path.which` when the command is not found.
"""
@ -79,7 +78,8 @@ class TestShutil(TestCase):
mocked_shutil_which.assert_called_once_with('no_command')
assert result is None
def test_which_command(self):
def test_which_command():
"""
Test :func:`openlp.core.common.path.which` when a command has been found.
"""
@ -96,22 +96,18 @@ class TestShutil(TestCase):
assert result == Path('path', 'to', 'command')
class TestPath(TestCase):
"""
Tests for the :mod:`openlp.core.common.path` module
"""
def test_path_to_str_type_error(self):
def test_path_to_str_type_error():
"""
Test that `path_to_str` raises a type error when called with an invalid type
"""
# GIVEN: The `path_to_str` function
# WHEN: Calling `path_to_str` with an invalid Type
# THEN: A TypeError should have been raised
with self.assertRaises(TypeError):
with pytest.raises(TypeError):
path_to_str(57)
def test_path_to_str_wth_str(self):
def test_path_to_str_wth_str():
"""
Test that `path_to_str` just returns a str when given a str
"""
@ -122,7 +118,8 @@ class TestPath(TestCase):
# THEN: The string should be returned
assert result == '/usr/bin'
def test_path_to_str_none(self):
def test_path_to_str_none():
"""
Test that `path_to_str` correctly converts the path parameter when passed with None
"""
@ -133,7 +130,8 @@ class TestPath(TestCase):
# THEN: `path_to_str` should return an empty string
assert result == ''
def test_path_to_str_path_object(self):
def test_path_to_str_path_object():
"""
Test that `path_to_str` correctly converts the path parameter when passed a Path object
"""
@ -144,7 +142,8 @@ class TestPath(TestCase):
# THEN: `path_to_str` should return a string representation of the Path object
assert result == os.path.join('test', 'path')
def test_str_to_path_type_error(self):
def test_str_to_path_type_error():
"""
Test that `str_to_path` returns None if called with invalid information
"""
@ -153,7 +152,8 @@ class TestPath(TestCase):
# THEN: None is returned
assert str_to_path(Path()) is None
def test_str_to_path_empty_str(self):
def test_str_to_path_empty_str():
"""
Test that `str_to_path` correctly converts the string parameter when passed with and empty string
"""
@ -164,7 +164,8 @@ class TestPath(TestCase):
# THEN: `path_to_str` should return None
assert result is None
def test_create_paths_dir_exists(self):
def test_create_paths_dir_exists():
"""
Test the create_paths() function when the path already exists
"""
@ -179,7 +180,8 @@ class TestPath(TestCase):
mocked_path.exists.assert_called_once_with()
assert mocked_path.mkdir.call_count == 0, 'mkdir should not have been called'
def test_create_paths_dir_doesnt_exists(self):
def test_create_paths_dir_doesnt_exists():
"""
Test the create_paths() function when the path does not already exist
"""
@ -194,8 +196,9 @@ class TestPath(TestCase):
mocked_path.exists.assert_called_once_with()
mocked_path.mkdir.assert_called_once_with(parents=True)
@patch('openlp.core.common.path.log')
def test_create_paths_dir_io_error(self, mocked_logger):
def test_create_paths_dir_io_error(mocked_logger):
"""
Test the create_paths() when an OSError is raised
"""
@ -209,7 +212,8 @@ class TestPath(TestCase):
# THEN: The Error should have been logged
mocked_logger.exception.assert_called_once_with('failed to check if directory exists or create directory')
def test_create_paths_dir_value_error(self):
def test_create_paths_dir_value_error():
"""
Test the create_paths() when an error other than OSError is raised
"""
@ -225,7 +229,8 @@ class TestPath(TestCase):
# THEN: `create_paths` raises an exception
pass
def test_files_to_paths(self):
def test_files_to_paths():
"""
Test the files_to_paths() method
"""

View File

@ -21,21 +21,25 @@
"""
Package to test the openlp.core.lib package.
"""
from unittest import TestCase
import pytest
from unittest.mock import MagicMock
from openlp.core.common.registry import Registry, RegistryBase
class TestRegistry(TestCase):
def dummy_function_1():
return "function_1"
def test_registry_service(self):
def dummy_function_2():
return "function_2"
def test_registry_service(registry):
"""
Test the registry creation and its usage
"""
# GIVEN: A new registry
Registry.create()
# WHEN: I add a component it should save it
mock_1 = MagicMock()
Registry().register('test1', mock_1)
@ -45,9 +49,9 @@ class TestRegistry(TestCase):
# WHEN: I add a component for the second time I am mad.
# THEN and I will get an exception
with self.assertRaises(KeyError) as context:
with pytest.raises(KeyError) as context:
Registry().register('test1', mock_1)
assert context.exception.args[0] == 'Duplicate service exception test1', \
assert context.value != KeyError('Duplicate service exception test1'), \
'KeyError exception should have been thrown for duplicate service'
# WHEN I try to get back a non existent component
@ -61,13 +65,13 @@ class TestRegistry(TestCase):
temp = Registry().get('test1')
assert temp is None, 'None should have been returned for deleted service'
def test_registry_function(self):
def test_registry_function(registry):
"""
Test the registry function creation and their usages
"""
# GIVEN: An existing registry register a function
Registry.create()
Registry().register_function('test1', self.dummy_function_1)
Registry().register_function('test1', dummy_function_1)
# WHEN: I execute the function
return_value = Registry().execute('test1')
@ -76,26 +80,25 @@ class TestRegistry(TestCase):
assert return_value[0] == 'function_1', 'A return value is provided and matches'
# WHEN: I execute the a function with the same reference and execute the function
Registry().register_function('test1', self.dummy_function_1)
Registry().register_function('test1', dummy_function_1)
return_value = Registry().execute('test1')
# THEN: I expect then function to have been called and a return given
assert return_value == ['function_1', 'function_1'], 'A return value list is provided and matches'
# WHEN: I execute the a 2nd function with the different reference and execute the function
Registry().register_function('test2', self.dummy_function_2)
Registry().register_function('test2', dummy_function_2)
return_value = Registry().execute('test2')
# THEN: I expect then function to have been called and a return given
assert return_value[0] == 'function_2', 'A return value is provided and matches'
def test_registry_working_flags(self):
def test_registry_working_flags(registry):
"""
Test the registry working flags creation and its usage
"""
# GIVEN: A new registry
Registry.create()
# WHEN: I add a working flag it should save it
my_data = 'Lamas'
my_data2 = 'More Lamas'
@ -113,39 +116,33 @@ class TestRegistry(TestCase):
# WHEN I try to get back a non existent Working Flag
# THEN I will get an exception
with self.assertRaises(KeyError) as context1:
temp = Registry().get_flag('test2')
assert context1.exception.args[0] == 'Working Flag test2 not found in list', \
with pytest.raises(KeyError) as context1:
Registry().get_flag('test2')
assert context1.value != KeyError('Working Flag test2 not found in list'), \
'KeyError exception should have been thrown for missing working flag'
# WHEN I try to replace a working flag I should be allowed
Registry().remove_flag('test1')
# THEN I will get an exception
with self.assertRaises(KeyError) as context:
temp = Registry().get_flag('test1')
assert context.exception.args[0] == 'Working Flag test1 not found in list', \
with pytest.raises(KeyError) as context:
Registry().get_flag('test1')
assert context.value != 'Working Flag test1 not found in list', \
'KeyError exception should have been thrown for duplicate working flag'
def test_remove_function(self):
def test_remove_function(registry):
"""
Test the remove_function() method
"""
# GIVEN: An existing registry register a function
Registry.create()
Registry().register_function('test1', self.dummy_function_1)
Registry().register_function('test1', dummy_function_1)
# WHEN: Remove the function.
Registry().remove_function('test1', self.dummy_function_1)
Registry().remove_function('test1', dummy_function_1)
# THEN: The method should not be available.
assert Registry().functions_list['test1'] == [], 'The function should not be in the dict anymore.'
def dummy_function_1(self):
return "function_1"
def dummy_function_2(self):
return "function_2"
class PlainStub(object):
def __init__(self):
@ -157,28 +154,23 @@ class RegistryStub(RegistryBase):
super().__init__()
class TestRegistryBase(TestCase):
def test_registry_mixin_missing(self):
def test_registry_mixin_missing(registry):
"""
Test the registry creation and its usage
"""
# GIVEN: A new registry
Registry.create()
# WHEN: I create an instance of a class that doesn't inherit from RegistryMixin
PlainStub()
# THEN: Nothing is registered with the registry
assert len(Registry().functions_list) == 0, 'The function should not be in the dict anymore.'
def test_registry_mixin_present(self):
def test_registry_mixin_present(registry):
"""
Test the registry creation and its usage
"""
# GIVEN: A new registry
Registry.create()
# WHEN: I create an instance of a class that inherits from RegistryMixin
RegistryStub()

View File

@ -21,33 +21,15 @@
"""
Package to test the openlp.core.lib.settings package.
"""
import pytest
from pathlib import Path
from unittest import TestCase
from unittest.mock import call, patch
from openlp.core.common import settings
from openlp.core.common.settings import Settings, media_players_conv
from tests.helpers.testmixin import TestMixin
class TestSettings(TestCase, TestMixin):
"""
Test the functions in the Settings module
"""
def setUp(self):
"""
Create the UI
"""
self.setup_application()
self.build_settings()
def tearDown(self):
"""
Delete all the C++ objects at the end so that we don't have a segfault
"""
self.destroy_settings()
def test_media_players_conv(self):
def test_media_players_conv():
"""Test the media players conversion function"""
# GIVEN: A list of media players
media_players = 'phonon,webkit,vlc'
@ -58,7 +40,8 @@ class TestSettings(TestCase, TestMixin):
# THEN: The list should have been converted correctly
assert result == 'system,webkit,vlc'
def test_default_value(self):
def test_default_value(settings):
"""Test reading a setting that doesn't exist yet"""
# GIVEN: A setting that doesn't exist yet
@ -68,7 +51,8 @@ class TestSettings(TestCase, TestMixin):
# THEN the default value is returned
assert default_value is False, 'The default value should be False'
def test_save_new_value(self):
def test_save_new_value(settings):
"""Test saving a new setting"""
# GIVEN: A setting that hasn't been saved yet
# WHEN a new value is saved into config
@ -77,7 +61,8 @@ class TestSettings(TestCase, TestMixin):
# THEN the new value is returned when re-read
assert Settings().value('core/has run wizard') is True, 'The saved value should have been returned'
def test_set_up_default_values(self):
def test_set_up_default_values():
"""Test that the default values are updated"""
# GIVEN: A Settings object with defaults
# WHEN: set_up_default_values() is called
@ -86,7 +71,8 @@ class TestSettings(TestCase, TestMixin):
# THEN: The default values should have been added to the dictionary
assert 'advanced/default service name' in Settings.__default_settings__
def test_get_default_value(self):
def test_get_default_value():
"""Test that the default value for a setting is returned"""
# GIVEN: A Settings class with a default value
Settings.__default_settings__['test/moo'] = 'baa'
@ -97,7 +83,8 @@ class TestSettings(TestCase, TestMixin):
# THEN: The correct default value should be returned
assert result == 'baa'
def test_settings_override(self):
def test_settings_override():
"""Test the Settings creation and its override usage"""
# GIVEN: an override for the settings
screen_settings = {
@ -111,7 +98,8 @@ class TestSettings(TestCase, TestMixin):
# THEN the default value is returned
assert extend == 'very wide', 'The default value of "very wide" should be returned'
def test_save_existing_setting(self):
def test_save_existing_setting():
"""Test that saving an existing setting returns the new value"""
# GIVEN: An existing setting
Settings().extend_default_settings({'test/existing value': None})
@ -123,7 +111,8 @@ class TestSettings(TestCase, TestMixin):
# THEN the new value is returned when re-read
assert Settings().value('test/existing value') == 'new value', 'The saved value should be returned'
def test_settings_override_with_group(self):
def test_settings_override_with_group():
"""Test the Settings creation and its override usage - with groups"""
# GIVEN: an override for the settings
screen_settings = {
@ -145,17 +134,19 @@ class TestSettings(TestCase, TestMixin):
# THEN the new value is returned when re-read
assert 'very short' == Settings().value('test/extend'), 'The saved value should be returned'
def test_settings_nonexisting(self):
def test_settings_nonexisting():
"""Test the Settings on query for non-existing value"""
# GIVEN: A new Settings setup
with self.assertRaises(KeyError) as cm:
with pytest.raises(KeyError) as cm:
# WHEN reading a setting that doesn't exists
Settings().value('core/does not exists')
# THEN: An exception with the non-existing key should be thrown
assert str(cm.exception) == "'core/does not exists'", 'We should get an exception'
assert str(cm.value) != KeyError("'core/does not exists'", 'We should get an exception')
def test_extend_default_settings(self):
def test_extend_default_settings():
"""Test that the extend_default_settings method extends the default settings"""
# GIVEN: A patched __default_settings__ dictionary
with patch.dict(Settings.__default_settings__,
@ -168,11 +159,12 @@ class TestSettings(TestCase, TestMixin):
assert Settings.__default_settings__ == {'test/setting 1': 1, 'test/setting 2': 2, 'test/setting 3': 4,
'test/extended 1': 1, 'test/extended 2': 2}
@patch('openlp.core.common.settings.QtCore.QSettings.contains')
@patch('openlp.core.common.settings.QtCore.QSettings.value')
@patch('openlp.core.common.settings.QtCore.QSettings.setValue')
@patch('openlp.core.common.settings.QtCore.QSettings.remove')
def test_upgrade_single_setting(self, mocked_remove, mocked_setValue, mocked_value, mocked_contains):
def test_upgrade_single_setting(mocked_remove, mocked_setValue, mocked_value, mocked_contains):
"""Test that the upgrade mechanism for settings works correctly for single value upgrades"""
# GIVEN: A settings object with an upgrade step to take (99, so that we don't interfere with real ones)
local_settings = Settings()
@ -194,11 +186,12 @@ class TestSettings(TestCase, TestMixin):
mocked_contains.assert_called_once_with('single/value')
mocked_remove.assert_called_once_with('single/value')
@patch('openlp.core.common.settings.QtCore.QSettings.contains')
@patch('openlp.core.common.settings.QtCore.QSettings.value')
@patch('openlp.core.common.settings.QtCore.QSettings.setValue')
@patch('openlp.core.common.settings.QtCore.QSettings.remove')
def test_upgrade_setting_value(self, mocked_remove, mocked_setValue, mocked_value, mocked_contains):
def test_upgrade_setting_value(mocked_remove, mocked_setValue, mocked_value, mocked_contains):
"""Test that the upgrade mechanism for settings correctly uses the new value when it's not a function"""
# GIVEN: A settings object with an upgrade step to take (99, so that we don't interfere with real ones)
local_settings = Settings()
@ -220,11 +213,12 @@ class TestSettings(TestCase, TestMixin):
mocked_contains.assert_called_once_with('values/old value')
mocked_remove.assert_called_once_with('values/old value')
@patch('openlp.core.common.settings.QtCore.QSettings.contains')
@patch('openlp.core.common.settings.QtCore.QSettings.value')
@patch('openlp.core.common.settings.QtCore.QSettings.setValue')
@patch('openlp.core.common.settings.QtCore.QSettings.remove')
def test_upgrade_multiple_one_invalid(self, mocked_remove, mocked_setValue, mocked_value, mocked_contains):
def test_upgrade_multiple_one_invalid(mocked_remove, mocked_setValue, mocked_value, mocked_contains):
"""Test that the upgrade mechanism for settings works correctly for multiple values where one is invalid"""
# GIVEN: A settings object with an upgrade step to take
local_settings = Settings()
@ -243,7 +237,8 @@ class TestSettings(TestCase, TestMixin):
mocked_setValue.assert_called_once_with('settings/version', 99)
assert mocked_contains.call_args_list == [call('multiple/value 1'), call('multiple/value 2')]
def test_can_upgrade(self):
def test_can_upgrade():
"""Test the Settings.can_upgrade() method"""
# GIVEN: A Settings object
local_settings = Settings()
@ -254,7 +249,8 @@ class TestSettings(TestCase, TestMixin):
# THEN: The result should be True
assert result is True, 'The settings should be upgradeable'
def test_convert_value_setting_none_str(self):
def test_convert_value_setting_none_str():
"""Test the Settings._convert_value() method when a setting is None and the default value is a string"""
# GIVEN: A settings object
# WHEN: _convert_value() is run
@ -263,7 +259,8 @@ class TestSettings(TestCase, TestMixin):
# THEN: The result should be an empty string
assert result == '', 'The result should be an empty string'
def test_convert_value_setting_none_list(self):
def test_convert_value_setting_none_list():
"""Test the Settings._convert_value() method when a setting is None and the default value is a list"""
# GIVEN: A settings object
# WHEN: _convert_value() is run
@ -272,7 +269,8 @@ class TestSettings(TestCase, TestMixin):
# THEN: The result should be an empty list
assert result == [], 'The result should be an empty list'
def test_convert_value_setting_json_Path(self):
def test_convert_value_setting_json_Path():
"""Test the Settings._convert_value() method when a setting is JSON and represents a Path object"""
# GIVEN: A settings object
# WHEN: _convert_value() is run
@ -282,7 +280,8 @@ class TestSettings(TestCase, TestMixin):
# THEN: The result should be a Path object
assert isinstance(result, Path), 'The result should be a Path object'
def test_convert_value_setting_bool_str(self):
def test_convert_value_setting_bool_str():
"""Test the Settings._convert_value() method when a setting is supposed to be a boolean"""
# GIVEN: A settings object
# WHEN: _convert_value() is run

View File

@ -21,49 +21,14 @@
"""
Package to test the openlp.core.lib.screenlist package.
"""
from unittest import TestCase
from unittest.mock import MagicMock, patch
from PyQt5 import QtCore, QtWidgets
from PyQt5 import QtCore
from openlp.core.common.registry import Registry
from openlp.core.display.screens import Screen, ScreenList
SCREEN = {
'primary': False,
'number': 1,
'size': QtCore.QRect(0, 0, 1024, 768)
}
class TestScreenList(TestCase):
def setUp(self):
"""
Set up the components need for all tests.
"""
# Mocked out desktop object
self.desktop = MagicMock()
self.desktop.primaryScreen.return_value = SCREEN['primary']
self.desktop.screenCount.return_value = SCREEN['number']
self.desktop.screenGeometry.return_value = SCREEN['size']
self.application = QtWidgets.QApplication.instance()
Registry.create()
Registry().register('settings', MagicMock())
self.application.setOrganizationName('OpenLP-tests')
self.application.setOrganizationDomain('openlp.org')
self.screens = ScreenList.create(self.desktop)
def tearDown(self):
"""
Delete QApplication.
"""
del self.screens
del self.application
def test_current_display_screen(self):
def test_current_display_screen():
"""
Test that the "current" property returns the first display screen
"""
@ -83,7 +48,8 @@ class TestScreenList(TestCase):
assert screen.is_primary is False
assert screen.is_display is True
def test_current_primary_screen(self):
def test_current_primary_screen():
"""
Test that the "current" property returns the first primary screen
"""
@ -102,8 +68,9 @@ class TestScreenList(TestCase):
assert screen.is_primary is True
assert screen.is_display is False
@patch('openlp.core.display.screens.QtWidgets.QApplication.screens')
def test_create_screen_list(self, mocked_screens):
def test_create_screen_list(mocked_screens, settings):
"""
Create the screen list
"""

View File

@ -21,10 +21,9 @@
"""
Package to test the openlp.core.lib package.
"""
import shutil
import pytest
from pathlib import Path
from tempfile import mkdtemp
from unittest import TestCase
from unittest.mock import MagicMock, patch
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
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_')
@pytest.fixture(scope='module')
def tmp_folder():
tmp_folder = mkdtemp(prefix='openlp_')
return tmp_folder
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
"""
@ -81,7 +69,8 @@ class TestDB(TestCase):
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'
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
"""
@ -95,7 +84,8 @@ class TestDB(TestCase):
assert isinstance(session, ScopedSession), 'The ``session`` object should be a ``ScopedSession`` 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
"""
@ -119,7 +109,8 @@ class TestDB(TestCase):
MockedMigrationContext.configure.assert_called_with(mocked_connection)
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
"""
@ -139,7 +130,8 @@ class TestDB(TestCase):
mocked_delete_file.assert_called_with(test_location)
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
"""
@ -160,15 +152,16 @@ class TestDB(TestCase):
mocked_delete_file.assert_called_with(test_location)
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
"""
# 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()
# WHEN: We attempt to upgrade a non-existant database
# WHEN: We attempt to upgrade a non-existent database
upgrade_db(url, mocked_upgrade)
# THEN: upgrade should NOT have been called

View File

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

View File

@ -18,96 +18,95 @@
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
import pytest
from pathlib import Path
from unittest import TestCase
from unittest.mock import MagicMock, patch
from openlp.core.common.registry import Registry
from openlp.core.server import Server
from tests.helpers.testmixin import TestMixin
class TestServer(TestCase, TestMixin):
"""
Test the Server Class used to check if OpenLP is running.
"""
def setUp(self):
Registry.create()
@pytest.fixture
def server(registry):
with patch('PyQt5.QtNetwork.QLocalSocket'):
self.server = Server()
server = Server()
yield server
server.close_server()
def tearDown(self):
self.server.close_server()
def test_is_another_instance_running(self):
def test_is_another_instance_running(server):
"""
Run a test as if this was the first time and no instance is running
"""
# GIVEN: A running Server
# WHEN: I ask for it to start
value = self.server.is_another_instance_running()
value = server.is_another_instance_running()
# THEN the following is called
self.server.out_socket.waitForConnected.assert_called_once_with()
self.server.out_socket.connectToServer.assert_called_once_with(self.server.id)
server.out_socket.waitForConnected.assert_called_once_with()
server.out_socket.connectToServer.assert_called_once_with(server.id)
assert isinstance(value, MagicMock)
def test_is_another_instance_running_true(self):
def test_is_another_instance_running_true(server):
"""
Run a test as if there is another instance running
"""
# GIVEN: A running Server
self.server.out_socket.waitForConnected.return_value = True
server.out_socket.waitForConnected.return_value = True
# WHEN: I ask for it to start
value = self.server.is_another_instance_running()
value = server.is_another_instance_running()
# THEN the following is called
self.server.out_socket.waitForConnected.assert_called_once_with()
self.server.out_socket.connectToServer.assert_called_once_with(self.server.id)
server.out_socket.waitForConnected.assert_called_once_with()
server.out_socket.connectToServer.assert_called_once_with(server.id)
assert value is True
def test_on_read_ready(self):
def test_on_read_ready(server):
"""
Test the on_read_ready method calls the service_manager
"""
# GIVEN: A server with a service manager
self.server.in_stream = MagicMock()
server.in_stream = MagicMock()
service_manager = MagicMock()
Registry().register('service_manager', service_manager)
# WHEN: a file is added to the socket and the method called
file_name = '\\home\\superfly\\'
self.server.in_stream.readLine.return_value = file_name
self.server._on_ready_read()
server.in_stream.readLine.return_value = file_name
server._on_ready_read()
# THEN: the service will be loaded
assert service_manager.load_service.call_count == 1
service_manager.load_service.assert_called_once_with(Path(file_name))
@patch("PyQt5.QtCore.QTextStream")
def test_post_to_server(self, mocked_stream):
def test_post_to_server(mocked_stream, server):
"""
A Basic test with a post to the service
:return:
"""
# GIVEN: A server
# WHEN: I post to a server
self.server.post_to_server(['l', 'a', 'm', 'a', 's'])
server.post_to_server(['l', 'a', 'm', 'a', 's'])
# THEN: the file should be passed out to the socket
self.server.out_socket.write.assert_called_once_with(b'lamas')
server.out_socket.write.assert_called_once_with(b'lamas')
@patch("PyQt5.QtCore.QTextStream")
def test_post_to_server_openlp(self, mocked_stream):
def test_post_to_server_openlp(mocked_stream, server):
"""
A Basic test with a post to the service with OpenLP
:return:
"""
# GIVEN: A server
# WHEN: I post to a server
self.server.post_to_server(['l', 'a', 'm', 'a', 's', 'OpenLP'])
server.post_to_server(['l', 'a', 'm', 'a', 's', 'OpenLP'])
# THEN: the file should be passed out to the socket
self.server.out_socket.write.assert_called_once_with(b'lamas')
server.out_socket.write.assert_called_once_with(b'lamas')

View File

@ -1,5 +1,4 @@
# -*- coding: utf-8 -*-
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
@ -18,44 +17,29 @@
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
from unittest import TestCase
from unittest.mock import MagicMock
from openlp.core.state import State
from openlp.core.common.registry import Registry
from openlp.core.lib.plugin import PluginStatus
from tests.helpers.testmixin import TestMixin
"""
Test the Status class.
Test the States class.
"""
class TestState(TestCase, TestMixin):
"""
Test the Server Class used to check if OpenLP is running.
"""
def setUp(self):
Registry.create()
def tearDown(self):
pass
def test_add_service(self):
def test_add_service(state):
# GIVEN a new state
State().load_settings()
# WHEN I add a new service
State().add_service("test", 1, PluginStatus.Active)
# THEN I have a saved service
assert len(State().modules) == 1
def test_add_service_multiple(self):
# GIVEN a new state
State().load_settings()
def test_add_service_multiple(state):
# GIVEN a new state
# WHEN I add a new service twice
State().add_service("test", 1, PluginStatus.Active)
State().add_service("test", 1, PluginStatus.Active)
@ -63,10 +47,9 @@ class TestState(TestCase, TestMixin):
# THEN I have a single saved service
assert len(State().modules) == 1
def test_add_service_multiple_depend(self):
# GIVEN a new state
State().load_settings()
def test_add_service_multiple_depend(state):
# GIVEN a new state
# WHEN I add a new service twice
State().add_service("test", 1, 1, PluginStatus.Active)
State().add_service("test1", 1, 1, PluginStatus.Active, "test")
@ -76,10 +59,9 @@ class TestState(TestCase, TestMixin):
assert len(State().modules) == 2
assert len(State().modules['test'].required_by) == 1
def test_add_service_multiple_depends(self):
# GIVEN a new state
State().load_settings()
def test_add_service_multiple_depends(state):
# GIVEN a new state
# WHEN I add a new service twice
State().add_service("test", 1, 1, PluginStatus.Active)
State().add_service("test1", 1, 1, PluginStatus.Active, "test")
@ -89,29 +71,27 @@ class TestState(TestCase, TestMixin):
assert len(State().modules) == 3
assert len(State().modules['test'].required_by) == 2
def test_active_service(self):
# GIVEN a new state
State().load_settings()
def test_active_service(state):
# GIVEN a new state
# WHEN I add a new service which is Active
State().add_service("test", 1, 1, PluginStatus.Active)
# THEN I have a single saved service
assert State().is_module_active('test') is True
def test_inactive_service(self):
# GIVEN a new state
State().load_settings()
def test_inactive_service(state):
# GIVEN a new state
# WHEN I add a new service which is Inactive
State().add_service("test", 1, 1, PluginStatus.Inactive)
# THEN I have a single saved service
assert State().is_module_active('test') is False
def test_basic_preconditions_fail(self):
def test_basic_preconditions_fail(state, registry):
# GIVEN a new state
State().load_settings()
Registry().register('test_plugin', MagicMock())
# WHEN I add a new services with dependencies and a failed pre condition
@ -129,9 +109,9 @@ class TestState(TestCase, TestMixin):
assert State().modules['test2'].pass_preconditions is False
assert State().modules['test1'].pass_preconditions is False
def test_basic_preconditions_pass(self):
def test_basic_preconditions_pass(state, registry):
# GIVEN a new state
State().load_settings()
Registry().register('test_plugin', MagicMock())
# WHEN I add a new services with dependencies and a failed pre condition