|
| 1 | +# SPDX-FileCopyrightText: 2022 Contributors to the Power Grid Model IO project <dynamic.grid.calculation@alliander.com> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +from pathlib import Path |
| 7 | +from unittest.mock import MagicMock, patch |
| 8 | + |
| 9 | +import pytest |
| 10 | +import structlog.testing |
| 11 | + |
| 12 | +from power_grid_model_io.utils.zip_files import extract |
| 13 | + |
| 14 | +from ...utils import MockTqdm, assert_log_exists |
| 15 | + |
| 16 | +DATA_DIR = Path(__file__).parents[2] / "data" / "zip_files" |
| 17 | +ZIP1 = DATA_DIR / "foo.zip" |
| 18 | +ZIP2 = DATA_DIR / "foo-bar.zip" |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture() |
| 22 | +def temp_dir(): |
| 23 | + with tempfile.TemporaryDirectory() as tmp: |
| 24 | + yield Path(tmp) |
| 25 | + |
| 26 | + |
| 27 | +@patch("power_grid_model_io.utils.download.tqdm") |
| 28 | +def test_extract(mock_tqdm: MagicMock, temp_dir: Path): |
| 29 | + # Arrange |
| 30 | + src_file_path = temp_dir / "compressed.zip" |
| 31 | + dst_dir_path = temp_dir / "extracted" |
| 32 | + shutil.copyfile(ZIP2, src_file_path) |
| 33 | + mock_tqdm.side_effect = MockTqdm |
| 34 | + |
| 35 | + # Act |
| 36 | + extract_dir_path = extract(src_file_path=src_file_path, dst_dir_path=dst_dir_path) |
| 37 | + |
| 38 | + # Assert |
| 39 | + assert extract_dir_path == dst_dir_path |
| 40 | + assert (dst_dir_path / "foo.txt").is_file() |
| 41 | + assert (dst_dir_path / "folder/bar.txt").is_file() |
| 42 | + |
| 43 | + |
| 44 | +@patch("power_grid_model_io.utils.download.tqdm") |
| 45 | +def test_extract__auto_dir(mock_tqdm: MagicMock, temp_dir: Path): |
| 46 | + # Arrange |
| 47 | + src_file_path = temp_dir / "compressed.zip" |
| 48 | + shutil.copyfile(ZIP2, src_file_path) |
| 49 | + mock_tqdm.side_effect = MockTqdm |
| 50 | + |
| 51 | + # Act |
| 52 | + extract_dir_path = extract(src_file_path=src_file_path) |
| 53 | + |
| 54 | + # Assert |
| 55 | + assert extract_dir_path == temp_dir / "compressed" |
| 56 | + assert (temp_dir / "compressed" / "foo.txt").is_file() |
| 57 | + assert (temp_dir / "compressed" / "folder" / "bar.txt").is_file() |
| 58 | + |
| 59 | + |
| 60 | +def test_extract__invalid_file_extension(): |
| 61 | + # Act / Assert |
| 62 | + with pytest.raises(ValueError, match=r"Only files with \.zip extension are supported, got tempfile\.download"): |
| 63 | + extract(src_file_path=Path("/tmp/dir/tempfile.download")) |
| 64 | + |
| 65 | + |
| 66 | +@patch("power_grid_model_io.utils.download.tqdm") |
| 67 | +def test_extract__file_exists(mock_tqdm: MagicMock, temp_dir: Path): |
| 68 | + # Arrange |
| 69 | + src_file_path = temp_dir / "compressed.zip" |
| 70 | + dst_dir_path = temp_dir / "extracted" |
| 71 | + shutil.copyfile(ZIP2, src_file_path) |
| 72 | + mock_tqdm.side_effect = MockTqdm |
| 73 | + |
| 74 | + dst_dir_path.mkdir() |
| 75 | + with open(dst_dir_path / "foo.txt", "wb") as fp: |
| 76 | + fp.write(b"\0") |
| 77 | + |
| 78 | + # Act / Assert |
| 79 | + with pytest.raises(FileExistsError, match=r"Destination file .*foo\.txt exists and is not empty"): |
| 80 | + extract(src_file_path=src_file_path, dst_dir_path=dst_dir_path) |
| 81 | + |
| 82 | + |
| 83 | +@patch("power_grid_model_io.utils.download.tqdm") |
| 84 | +def test_extract__skip_if_exists(mock_tqdm: MagicMock, temp_dir: Path): |
| 85 | + # Arrange |
| 86 | + src_file_path = temp_dir / "compressed.zip" |
| 87 | + dst_dir_path = temp_dir / "compressed" |
| 88 | + shutil.copyfile(ZIP2, src_file_path) |
| 89 | + mock_tqdm.side_effect = MockTqdm |
| 90 | + |
| 91 | + dst_dir_path.mkdir() |
| 92 | + with open(dst_dir_path / "foo.txt", "wb") as fp: |
| 93 | + fp.write(b"\0") |
| 94 | + |
| 95 | + # Act / Assert |
| 96 | + with structlog.testing.capture_logs() as capture: |
| 97 | + extract(src_file_path=src_file_path, dst_dir_path=dst_dir_path, skip_if_exists=True) |
| 98 | + assert_log_exists( |
| 99 | + capture, "debug", "Skip file extraction, destination file exists", dst_file_path=dst_dir_path / "foo.txt" |
| 100 | + ) |
| 101 | + |
| 102 | + |
| 103 | +@patch("power_grid_model_io.utils.download.tqdm") |
| 104 | +def test_extract__return_subdir_path(mock_tqdm: MagicMock, temp_dir: Path): |
| 105 | + # Arrange |
| 106 | + src_file_path = temp_dir / "foo.zip" |
| 107 | + shutil.copyfile(ZIP1, src_file_path) |
| 108 | + mock_tqdm.side_effect = MockTqdm |
| 109 | + |
| 110 | + # Act |
| 111 | + extract_dir_path = extract(src_file_path=src_file_path) |
| 112 | + |
| 113 | + # Assert |
| 114 | + assert extract_dir_path == temp_dir / "foo" / "foo" |
| 115 | + assert (temp_dir / "foo" / "foo" / "foo.txt").is_file() |
0 commit comments