Moved some stuff around and added some more tests

This commit is contained in:
Raoul Snyman 2013-01-20 22:53:58 +02:00
parent 8c916254e4
commit 1f51426a78
4 changed files with 33 additions and 8 deletions

View File

@ -5,7 +5,8 @@ from unittest import TestCase
from mock import MagicMock, patch
from openlp.core.lib import str_to_bool, translate, check_directory_exists, get_text_file_string, build_icon
from openlp.core.lib import str_to_bool, translate, check_directory_exists, get_text_file_string, build_icon, \
image_to_byte
class TestLib(TestCase):
@ -217,7 +218,6 @@ class TestLib(TestCase):
Test the build_icon() function with a resource URI
"""
with patch(u'openlp.core.lib.QtGui') as MockedQtGui, \
patch(u'openlp.core.lib.QtGui.QIcon') as MockedQIcon, \
patch(u'openlp.core.lib.QtGui.QPixmap') as MockedQPixmap:
# GIVEN: A mocked QIcon and a mocked QPixmap
MockedQtGui.QIcon = MagicMock
@ -230,7 +230,32 @@ class TestLib(TestCase):
result = build_icon(resource_uri)
# THEN: The result should be our mocked QIcon
MockedQtGui.QIcon.assert_called_with(MockedQIcon)
MockedQPixmap.assert_called_with(resource_uri)
MockedQtGui.QIcon.addPixmap.assert_called_with(MockedQIcon, 'mocked_pixmap', 1, 2)
# 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.
assert isinstance(result, MagicMock), u'The result should be a MagicMock, because we mocked it out'
def image_to_byte_test(self):
"""
Test the image_to_byte() function
"""
with patch(u'openlp.core.lib.QtCore') as MockedQtCore:
# GIVEN: A set of mocked-out Qt classes
mocked_byte_array = MagicMock()
MockedQtCore.QByteArray.return_value = mocked_byte_array
mocked_byte_array.toBase64.return_value = u'base64mock'
mocked_buffer = MagicMock()
MockedQtCore.QBuffer.return_value = mocked_buffer
MockedQtCore.QIODevice.WriteOnly = u'writeonly'
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)
mocked_buffer.open.assert_called_with(u'writeonly')
mocked_image.save.assert_called_with(mocked_buffer, "PNG")
mocked_byte_array.toBase64.assert_called_with()
assert result == u'base64mock', u'The result should be the return value of the mocked out base64 method'

View File

@ -16,7 +16,7 @@ VERSE = u'The Lord said to {r}Noah{/r}: \n'\
'r{/pk}{o}e{/o}{pp}n{/pp} of the Lord\n'
FOOTER = [u'Arky Arky (Unknown)', u'Public Domain', u'CCLI 123456']
TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'resources'))
TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
class TestServiceItem(TestCase):
@ -160,4 +160,4 @@ class TestServiceItem(TestCase):
service_item.validate_item([u'png'])
# THEN the service item should not be valid
assert service_item.is_valid is False, u'The service item is not valid'
assert service_item.is_valid is False, u'The service item is not valid'

View File

@ -1,5 +1,5 @@
"""
Package to test the openlp.core.ui package.
Package to test the openlp.core.ui package.
"""
import sys
@ -45,4 +45,4 @@ class TestStartTimeDialog(TestCase):
mocked_serviceitem.end_time = 3701
self.form.item = mocked_serviceitem
#self.form.exec_()
#self.form.exec_()

View File

Before

Width:  |  Height:  |  Size: 213 KiB

After

Width:  |  Height:  |  Size: 213 KiB