|
| 1 | +"""test_folder_utils.py""" |
| 2 | + |
| 3 | +import unittest |
| 4 | +import pathlib |
| 5 | +from unittest import mock |
| 6 | + |
| 7 | +from pownet import folder_utils |
| 8 | + |
| 9 | + |
| 10 | +class TestFolderUtils(unittest.TestCase): |
| 11 | + |
| 12 | + def test_get_pownet_dir(self): |
| 13 | + # Path to the actual folder_utils.py file |
| 14 | + utils_file_path = pathlib.Path(folder_utils.__file__).resolve() |
| 15 | + # Expected: three levels up from folder_utils.py |
| 16 | + # .../utils/ -> .../pownet/ -> .../src/ |
| 17 | + expected_dir = utils_file_path.parent.parent.parent |
| 18 | + |
| 19 | + self.assertEqual(pathlib.Path(folder_utils.get_pownet_dir()), expected_dir) |
| 20 | + self.assertTrue(expected_dir.is_dir()) |
| 21 | + |
| 22 | + def test_get_home_dir(self): |
| 23 | + with mock.patch( |
| 24 | + "os.path.expanduser", return_value="/mocked/home/user" |
| 25 | + ) as mocked_expanduser: |
| 26 | + self.assertEqual(folder_utils.get_home_dir(), "/mocked/home/user") |
| 27 | + mocked_expanduser.assert_called_once_with("~") |
| 28 | + |
| 29 | + def test_get_database_dir(self): |
| 30 | + utils_file_path = pathlib.Path(folder_utils.__file__).resolve() |
| 31 | + # Expected: .../utils/database/ |
| 32 | + expected_dir = utils_file_path.parent / "database" |
| 33 | + |
| 34 | + self.assertEqual(pathlib.Path(folder_utils.get_database_dir()), expected_dir) |
| 35 | + self.assertTrue(expected_dir.is_dir()) |
| 36 | + |
| 37 | + def test_get_test_dir(self): |
| 38 | + # Scenario 1: Using the actual get_pownet_dir |
| 39 | + pownet_dir_path = pathlib.Path(folder_utils.get_pownet_dir()) |
| 40 | + # The function joins get_pownet_dir() with "src" and "test_pownet" |
| 41 | + expected_dir = pownet_dir_path / "src" / "test_pownet" |
| 42 | + self.assertEqual(pathlib.Path(folder_utils.get_test_dir()), expected_dir) |
| 43 | + |
| 44 | + # Scenario 2: Mocking get_pownet_dir for isolation |
| 45 | + with mock.patch( |
| 46 | + "pownet.folder_utils.get_pownet_dir", |
| 47 | + return_value="/fake/pownet_root_dir", |
| 48 | + ) as mocked_get_pownet: |
| 49 | + expected_dir_mocked = pathlib.Path("/fake/pownet_root_dir/src/test_pownet") |
| 50 | + self.assertEqual( |
| 51 | + pathlib.Path(folder_utils.get_test_dir()), expected_dir_mocked |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + unittest.main() |
0 commit comments