Files
OpenBB/cli/tests/test_config_style.py
Danglewood 9a30186174 [V5] CLI 2.0 - Pluggable Backends, Non-TTY Default, HTTP Dispatcher, Spec Files + Codegen (#7433)
* stash some changes

* add more robust testing

* mypy

* point PR at V5

* introduce spec file

* codespell

* test fix

* fix workflow environment setup

* fix workflow environment setup

* fix workflow environment setup

* add pyyaml to dependencies

* split lint jobs

* fix workflow environment setup

* fix workflow environment setup

* workflow env setup

* workflow env setup

* clean up code comments

* add auth hook entrypoints

* codespell

* add codegen feature

* codespell

* move _unpack into dispatchers for consistency with codegen packages

* surface nested models in the response

* fix missing coverage in CI

* socrata updates

* test fix

* detect plotly output

* add --include and --exclude flags from generate-extension command

* cap test matrix at python 3.14

* no useless comments

* platform controller command description split

* merge URL overloads from path params

* exclude none and unset from model dump

---------

Co-authored-by: deeleeramone <>
Co-authored-by: Copilot <copilot@github.com>
2026-06-01 19:14:38 +03:00

88 lines
3.0 KiB
Python

"""Test Config Style."""
from pathlib import Path
from unittest.mock import MagicMock, patch
import pytest
from openbb_cli.config.style import Style
@pytest.fixture
def mock_style_directory(tmp_path):
"""Fixture to create a mock styles directory."""
(tmp_path / "styles" / "default").mkdir(parents=True, exist_ok=True)
return tmp_path / "styles"
@pytest.fixture
def style(mock_style_directory):
"""Fixture to create a Style instance for testing."""
return Style(directory=mock_style_directory)
def test_initialization(style):
"""Test that Style class initializes with default properties."""
assert style.line_width == 1.5
assert isinstance(style.console_style, dict)
@patch("pathlib.Path.exists", MagicMock(return_value=True))
@patch("pathlib.Path.rglob")
def test_load_styles(mock_rglob, style, mock_style_directory):
"""Test loading styles from directories."""
mock_rglob.return_value = [mock_style_directory / "default" / "dark.richstyle.json"]
style._load(mock_style_directory)
assert "dark" in style.console_styles_available
@patch("builtins.open", new_callable=MagicMock)
@patch("json.load", MagicMock(return_value={"background": "black"}))
def test_from_json(mock_open, style, mock_style_directory):
"""Test loading style from a JSON file."""
json_file = mock_style_directory / "dark.richstyle.json"
result = style._from_json(json_file)
assert result == {"background": "black"}
mock_open.assert_called_once_with(json_file)
def test_apply_invalid_style(style, mock_style_directory, capsys):
"""Test applying an invalid style and falling back to default."""
style.apply("nonexistent", mock_style_directory)
captured = capsys.readouterr()
assert "Invalid console style" in captured.out
def test_available_styles(style):
"""Test listing available styles."""
style.console_styles_available = {"dark": Path("/path/to/dark.richstyle.json")}
assert "dark" in style.available_styles
def test_apply_style_loaded_from_directory(style, mock_style_directory):
"""Style not in initial dict gets loaded via _load() and applied."""
target = mock_style_directory / "default" / "custom.richstyle.json"
target.parent.mkdir(parents=True, exist_ok=True)
target.write_text("{}")
def fake_load(directory):
style.console_styles_available["custom"] = target
with (
patch.object(style, "_load", side_effect=fake_load),
patch.object(style, "_from_json", return_value={"background": "white"}),
):
style.apply("custom", mock_style_directory)
assert style.console_style == {"background": "white"}
def test_apply_invalid_with_no_default_prints_error(
style, mock_style_directory, capsys
):
"""When neither the requested style nor 'dark' exists, error is printed."""
style.console_styles_available = {}
with patch.object(style, "_load"):
style.apply("missing", mock_style_directory)
captured = capsys.readouterr()
assert "Error loading default" in captured.out