2013-12-13 18:42:26 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: autoindent shiftwidth=4 expandtab textwidth=120 tabstop=4 softtabstop=4
|
|
|
|
|
|
|
|
###############################################################################
|
|
|
|
# OpenLP - Open Source Lyrics Projection #
|
|
|
|
# --------------------------------------------------------------------------- #
|
2017-12-29 09:15:48 +00:00
|
|
|
# Copyright (c) 2008-2018 OpenLP Developers #
|
2013-12-13 18:42:26 +00:00
|
|
|
# --------------------------------------------------------------------------- #
|
|
|
|
# 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 #
|
|
|
|
###############################################################################
|
|
|
|
"""
|
|
|
|
Package to test the openlp.core.lib package.
|
|
|
|
"""
|
|
|
|
from unittest import TestCase
|
2017-10-23 22:09:57 +00:00
|
|
|
from unittest.mock import MagicMock
|
2013-12-13 18:42:26 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
from openlp.core.common.registry import Registry, RegistryBase
|
2013-12-13 18:42:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestRegistry(TestCase):
|
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_registry_service(self):
|
2013-12-13 18:42:26 +00:00
|
|
|
"""
|
|
|
|
Test the registry creation and its usage
|
|
|
|
"""
|
|
|
|
# GIVEN: A new registry
|
|
|
|
Registry.create()
|
|
|
|
|
|
|
|
# WHEN: I add a component it should save it
|
|
|
|
mock_1 = MagicMock()
|
|
|
|
Registry().register('test1', mock_1)
|
|
|
|
|
|
|
|
# THEN: we should be able retrieve the saved component
|
|
|
|
assert Registry().get('test1') == mock_1, 'The saved service can be retrieved and matches'
|
|
|
|
|
|
|
|
# WHEN: I add a component for the second time I am mad.
|
|
|
|
# THEN and I will get an exception
|
|
|
|
with self.assertRaises(KeyError) as context:
|
|
|
|
Registry().register('test1', mock_1)
|
2017-12-09 16:29:58 +00:00
|
|
|
assert context.exception.args[0] == 'Duplicate service exception test1', \
|
|
|
|
'KeyError exception should have been thrown for duplicate service'
|
2013-12-13 18:42:26 +00:00
|
|
|
|
|
|
|
# WHEN I try to get back a non existent component
|
|
|
|
# THEN I will get an exception
|
2014-01-01 14:59:57 +00:00
|
|
|
temp = Registry().get('test2')
|
2017-12-15 16:30:10 +00:00
|
|
|
assert temp is None, 'None should have been returned for missing service'
|
2013-12-13 18:42:26 +00:00
|
|
|
|
2016-05-21 05:24:05 +00:00
|
|
|
# WHEN I try to replace a component I should be allowed
|
2013-12-13 18:42:26 +00:00
|
|
|
Registry().remove('test1')
|
|
|
|
# THEN I will get an exception
|
2014-01-01 14:59:57 +00:00
|
|
|
temp = Registry().get('test1')
|
2017-12-15 16:30:10 +00:00
|
|
|
assert temp is None, 'None should have been returned for deleted service'
|
2013-12-13 18:42:26 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_registry_function(self):
|
2013-12-13 18:42:26 +00:00
|
|
|
"""
|
|
|
|
Test the registry function creation and their usages
|
|
|
|
"""
|
|
|
|
# GIVEN: An existing registry register a function
|
|
|
|
Registry.create()
|
|
|
|
Registry().register_function('test1', self.dummy_function_1)
|
|
|
|
|
|
|
|
# WHEN: I execute the function
|
|
|
|
return_value = Registry().execute('test1')
|
|
|
|
|
|
|
|
# THEN: I expect then function to have been called and a return given
|
2017-12-09 16:29:58 +00:00
|
|
|
assert return_value[0] == 'function_1', 'A return value is provided and matches'
|
2013-12-13 18:42:26 +00:00
|
|
|
|
|
|
|
# WHEN: I execute the a function with the same reference and execute the function
|
|
|
|
Registry().register_function('test1', self.dummy_function_1)
|
|
|
|
return_value = Registry().execute('test1')
|
|
|
|
|
|
|
|
# THEN: I expect then function to have been called and a return given
|
2017-12-09 16:29:58 +00:00
|
|
|
assert return_value == ['function_1', 'function_1'], 'A return value list is provided and matches'
|
2013-12-13 18:42:26 +00:00
|
|
|
|
|
|
|
# WHEN: I execute the a 2nd function with the different reference and execute the function
|
|
|
|
Registry().register_function('test2', self.dummy_function_2)
|
|
|
|
return_value = Registry().execute('test2')
|
|
|
|
|
|
|
|
# THEN: I expect then function to have been called and a return given
|
2017-12-09 16:29:58 +00:00
|
|
|
assert return_value[0] == 'function_2', 'A return value is provided and matches'
|
2013-12-13 18:42:26 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_registry_working_flags(self):
|
2016-05-21 05:24:05 +00:00
|
|
|
"""
|
|
|
|
Test the registry working flags creation and its usage
|
|
|
|
"""
|
|
|
|
# GIVEN: A new registry
|
|
|
|
Registry.create()
|
|
|
|
|
|
|
|
# WHEN: I add a working flag it should save it
|
|
|
|
my_data = 'Lamas'
|
2016-05-24 16:28:11 +00:00
|
|
|
my_data2 = 'More Lamas'
|
2016-05-21 05:24:05 +00:00
|
|
|
Registry().set_flag('test1', my_data)
|
|
|
|
|
|
|
|
# THEN: we should be able retrieve the saved component
|
2016-05-24 16:28:11 +00:00
|
|
|
temp = Registry().get_flag('test1')
|
2017-12-09 16:29:58 +00:00
|
|
|
assert temp == my_data, 'The value should have been saved'
|
2016-05-24 16:28:11 +00:00
|
|
|
|
|
|
|
# WHEN: I add a component for the second time I am not mad.
|
|
|
|
# THEN and I will not get an exception
|
|
|
|
Registry().set_flag('test1', my_data2)
|
|
|
|
temp = Registry().get_flag('test1')
|
2017-12-09 16:29:58 +00:00
|
|
|
assert temp == my_data2, 'The value should have been updated'
|
2016-05-21 05:24:05 +00:00
|
|
|
|
|
|
|
# WHEN I try to get back a non existent Working Flag
|
|
|
|
# THEN I will get an exception
|
|
|
|
with self.assertRaises(KeyError) as context1:
|
|
|
|
temp = Registry().get_flag('test2')
|
2017-12-09 16:29:58 +00:00
|
|
|
assert context1.exception.args[0] == 'Working Flag test2 not found in list', \
|
|
|
|
'KeyError exception should have been thrown for missing working flag'
|
2016-05-21 05:24:05 +00:00
|
|
|
|
|
|
|
# WHEN I try to replace a working flag I should be allowed
|
|
|
|
Registry().remove_flag('test1')
|
|
|
|
# THEN I will get an exception
|
|
|
|
with self.assertRaises(KeyError) as context:
|
|
|
|
temp = Registry().get_flag('test1')
|
2017-12-09 16:29:58 +00:00
|
|
|
assert context.exception.args[0] == 'Working Flag test1 not found in list', \
|
|
|
|
'KeyError exception should have been thrown for duplicate working flag'
|
2016-05-21 05:24:05 +00:00
|
|
|
|
2016-05-31 21:40:13 +00:00
|
|
|
def test_remove_function(self):
|
2014-03-31 17:01:09 +00:00
|
|
|
"""
|
|
|
|
Test the remove_function() method
|
|
|
|
"""
|
|
|
|
# GIVEN: An existing registry register a function
|
|
|
|
Registry.create()
|
|
|
|
Registry().register_function('test1', self.dummy_function_1)
|
|
|
|
|
|
|
|
# WHEN: Remove the function.
|
|
|
|
Registry().remove_function('test1', self.dummy_function_1)
|
|
|
|
|
|
|
|
# THEN: The method should not be available.
|
2017-12-15 16:30:10 +00:00
|
|
|
assert Registry().functions_list['test1'] == [], 'The function should not be in the dict anymore.'
|
2014-03-31 17:01:09 +00:00
|
|
|
|
2013-12-13 18:42:26 +00:00
|
|
|
def dummy_function_1(self):
|
|
|
|
return "function_1"
|
|
|
|
|
|
|
|
def dummy_function_2(self):
|
|
|
|
return "function_2"
|
2017-10-07 07:05:07 +00:00
|
|
|
|
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
class PlainStub(object):
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
2017-10-07 07:05:07 +00:00
|
|
|
|
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
class RegistryStub(RegistryBase):
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
2017-10-07 07:05:07 +00:00
|
|
|
|
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
class TestRegistryBase(TestCase):
|
2017-10-07 07:05:07 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
def test_registry_mixin_missing(self):
|
2017-10-07 07:05:07 +00:00
|
|
|
"""
|
2017-10-23 22:09:57 +00:00
|
|
|
Test the registry creation and its usage
|
2017-10-07 07:05:07 +00:00
|
|
|
"""
|
2017-10-23 22:09:57 +00:00
|
|
|
# GIVEN: A new registry
|
|
|
|
Registry.create()
|
2017-10-07 07:05:07 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
# WHEN: I create an instance of a class that doesn't inherit from RegistryMixin
|
|
|
|
PlainStub()
|
2017-10-07 07:05:07 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
# THEN: Nothing is registered with the registry
|
2017-12-09 16:29:58 +00:00
|
|
|
assert len(Registry().functions_list) == 0, 'The function should not be in the dict anymore.'
|
2017-10-07 07:05:07 +00:00
|
|
|
|
2017-10-23 22:09:57 +00:00
|
|
|
def test_registry_mixin_present(self):
|
2017-10-07 07:05:07 +00:00
|
|
|
"""
|
2017-10-23 22:09:57 +00:00
|
|
|
Test the registry creation and its usage
|
2017-10-07 07:05:07 +00:00
|
|
|
"""
|
2017-10-23 22:09:57 +00:00
|
|
|
# GIVEN: A new registry
|
|
|
|
Registry.create()
|
|
|
|
|
|
|
|
# WHEN: I create an instance of a class that inherits from RegistryMixin
|
|
|
|
RegistryStub()
|
|
|
|
|
|
|
|
# THEN: The bootstrap methods should be registered
|
2017-12-09 16:29:58 +00:00
|
|
|
assert len(Registry().functions_list) == 2, 'The bootstrap functions should be in the dict.'
|