mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-05-06 22:12:12 +08:00
test: add coverage for CLI utility functions (#7422)
Co-authored-by: Danglewood <85772166+deeleeramone@users.noreply.github.com>
This commit is contained in:
@@ -6,13 +6,16 @@ from unittest.mock import MagicMock, patch
|
||||
|
||||
import pytest
|
||||
from openbb_cli.controllers.utils import (
|
||||
check_file_type_saved,
|
||||
check_non_negative,
|
||||
check_positive,
|
||||
get_flair_and_username,
|
||||
get_user_agent,
|
||||
parse_and_split_input,
|
||||
parse_unknown_args_to_dict,
|
||||
print_goodbye,
|
||||
remove_file,
|
||||
validate_register_key,
|
||||
welcome_message,
|
||||
)
|
||||
|
||||
@@ -138,3 +141,100 @@ def test_get_user_agent():
|
||||
"""Test getting the user agent."""
|
||||
result = get_user_agent()
|
||||
assert result.startswith("Mozilla/5.0")
|
||||
|
||||
|
||||
# --- Tests for parse_unknown_args_to_dict ---
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"args, expected",
|
||||
[
|
||||
(["--key", "value"], {"key": "value"}),
|
||||
(["--a", "1", "--b", "hello"], {"a": 1, "b": "hello"}),
|
||||
(["--flag"], {}),
|
||||
(None, {}),
|
||||
([], {}),
|
||||
],
|
||||
)
|
||||
def test_parse_unknown_args_to_dict_basic(args, expected, mock_session):
|
||||
"""Test basic key-value parsing and edge cases."""
|
||||
result = parse_unknown_args_to_dict(args)
|
||||
assert result == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"args, expected",
|
||||
[
|
||||
(["--num", "42"], {"num": 42}),
|
||||
(["--pi", "3.14"], {"pi": 3.14}),
|
||||
(["--items", "[1, 2, 3]"], {"items": [1, 2, 3]}),
|
||||
(["--mapping", "{'a': 1}"], {"mapping": {"a": 1}}),
|
||||
(["--bool", "True"], {"bool": True}),
|
||||
(["--plain", "not-a-literal"], {"plain": "not-a-literal"}),
|
||||
],
|
||||
)
|
||||
def test_parse_unknown_args_to_dict_literal_eval(args, expected, mock_session):
|
||||
"""Test that ast.literal_eval correctly parses typed values."""
|
||||
result = parse_unknown_args_to_dict(args)
|
||||
assert result == expected
|
||||
|
||||
|
||||
def test_parse_unknown_args_to_dict_missing_value(mock_session):
|
||||
"""Test that a trailing --flag with no value prints a warning."""
|
||||
result = parse_unknown_args_to_dict(["--orphan"])
|
||||
assert result == {}
|
||||
mock_session.console.print.assert_called_once()
|
||||
|
||||
|
||||
# --- Tests for validate_register_key ---
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"key, should_raise",
|
||||
[
|
||||
("my_api_key", False),
|
||||
("secret_123", False),
|
||||
("OBB_KEY", True),
|
||||
("my_OBB_key", True),
|
||||
("OBB", True),
|
||||
],
|
||||
)
|
||||
def test_validate_register_key(key, should_raise):
|
||||
"""Test that keys containing 'OBB' are rejected."""
|
||||
if should_raise:
|
||||
with pytest.raises(argparse.ArgumentTypeError, match="OBB"):
|
||||
validate_register_key(key)
|
||||
else:
|
||||
assert validate_register_key(key) == key
|
||||
|
||||
|
||||
# --- Tests for check_file_type_saved ---
|
||||
|
||||
|
||||
def test_check_file_type_saved_valid(mock_session):
|
||||
"""Test valid filenames are accepted."""
|
||||
checker = check_file_type_saved(valid_types=[".csv", ".json"])
|
||||
assert checker("report.csv") == "report.csv"
|
||||
assert checker("data.json") == "data.json"
|
||||
assert checker("a.csv,b.json") == "a.csv,b.json"
|
||||
|
||||
|
||||
def test_check_file_type_saved_invalid(mock_session):
|
||||
"""Test invalid filenames are rejected with a warning."""
|
||||
checker = check_file_type_saved(valid_types=[".csv"])
|
||||
result = checker("report.xlsx")
|
||||
assert result == ""
|
||||
mock_session.console.print.assert_called()
|
||||
|
||||
|
||||
def test_check_file_type_saved_empty(mock_session):
|
||||
"""Test empty input returns empty string."""
|
||||
checker = check_file_type_saved(valid_types=[".csv"])
|
||||
assert checker("") == ""
|
||||
assert checker() == ""
|
||||
|
||||
|
||||
def test_check_file_type_saved_no_valid_types(mock_session):
|
||||
"""Test that None valid_types returns empty string."""
|
||||
checker = check_file_type_saved(valid_types=None)
|
||||
assert checker("anything.csv") == ""
|
||||
|
||||
Reference in New Issue
Block a user