From 27ff615d4a5081bf65d6a7582b3dcd745bdc0e79 Mon Sep 17 00:00:00 2001 From: Gustavo Henrique Santos Souza de Miranda Date: Mon, 21 Jul 2025 18:03:01 -0300 Subject: [PATCH] Add the tests for the directory manager --- tests/utils/test_directory_manager.py | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 tests/utils/test_directory_manager.py diff --git a/tests/utils/test_directory_manager.py b/tests/utils/test_directory_manager.py new file mode 100644 index 0000000..eb3846b --- /dev/null +++ b/tests/utils/test_directory_manager.py @@ -0,0 +1,71 @@ +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()