110 lines
2.7 KiB
Python
110 lines
2.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
from unittest import mock
|
|
|
|
from bthlabs_jsonrpc_core import registry
|
|
|
|
|
|
def test_init():
|
|
# When
|
|
result = registry.MethodRegistry()
|
|
|
|
# Then
|
|
assert result.registry == {'jsonrpc': {}}
|
|
|
|
|
|
@mock.patch.object(registry.MethodRegistry, '__init__')
|
|
def test_shared_registry(mock_init: mock.Mock):
|
|
# Given
|
|
mock_init.return_value = None
|
|
|
|
# When
|
|
result = registry.MethodRegistry.shared_registry()
|
|
|
|
# Then
|
|
assert isinstance(result, registry.MethodRegistry)
|
|
|
|
assert registry.MethodRegistry.INSTANCE is result
|
|
|
|
assert mock_init.called is True
|
|
|
|
# After
|
|
registry.MethodRegistry.INSTANCE = None
|
|
|
|
|
|
@mock.patch.object(registry.MethodRegistry, '__init__')
|
|
def test_shared_registry_with_instance(mock_init: mock.Mock,
|
|
fake_method_registry: mock.Mock):
|
|
# Given
|
|
mock_init.return_value = None
|
|
|
|
registry.MethodRegistry.INSTANCE = fake_method_registry
|
|
|
|
# When
|
|
result = registry.MethodRegistry.shared_registry()
|
|
|
|
# Then
|
|
assert result is fake_method_registry
|
|
|
|
assert registry.MethodRegistry.INSTANCE is fake_method_registry
|
|
|
|
assert mock_init.called is False
|
|
|
|
# After
|
|
registry.MethodRegistry.INSTANCE = None
|
|
|
|
|
|
def test_register_method(fake_handler: mock.Mock):
|
|
# Given
|
|
the_registry = registry.MethodRegistry()
|
|
|
|
# When'
|
|
the_registry.register_method('testing', 'test', fake_handler)
|
|
|
|
# Then
|
|
expected_namespace = {'test': fake_handler}
|
|
assert the_registry.registry['testing'] == expected_namespace
|
|
|
|
|
|
def test_register_method_existing_namespace(fake_handler: mock.Mock):
|
|
# Given
|
|
spam_handler = mock.Mock()
|
|
|
|
the_registry = registry.MethodRegistry()
|
|
the_registry.registry['jsonrpc']['spam'] = spam_handler
|
|
|
|
# When'
|
|
the_registry.register_method('jsonrpc', 'test', fake_handler)
|
|
|
|
# Then
|
|
expected_namespace = {'spam': spam_handler, 'test': fake_handler}
|
|
assert the_registry.registry['jsonrpc'] == expected_namespace
|
|
|
|
|
|
def test_get_methods():
|
|
# Given
|
|
the_registry = registry.MethodRegistry()
|
|
the_registry.registry['jsonrpc']['spam'] = mock.Mock()
|
|
the_registry.registry['jsonrpc']['eggs'] = mock.Mock()
|
|
|
|
# When'
|
|
result = the_registry.get_methods('jsonrpc')
|
|
|
|
# Then
|
|
expected_methods = {'spam', 'eggs'}
|
|
assert set(result) == expected_methods
|
|
|
|
|
|
def test_get_handler(fake_handler: mock.Mock):
|
|
# Given
|
|
spam_handler = mock.Mock()
|
|
|
|
the_registry = registry.MethodRegistry()
|
|
the_registry.registry['jsonrpc']['spam'] = spam_handler
|
|
the_registry.registry['jsonrpc']['eggs'] = fake_handler
|
|
|
|
# When'
|
|
result = the_registry.get_handler('jsonrpc', 'eggs')
|
|
|
|
# Then
|
|
assert result is fake_handler
|