forked from openlp/openlp
Make the i18n script work with trunk
Moved tests bzr-revno: 2391
This commit is contained in:
commit
c092bc991c
@ -63,7 +63,7 @@ import webbrowser
|
||||
from optparse import OptionParser
|
||||
from PyQt4 import QtCore
|
||||
|
||||
SERVER_URL = 'http://www.transifex.net/api/2/project/openlp/'
|
||||
SERVER_URL = 'http://www.transifex.net/api/2/project/openlp/resource/openlp-22x/'
|
||||
IGNORED_PATHS = ['scripts']
|
||||
IGNORED_FILES = ['setup.py']
|
||||
|
||||
@ -193,27 +193,26 @@ def download_translations():
|
||||
if not password:
|
||||
password = getpass(' Transifex password: ')
|
||||
# First get the list of languages
|
||||
url = SERVER_URL + 'resource/ents/'
|
||||
base64string = base64.encodebytes('%s:%s' % (username, password))[:-1]
|
||||
auth_header = 'Basic %s' % base64string
|
||||
request = urllib.request.Request(url + '?details')
|
||||
base64string = base64.encodebytes(('%s:%s' % (username, password)).encode())[:-1]
|
||||
auth_header = 'Basic %s' % base64string.decode()
|
||||
request = urllib.request.Request(SERVER_URL + '?details')
|
||||
request.add_header('Authorization', auth_header)
|
||||
print_verbose('Downloading list of languages from: %s' % url)
|
||||
print_verbose('Downloading list of languages from: %s' % SERVER_URL)
|
||||
try:
|
||||
json_response = urllib.request.urlopen(request)
|
||||
except urllib.error.HTTPError:
|
||||
print_quiet('Username or password incorrect.')
|
||||
return False
|
||||
json_dict = json.loads(json_response.read())
|
||||
json_dict = json.loads(json_response.read().decode())
|
||||
languages = [lang['code'] for lang in json_dict['available_languages']]
|
||||
for language in languages:
|
||||
lang_url = url + 'translation/%s/?file' % language
|
||||
lang_url = SERVER_URL + 'translation/%s/?file' % language
|
||||
request = urllib.request.Request(lang_url)
|
||||
request.add_header('Authorization', auth_header)
|
||||
filename = os.path.join(os.path.abspath('..'), 'resources', 'i18n', language + '.ts')
|
||||
print_verbose('Get Translation File: %s' % filename)
|
||||
response = urllib.request.urlopen(request)
|
||||
fd = open(filename, 'w')
|
||||
fd = open(filename, 'wb')
|
||||
fd.write(response.read())
|
||||
fd.close()
|
||||
print_quiet(' Done.')
|
||||
|
@ -32,7 +32,7 @@ Functional tests to test the AppLocation class and related methods.
|
||||
|
||||
from unittest import TestCase
|
||||
|
||||
from openlp.core.common import de_hump, trace_error_handler
|
||||
from openlp.core.common import check_directory_exists, de_hump, trace_error_handler, translate
|
||||
from tests.functional import MagicMock, patch
|
||||
|
||||
|
||||
@ -40,6 +40,45 @@ class TestCommonFunctions(TestCase):
|
||||
"""
|
||||
A test suite to test out various functions in the openlp.core.common module.
|
||||
"""
|
||||
def check_directory_exists_test(self):
|
||||
"""
|
||||
Test the check_directory_exists() function
|
||||
"""
|
||||
with patch('openlp.core.lib.os.path.exists') as mocked_exists, \
|
||||
patch('openlp.core.lib.os.makedirs') as mocked_makedirs:
|
||||
# GIVEN: A directory to check and a mocked out os.makedirs and os.path.exists
|
||||
directory_to_check = 'existing/directory'
|
||||
|
||||
# WHEN: os.path.exists returns True and we check to see if the directory exists
|
||||
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)
|
||||
self.assertIsNot(mocked_makedirs.called, 'os.makedirs should not have been called')
|
||||
|
||||
# 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)
|
||||
|
||||
# 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
|
||||
mocked_exists.assert_called_with(directory_to_check)
|
||||
self.assertRaises(ValueError, check_directory_exists, directory_to_check)
|
||||
|
||||
def de_hump_conversion_test(self):
|
||||
"""
|
||||
Test the de_hump function with a class name
|
||||
@ -81,3 +120,22 @@ class TestCommonFunctions(TestCase):
|
||||
# THEN: The mocked_logger.error() method should have been called with the correct parameters
|
||||
mocked_logger.error.assert_called_with(
|
||||
'OpenLP Error trace\n File openlp.fake at line 56 \n\t called trace_error_handler_test')
|
||||
|
||||
def translate_test(self):
|
||||
"""
|
||||
Test the translate() function
|
||||
"""
|
||||
# GIVEN: A string to translate and a mocked Qt translate function
|
||||
context = 'OpenLP.Tests'
|
||||
text = 'Untranslated string'
|
||||
comment = 'A comment'
|
||||
encoding = 1
|
||||
n = 1
|
||||
mocked_translate = MagicMock(return_value='Translated string')
|
||||
|
||||
# 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)
|
||||
self.assertEqual('Translated string', result, 'The translated string should have been returned')
|
||||
|
@ -36,9 +36,8 @@ from datetime import datetime, timedelta
|
||||
|
||||
from PyQt4 import QtCore, QtGui
|
||||
|
||||
from openlp.core.common import check_directory_exists, translate
|
||||
from openlp.core.lib import str_to_bool, create_thumb, get_text_file_string, \
|
||||
build_icon, image_to_byte, check_item_selected, validate_thumb, create_separated_list, clean_tags, expand_tags
|
||||
from openlp.core.lib import build_icon, check_item_selected, clean_tags, create_thumb, create_separated_list, \
|
||||
expand_tags, get_text_file_string, image_to_byte, resize_image, str_to_bool, validate_thumb
|
||||
from tests.functional import MagicMock, patch
|
||||
|
||||
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'resources'))
|
||||
@ -152,64 +151,6 @@ class TestLib(TestCase):
|
||||
# THEN: we should get back a true
|
||||
self.assertTrue(str_result, 'The result should be True')
|
||||
|
||||
def translate_test(self):
|
||||
"""
|
||||
Test the translate() function
|
||||
"""
|
||||
# GIVEN: A string to translate and a mocked Qt translate function
|
||||
context = 'OpenLP.Tests'
|
||||
text = 'Untranslated string'
|
||||
comment = 'A comment'
|
||||
encoding = 1
|
||||
n = 1
|
||||
mocked_translate = MagicMock(return_value='Translated string')
|
||||
|
||||
# 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)
|
||||
self.assertEqual('Translated string', result, 'The translated string should have been returned')
|
||||
|
||||
def check_directory_exists_test(self):
|
||||
"""
|
||||
Test the check_directory_exists() function
|
||||
"""
|
||||
with patch('openlp.core.lib.os.path.exists') as mocked_exists, \
|
||||
patch('openlp.core.lib.os.makedirs') as mocked_makedirs:
|
||||
# GIVEN: A directory to check and a mocked out os.makedirs and os.path.exists
|
||||
directory_to_check = 'existing/directory'
|
||||
|
||||
# WHEN: os.path.exists returns True and we check to see if the directory exists
|
||||
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)
|
||||
self.assertIsNot(mocked_makedirs.called, 'os.makedirs should not have been called')
|
||||
|
||||
# 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)
|
||||
|
||||
# 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
|
||||
mocked_exists.assert_called_with(directory_to_check)
|
||||
self.assertRaises(ValueError, check_directory_exists, directory_to_check)
|
||||
|
||||
def get_text_file_string_no_file_test(self):
|
||||
"""
|
||||
Test the get_text_file_string() function when a file does not exist
|
||||
@ -353,7 +294,7 @@ class TestLib(TestCase):
|
||||
Test that the check_item_selected() function returns True when there are selected indexes
|
||||
"""
|
||||
# GIVEN: A mocked out QtGui module and a list widget with selected indexes
|
||||
MockedQtGui = patch('openlp.core.lib.QtGui')
|
||||
mocked_QtGui = patch('openlp.core.lib.QtGui')
|
||||
mocked_list_widget = MagicMock()
|
||||
mocked_list_widget.selectedIndexes.return_value = True
|
||||
message = 'message'
|
||||
@ -508,6 +449,27 @@ class TestLib(TestCase):
|
||||
mocked_os.stat.assert_any_call(thumb_path)
|
||||
assert result is False, 'The result should be False'
|
||||
|
||||
def resize_thumb_test(self):
|
||||
"""
|
||||
Test the resize_thumb() function
|
||||
"""
|
||||
# GIVEN: A path to an image.
|
||||
image_path = os.path.join(TEST_PATH, 'church.jpg')
|
||||
wanted_width = 777
|
||||
wanted_height = 72
|
||||
# We want the background to be white.
|
||||
wanted_background_hex = '#FFFFFF'
|
||||
wanted_background_rgb = QtGui.QColor(wanted_background_hex).rgb()
|
||||
|
||||
# WHEN: Resize the image and add a background.
|
||||
image = resize_image(image_path, wanted_width, wanted_height, wanted_background_hex)
|
||||
|
||||
# THEN: Check if the size is correct and the background was set.
|
||||
result_size = image.size()
|
||||
self.assertEqual(wanted_height, result_size.height(), 'The image should have the requested height.')
|
||||
self.assertEqual(wanted_width, result_size.width(), 'The image should have the requested width.')
|
||||
self.assertEqual(image.pixel(0, 0), wanted_background_rgb, 'The background should be white.')
|
||||
|
||||
def create_separated_list_qlocate_test(self):
|
||||
"""
|
||||
Test the create_separated_list function using the Qt provided method
|
||||
|
Loading…
Reference in New Issue
Block a user