|
| 1 | +import glob |
| 2 | +import os |
| 3 | +import re |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | +from pathlib import Path |
| 7 | +from typing import List |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from datashuttle.utils.folders import search_for_wildcards |
| 12 | + |
| 13 | + |
| 14 | +# Dummy implementation for canonical_tags |
| 15 | +class DummyCanonicalTags: |
| 16 | + @staticmethod |
| 17 | + def tags(x: str) -> str: |
| 18 | + if x == "*": |
| 19 | + return "@*@" |
| 20 | + return x |
| 21 | + |
| 22 | + |
| 23 | +# Patch canonical_tags so that tags("*") returns "@*@" |
| 24 | +@pytest.fixture(autouse=True) |
| 25 | +def patch_canonical_tags(monkeypatch): |
| 26 | + from datashuttle.configs import canonical_tags |
| 27 | + |
| 28 | + monkeypatch.setattr(canonical_tags, "tags", DummyCanonicalTags.tags) |
| 29 | + |
| 30 | + |
| 31 | +# Dummy implementation for search_sub_or_ses_level that simply performs globbing. |
| 32 | +def dummy_search_sub_or_ses_level( |
| 33 | + cfg, base_folder: Path, local_or_central: str, *args, search_str: str |
| 34 | +): |
| 35 | + pattern = os.path.join(str(base_folder), search_str) |
| 36 | + matches: List[str] = sorted(glob.glob(pattern)) |
| 37 | + return (matches,) |
| 38 | + |
| 39 | + |
| 40 | +# Patch search_sub_or_ses_level in the module where search_for_wildcards is defined. |
| 41 | +@pytest.fixture(autouse=True) |
| 42 | +def patch_search_sub_or_ses_level(monkeypatch): |
| 43 | + monkeypatch.setattr( |
| 44 | + "datashuttle.utils.folders.search_sub_or_ses_level", |
| 45 | + dummy_search_sub_or_ses_level, |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +# Dummy implementation for get_values_from_bids_formatted_name. |
| 50 | +def dummy_get_values_from_bids_formatted_name(name: str, key: str) -> dict: |
| 51 | + # Expect name format: "sub-01_date-YYYYMMDD" |
| 52 | + m = re.search(r"date-(\d{8})", name) |
| 53 | + if m: |
| 54 | + return {key: m.group(1)} |
| 55 | + return {} |
| 56 | + |
| 57 | + |
| 58 | +# Patch get_values_from_bids_formatted_name. |
| 59 | +@pytest.fixture(autouse=True) |
| 60 | +def patch_get_values_from_bids(monkeypatch): |
| 61 | + monkeypatch.setattr( |
| 62 | + "datashuttle.utils.utils.get_values_from_bids_formatted_name", |
| 63 | + dummy_get_values_from_bids_formatted_name, |
| 64 | + ) |
| 65 | + |
| 66 | + |
| 67 | +# Fixture to create a temporary directory with a simulated folder structure. |
| 68 | +@pytest.fixture |
| 69 | +def temp_project_dir() -> Path: # type: ignore |
| 70 | + temp_dir = Path(tempfile.mkdtemp()) |
| 71 | + # Create folders with names in the format "sub-01_date-YYYYMMDD" |
| 72 | + folder_dates = [ |
| 73 | + "20250305", |
| 74 | + "20250306", |
| 75 | + "20250307", |
| 76 | + "20250308", |
| 77 | + "20250309", |
| 78 | + "20250310", |
| 79 | + ] |
| 80 | + for date_str in folder_dates: |
| 81 | + folder_name = f"sub-01_date-{date_str}" |
| 82 | + os.mkdir(temp_dir / folder_name) |
| 83 | + yield temp_dir |
| 84 | + shutil.rmtree(temp_dir) |
| 85 | + |
| 86 | + |
| 87 | +def test_date_range_wildcard(temp_project_dir: Path): |
| 88 | + """ |
| 89 | + When given a date-range wildcard pattern like "sub-01_20250306@DATETO@20250309", |
| 90 | + only folders whose embedded date falls between 20250306 and 20250309 (inclusive) |
| 91 | + should be returned. |
| 92 | + """ |
| 93 | + |
| 94 | + class Configs: |
| 95 | + pass |
| 96 | + |
| 97 | + cfg = Configs() |
| 98 | + base_folder = temp_project_dir |
| 99 | + local_or_central = "local" |
| 100 | + pattern = "sub-01_20250306@DATETO@20250309" |
| 101 | + result = search_for_wildcards( |
| 102 | + cfg, base_folder, local_or_central, [pattern] |
| 103 | + ) |
| 104 | + |
| 105 | + # Extract the dates from the returned folder names. |
| 106 | + found_dates = set() |
| 107 | + for folder in result: |
| 108 | + basename = os.path.basename(folder) |
| 109 | + m = re.search(r"date-(\d{8})", basename) |
| 110 | + if m: |
| 111 | + found_dates.add(m.group(1)) |
| 112 | + |
| 113 | + expected_dates = {"20250306", "20250307", "20250308", "20250309"} |
| 114 | + assert found_dates == expected_dates |
| 115 | + |
| 116 | + |
| 117 | +def test_simple_wildcard(temp_project_dir: Path): |
| 118 | + """ |
| 119 | + When given a simple wildcard pattern like "sub-01_@*@", |
| 120 | + all folders should be returned. |
| 121 | + """ |
| 122 | + |
| 123 | + class Configs: |
| 124 | + pass |
| 125 | + |
| 126 | + cfg = Configs() |
| 127 | + base_folder = temp_project_dir |
| 128 | + local_or_central = "local" |
| 129 | + pattern = "sub-01_@*@" |
| 130 | + result = search_for_wildcards( |
| 131 | + cfg, base_folder, local_or_central, [pattern] |
| 132 | + ) |
| 133 | + # We expect six folders. |
| 134 | + assert len(result) == 6 |
0 commit comments