2013-01-22 21:59:45 +00:00
|
|
|
"""
|
|
|
|
Package to test the openlp.core.lib package.
|
|
|
|
"""
|
|
|
|
import os
|
|
|
|
|
|
|
|
from unittest import TestCase
|
|
|
|
from mock import MagicMock
|
2013-01-23 21:51:45 +00:00
|
|
|
from openlp.core.lib import Registry
|
2013-01-22 21:59:45 +00:00
|
|
|
|
|
|
|
TESTPATH = os.path.abspath(os.path.join(os.path.dirname(__file__), u'..', u'..', u'resources'))
|
|
|
|
|
2013-01-24 20:04:18 +00:00
|
|
|
class TestRegistry(TestCase):
|
2013-01-22 21:59:45 +00:00
|
|
|
|
|
|
|
def registry_basic_test(self):
|
|
|
|
"""
|
2013-01-25 20:50:18 +00:00
|
|
|
Test the registry creation and its usage
|
2013-01-22 21:59:45 +00:00
|
|
|
"""
|
|
|
|
# GIVEN: A new registry
|
|
|
|
registry = Registry.create()
|
|
|
|
|
2013-01-25 20:50:18 +00:00
|
|
|
# WHEN: I add a component it should save it
|
2013-01-22 21:59:45 +00:00
|
|
|
mock_1 = MagicMock()
|
|
|
|
Registry().register(u'test1', mock_1)
|
|
|
|
|
2013-01-25 20:50:18 +00:00
|
|
|
# THEN: we should be able retrieve the saved component
|
2013-01-24 06:00:51 +00:00
|
|
|
assert Registry().get(u'test1') == mock_1, u'The saved service can be retrieved and matches'
|
2013-01-22 21:59:45 +00:00
|
|
|
|
2013-01-25 20:50:18 +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-01-24 06:00:51 +00:00
|
|
|
Registry().register(u'test1', mock_1)
|
2013-01-25 20:50:18 +00:00
|
|
|
self.assertEqual(context.exception[0], u'Duplicate service exception test1',
|
2013-01-30 18:18:28 +00:00
|
|
|
u'KeyError exception should have been thrown for duplicate service')
|
2013-01-24 06:00:51 +00:00
|
|
|
|
2013-01-25 20:50:18 +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:
|
|
|
|
temp = Registry().get(u'test2')
|
2013-01-25 20:50:18 +00:00
|
|
|
self.assertEqual(context.exception[0], u'Service test2 not found in list',
|
2013-01-30 18:18:28 +00:00
|
|
|
u'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)
|
|
|
|
Registry().remove(u'test1')
|
|
|
|
# THEN I will get an exception
|
|
|
|
with self.assertRaises(KeyError) as context:
|
|
|
|
temp = Registry().get(u'test1')
|
|
|
|
self.assertEqual(context.exception[0], u'Service test1 not found in list',
|
2013-01-30 18:18:28 +00:00
|
|
|
u'KeyError exception should have been thrown for deleted service')
|