2024-06-27 20:08:41 +02:00

34 lines
1.4 KiB
Python

from pathlib import Path
from iottb.main import load_config
class TestLoadConfig:
# Loads configuration from an existing file
def test_loads_config_from_existing_file(self, mocker):
cfg_file = mocker.Mock()
cfg_file.is_file.return_value = True
mock_open = mocker.mock_open(
read_data=f'{{"DefaultDatabase": "test_db", "DefaultDatabasePath": "{Path.home()}/user"}}')
mocker.patch('builtins.open', mock_open)
result = load_config(cfg_file)
assert result == {"DefaultDatabase": "test_db", "DefaultDatabasePath": f"{Path.home()}/user"}
cfg_file.is_file.assert_called_once()
mock_open.assert_called_once_with(cfg_file, 'r')
# File path is invalid or inaccessible
def test_file_path_invalid_or_inaccessible(self, mocker):
cfg_file = mocker.Mock()
cfg_file.is_file.return_value = False
mock_create_default_config = mocker.patch('iottb.main.create_default_config',
return_value={"DefaultDatabase": "default_db",
"DefaultDatabasePath": f"{Path.home()}/default"})
result = load_config(cfg_file)
assert result == {"DefaultDatabase": "default_db", "DefaultDatabasePath": f"{Path.home()}/default"}
cfg_file.is_file.assert_called_once()
mock_create_default_config.assert_called_once_with(cfg_file)