|
8 | 8 | from cg.constants import SequencingFileTag
|
9 | 9 | from cg.constants.subject import Sex
|
10 | 10 | from cg.meta.clean.clean_flow_cells import CleanFlowCellAPI
|
| 11 | +from cg.meta.clean.clean_retrieved_spring_files import CleanRetrievedSpringFilesAPI |
11 | 12 | from cg.models.flow_cell.flow_cell import FlowCellDirectoryData
|
12 | 13 | from cg.store import Store
|
13 | 14 | from cg.store.models import Flowcell, Sample
|
@@ -234,3 +235,119 @@ def hk_sample_bundle_for_flow_cell_not_to_clean(
|
234 | 235 | "expires": timestamp_yesterday,
|
235 | 236 | "files": [],
|
236 | 237 | }
|
| 238 | + |
| 239 | + |
| 240 | +@pytest.fixture |
| 241 | +def clean_retrieved_spring_files_api( |
| 242 | + real_housekeeper_api: HousekeeperAPI, tmp_path |
| 243 | +) -> CleanRetrievedSpringFilesAPI: |
| 244 | + """Returns a CleanRetrievedSpringFilesAPI.""" |
| 245 | + real_housekeeper_api.root_dir = tmp_path |
| 246 | + return CleanRetrievedSpringFilesAPI(housekeeper_api=real_housekeeper_api, dry_run=False) |
| 247 | + |
| 248 | + |
| 249 | +@pytest.fixture |
| 250 | +def path_to_old_retrieved_spring_file() -> str: |
| 251 | + return Path("path", "to", "old_retrieved_spring_file").as_posix() |
| 252 | + |
| 253 | + |
| 254 | +@pytest.fixture |
| 255 | +def path_to_newly_retrieved_spring_file() -> str: |
| 256 | + return Path("path", "to", "newly_retrieved_spring_file").as_posix() |
| 257 | + |
| 258 | + |
| 259 | +@pytest.fixture |
| 260 | +def path_to_archived_but_not_retrieved_spring_file() -> str: |
| 261 | + return Path("path", "to", "archived_spring_file").as_posix() |
| 262 | + |
| 263 | + |
| 264 | +@pytest.fixture |
| 265 | +def path_to_fastq_file() -> str: |
| 266 | + return Path("path", "to", "fastq_file").as_posix() |
| 267 | + |
| 268 | + |
| 269 | +@pytest.fixture |
| 270 | +def paths_for_populated_clean_retrieved_spring_files_api( |
| 271 | + path_to_old_retrieved_spring_file: str, |
| 272 | + path_to_newly_retrieved_spring_file: str, |
| 273 | + path_to_archived_but_not_retrieved_spring_file: str, |
| 274 | + path_to_fastq_file: str, |
| 275 | +) -> list[str]: |
| 276 | + return [ |
| 277 | + path_to_old_retrieved_spring_file, |
| 278 | + path_to_newly_retrieved_spring_file, |
| 279 | + path_to_archived_but_not_retrieved_spring_file, |
| 280 | + path_to_fastq_file, |
| 281 | + ] |
| 282 | + |
| 283 | + |
| 284 | +@pytest.fixture |
| 285 | +def retrieved_test_bundle_name() -> str: |
| 286 | + return "retrieved_test_bundle" |
| 287 | + |
| 288 | + |
| 289 | +@pytest.fixture |
| 290 | +def path_to_old_spring_file_in_housekeeper( |
| 291 | + retrieved_test_bundle_name: str, path_to_old_retrieved_spring_file |
| 292 | +) -> str: |
| 293 | + return Path( |
| 294 | + retrieved_test_bundle_name, |
| 295 | + str(datetime.today().date()), |
| 296 | + Path(path_to_old_retrieved_spring_file).name, |
| 297 | + ).as_posix() |
| 298 | + |
| 299 | + |
| 300 | +@pytest.fixture |
| 301 | +def populated_clean_retrieved_spring_files_api( |
| 302 | + clean_retrieved_spring_files_api: CleanRetrievedSpringFilesAPI, |
| 303 | + paths_for_populated_clean_retrieved_spring_files_api: list[str], |
| 304 | + retrieved_test_bundle_name: str, |
| 305 | + archival_job_id_miria: int, |
| 306 | + retrieval_job_id_miria: int, |
| 307 | + timestamp: datetime, |
| 308 | + timestamp_yesterday: datetime, |
| 309 | + old_timestamp: datetime, |
| 310 | + tmp_path, |
| 311 | +) -> CleanRetrievedSpringFilesAPI: |
| 312 | + """ |
| 313 | + Returns a populated CleanRetrievedSpringFilesAPI, containing a bundle with one version and the following files: |
| 314 | + - an archived Spring file which has not been retrieved |
| 315 | + - an archived Spring file which was retrieved 1 day ago |
| 316 | + - an archived Spring file which was retrieved in the year 1900 |
| 317 | + - a Fastq file |
| 318 | + """ |
| 319 | + clean_retrieved_spring_files_api.housekeeper_api.add_bundle_and_version_if_non_existent( |
| 320 | + bundle_name=retrieved_test_bundle_name |
| 321 | + ) |
| 322 | + clean_retrieved_spring_files_api.housekeeper_api.commit() |
| 323 | + for path in paths_for_populated_clean_retrieved_spring_files_api: |
| 324 | + tags: list[str] = ( |
| 325 | + [SequencingFileTag.SPRING] |
| 326 | + if SequencingFileTag.SPRING in path |
| 327 | + else [SequencingFileTag.FASTQ] |
| 328 | + ) |
| 329 | + Path(tmp_path, path).parent.mkdir(parents=True, exist_ok=True) |
| 330 | + file_to_add = Path(tmp_path, path) |
| 331 | + file_to_add.touch() |
| 332 | + clean_retrieved_spring_files_api.housekeeper_api.add_and_include_file_to_latest_version( |
| 333 | + file=file_to_add, bundle_name=retrieved_test_bundle_name, tags=tags |
| 334 | + ) |
| 335 | + for file in clean_retrieved_spring_files_api.housekeeper_api.get_files( |
| 336 | + bundle=retrieved_test_bundle_name |
| 337 | + ): |
| 338 | + Path(file.full_path).parent.mkdir(parents=True, exist_ok=True) |
| 339 | + Path(file.full_path).touch() |
| 340 | + if "spring" in file.path: |
| 341 | + clean_retrieved_spring_files_api.housekeeper_api.add_archives( |
| 342 | + files=[file], archive_task_id=archival_job_id_miria |
| 343 | + ) |
| 344 | + file.archive.archived_at = timestamp |
| 345 | + if "retrieved" in file.path: |
| 346 | + file.archive.retrieval_task_id = retrieval_job_id_miria |
| 347 | + file.archive.retrieved_at = ( |
| 348 | + old_timestamp if "old" in file.path else timestamp_yesterday |
| 349 | + ) |
| 350 | + clean_retrieved_spring_files_api.housekeeper_api.add_commit(file.archive) |
| 351 | + |
| 352 | + clean_retrieved_spring_files_api.housekeeper_api.commit() |
| 353 | + return clean_retrieved_spring_files_api |
0 commit comments