File size: 2,809 Bytes
b93364a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Test the base platform controller."""

from unittest.mock import MagicMock, Mock, patch

import pytest
from openbb_cli.controllers.base_platform_controller import (
    PlatformController,
    Session,
)

# pylint: disable=protected-access, unused-variable, redefined-outer-name


@pytest.fixture
def platform_controller():
    """Return a platform controller."""
    session = Session()  # noqa: F841
    translators = {"test_command": MagicMock(), "test_menu": MagicMock()}  # noqa: F841
    translators["test_command"]._parser = Mock(
        _actions=[Mock(dest="data", choices=[], type=str, nargs=None)]
    )
    translators["test_command"].execute_func = Mock(return_value=Mock())
    translators["test_menu"]._parser = Mock(
        _actions=[Mock(dest="data", choices=[], type=str, nargs=None)]
    )
    translators["test_menu"].execute_func = Mock(return_value=Mock())

    controller = PlatformController(
        name="test", parent_path=["platform"], translators=translators
    )
    return controller


@pytest.mark.integration
def test_platform_controller_initialization(platform_controller):
    """Test the initialization of the platform controller."""
    expected_path = "/platform/test/"
    assert (
        expected_path == platform_controller.PATH
    ), "Controller path was not set correctly"


@pytest.mark.integration
def test_command_generation(platform_controller):
    """Test the generation of commands."""
    command_name = "test_command"
    mock_execute_func = Mock(return_value=(Mock(), None))
    platform_controller.translators[command_name].execute_func = mock_execute_func

    platform_controller._generate_command_call(
        name=command_name, translator=platform_controller.translators[command_name]
    )
    command_method_name = f"call_{command_name}"
    assert hasattr(
        platform_controller, command_method_name
    ), "Command method was not created"


@patch(
    "openbb_cli.controllers.base_platform_controller.PlatformController._link_obbject_to_data_processing_commands"
)
@patch(
    "openbb_cli.controllers.base_platform_controller.PlatformController._generate_commands"
)
@patch(
    "openbb_cli.controllers.base_platform_controller.PlatformController._generate_sub_controllers"
)
@pytest.mark.integration
def test_platform_controller_calls(
    mock_sub_controllers, mock_commands, mock_link_commands
):
    """Test the calls of the platform controller."""
    translators = {"test_command": Mock()}
    translators["test_command"].parser = Mock()
    translators["test_command"].execute_func = Mock()
    _ = PlatformController(
        name="test", parent_path=["platform"], translators=translators
    )
    mock_sub_controllers.assert_called_once()
    mock_commands.assert_called_once()
    mock_link_commands.assert_called_once()