2025-11-04 08:09:12 +01:00
|
|
|
from unittest.mock import mock_open, patch
|
|
|
|
|
|
|
|
|
|
from pr.core.config_loader import (
|
|
|
|
|
_load_config_file,
|
|
|
|
|
_parse_value,
|
|
|
|
|
create_default_config,
|
|
|
|
|
load_config,
|
|
|
|
|
)
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
|
|
|
|
def test_parse_value_string():
|
2025-11-04 08:09:12 +01:00
|
|
|
assert _parse_value("hello") == "hello"
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
|
|
|
|
def test_parse_value_int():
|
2025-11-04 08:09:12 +01:00
|
|
|
assert _parse_value("123") == 123
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
|
|
|
|
def test_parse_value_float():
|
2025-11-04 08:09:12 +01:00
|
|
|
assert _parse_value("1.23") == 1.23
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
|
|
|
|
def test_parse_value_bool_true():
|
2025-11-04 08:09:12 +01:00
|
|
|
assert _parse_value("true") == True
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
|
|
|
|
def test_parse_value_bool_false():
|
2025-11-04 08:09:12 +01:00
|
|
|
assert _parse_value("false") == False
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
|
|
|
|
def test_parse_value_bool_upper():
|
2025-11-04 08:09:12 +01:00
|
|
|
assert _parse_value("TRUE") == True
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
@patch("os.path.exists", return_value=False)
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_load_config_file_not_exists(mock_exists):
|
2025-11-04 08:09:12 +01:00
|
|
|
config = _load_config_file("test.ini")
|
2025-11-04 05:57:23 +01:00
|
|
|
assert config == {}
|
|
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
|
|
|
|
|
@patch("os.path.exists", return_value=True)
|
|
|
|
|
@patch("configparser.ConfigParser")
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_load_config_file_exists(mock_parser_class, mock_exists):
|
|
|
|
|
mock_parser = mock_parser_class.return_value
|
2025-11-04 08:09:12 +01:00
|
|
|
mock_parser.sections.return_value = ["api"]
|
|
|
|
|
mock_parser.items.return_value = [("key", "value")]
|
|
|
|
|
config = _load_config_file("test.ini")
|
|
|
|
|
assert "api" in config
|
|
|
|
|
assert config["api"]["key"] == "value"
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
|
|
|
|
|
@patch("pr.core.config_loader._load_config_file")
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_load_config(mock_load):
|
2025-11-04 08:09:12 +01:00
|
|
|
mock_load.side_effect = [{"api": {"key": "global"}}, {"api": {"key": "local"}}]
|
2025-11-04 05:57:23 +01:00
|
|
|
config = load_config()
|
2025-11-04 08:09:12 +01:00
|
|
|
assert config["api"]["key"] == "local"
|
|
|
|
|
|
2025-11-04 05:57:23 +01:00
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
@patch("builtins.open", new_callable=mock_open)
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_create_default_config(mock_file):
|
2025-11-04 08:09:12 +01:00
|
|
|
result = create_default_config("test.ini")
|
2025-11-04 05:57:23 +01:00
|
|
|
assert result == True
|
2025-11-04 08:09:12 +01:00
|
|
|
mock_file.assert_called_once_with("test.ini", "w")
|
2025-11-04 05:57:23 +01:00
|
|
|
handle = mock_file()
|
|
|
|
|
handle.write.assert_called_once()
|
|
|
|
|
|
2025-11-04 08:09:12 +01:00
|
|
|
|
|
|
|
|
@patch("builtins.open", side_effect=Exception("error"))
|
2025-11-04 05:57:23 +01:00
|
|
|
def test_create_default_config_error(mock_file):
|
2025-11-04 08:09:12 +01:00
|
|
|
result = create_default_config("test.ini")
|
|
|
|
|
assert result == False
|