test: expand configure load tests with fixture and error cases
This commit is contained in:
@@ -1,32 +1,68 @@
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
from skywipe.configure import Configuration
|
||||
|
||||
|
||||
def test_configuration_load_missing_file(monkeypatch, tmp_path):
|
||||
@pytest.fixture
|
||||
def config_with_tmp_path(monkeypatch, tmp_path):
|
||||
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
||||
config = Configuration()
|
||||
with pytest.raises(FileNotFoundError):
|
||||
config.load()
|
||||
return Configuration()
|
||||
|
||||
|
||||
def test_configuration_load_empty_file(monkeypatch, tmp_path):
|
||||
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
||||
config = Configuration()
|
||||
config.config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config.config_file.write_text("")
|
||||
def test_configuration_load_missing_file(config_with_tmp_path):
|
||||
with pytest.raises(FileNotFoundError, match="Configuration file not found"):
|
||||
config_with_tmp_path.load()
|
||||
|
||||
|
||||
def test_configuration_load_empty_file(config_with_tmp_path):
|
||||
config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config_with_tmp_path.config_file.write_text("")
|
||||
|
||||
with pytest.raises(ValueError, match="empty or invalid"):
|
||||
config.load()
|
||||
config_with_tmp_path.load()
|
||||
|
||||
|
||||
def test_configuration_load_invalid_yaml(monkeypatch, tmp_path):
|
||||
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
||||
config = Configuration()
|
||||
config.config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config.config_file.write_text(": bad")
|
||||
def test_configuration_load_invalid_yaml(config_with_tmp_path):
|
||||
config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config_with_tmp_path.config_file.write_text(": bad")
|
||||
|
||||
with pytest.raises(ValueError, match="Invalid YAML"):
|
||||
config.load()
|
||||
config_with_tmp_path.load()
|
||||
|
||||
|
||||
def test_configuration_load_valid_file(config_with_tmp_path):
|
||||
config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config_data = {
|
||||
"handle": "alice.bsky.social",
|
||||
"password": "password123",
|
||||
"batch_size": 10,
|
||||
"delay": 1,
|
||||
"verbose": True
|
||||
}
|
||||
with open(config_with_tmp_path.config_file, "w") as f:
|
||||
yaml.dump(config_data, f)
|
||||
|
||||
result = config_with_tmp_path.load()
|
||||
assert result == config_data
|
||||
|
||||
|
||||
def test_configuration_load_file_read_error(config_with_tmp_path):
|
||||
config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config_with_tmp_path.config_file.write_text("handle: alice")
|
||||
|
||||
with patch("builtins.open", side_effect=IOError("Permission denied")):
|
||||
with pytest.raises(ValueError, match="Failed to read configuration file"):
|
||||
config_with_tmp_path.load()
|
||||
|
||||
|
||||
def test_configuration_load_file_os_error(config_with_tmp_path):
|
||||
config_with_tmp_path.config_file.parent.mkdir(parents=True, exist_ok=True)
|
||||
config_with_tmp_path.config_file.write_text("handle: alice")
|
||||
|
||||
with patch("builtins.open", side_effect=OSError("File is locked")):
|
||||
with pytest.raises(ValueError, match="Failed to read configuration file"):
|
||||
config_with_tmp_path.load()
|
||||
|
||||
Reference in New Issue
Block a user