openlp/tests/openlp_core/common/test_mixins.py

101 lines
3.7 KiB
Python
Raw Normal View History

2014-07-02 18:38:10 +00:00
# -*- coding: utf-8 -*-
2019-04-13 13:00:22 +00:00
##########################################################################
# OpenLP - Open Source Lyrics Projection #
# ---------------------------------------------------------------------- #
2020-12-30 21:42:49 +00:00
# Copyright (c) 2008-2021 OpenLP Developers #
2019-04-13 13:00:22 +00:00
# ---------------------------------------------------------------------- #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see <https://www.gnu.org/licenses/>. #
##########################################################################
2014-07-02 18:38:10 +00:00
"""
2020-02-13 20:50:39 +00:00
Package to test the openlp.core.common.mixins package.
2014-07-02 18:38:10 +00:00
"""
2020-02-13 20:50:39 +00:00
import pytest
2017-10-23 22:09:57 +00:00
from unittest.mock import MagicMock, patch
2014-07-02 18:38:10 +00:00
2017-10-23 22:09:57 +00:00
from openlp.core.common.mixins import RegistryProperties
2017-10-07 07:05:07 +00:00
from openlp.core.common.registry import Registry
2014-07-02 18:38:10 +00:00
2017-10-07 07:05:07 +00:00
2020-02-13 20:50:39 +00:00
@pytest.fixture
def registry_env():
"""An instance of the Registry"""
Registry.create()
class Test(RegistryProperties):
pass
return Test()
def test_no_application(registry_env):
2017-10-23 22:09:57 +00:00
"""
2020-02-13 20:50:39 +00:00
Test property if no registry value assigned
"""
# GIVEN an Empty Registry
# WHEN there is no Application
# THEN the application should be none
assert registry_env.application is None, 'The application value should be None'
def test_application(registry_env):
2017-10-23 22:09:57 +00:00
"""
2020-02-13 20:50:39 +00:00
Test property if registry value assigned
"""
# GIVEN an Empty Registry
application = MagicMock()
# WHEN the application is registered
Registry().register('application', application)
# THEN the application should be none
assert registry_env.application == application, 'The application value should match'
@patch('openlp.core.common.mixins.is_win')
def test_application_on_windows(mocked_is_win, registry_env):
"""
Test property if registry value assigned on Windows
"""
# GIVEN an Empty Registry and we're on Windows
application = MagicMock()
mocked_is_win.return_value = True
# WHEN the application is registered
Registry().register('application', application)
# THEN the application should be none
assert registry_env.application == application, 'The application value should match'
@patch('openlp.core.common.mixins.is_win')
def test_get_application_on_windows(mocked_is_win):
"""
Set that getting the application object on Windows happens dynamically
"""
# GIVEN an Empty Registry and we're on Windows
mocked_is_win.return_value = True
mock_application = MagicMock()
reg_props = RegistryProperties()
registry = Registry()
# WHEN the application is accessed
with patch.object(registry, 'get') as mocked_get:
mocked_get.return_value = mock_application
actual_application = reg_props.application
# THEN the application should be the mock object, and the correct function should have been called
assert mock_application == actual_application, 'The application value should match'
mocked_is_win.assert_called_with()
mocked_get.assert_called_with('application')