57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
# -*- coding: utf-8 -*-
|
|
# type: ignore
|
|
from __future__ import annotations
|
|
|
|
from unittest import mock
|
|
|
|
from bthlabs_jsonrpc_core import decorators
|
|
from bthlabs_jsonrpc_core.registry import MethodRegistry
|
|
|
|
|
|
@mock.patch.object(decorators.MethodRegistry, 'shared_registry')
|
|
def test_default_namespace(mock_shared_registry: mock.Mock,
|
|
fake_method_registry: mock.Mock,
|
|
fake_handler: mock.Mock):
|
|
# Given
|
|
mock_shared_registry.return_value = fake_method_registry
|
|
|
|
decorator = decorators.register_method('test')
|
|
|
|
# When
|
|
result = decorator(fake_handler)
|
|
|
|
# Then
|
|
assert result is fake_handler
|
|
assert result.jsonrpc_method == 'test'
|
|
assert result.jsonrpc_namespace == MethodRegistry.DEFAULT_NAMESPACE
|
|
|
|
assert mock_shared_registry.called is True
|
|
|
|
fake_method_registry.register_method.assert_called_with(
|
|
MethodRegistry.DEFAULT_NAMESPACE, 'test', fake_handler,
|
|
)
|
|
|
|
|
|
@mock.patch.object(decorators.MethodRegistry, 'shared_registry')
|
|
def test_custom_namespace(mock_shared_registry: mock.Mock,
|
|
fake_method_registry: mock.Mock,
|
|
fake_handler: mock.Mock):
|
|
# Given
|
|
mock_shared_registry.return_value = fake_method_registry
|
|
|
|
decorator = decorators.register_method('test', namespace='testing')
|
|
|
|
# When
|
|
result = decorator(fake_handler)
|
|
|
|
# Then
|
|
assert result is fake_handler
|
|
assert result.jsonrpc_method == 'test'
|
|
assert result.jsonrpc_namespace == 'testing'
|
|
|
|
assert mock_shared_registry.called is True
|
|
|
|
fake_method_registry.register_method.assert_called_with(
|
|
'testing', 'test', fake_handler,
|
|
)
|