mirror of
https://github.com/gmbrax/Pilgrim.git
synced 2026-11-10 01:20:16 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bf64255fe2 |
@@ -0,0 +1,33 @@
|
|||||||
|
from pilgrim.service.servicemanager import ServiceManager
|
||||||
|
from unittest.mock import patch, MagicMock
|
||||||
|
|
||||||
|
def test_initial_state_is_none():
|
||||||
|
manager = ServiceManager()
|
||||||
|
assert manager.get_session() is None
|
||||||
|
assert manager.get_entry_service() is None
|
||||||
|
assert manager.get_photo_service() is None
|
||||||
|
assert manager.get_travel_diary_service() is None
|
||||||
|
|
||||||
|
@patch('pilgrim.service.servicemanager.TravelDiaryService')
|
||||||
|
@patch('pilgrim.service.servicemanager.PhotoService')
|
||||||
|
@patch('pilgrim.service.servicemanager.EntryService')
|
||||||
|
def test_get_services_instantiates_with_correct_session(
|
||||||
|
mock_entry_service, mock_photo_service, mock_travel_diary_service
|
||||||
|
):
|
||||||
|
manager = ServiceManager()
|
||||||
|
mock_session = MagicMock()
|
||||||
|
manager.set_session(mock_session)
|
||||||
|
|
||||||
|
entry_service_instance = manager.get_entry_service()
|
||||||
|
photo_service_instance = manager.get_photo_service()
|
||||||
|
travel_diary_service_instance = manager.get_travel_diary_service()
|
||||||
|
|
||||||
|
mock_entry_service.assert_called_once()
|
||||||
|
mock_photo_service.assert_called_once()
|
||||||
|
mock_travel_diary_service.assert_called_once()
|
||||||
|
mock_entry_service.assert_called_once_with(mock_session)
|
||||||
|
mock_photo_service.assert_called_once_with(mock_session)
|
||||||
|
mock_travel_diary_service.assert_called_once_with(mock_session)
|
||||||
|
assert entry_service_instance == mock_entry_service.return_value
|
||||||
|
assert photo_service_instance == mock_photo_service.return_value
|
||||||
|
assert travel_diary_service_instance == mock_travel_diary_service.return_value
|
||||||
@@ -1,71 +0,0 @@
|
|||||||
import shutil
|
|
||||||
from pathlib import Path
|
|
||||||
from unittest.mock import patch
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
from pilgrim.utils.directory_manager import DirectoryManager
|
|
||||||
|
|
||||||
@patch('os.chmod')
|
|
||||||
@patch('pathlib.Path.home')
|
|
||||||
def test_get_config_directory_creates_dir_in_fake_home(mock_home, mock_chmod, tmp_path: Path):
|
|
||||||
mock_home.return_value = tmp_path
|
|
||||||
|
|
||||||
expected_config_dir = tmp_path / ".pilgrim"
|
|
||||||
assert not expected_config_dir.exists()
|
|
||||||
result_path = DirectoryManager.get_config_directory()
|
|
||||||
assert result_path == expected_config_dir
|
|
||||||
assert expected_config_dir.exists()
|
|
||||||
mock_chmod.assert_called_once_with(expected_config_dir, 0o700)
|
|
||||||
|
|
||||||
@patch('shutil.copy2')
|
|
||||||
@patch('pathlib.Path.home')
|
|
||||||
def test_get_database_path_no_migration(mock_home, mock_copy, tmp_path: Path):
|
|
||||||
mock_home.return_value = tmp_path
|
|
||||||
expected_db_path = tmp_path / ".pilgrim" / "database.db"
|
|
||||||
result_path = DirectoryManager.get_database_path()
|
|
||||||
assert result_path == expected_db_path
|
|
||||||
mock_copy.assert_not_called()
|
|
||||||
|
|
||||||
@patch('shutil.copy2')
|
|
||||||
@patch('pathlib.Path.home')
|
|
||||||
def test_get_database_path_with_migration(mock_home, mock_copy, tmp_path: Path, monkeypatch):
|
|
||||||
fake_home_dir = tmp_path / "home"
|
|
||||||
fake_project_dir = tmp_path / "project"
|
|
||||||
fake_home_dir.mkdir()
|
|
||||||
fake_project_dir.mkdir()
|
|
||||||
|
|
||||||
(fake_project_dir / "database.db").touch()
|
|
||||||
mock_home.return_value = fake_home_dir
|
|
||||||
monkeypatch.chdir(fake_project_dir)
|
|
||||||
result_path = DirectoryManager.get_database_path()
|
|
||||||
expected_db_path = fake_home_dir / ".pilgrim" / "database.db"
|
|
||||||
assert result_path == expected_db_path
|
|
||||||
|
|
||||||
mock_copy.assert_called_once_with(
|
|
||||||
Path("database.db"),
|
|
||||||
expected_db_path
|
|
||||||
)
|
|
||||||
|
|
||||||
@patch('os.chmod')
|
|
||||||
@patch('pathlib.Path.home')
|
|
||||||
def test_diary_path_methods_construct_correctly(mock_home, mock_chmod, tmp_path: Path):
|
|
||||||
mock_home.return_value = tmp_path
|
|
||||||
images_path = DirectoryManager.get_diary_images_directory("minha-viagem")
|
|
||||||
expected_path = tmp_path / ".pilgrim" / "diaries" / "minha-viagem" / "data" / "images"
|
|
||||||
assert images_path == expected_path
|
|
||||||
assert (tmp_path / ".pilgrim" / "diaries").exists()
|
|
||||||
|
|
||||||
@patch('shutil.copy2')
|
|
||||||
@patch('pathlib.Path.home')
|
|
||||||
def test_get_database_path_handles_migration_error(mock_home, mock_copy, tmp_path: Path, monkeypatch):
|
|
||||||
fake_home_dir = tmp_path / "home"
|
|
||||||
fake_project_dir = tmp_path / "project"
|
|
||||||
fake_home_dir.mkdir()
|
|
||||||
fake_project_dir.mkdir()
|
|
||||||
(fake_project_dir / "database.db").touch()
|
|
||||||
mock_home.return_value = fake_home_dir
|
|
||||||
mock_copy.side_effect = shutil.Error("O disco está cheio!")
|
|
||||||
monkeypatch.chdir(fake_project_dir)
|
|
||||||
with pytest.raises(RuntimeError, match="Failed to migrate database"):
|
|
||||||
DirectoryManager.get_database_path()
|
|
||||||
mock_copy.assert_called_once()
|
|
||||||
Reference in New Issue
Block a user