Add tests for new registry function

This commit is contained in:
Tim Bentley 2016-06-05 21:36:42 +01:00
parent 05be4e606c
commit fad8b35660
3 changed files with 49 additions and 2 deletions

View File

@ -24,6 +24,7 @@ Provide Registry Services
"""
import logging
import sys
import re
from openlp.core.common import trace_error_handler
@ -56,6 +57,7 @@ class Registry(object):
registry.service_list = {}
registry.functions_list = {}
registry.working_flags = {}
registry.remote_apis = {}
# Allow the tests to remove Registry entries but not the live system
registry.running_under_test = 'nose' in sys.argv[0]
registry.initialising = True
@ -176,3 +178,29 @@ class Registry(object):
"""
if key in self.working_flags:
del self.working_flags[key]
def remote_api(self, path, function, secure=False):
"""
Sets a working_flag based on the key passed in.
:param path: The working_flag to be created this is usually a major class like "renderer" or "main_window" .
:param function: The data to be saved.
:param secure: The data to be saved.
"""
self.remote_apis[path] = {'function': function, 'secure': secure}
def remote_execute(self, url_path):
"""
Execute all the handlers associated with the event and return an array of results.
:param url_path: The url path to be found
"""
for url, funcs in self.remote_apis.items():
a = url
match = re.match(url, url_path)
if match:
log.debug('Route "{route}" matched "{path}"'.format(route=url_path, path=url_path))
args = []
for param in match.groups():
args.append(param)
return funcs, args

View File

@ -25,8 +25,6 @@ The :mod:`http` module contains the API web server. This is a lightweight web se
with OpenLP. It uses JSON to communicate with the remotes.
"""
import ssl
import socket
import logging
import time

View File

@ -149,3 +149,24 @@ class TestRegistry(TestCase):
def dummy_function_2(self):
return "function_2"
def test_registry_remote_apis(self):
"""
Test the registry working flags creation and its usage
"""
# GIVEN: A new registry
Registry.create()
# WHEN: I register a API to function
func = MagicMock()
func1 = MagicMock()
Registry().remote_api(r'^/api/poll$', func)
Registry().remote_api(r'^/main/poll$', func1, True)
# THEN: When I look to a function
call_structure, args = Registry().remote_execute('/api/poll')
self.assertEqual(call_structure['function'], func, 'Calling function should match')
self.assertEqual(call_structure['secure'], False, 'Calling function should not require security')
call_structure, args = Registry().remote_execute('/main/poll')
self.assertEqual(call_structure['function'], func1, 'Calling function should match')
self.assertEqual(call_structure['secure'], True, 'Calling function should not require security')