56 lines
1.8 KiB
Python
56 lines
1.8 KiB
Python
|
|
import pytest
|
||
|
|
from unittest.mock import patch, mock_open
|
||
|
|
import os
|
||
|
|
from pr.core.config_loader import load_config, _load_config_file, _parse_value, create_default_config
|
||
|
|
|
||
|
|
def test_parse_value_string():
|
||
|
|
assert _parse_value('hello') == 'hello'
|
||
|
|
|
||
|
|
def test_parse_value_int():
|
||
|
|
assert _parse_value('123') == 123
|
||
|
|
|
||
|
|
def test_parse_value_float():
|
||
|
|
assert _parse_value('1.23') == 1.23
|
||
|
|
|
||
|
|
def test_parse_value_bool_true():
|
||
|
|
assert _parse_value('true') == True
|
||
|
|
|
||
|
|
def test_parse_value_bool_false():
|
||
|
|
assert _parse_value('false') == False
|
||
|
|
|
||
|
|
def test_parse_value_bool_upper():
|
||
|
|
assert _parse_value('TRUE') == True
|
||
|
|
|
||
|
|
@patch('os.path.exists', return_value=False)
|
||
|
|
def test_load_config_file_not_exists(mock_exists):
|
||
|
|
config = _load_config_file('test.ini')
|
||
|
|
assert config == {}
|
||
|
|
|
||
|
|
@patch('os.path.exists', return_value=True)
|
||
|
|
@patch('configparser.ConfigParser')
|
||
|
|
def test_load_config_file_exists(mock_parser_class, mock_exists):
|
||
|
|
mock_parser = mock_parser_class.return_value
|
||
|
|
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'
|
||
|
|
|
||
|
|
@patch('pr.core.config_loader._load_config_file')
|
||
|
|
def test_load_config(mock_load):
|
||
|
|
mock_load.side_effect = [{'api': {'key': 'global'}}, {'api': {'key': 'local'}}]
|
||
|
|
config = load_config()
|
||
|
|
assert config['api']['key'] == 'local'
|
||
|
|
|
||
|
|
@patch('builtins.open', new_callable=mock_open)
|
||
|
|
def test_create_default_config(mock_file):
|
||
|
|
result = create_default_config('test.ini')
|
||
|
|
assert result == True
|
||
|
|
mock_file.assert_called_once_with('test.ini', 'w')
|
||
|
|
handle = mock_file()
|
||
|
|
handle.write.assert_called_once()
|
||
|
|
|
||
|
|
@patch('builtins.open', side_effect=Exception('error'))
|
||
|
|
def test_create_default_config_error(mock_file):
|
||
|
|
result = create_default_config('test.ini')
|
||
|
|
assert result == False
|