|
1 | 1 | """ """ |
2 | 2 |
|
3 | 3 | import copy |
| 4 | +import shutil |
4 | 5 | from pathlib import Path |
5 | 6 |
|
6 | 7 | import pandas as pd |
7 | 8 | import pytest |
8 | 9 |
|
| 10 | +from datashuttle.utils import rclone |
| 11 | + |
9 | 12 | from .. import test_utils |
10 | 13 | from ..base import BaseTest |
11 | 14 | from .file_conflicts_pathtable import get_pathtable |
@@ -159,3 +162,81 @@ def create_all_pathtable_files(self, pathtable): |
159 | 162 |
|
160 | 163 | def central_from_local(self, path_): |
161 | 164 | return Path(str(copy.copy(path_)).replace("local", "central")) |
| 165 | + |
| 166 | + # ------------------------------------------------------------------------- |
| 167 | + # Transfer |
| 168 | + # ------------------------------------------------------------------------- |
| 169 | + |
| 170 | + def run_and_check_transfers( |
| 171 | + self, project, pathtable, sub_names, ses_names, datatype |
| 172 | + ): |
| 173 | + true_central_path = project.cfg["central_path"] |
| 174 | + tmp_central_path = ( |
| 175 | + project.cfg["central_path"] / "tmp" / project.project_name |
| 176 | + ) |
| 177 | + self.remake_logging_path(project) |
| 178 | + |
| 179 | + project.update_config_file(central_path=tmp_central_path) |
| 180 | + |
| 181 | + project.upload_custom( |
| 182 | + "rawdata", sub_names, ses_names, datatype, init_log=False |
| 183 | + ) |
| 184 | + |
| 185 | + expected_transferred_paths = self.get_expected_transferred_paths( |
| 186 | + pathtable, sub_names, ses_names, datatype |
| 187 | + ) |
| 188 | + |
| 189 | + transferred_files = test_utils.recursive_search_central(project) |
| 190 | + paths_to_transferred_files = self.remove_path_before_rawdata( |
| 191 | + transferred_files |
| 192 | + ) |
| 193 | + |
| 194 | + assert sorted(paths_to_transferred_files) == sorted( |
| 195 | + expected_transferred_paths |
| 196 | + ) |
| 197 | + |
| 198 | + # Now, move data from the central path where the project is |
| 199 | + # setup, to a temp local folder to test download. |
| 200 | + true_local_path = project.cfg["local_path"] |
| 201 | + tmp_local_path = ( |
| 202 | + project.cfg["local_path"] / "tmp" / project.project_name |
| 203 | + ) |
| 204 | + tmp_local_path.mkdir(exist_ok=True, parents=True) |
| 205 | + |
| 206 | + project.update_config_file(local_path=tmp_local_path) |
| 207 | + project.update_config_file(central_path=true_central_path) |
| 208 | + |
| 209 | + project.download_custom( |
| 210 | + "rawdata", sub_names, ses_names, datatype, init_log=False |
| 211 | + ) |
| 212 | + |
| 213 | + # Find the transferred paths, tidy them up |
| 214 | + # and check expected paths were transferred. |
| 215 | + all_transferred = list((tmp_local_path / "rawdata").glob("**/*")) |
| 216 | + all_transferred = [ |
| 217 | + path_ for path_ in all_transferred if path_.is_file() |
| 218 | + ] |
| 219 | + |
| 220 | + paths_to_transferred_files = self.remove_path_before_rawdata( |
| 221 | + all_transferred |
| 222 | + ) |
| 223 | + |
| 224 | + assert sorted(paths_to_transferred_files) == sorted( |
| 225 | + expected_transferred_paths |
| 226 | + ) |
| 227 | + |
| 228 | + rclone.call_rclone( |
| 229 | + f"purge {project.cfg.get_rclone_config_name()}:{tmp_central_path.as_posix()}" |
| 230 | + ) |
| 231 | + |
| 232 | + shutil.rmtree(tmp_local_path) |
| 233 | + |
| 234 | + self.remake_logging_path(project) |
| 235 | + project.update_config_file(local_path=true_local_path) |
| 236 | + |
| 237 | + def remake_logging_path(self, project): |
| 238 | + """ |
| 239 | + Need to do this to compensate for switching |
| 240 | + local_path location in the test environment. |
| 241 | + """ |
| 242 | + project.get_logging_path().mkdir(parents=True, exist_ok=True) |
0 commit comments