Add a test

This commit is contained in:
Raoul Snyman 2017-03-27 21:53:46 -07:00
parent a62d9c6a41
commit 12cfe103b7

View File

@ -24,6 +24,8 @@ import sys
from unittest import TestCase
from unittest.mock import MagicMock, patch
from PyQt5 import QtWidgets
from openlp.core import OpenLP, parse_options
@ -156,3 +158,73 @@ class TestOpenLP(TestCase):
assert result is False
del app
@patch('openlp.core.QtCore.QSharedMemory')
def test_is_already_running_not_running(self, MockedSharedMemory):
"""
Test the is_already_running() method when OpenLP is NOT running
"""
# GIVEN: An OpenLP app and some mocks
mocked_shared_memory = MagicMock()
mocked_shared_memory.attach.return_value = False
MockedSharedMemory.return_value = mocked_shared_memory
app = OpenLP([])
# WHEN: is_already_running() is called
result = app.is_already_running()
# THEN: The result should be false
MockedSharedMemory.assert_called_once_with('OpenLP')
mocked_shared_memory.attach.assert_called_once_with()
mocked_shared_memory.create.assert_called_once_with(1)
assert result is False
@patch('openlp.core.QtWidgets.QMessageBox.critical')
@patch('openlp.core.QtWidgets.QMessageBox.StandardButtons')
@patch('openlp.core.QtCore.QSharedMemory')
def test_is_already_running_is_running_continue(self, MockedSharedMemory, MockedStandardButtons, mocked_critical):
"""
Test the is_already_running() method when OpenLP IS running and the user chooses to continue
"""
# GIVEN: An OpenLP app and some mocks
mocked_shared_memory = MagicMock()
mocked_shared_memory.attach.return_value = True
MockedSharedMemory.return_value = mocked_shared_memory
MockedStandardButtons.return_value = 0
mocked_critical.return_value = QtWidgets.QMessageBox.Yes
app = OpenLP([])
# WHEN: is_already_running() is called
result = app.is_already_running()
# THEN: The result should be false
MockedSharedMemory.assert_called_once_with('OpenLP')
mocked_shared_memory.attach.assert_called_once_with()
MockedStandardButtons.assert_called_once_with(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
mocked_critical.assert_called_once_with(None, 'Error', 'OpenLP is already running. Do you wish to continue?', 0)
assert result is False
@patch('openlp.core.QtWidgets.QMessageBox.critical')
@patch('openlp.core.QtWidgets.QMessageBox.StandardButtons')
@patch('openlp.core.QtCore.QSharedMemory')
def test_is_already_running_is_running_stop(self, MockedSharedMemory, MockedStandardButtons, mocked_critical):
"""
Test the is_already_running() method when OpenLP IS running and the user chooses to stop
"""
# GIVEN: An OpenLP app and some mocks
mocked_shared_memory = MagicMock()
mocked_shared_memory.attach.return_value = True
MockedSharedMemory.return_value = mocked_shared_memory
MockedStandardButtons.return_value = 0
mocked_critical.return_value = QtWidgets.QMessageBox.No
app = OpenLP([])
# WHEN: is_already_running() is called
result = app.is_already_running()
# THEN: The result should be false
MockedSharedMemory.assert_called_once_with('OpenLP')
mocked_shared_memory.attach.assert_called_once_with()
MockedStandardButtons.assert_called_once_with(QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
mocked_critical.assert_called_once_with(None, 'Error', 'OpenLP is already running. Do you wish to continue?', 0)
assert result is True