openlp/tests/functional/openlp_core_lib/test_registry.py

113 lines
5.4 KiB
Python
Raw Normal View History

# -*- 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 #
###############################################################################
2013-01-22 21:59:45 +00:00
"""
Package to test the openlp.core.lib package.
2013-01-22 21:59:45 +00:00
"""
import os
from unittest import TestCase
2013-01-23 21:51:45 +00:00
from openlp.core.lib import Registry
from tests.functional import MagicMock
2013-01-22 21:59:45 +00:00
2013-08-31 18:17:38 +00:00
TEST_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', 'resources'))
2013-01-22 21:59:45 +00:00
2013-01-24 20:04:18 +00:00
class TestRegistry(TestCase):
2013-01-22 21:59:45 +00:00
2013-02-04 19:25:12 +00:00
def registry_service_test(self):
2013-01-22 21:59:45 +00:00
"""
Test the registry creation and its usage
2013-01-22 21:59:45 +00:00
"""
# GIVEN: A new registry
2013-02-09 17:07:33 +00:00
Registry.create()
2013-01-22 21:59:45 +00:00
# WHEN: I add a component it should save it
2013-01-22 21:59:45 +00:00
mock_1 = MagicMock()
2013-08-31 18:17:38 +00:00
Registry().register('test1', mock_1)
2013-01-22 21:59:45 +00:00
# THEN: we should be able retrieve the saved component
2013-08-31 18:17:38 +00:00
assert Registry().get('test1') == mock_1, 'The saved service can be retrieved and matches'
2013-01-22 21:59:45 +00:00
# WHEN: I add a component for the second time I am mad.
# THEN and I will get an exception
2013-01-24 20:04:18 +00:00
with self.assertRaises(KeyError) as context:
2013-08-31 18:17:38 +00:00
Registry().register('test1', mock_1)
self.assertEqual(context.exception.args[0], 'Duplicate service exception test1',
2013-10-02 21:37:00 +00:00
'KeyError exception should have been thrown for duplicate service')
2013-01-24 06:00:51 +00:00
# WHEN I try to get back a non existent component
2013-01-24 06:00:51 +00:00
# THEN I will get an exception
2013-01-24 20:04:18 +00:00
with self.assertRaises(KeyError) as context:
2013-08-31 18:17:38 +00:00
temp = Registry().get('test2')
self.assertEqual(context.exception.args[0], 'Service test2 not found in list',
2013-10-02 21:37:00 +00:00
'KeyError exception should have been thrown for missing service')
2013-01-27 20:59:02 +00:00
# WHEN I try to replace a component I should be allowed (testing only)
2013-08-31 18:17:38 +00:00
Registry().remove('test1')
2013-01-27 20:59:02 +00:00
# THEN I will get an exception
with self.assertRaises(KeyError) as context:
2013-08-31 18:17:38 +00:00
temp = Registry().get('test1')
self.assertEqual(context.exception.args[0], 'Service test1 not found in list',
2013-10-02 21:37:00 +00:00
'KeyError exception should have been thrown for deleted service')
2013-02-04 19:25:12 +00:00
def registry_function_test(self):
"""
2013-02-06 19:25:16 +00:00
Test the registry function creation and their usages
2013-02-04 19:25:12 +00:00
"""
# GIVEN: An existing registry register a function
Registry.create()
2013-08-31 18:17:38 +00:00
Registry().register_function('test1', self.dummy_function_1)
2013-02-04 19:25:12 +00:00
# WHEN: I execute the function
2013-08-31 18:17:38 +00:00
return_value = Registry().execute('test1')
2013-02-04 19:25:12 +00:00
# THEN: I expect then function to have been called and a return given
2013-08-31 18:17:38 +00:00
self.assertEqual(return_value[0], 'function_1', 'A return value is provided and matches')
2013-02-04 19:25:12 +00:00
# WHEN: I execute the a function with the same reference and execute the function
2013-08-31 18:17:38 +00:00
Registry().register_function('test1', self.dummy_function_1)
return_value = Registry().execute('test1')
2013-02-04 19:25:12 +00:00
# THEN: I expect then function to have been called and a return given
2013-08-31 18:17:38 +00:00
self.assertEqual(return_value, ['function_1', 'function_1'], 'A return value list is provided and matches')
2013-02-04 19:25:12 +00:00
# WHEN: I execute the a 2nd function with the different reference and execute the function
2013-08-31 18:17:38 +00:00
Registry().register_function('test2', self.dummy_function_2)
return_value = Registry().execute('test2')
2013-02-04 19:25:12 +00:00
# THEN: I expect then function to have been called and a return given
2013-08-31 18:17:38 +00:00
self.assertEqual(return_value[0], 'function_2', 'A return value is provided and matches')
2013-02-04 19:25:12 +00:00
def dummy_function_1(self):
return "function_1"
def dummy_function_2(self):
2013-07-17 14:38:14 +00:00
return "function_2"