|
| 1 | +from pathlib import Path |
1 | 2 | from unittest.mock import Mock, patch |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from upath import UPath |
4 | 6 |
|
5 | | -from webknossos.utils import call_with_retries |
| 7 | +from webknossos.utils import call_with_retries, dump_path |
6 | 8 |
|
7 | 9 |
|
8 | 10 | def test_call_with_retries_success() -> None: |
@@ -68,3 +70,122 @@ def test_call_with_retries_direct_failure(mock_sleep: Mock) -> None: |
68 | 70 |
|
69 | 71 | assert mock_fn.call_count == 1 # Only called once, no retries |
70 | 72 | assert mock_sleep.call_count == 0 # Sleep not called since it failed immediately |
| 73 | + |
| 74 | + |
| 75 | +def test_dump_path(tmp_path: Path) -> None: |
| 76 | + tmp_path = UPath(tmp_path.resolve()) |
| 77 | + |
| 78 | + # relative enclosed |
| 79 | + dataset_path = tmp_path / "test_dataset" |
| 80 | + path = dataset_path / "test.txt" |
| 81 | + assert dump_path(path, dataset_path) == "./test.txt" |
| 82 | + |
| 83 | + # relative not enclosed |
| 84 | + dataset_path = tmp_path / "test_dataset" |
| 85 | + path = dataset_path / ".." / "test_dataset2" / "test.txt" |
| 86 | + assert ( |
| 87 | + dump_path(path, dataset_path) == f"{tmp_path.as_posix()}/test_dataset2/test.txt" |
| 88 | + ) |
| 89 | + |
| 90 | + # dataset path with .. |
| 91 | + dataset_path = tmp_path / "test_dataset" / ".." / "test_dataset2" |
| 92 | + path = tmp_path / "test_dataset" / "test.txt" |
| 93 | + assert ( |
| 94 | + dump_path(path, dataset_path) == f"{tmp_path.as_posix()}/test_dataset/test.txt" |
| 95 | + ) |
| 96 | + |
| 97 | + dataset_path = tmp_path / "test_dataset" / ".." / "test_dataset2" |
| 98 | + path = tmp_path / "test_dataset2" / "test.txt" |
| 99 | + assert dump_path(path, dataset_path) == "./test.txt" |
| 100 | + |
| 101 | + # dataset path is a prefix |
| 102 | + dataset_path = tmp_path / "test_dataset" |
| 103 | + path = tmp_path / "test_dataset_longer" / "test.txt" |
| 104 | + assert ( |
| 105 | + dump_path(path, dataset_path) |
| 106 | + == f"{tmp_path.as_posix()}/test_dataset_longer/test.txt" |
| 107 | + ) |
| 108 | + |
| 109 | + # s3 non relative |
| 110 | + dataset_path = tmp_path / "test_dataset" |
| 111 | + path = UPath( |
| 112 | + "s3://bucket/test.txt", |
| 113 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 114 | + ) |
| 115 | + assert dump_path(path, dataset_path) == "s3://s3.amazonaws.com/bucket/test.txt" |
| 116 | + |
| 117 | + # s3 relative |
| 118 | + dataset_path = UPath( |
| 119 | + "s3://bucket/test_dataset", |
| 120 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 121 | + ) |
| 122 | + path = dataset_path / "test.txt" |
| 123 | + assert dump_path(path, dataset_path) == "./test.txt" |
| 124 | + |
| 125 | + # s3 dataset path is a prefix |
| 126 | + dataset_path = UPath( |
| 127 | + "s3://bucket/test_dataset", |
| 128 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 129 | + ) |
| 130 | + path = ( |
| 131 | + UPath( |
| 132 | + "s3://bucket/test_dataset_longer", |
| 133 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 134 | + ) |
| 135 | + / "test.txt" |
| 136 | + ) |
| 137 | + assert ( |
| 138 | + dump_path(path, dataset_path) |
| 139 | + == "s3://s3.amazonaws.com/bucket/test_dataset_longer/test.txt" |
| 140 | + ) |
| 141 | + |
| 142 | + # s3 with .. |
| 143 | + dataset_path = UPath( |
| 144 | + "s3://bucket/test_dataset", |
| 145 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 146 | + ) |
| 147 | + path = dataset_path / ".." / "test_dataset2" / "test.txt" |
| 148 | + assert ( |
| 149 | + dump_path(path, dataset_path) |
| 150 | + == "s3://s3.amazonaws.com/bucket/test_dataset2/test.txt" |
| 151 | + ) |
| 152 | + |
| 153 | + path = dataset_path / ".." / "test_dataset2" / "test.txt" |
| 154 | + assert ( |
| 155 | + dump_path(path, dataset_path) |
| 156 | + == "s3://s3.amazonaws.com/bucket/test_dataset2/test.txt" |
| 157 | + ) |
| 158 | + |
| 159 | + path = dataset_path / ".." / "test_dataset" / "test.txt" |
| 160 | + assert dump_path(path, dataset_path) == "./test.txt" |
| 161 | + |
| 162 | + # s3 dataset with .. |
| 163 | + dataset_path = ( |
| 164 | + UPath( |
| 165 | + "s3://bucket/test_dataset", |
| 166 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 167 | + ) |
| 168 | + / ".." |
| 169 | + / "test_dataset2" |
| 170 | + ) |
| 171 | + path = dataset_path / "test.txt" |
| 172 | + assert dump_path(path, dataset_path) == "./test.txt" |
| 173 | + |
| 174 | + path = UPath( |
| 175 | + "s3://bucket/test_dataset/test.txt", |
| 176 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 177 | + ) |
| 178 | + assert ( |
| 179 | + dump_path(path, dataset_path) |
| 180 | + == "s3://s3.amazonaws.com/bucket/test_dataset/test.txt" |
| 181 | + ) |
| 182 | + |
| 183 | + path = ( |
| 184 | + UPath( |
| 185 | + "s3://bucket/", |
| 186 | + client_kwargs={"endpoint_url": "https://s3.amazonaws.com"}, |
| 187 | + ) |
| 188 | + / "test_dataset2" |
| 189 | + / "test.txt" |
| 190 | + ) |
| 191 | + assert dump_path(path, dataset_path) == "./test.txt" |
0 commit comments