Add test utils and move a function to there.

This commit is contained in:
Patrick Zimmermann 2013-04-27 11:54:57 +02:00
parent edb0eab31d
commit 84e9f6f1b1
5 changed files with 70 additions and 33 deletions

0
tests/__init__.py Normal file
View File

View File

@ -2,11 +2,12 @@
Package to test the openlp.core.lib package.
"""
import os
import cPickle
from unittest import TestCase
from mock import MagicMock, patch
from openlp.core.lib import ItemCapabilities, ServiceItem, Registry
from tests.utils.osdinteraction import read_service_from_file
from tests.utils.constants import TEST_RESOURCES_PATH
VERSE = u'The Lord said to {r}Noah{/r}: \n'\
@ -18,8 +19,6 @@ 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']
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
class TestServiceItem(TestCase):
@ -78,7 +77,7 @@ class TestServiceItem(TestCase):
service_item.name = u'test'
# WHEN: adding image to a service item
test_image = os.path.join(TEST_PATH, u'church.jpg')
test_image = os.path.join(TEST_RESOURCES_PATH, u'church.jpg')
service_item.add_from_image(test_image, u'Image Title')
# THEN: We should get back a valid service item
@ -133,8 +132,8 @@ class TestServiceItem(TestCase):
service_item.name = u'test'
# WHEN: adding image to a service item
test_file = os.path.join(TEST_PATH, u'church.jpg')
service_item.add_from_command(TEST_PATH, u'church.jpg', test_file)
test_file = os.path.join(TEST_RESOURCES_PATH, u'church.jpg')
service_item.add_from_command(TEST_RESOURCES_PATH, u'church.jpg', test_file)
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
@ -151,7 +150,7 @@ class TestServiceItem(TestCase):
assert len(service) == 2, u'The saved service should have two parts'
assert service[u'header'][u'name'] == u'test', u'A test plugin should be returned'
assert service[u'data'][0][u'title'] == u'church.jpg', u'The first title name should be "church,jpg"'
assert service[u'data'][0][u'path'] == TEST_PATH, u'The path should match the input path'
assert service[u'data'][0][u'path'] == TEST_RESOURCES_PATH, u'The path should match the input path'
assert service[u'data'][0][u'image'] == test_file, u'The image should match the full path to image'
# WHEN validating a service item
@ -170,13 +169,12 @@ class TestServiceItem(TestCase):
"""
Test the Service Item - adding a custom slide from a saved service
"""
# GIVEN: A new service item and a mocked add icon function
# GIVEN: A new service item
service_item = ServiceItem(None)
service_item.add_icon = MagicMock()
# WHEN: adding a custom from a saved Service
line = self.convert_file_service_item(u'serviceitem_custom_1.osd')
service_item.set_from_service(line)
service = read_service_from_file(u'serviceitem_custom_1.osd')
service_item.set_from_service(service[0])
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
@ -195,18 +193,17 @@ class TestServiceItem(TestCase):
"""
Test the Service Item - adding an image from a saved service
"""
# GIVEN: A new service item and a mocked add icon function
# GIVEN: A new service item
image_name = u'image_1.jpg'
test_file = os.path.join(TEST_PATH, image_name)
test_file = os.path.join(TEST_RESOURCES_PATH, image_name)
frame_array = {u'path': test_file, u'title': image_name}
service_item = ServiceItem(None)
service_item.add_icon = MagicMock()
# WHEN: adding an image from a saved Service and mocked exists
line = self.convert_file_service_item(u'serviceitem_image_1.osd')
service = read_service_from_file(u'serviceitem_image_1.osd')
with patch('os.path.exists'):
service_item.set_from_service(line, TEST_PATH)
service_item.set_from_service(service[0], TEST_RESOURCES_PATH)
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
@ -230,7 +227,7 @@ class TestServiceItem(TestCase):
"""
Test the Service Item - adding an image from a saved local service
"""
# GIVEN: A new service item and a mocked add icon function
# GIVEN: A new service item
image_name1 = u'image_1.jpg'
image_name2 = u'image_2.jpg'
test_file1 = os.path.join(u'/home/openlp', image_name1)
@ -239,12 +236,11 @@ class TestServiceItem(TestCase):
frame_array2 = {u'path': test_file2, u'title': image_name2}
service_item = ServiceItem(None)
service_item.add_icon = MagicMock()
# WHEN: adding an image from a saved Service and mocked exists
line = self.convert_file_service_item(u'serviceitem_image_2.osd')
service = read_service_from_file(u'serviceitem_image_2.osd')
with patch('os.path.exists'):
service_item.set_from_service(line)
service_item.set_from_service(service[0])
# THEN: We should get back a valid service item
assert service_item.is_valid is True, u'The new service item should be valid'
@ -267,16 +263,3 @@ class TestServiceItem(TestCase):
u'This service item should be able to be run in a can be made to Loop'
assert service_item.is_capable(ItemCapabilities.CanAppend) is True, \
u'This service item should be able to have new items added to it'
def convert_file_service_item(self, name):
service_file = os.path.join(TEST_PATH, name)
try:
open_file = open(service_file, u'r')
items = cPickle.load(open_file)
first_line = items[0]
except IOError:
first_line = u''
finally:
open_file.close()
return first_line

0
tests/utils/__init__.py Normal file
View File

5
tests/utils/constants.py Normal file
View File

@ -0,0 +1,5 @@
import os
OPENLP_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..'))
TEST_RESOURCES_PATH = os.path.join(OPENLP_PATH, u'tests', u'resources')

View File

@ -0,0 +1,49 @@
# -*- 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 #
###############################################################################
"""
The :mod:`osdinteraction` provides miscellaneous functions for interacting with
OSD files.
"""
import os
import cPickle
from tests.utils.constants import TEST_RESOURCES_PATH
def read_service_from_file(file_name):
"""
Reads an OSD file and returns the first service item found therein.
@param file_name: File name of an OSD file residing in the tests/resources folder.
@return: The service contained in the file.
"""
service_file = os.path.join(TEST_RESOURCES_PATH, file_name)
with open(service_file, u'r') as open_file:
service = cPickle.load(open_file)
return service