summaryrefslogtreecommitdiffstats
path: root/openbb_platform/core/tests/app/static/test_container.py
blob: f27688699f8e21854a0c0684457b13e0a8ef5799 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Test the container.py file."""

from typing import Optional
from unittest.mock import patch

import pytest
from openbb_core.app.command_runner import CommandRunner
from openbb_core.app.model.abstract.error import OpenBBError
from openbb_core.app.model.defaults import Defaults
from openbb_core.app.model.user_settings import UserSettings
from openbb_core.app.static.container import Container
from pydantic import BaseModel, SecretStr

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


@pytest.fixture(scope="module")
def container():
    """Set up test container class."""

    class MockCredentials(BaseModel):
        provider_1_api_key: Optional[SecretStr] = None
        provider_2_api_key: Optional[SecretStr] = "test_key"

    MockCredentials.origins = {
        "provider_1": ["provider_1_api_key"],
        "provider_2": ["provider_2_api_key"],
        "provider_3": [],
    }

    mock_user_settings = UserSettings()
    mock_user_settings.credentials = MockCredentials()
    mock_user_settings.defaults = Defaults(
        commands={
            "/test/command": {"provider": "provider_1"},
            "test.first_wins.command": {"provider": ["provider_1", "provider_2"]},
            "test.not_available.command": {"provider": ["x", "y", "z"]},
        }
    )
    return Container(CommandRunner(user_settings=mock_user_settings))


def test_container_init(container):
    """Test container init."""
    assert container


@patch("openbb_core.app.command_runner.CommandRunner.sync_run")
def test_container__run(mock_sync_run, container):
    """Test container _run method."""
    container._run()
    mock_sync_run.assert_called_once()


def test_container__check_credentials(container):
    """Test container _check_credentials method."""
    assert container._check_credentials("provider_1") is False
    assert container._check_credentials("provider_2") is True
    assert container._check_credentials("provider_3") is True


@pytest.mark.parametrize(
    "choice, command, default_priority, expected, error_msg",
    [
        # Provider set in args
        ("fmp", ..., ..., "fmp", None),
        # Provider not set in args or config, fallback to provider without keys
        (
            None,
            "test.no_config.command",
            ("provider_1", "provider_3"),
            "provider_3",
            None,
        ),
        # Provider priority set in config, first with key wins
        (
            None,
            "test.first_wins.command",
            ("provider_1", "provider_2", "provider_3"),
            "provider_2",
            None,
        ),
        # Provider priority set in config, with providers not available for the command
        (
            None,
            "test.not_available.command",
            ("provider_1", "provider_2"),
            OpenBBError,
            "Fallback failed, please specify the provider. Tried: x, y, z.",
        ),
    ],
)
def test_container__get_provider(
    choice, command, default_priority, expected, error_msg, container
):
    """Test container _get_provider method."""
    if expected is OpenBBError:
        with pytest.raises(expected, match=error_msg):
            container._get_provider(choice, command, default_priority)
    else:
        result = container._get_provider(choice, command, default_priority)
        assert result == expected