2013-09-19 21:02:28 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# Copyright (c) 2008-2013 Raoul Snyman #
|
|
|
|
# Portions copyright (c) 2008-2013 Tim Bentley, Gerald Britton, Jonathan #
|
|
|
|
# Corwin, Samuel Findlay, Michael Gorven, Scott Guerrieri, Matthias Hub, #
|
|
|
|
# Meinert Jordan, Armin Köhler, Erik Lundin, Edwin Lunando, Brian T. Meyer. #
|
|
|
|
# Joshua Miller, Stevan Pettit, Andreas Preikschat, Mattias Põldaru, #
|
|
|
|
# Christian Richter, Philip Ridout, Simon Scudder, Jeffrey Smith, #
|
|
|
|
# Maikel Stuivenberg, Martin Thompson, Jon Tibble, Dave Warnock, #
|
|
|
|
# Frode Woldsund, Martin Zibricky, Patrick Zimmermann #
|
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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; version 2 of the License. #
|
|
|
|
# #
|
|
|
|
# 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, write to the Free Software Foundation, Inc., 59 #
|
|
|
|
# Temple Place, Suite 330, Boston, MA 02111-1307 USA #
|
|
|
|
###############################################################################
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
|
|
|
Package to test the openlp.core.lib package.
|
|
|
|
"""
|
2013-05-18 08:44:03 +00:00
|
|
|
import os
|
|
|
|
|
2012-12-06 22:19:17 +00:00
|
|
|
from unittest import TestCase
|
2013-01-21 13:17:27 +00:00
|
|
|
from datetime import datetime, timedelta
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-05-18 08:44:03 +00:00
|
|
|
from PyQt4 import QtCore, QtGui
|
|
|
|
|
|
|
|
from openlp.core.lib import str_to_bool, create_thumb, translate, check_directory_exists, get_text_file_string, \
|
|
|
|
build_icon, image_to_byte, check_item_selected, validate_thumb, create_separated_list, clean_tags, expand_tags
|
2013-09-19 21:02:28 +00:00
|
|
|
from tests.functional import MagicMock, patch
|
2013-05-18 08:44:03 +00:00
|
|
|
|
2013-08-31 18:17:38 +00:00
|
|
|
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'resources'))
|
2012-12-07 17:47:33 +00:00
|
|
|
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2012-12-07 21:15:10 +00:00
|
|
|
class TestLib(TestCase):
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_bool_true_test(self):
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
2013-09-29 21:21:47 +00:00
|
|
|
Test the str_to_bool function with boolean input of True
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
2013-03-30 08:46:34 +00:00
|
|
|
# GIVEN: A boolean value set to true
|
2012-12-06 22:19:17 +00:00
|
|
|
true_boolean = True
|
|
|
|
|
|
|
|
# WHEN: We "convert" it to a bool
|
|
|
|
true_result = str_to_bool(true_boolean)
|
|
|
|
|
|
|
|
# THEN: We should get back a True bool
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertIsInstance(true_result, bool, 'The result should be a boolean')
|
|
|
|
self.assertTrue(true_result, 'The result should be True')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_bool_false_test(self):
|
|
|
|
"""
|
|
|
|
Test the str_to_bool function with boolean input of False
|
|
|
|
"""
|
2013-03-30 08:46:34 +00:00
|
|
|
# GIVEN: A boolean value set to false
|
2012-12-06 22:19:17 +00:00
|
|
|
false_boolean = False
|
|
|
|
|
|
|
|
# WHEN: We "convert" it to a bool
|
|
|
|
false_result = str_to_bool(false_boolean)
|
|
|
|
|
|
|
|
# THEN: We should get back a True bool
|
2013-09-29 21:28:53 +00:00
|
|
|
self.assertIsInstance(false_result, bool, 'The result should be a boolean')
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(false_result, 'The result should be True')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_integer_test(self):
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
2013-09-29 21:21:47 +00:00
|
|
|
Test the str_to_bool function with an integer input
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: An integer value
|
|
|
|
int_string = 1
|
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
int_result = str_to_bool(int_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(int_result, 'The result should be False')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_invalid_string_test(self):
|
|
|
|
"""
|
|
|
|
Test the str_to_bool function with an invalid string
|
|
|
|
"""
|
2012-12-06 22:19:17 +00:00
|
|
|
# GIVEN: An string value with completely invalid input
|
2013-08-31 18:17:38 +00:00
|
|
|
invalid_string = 'my feet are wet'
|
2012-12-06 22:19:17 +00:00
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
str_result = str_to_bool(invalid_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(str_result, 'The result should be False')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_string_false_test(self):
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
2013-09-29 21:21:47 +00:00
|
|
|
Test the str_to_bool function with a string saying "false"
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A string set to "false"
|
2013-08-31 18:17:38 +00:00
|
|
|
false_string = 'false'
|
2012-12-06 22:19:17 +00:00
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
false_result = str_to_bool(false_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(false_result, 'The result should be False')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_string_no_test(self):
|
|
|
|
"""
|
|
|
|
Test the str_to_bool function with a string saying "NO"
|
|
|
|
"""
|
2012-12-06 22:19:17 +00:00
|
|
|
# GIVEN: An string set to "NO"
|
2013-08-31 18:17:38 +00:00
|
|
|
no_string = 'NO'
|
2012-12-06 22:19:17 +00:00
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
str_result = str_to_bool(no_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a false
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(str_result, 'The result should be False')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_true_string_value_test(self):
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
2013-09-29 21:21:47 +00:00
|
|
|
Test the str_to_bool function with a string set to "True"
|
2012-12-06 22:19:17 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A string set to "True"
|
2013-08-31 18:17:38 +00:00
|
|
|
true_string = 'True'
|
2012-12-06 22:19:17 +00:00
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
true_result = str_to_bool(true_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a true
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertTrue(true_result, 'The result should be True')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2013-09-29 21:21:47 +00:00
|
|
|
def str_to_bool_with_yes_string_value_test(self):
|
|
|
|
"""
|
|
|
|
Test the str_to_bool function with a string set to "yes"
|
|
|
|
"""
|
2012-12-06 22:19:17 +00:00
|
|
|
# GIVEN: An string set to "yes"
|
2013-08-31 18:17:38 +00:00
|
|
|
yes_string = 'yes'
|
2012-12-06 22:19:17 +00:00
|
|
|
|
|
|
|
# WHEN: we convert it to a bool
|
|
|
|
str_result = str_to_bool(yes_string)
|
|
|
|
|
|
|
|
# THEN: we should get back a true
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertTrue(str_result, 'The result should be True')
|
2012-12-06 22:19:17 +00:00
|
|
|
|
2012-12-07 17:47:33 +00:00
|
|
|
def translate_test(self):
|
|
|
|
"""
|
|
|
|
Test the translate() function
|
|
|
|
"""
|
|
|
|
# GIVEN: A string to translate and a mocked Qt translate function
|
2013-08-31 18:17:38 +00:00
|
|
|
context = 'OpenLP.Tests'
|
|
|
|
text = 'Untranslated string'
|
|
|
|
comment = 'A comment'
|
2012-12-07 17:47:33 +00:00
|
|
|
encoding = 1
|
|
|
|
n = 1
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_translate = MagicMock(return_value='Translated string')
|
2012-12-07 17:47:33 +00:00
|
|
|
|
|
|
|
# WHEN: we call the translate function
|
|
|
|
result = translate(context, text, comment, encoding, n, mocked_translate)
|
|
|
|
|
|
|
|
# THEN: the translated string should be returned, and the mocked function should have been called
|
|
|
|
mocked_translate.assert_called_with(context, text, comment, encoding, n)
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertEqual('Translated string', result, 'The translated string should have been returned')
|
2012-12-07 17:47:33 +00:00
|
|
|
|
2012-12-07 18:10:24 +00:00
|
|
|
def check_directory_exists_test(self):
|
|
|
|
"""
|
|
|
|
Test the check_directory_exists() function
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.os.path.exists') as mocked_exists, \
|
|
|
|
patch('openlp.core.lib.os.makedirs') as mocked_makedirs:
|
2012-12-07 18:10:24 +00:00
|
|
|
# GIVEN: A directory to check and a mocked out os.makedirs and os.path.exists
|
2013-08-31 18:17:38 +00:00
|
|
|
directory_to_check = 'existing/directory'
|
2012-12-07 18:10:24 +00:00
|
|
|
|
2012-12-07 20:50:38 +00:00
|
|
|
# WHEN: os.path.exists returns True and we check to see if the directory exists
|
2012-12-07 18:10:24 +00:00
|
|
|
mocked_exists.return_value = True
|
|
|
|
check_directory_exists(directory_to_check)
|
|
|
|
|
|
|
|
# THEN: Only os.path.exists should have been called
|
|
|
|
mocked_exists.assert_called_with(directory_to_check)
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertIsNot(mocked_makedirs.called, 'os.makedirs should not have been called')
|
2012-12-07 18:10:24 +00:00
|
|
|
|
|
|
|
# WHEN: os.path.exists returns False and we check the directory exists
|
|
|
|
mocked_exists.return_value = False
|
|
|
|
check_directory_exists(directory_to_check)
|
|
|
|
|
|
|
|
# THEN: Both the mocked functions should have been called
|
|
|
|
mocked_exists.assert_called_with(directory_to_check)
|
|
|
|
mocked_makedirs.assert_called_with(directory_to_check)
|
|
|
|
|
2012-12-07 19:08:28 +00:00
|
|
|
# WHEN: os.path.exists raises an IOError
|
|
|
|
mocked_exists.side_effect = IOError()
|
|
|
|
check_directory_exists(directory_to_check)
|
|
|
|
|
|
|
|
# THEN: We shouldn't get an exception though the mocked exists has been called
|
|
|
|
mocked_exists.assert_called_with(directory_to_check)
|
|
|
|
|
|
|
|
# WHEN: Some other exception is raised
|
|
|
|
mocked_exists.side_effect = ValueError()
|
|
|
|
|
|
|
|
# THEN: check_directory_exists raises an exception
|
2012-12-07 20:50:38 +00:00
|
|
|
mocked_exists.assert_called_with(directory_to_check)
|
2012-12-07 19:08:28 +00:00
|
|
|
self.assertRaises(ValueError, check_directory_exists, directory_to_check)
|
2012-12-10 20:48:37 +00:00
|
|
|
|
|
|
|
def get_text_file_string_no_file_test(self):
|
|
|
|
"""
|
|
|
|
Test the get_text_file_string() function when a file does not exist
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.os.path.isfile') as mocked_isfile:
|
2012-12-10 20:48:37 +00:00
|
|
|
# GIVEN: A mocked out isfile which returns true, and a text file name
|
2013-08-31 18:17:38 +00:00
|
|
|
filename = 'testfile.txt'
|
2012-12-10 20:48:37 +00:00
|
|
|
mocked_isfile.return_value = False
|
|
|
|
|
|
|
|
# WHEN: get_text_file_string is called
|
|
|
|
result = get_text_file_string(filename)
|
|
|
|
|
|
|
|
# THEN: The result should be False
|
|
|
|
mocked_isfile.assert_called_with(filename)
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(result, 'False should be returned if no file exists')
|
2012-12-10 20:48:37 +00:00
|
|
|
|
|
|
|
def get_text_file_string_read_error_test(self):
|
2013-01-07 12:05:19 +00:00
|
|
|
"""
|
|
|
|
Test the get_text_file_string() method when a read error happens
|
|
|
|
"""
|
2013-09-29 21:21:47 +00:00
|
|
|
with patch('openlp.core.lib.os.path.isfile') as mocked_isfile, \
|
|
|
|
patch('openlp.core.lib.open', create=True) as mocked_open:
|
2012-12-10 20:48:37 +00:00
|
|
|
# GIVEN: A mocked-out open() which raises an exception and isfile returns True
|
2013-08-31 18:17:38 +00:00
|
|
|
filename = 'testfile.txt'
|
2012-12-10 20:48:37 +00:00
|
|
|
mocked_isfile.return_value = True
|
|
|
|
mocked_open.side_effect = IOError()
|
|
|
|
|
|
|
|
# WHEN: get_text_file_string is called
|
|
|
|
result = get_text_file_string(filename)
|
|
|
|
|
|
|
|
# THEN: None should be returned
|
|
|
|
mocked_isfile.assert_called_with(filename)
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_open.assert_called_with(filename, 'r')
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertIsNone(result, 'None should be returned if the file cannot be opened')
|
2012-12-10 20:48:37 +00:00
|
|
|
|
|
|
|
def get_text_file_string_decode_error_test(self):
|
2013-01-07 12:05:19 +00:00
|
|
|
"""
|
|
|
|
Test the get_text_file_string() method when the contents cannot be decoded
|
|
|
|
"""
|
2013-09-29 21:21:47 +00:00
|
|
|
self.skipTest('Impossible to test due to conflicts when mocking out the "open" function')
|
2012-12-10 20:48:37 +00:00
|
|
|
|
2013-01-07 20:21:14 +00:00
|
|
|
def build_icon_with_qicon_test(self):
|
|
|
|
"""
|
|
|
|
Test the build_icon() function with a QIcon instance
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.QtGui') as MockedQtGui:
|
2013-01-07 20:21:14 +00:00
|
|
|
# GIVEN: A mocked QIcon
|
|
|
|
MockedQtGui.QIcon = MagicMock
|
|
|
|
mocked_icon = MockedQtGui.QIcon()
|
|
|
|
|
|
|
|
# WHEN: We pass a QIcon instance in
|
|
|
|
result = build_icon(mocked_icon)
|
|
|
|
|
|
|
|
# THEN: The result should be our mocked QIcon
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertIs(mocked_icon, result, 'The result should be the mocked QIcon')
|
2013-01-07 20:21:14 +00:00
|
|
|
|
|
|
|
def build_icon_with_resource_test(self):
|
|
|
|
"""
|
|
|
|
Test the build_icon() function with a resource URI
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.QtGui') as MockedQtGui, \
|
|
|
|
patch('openlp.core.lib.QtGui.QPixmap') as MockedQPixmap:
|
2013-01-07 20:21:14 +00:00
|
|
|
# GIVEN: A mocked QIcon and a mocked QPixmap
|
|
|
|
MockedQtGui.QIcon = MagicMock
|
|
|
|
MockedQtGui.QIcon.Normal = 1
|
|
|
|
MockedQtGui.QIcon.Off = 2
|
2013-08-31 18:17:38 +00:00
|
|
|
MockedQPixmap.return_value = 'mocked_pixmap'
|
|
|
|
resource_uri = ':/resource/uri'
|
2013-01-07 20:21:14 +00:00
|
|
|
|
|
|
|
# WHEN: We pass a QIcon instance in
|
|
|
|
result = build_icon(resource_uri)
|
|
|
|
|
|
|
|
# THEN: The result should be our mocked QIcon
|
|
|
|
MockedQPixmap.assert_called_with(resource_uri)
|
2013-01-20 20:53:58 +00:00
|
|
|
# There really should be more assert statements here but due to type checking and things they all break. The
|
|
|
|
# best we can do is to assert that we get back a MagicMock object.
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertIsInstance(result, MagicMock, 'The result should be a MagicMock, because we mocked it out')
|
2013-01-07 20:21:14 +00:00
|
|
|
|
2013-01-20 20:53:58 +00:00
|
|
|
def image_to_byte_test(self):
|
|
|
|
"""
|
|
|
|
Test the image_to_byte() function
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.QtCore') as MockedQtCore:
|
2013-01-20 20:53:58 +00:00
|
|
|
# GIVEN: A set of mocked-out Qt classes
|
|
|
|
mocked_byte_array = MagicMock()
|
|
|
|
MockedQtCore.QByteArray.return_value = mocked_byte_array
|
2013-09-09 21:10:40 +00:00
|
|
|
mocked_byte_array.toBase64.return_value = QtCore.QByteArray('base64mock')
|
2013-01-20 20:53:58 +00:00
|
|
|
mocked_buffer = MagicMock()
|
|
|
|
MockedQtCore.QBuffer.return_value = mocked_buffer
|
2013-08-31 18:17:38 +00:00
|
|
|
MockedQtCore.QIODevice.WriteOnly = 'writeonly'
|
2013-01-20 20:53:58 +00:00
|
|
|
mocked_image = MagicMock()
|
|
|
|
|
|
|
|
# WHEN: We convert an image to a byte array
|
|
|
|
result = image_to_byte(mocked_image)
|
|
|
|
|
|
|
|
# THEN: We should receive a value of u'base64mock'
|
|
|
|
MockedQtCore.QByteArray.assert_called_with()
|
|
|
|
MockedQtCore.QBuffer.assert_called_with(mocked_byte_array)
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_buffer.open.assert_called_with('writeonly')
|
2013-01-20 20:53:58 +00:00
|
|
|
mocked_image.save.assert_called_with(mocked_buffer, "PNG")
|
|
|
|
mocked_byte_array.toBase64.assert_called_with()
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertEqual('base64mock', result,
|
2013-10-02 21:37:00 +00:00
|
|
|
'The result should be the return value of the mocked out base64 method')
|
2013-01-20 21:09:52 +00:00
|
|
|
|
2013-05-18 08:44:03 +00:00
|
|
|
def create_thumb_with_size_test(self):
|
|
|
|
"""
|
|
|
|
Test the create_thumb() function
|
|
|
|
"""
|
|
|
|
# GIVEN: An image to create a thumb of.
|
2013-08-31 18:17:38 +00:00
|
|
|
image_path = os.path.join(TEST_PATH, 'church.jpg')
|
|
|
|
thumb_path = os.path.join(TEST_PATH, 'church_thumb.jpg')
|
2013-05-18 08:44:03 +00:00
|
|
|
thumb_size = QtCore.QSize(10, 20)
|
|
|
|
|
|
|
|
# Remove the thumb so that the test actually tests if the thumb will be created. Maybe it was not deleted in the
|
|
|
|
# last test.
|
|
|
|
try:
|
|
|
|
os.remove(thumb_path)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# Only continue when the thumb does not exist.
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(os.path.exists(thumb_path), 'Test was not run, because the thumb already exists.')
|
2013-05-18 08:44:03 +00:00
|
|
|
|
|
|
|
# WHEN: Create the thumb.
|
|
|
|
icon = create_thumb(image_path, thumb_path, size=thumb_size)
|
|
|
|
|
|
|
|
# THEN: Check if the thumb was created.
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertTrue(os.path.exists(thumb_path), 'Test was not ran, because the thumb already exists')
|
|
|
|
self.assertIsInstance(icon, QtGui.QIcon, 'The icon should be a QIcon')
|
|
|
|
self.assertFalse(icon.isNull(), 'The icon should not be null')
|
|
|
|
self.assertEqual(thumb_size, QtGui.QImageReader(thumb_path).size(), 'The thumb should have the given size')
|
2013-05-18 08:44:03 +00:00
|
|
|
|
|
|
|
# Remove the thumb so that the test actually tests if the thumb will be created.
|
|
|
|
try:
|
|
|
|
os.remove(thumb_path)
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
|
2013-01-20 21:09:52 +00:00
|
|
|
def check_item_selected_true_test(self):
|
|
|
|
"""
|
2013-05-18 08:44:03 +00:00
|
|
|
Test that the check_item_selected() function returns True when there are selected indexes
|
2013-01-20 21:09:52 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A mocked out QtGui module and a list widget with selected indexes
|
2013-08-31 18:17:38 +00:00
|
|
|
MockedQtGui = patch('openlp.core.lib.QtGui')
|
2013-01-20 21:09:52 +00:00
|
|
|
mocked_list_widget = MagicMock()
|
|
|
|
mocked_list_widget.selectedIndexes.return_value = True
|
2013-08-31 18:17:38 +00:00
|
|
|
message = 'message'
|
2013-01-20 21:09:52 +00:00
|
|
|
|
|
|
|
# WHEN: We check if there are selected items
|
|
|
|
result = check_item_selected(mocked_list_widget, message)
|
|
|
|
|
|
|
|
# THEN: The selectedIndexes function should have been called and the result should be true
|
|
|
|
mocked_list_widget.selectedIndexes.assert_called_with()
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertTrue(result, 'The result should be True')
|
2013-01-20 21:09:52 +00:00
|
|
|
|
|
|
|
def check_item_selected_false_test(self):
|
|
|
|
"""
|
|
|
|
Test that the check_item_selected() function returns False when there are no selected indexes.
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked out QtGui module and a list widget with selected indexes
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.QtGui') as MockedQtGui, \
|
2013-09-29 21:21:47 +00:00
|
|
|
patch('openlp.core.lib.translate') as mocked_translate:
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_translate.return_value = 'mocked translate'
|
2013-01-20 21:09:52 +00:00
|
|
|
mocked_list_widget = MagicMock()
|
|
|
|
mocked_list_widget.selectedIndexes.return_value = False
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_list_widget.parent.return_value = 'parent'
|
|
|
|
message = 'message'
|
2013-01-20 21:09:52 +00:00
|
|
|
|
|
|
|
# WHEN: We check if there are selected items
|
|
|
|
result = check_item_selected(mocked_list_widget, message)
|
|
|
|
|
|
|
|
# THEN: The selectedIndexes function should have been called and the result should be true
|
|
|
|
mocked_list_widget.selectedIndexes.assert_called_with()
|
2013-08-31 18:17:38 +00:00
|
|
|
MockedQtGui.QMessageBox.information.assert_called_with('parent', 'mocked translate', 'message')
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertFalse(result, 'The result should be False')
|
2013-01-20 21:09:52 +00:00
|
|
|
|
2013-03-04 20:17:13 +00:00
|
|
|
def clean_tags_test(self):
|
|
|
|
"""
|
|
|
|
Test clean_tags() method.
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.FormattingTags.get_html_tags') as mocked_get_tags:
|
2013-03-04 20:17:13 +00:00
|
|
|
# GIVEN: Mocked get_html_tags() method.
|
|
|
|
mocked_get_tags.return_value = [{
|
2013-08-31 18:17:38 +00:00
|
|
|
'desc': 'Black',
|
|
|
|
'start tag': '{b}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:black">',
|
|
|
|
'end tag': '{/b}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
2013-03-04 20:17:13 +00:00
|
|
|
}]
|
2013-08-31 18:17:38 +00:00
|
|
|
string_to_pass = 'ASDF<br>foo{br}bar {b}black{/b}'
|
|
|
|
wanted_string = 'ASDF\nfoo\nbar black'
|
2013-03-04 20:44:34 +00:00
|
|
|
|
2013-03-04 20:17:13 +00:00
|
|
|
# WHEN: Clean the string.
|
|
|
|
result_string = clean_tags(string_to_pass)
|
|
|
|
|
2013-03-04 20:40:30 +00:00
|
|
|
# THEN: The strings should be identical.
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertEqual(wanted_string, result_string, 'The strings should be identical')
|
2013-03-04 20:17:13 +00:00
|
|
|
|
2013-03-05 14:36:01 +00:00
|
|
|
def expand_tags_test(self):
|
|
|
|
"""
|
|
|
|
Test the expand_tags() method.
|
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.FormattingTags.get_html_tags') as mocked_get_tags:
|
2013-03-05 14:36:01 +00:00
|
|
|
# GIVEN: Mocked get_html_tags() method.
|
|
|
|
mocked_get_tags.return_value = [
|
|
|
|
{
|
2013-08-31 18:17:38 +00:00
|
|
|
'desc': 'Black',
|
|
|
|
'start tag': '{b}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:black">',
|
|
|
|
'end tag': '{/b}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
2013-03-05 14:36:01 +00:00
|
|
|
},
|
|
|
|
{
|
2013-08-31 18:17:38 +00:00
|
|
|
'desc': 'Yellow',
|
|
|
|
'start tag': '{y}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:yellow">',
|
|
|
|
'end tag': '{/y}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
2013-03-05 14:36:01 +00:00
|
|
|
},
|
|
|
|
{
|
2013-08-31 18:17:38 +00:00
|
|
|
'desc': 'Green',
|
|
|
|
'start tag': '{g}',
|
|
|
|
'start html': '<span style="-webkit-text-fill-color:green">',
|
|
|
|
'end tag': '{/g}', 'end html': '</span>', 'protected': True,
|
|
|
|
'temporary': False
|
2013-03-05 14:36:01 +00:00
|
|
|
}
|
|
|
|
]
|
2013-08-31 18:17:38 +00:00
|
|
|
string_to_pass = '{b}black{/b}{y}yellow{/y}'
|
|
|
|
wanted_string = '<span style="-webkit-text-fill-color:black">black</span>' + \
|
2013-03-05 14:36:01 +00:00
|
|
|
'<span style="-webkit-text-fill-color:yellow">yellow</span>'
|
|
|
|
|
|
|
|
# WHEN: Replace the tags.
|
|
|
|
result_string = expand_tags(string_to_pass)
|
|
|
|
|
|
|
|
# THEN: The strings should be identical.
|
2013-09-29 21:21:47 +00:00
|
|
|
self.assertEqual(wanted_string, result_string, 'The strings should be identical.')
|
2013-03-05 14:36:01 +00:00
|
|
|
|
2013-01-21 13:57:09 +00:00
|
|
|
def validate_thumb_file_does_not_exist_test(self):
|
2013-01-21 13:04:18 +00:00
|
|
|
"""
|
2013-01-21 13:57:09 +00:00
|
|
|
Test the validate_thumb() function when the thumbnail does not exist
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked out os module, with path.exists returning False, and fake paths to a file and a thumb
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.os') as mocked_os:
|
|
|
|
file_path = 'path/to/file'
|
|
|
|
thumb_path = 'path/to/thumb'
|
2013-01-21 13:57:09 +00:00
|
|
|
mocked_os.path.exists.return_value = False
|
2013-02-06 17:36:36 +00:00
|
|
|
|
2013-01-21 13:57:09 +00:00
|
|
|
# WHEN: we run the validate_thumb() function
|
|
|
|
result = validate_thumb(file_path, thumb_path)
|
2013-02-06 17:36:36 +00:00
|
|
|
|
2013-01-21 13:57:09 +00:00
|
|
|
# THEN: we should have called a few functions, and the result should be False
|
|
|
|
mocked_os.path.exists.assert_called_with(thumb_path)
|
2013-08-31 18:17:38 +00:00
|
|
|
assert result is False, 'The result should be False'
|
2013-02-06 17:36:36 +00:00
|
|
|
|
2013-01-21 13:57:09 +00:00
|
|
|
def validate_thumb_file_exists_and_newer_test(self):
|
|
|
|
"""
|
|
|
|
Test the validate_thumb() function when the thumbnail exists and has a newer timestamp than the file
|
2013-01-21 13:04:18 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.os') as mocked_os:
|
|
|
|
file_path = 'path/to/file'
|
|
|
|
thumb_path = 'path/to/thumb'
|
2013-01-21 13:04:18 +00:00
|
|
|
file_mocked_stat = MagicMock()
|
|
|
|
file_mocked_stat.st_mtime = datetime.now()
|
|
|
|
thumb_mocked_stat = MagicMock()
|
|
|
|
thumb_mocked_stat.st_mtime = datetime.now() + timedelta(seconds=10)
|
|
|
|
mocked_os.path.exists.return_value = True
|
2013-01-21 13:57:09 +00:00
|
|
|
mocked_os.stat.side_effect = [file_mocked_stat, thumb_mocked_stat]
|
2013-01-21 13:04:18 +00:00
|
|
|
|
|
|
|
# WHEN: we run the validate_thumb() function
|
|
|
|
|
|
|
|
# THEN: we should have called a few functions, and the result should be True
|
2013-01-21 13:57:09 +00:00
|
|
|
#mocked_os.path.exists.assert_called_with(thumb_path)
|
|
|
|
|
|
|
|
def validate_thumb_file_exists_and_older_test(self):
|
|
|
|
"""
|
|
|
|
Test the validate_thumb() function when the thumbnail exists but is older than the file
|
|
|
|
"""
|
|
|
|
# GIVEN: A mocked out os module, functions rigged to work for us, and fake paths to a file and a thumb
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.os') as mocked_os:
|
|
|
|
file_path = 'path/to/file'
|
|
|
|
thumb_path = 'path/to/thumb'
|
2013-01-21 13:57:09 +00:00
|
|
|
file_mocked_stat = MagicMock()
|
|
|
|
file_mocked_stat.st_mtime = datetime.now()
|
|
|
|
thumb_mocked_stat = MagicMock()
|
|
|
|
thumb_mocked_stat.st_mtime = datetime.now() - timedelta(seconds=10)
|
|
|
|
mocked_os.path.exists.return_value = True
|
2013-01-21 20:15:10 +00:00
|
|
|
mocked_os.stat.side_effect = lambda fname: file_mocked_stat if fname == file_path else thumb_mocked_stat
|
2013-02-06 17:36:36 +00:00
|
|
|
|
2013-01-21 13:57:09 +00:00
|
|
|
# WHEN: we run the validate_thumb() function
|
|
|
|
result = validate_thumb(file_path, thumb_path)
|
|
|
|
|
|
|
|
# THEN: we should have called a few functions, and the result should be False
|
|
|
|
mocked_os.path.exists.assert_called_with(thumb_path)
|
|
|
|
mocked_os.stat.assert_any_call(file_path)
|
|
|
|
mocked_os.stat.assert_any_call(thumb_path)
|
2013-08-31 18:17:38 +00:00
|
|
|
assert result is False, 'The result should be False'
|
2013-02-06 17:36:36 +00:00
|
|
|
|
|
|
|
def create_separated_list_qlocate_test(self):
|
|
|
|
"""
|
2013-05-18 08:44:03 +00:00
|
|
|
Test the create_separated_list function using the Qt provided method
|
2013-02-06 17:36:36 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.Qt') as mocked_qt, \
|
|
|
|
patch('openlp.core.lib.QtCore.QLocale.createSeparatedList') as mocked_createSeparatedList:
|
2013-02-10 18:59:00 +00:00
|
|
|
# GIVEN: A list of strings and the mocked Qt module.
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_qt.PYQT_VERSION_STR = '4.9'
|
|
|
|
mocked_qt.qVersion.return_value = '4.8'
|
|
|
|
mocked_createSeparatedList.return_value = 'Author 1, Author 2, and Author 3'
|
|
|
|
string_list = ['Author 1', 'Author 2', 'Author 3']
|
2013-02-06 17:36:36 +00:00
|
|
|
|
2013-03-04 20:17:13 +00:00
|
|
|
# WHEN: We get a string build from the entries it the list and a separator.
|
2013-02-06 17:36:36 +00:00
|
|
|
string_result = create_separated_list(string_list)
|
|
|
|
|
2013-02-06 20:47:57 +00:00
|
|
|
# THEN: We should have "Author 1, Author 2, and Author 3"
|
2013-08-31 18:17:38 +00:00
|
|
|
assert string_result == 'Author 1, Author 2, and Author 3', 'The string should be u\'Author 1, ' \
|
2013-02-06 17:36:36 +00:00
|
|
|
'Author 2, and Author 3\'.'
|
|
|
|
|
|
|
|
def create_separated_list_empty_list_test(self):
|
|
|
|
"""
|
2013-05-18 08:44:03 +00:00
|
|
|
Test the create_separated_list function with an empty list
|
2013-02-06 17:36:36 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.Qt') as mocked_qt:
|
2013-02-10 18:59:00 +00:00
|
|
|
# GIVEN: An empty list and the mocked Qt module.
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_qt.PYQT_VERSION_STR = '4.8'
|
|
|
|
mocked_qt.qVersion.return_value = '4.7'
|
2013-02-06 17:36:36 +00:00
|
|
|
string_list = []
|
|
|
|
|
2013-03-04 20:17:13 +00:00
|
|
|
# WHEN: We get a string build from the entries it the list and a separator.
|
2013-02-06 17:36:36 +00:00
|
|
|
string_result = create_separated_list(string_list)
|
|
|
|
|
2013-02-06 20:47:57 +00:00
|
|
|
# THEN: We shoud have an emptry string.
|
2013-08-31 18:17:38 +00:00
|
|
|
assert string_result == '', 'The string sould be empty.'
|
2013-02-06 17:36:36 +00:00
|
|
|
|
|
|
|
def create_separated_list_with_one_item_test(self):
|
|
|
|
"""
|
2013-05-18 08:44:03 +00:00
|
|
|
Test the create_separated_list function with a list consisting of only one entry
|
2013-02-06 17:36:36 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.Qt') as mocked_qt:
|
2013-02-10 18:59:00 +00:00
|
|
|
# GIVEN: A list with a string and the mocked Qt module.
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_qt.PYQT_VERSION_STR = '4.8'
|
|
|
|
mocked_qt.qVersion.return_value = '4.7'
|
|
|
|
string_list = ['Author 1']
|
2013-02-06 17:36:36 +00:00
|
|
|
|
2013-03-04 20:17:13 +00:00
|
|
|
# WHEN: We get a string build from the entries it the list and a separator.
|
2013-02-06 17:36:36 +00:00
|
|
|
string_result = create_separated_list(string_list)
|
|
|
|
|
2013-02-06 20:47:57 +00:00
|
|
|
# THEN: We should have "Author 1"
|
2013-08-31 18:17:38 +00:00
|
|
|
assert string_result == 'Author 1', 'The string should be u\'Author 1\'.'
|
2013-02-06 17:36:36 +00:00
|
|
|
|
|
|
|
def create_separated_list_with_two_items_test(self):
|
|
|
|
"""
|
2013-05-18 08:44:03 +00:00
|
|
|
Test the create_separated_list function with a list of two entries
|
2013-02-06 17:36:36 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.Qt') as mocked_qt, patch('openlp.core.lib.translate') as mocked_translate:
|
2013-02-10 18:59:00 +00:00
|
|
|
# GIVEN: A list of strings and the mocked Qt module.
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_qt.PYQT_VERSION_STR = '4.8'
|
|
|
|
mocked_qt.qVersion.return_value = '4.7'
|
|
|
|
mocked_translate.return_value = '%s and %s'
|
|
|
|
string_list = ['Author 1', 'Author 2']
|
2013-02-06 17:36:36 +00:00
|
|
|
|
|
|
|
# WHEN: We get a string build from the entries it the list and a seperator.
|
|
|
|
string_result = create_separated_list(string_list)
|
|
|
|
|
2013-02-06 20:47:57 +00:00
|
|
|
# THEN: We should have "Author 1 and Author 2"
|
2013-08-31 18:17:38 +00:00
|
|
|
assert string_result == 'Author 1 and Author 2', 'The string should be u\'Author 1 and Author 2\'.'
|
2013-02-06 17:36:36 +00:00
|
|
|
|
|
|
|
def create_separated_list_with_three_items_test(self):
|
|
|
|
"""
|
2013-05-18 08:44:03 +00:00
|
|
|
Test the create_separated_list function with a list of three items
|
2013-02-06 17:36:36 +00:00
|
|
|
"""
|
2013-08-31 18:17:38 +00:00
|
|
|
with patch('openlp.core.lib.Qt') as mocked_qt, patch('openlp.core.lib.translate') as mocked_translate:
|
2013-02-10 18:59:00 +00:00
|
|
|
# GIVEN: A list with a string and the mocked Qt module.
|
2013-08-31 18:17:38 +00:00
|
|
|
mocked_qt.PYQT_VERSION_STR = '4.8'
|
|
|
|
mocked_qt.qVersion.return_value = '4.7'
|
2013-02-10 18:59:00 +00:00
|
|
|
# Always return the untranslated string.
|
|
|
|
mocked_translate.side_effect = lambda module, string_to_translate, comment: string_to_translate
|
2013-08-31 18:17:38 +00:00
|
|
|
string_list = ['Author 1', 'Author 2', 'Author 3']
|
2013-02-06 17:36:36 +00:00
|
|
|
|
|
|
|
# WHEN: We get a string build from the entries it the list and a seperator.
|
|
|
|
string_result = create_separated_list(string_list)
|
|
|
|
|
2013-02-06 20:47:57 +00:00
|
|
|
# THEN: We should have "Author 1, Author 2, and Author 3"
|
2013-08-31 18:17:38 +00:00
|
|
|
assert string_result == 'Author 1, Author 2, and Author 3', 'The string should be u\'Author 1, ' \
|
2013-02-06 19:45:11 +00:00
|
|
|
'Author 2, and Author 3\'.'
|