|
| 1 | +"""Tests for delete_orphaned functionality.""" |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | + |
| 6 | +from icloudpd.base import delete_orphaned |
| 7 | +from icloudpd.manifest import ManifestDB |
| 8 | + |
| 9 | + |
| 10 | +def _setup_manifest_with_files( |
| 11 | + tmpdir: str, |
| 12 | + entries: list[dict[str, str]], |
| 13 | +) -> ManifestDB: |
| 14 | + """Create a manifest DB with entries and corresponding files on disk.""" |
| 15 | + manifest = ManifestDB(tmpdir) |
| 16 | + manifest.open() |
| 17 | + for entry in entries: |
| 18 | + asset_id = entry["asset_id"] |
| 19 | + zone_id = entry.get("zone_id", "PrimarySync") |
| 20 | + local_path = entry["local_path"] |
| 21 | + asset_resource = entry.get("asset_resource", "resOriginal") |
| 22 | + # Create the file on disk |
| 23 | + full_path = os.path.join(tmpdir, local_path) |
| 24 | + os.makedirs(os.path.dirname(full_path), exist_ok=True) |
| 25 | + with open(full_path, "w") as f: |
| 26 | + f.write("test content") |
| 27 | + # Create XMP sidecar |
| 28 | + with open(full_path + ".xmp", "w") as f: |
| 29 | + f.write("<xmp>test</xmp>") |
| 30 | + # Add to manifest |
| 31 | + manifest.upsert( |
| 32 | + asset_id=asset_id, |
| 33 | + zone_id=zone_id, |
| 34 | + local_path=local_path, |
| 35 | + version_size=1, |
| 36 | + asset_resource=asset_resource, |
| 37 | + ) |
| 38 | + manifest.flush() |
| 39 | + return manifest |
| 40 | + |
| 41 | + |
| 42 | +class TestFindOrphaned: |
| 43 | + """Tests for ManifestDB.find_orphaned().""" |
| 44 | + |
| 45 | + def test_no_orphans(self, tmp_path: object) -> None: |
| 46 | + tmpdir = str(tmp_path) |
| 47 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 48 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 49 | + {"asset_id": "A2", "local_path": "2024-01/photo2.HEIC"}, |
| 50 | + ]) |
| 51 | + seen = {"A1", "A2"} |
| 52 | + orphans = manifest.find_orphaned(seen) |
| 53 | + assert len(orphans) == 0 |
| 54 | + manifest.close() |
| 55 | + |
| 56 | + def test_all_orphaned(self, tmp_path: object) -> None: |
| 57 | + tmpdir = str(tmp_path) |
| 58 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 59 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 60 | + {"asset_id": "A2", "local_path": "2024-01/photo2.HEIC"}, |
| 61 | + ]) |
| 62 | + seen: set[str] = set() |
| 63 | + orphans = manifest.find_orphaned(seen) |
| 64 | + assert len(orphans) == 2 |
| 65 | + assert {o.asset_id for o in orphans} == {"A1", "A2"} |
| 66 | + manifest.close() |
| 67 | + |
| 68 | + def test_partial_orphan(self, tmp_path: object) -> None: |
| 69 | + tmpdir = str(tmp_path) |
| 70 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 71 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 72 | + {"asset_id": "A2", "local_path": "2024-01/photo2.HEIC"}, |
| 73 | + {"asset_id": "A3", "local_path": "2024-01/photo3.HEIC"}, |
| 74 | + ]) |
| 75 | + seen = {"A1", "A3"} |
| 76 | + orphans = manifest.find_orphaned(seen) |
| 77 | + assert len(orphans) == 1 |
| 78 | + assert orphans[0].asset_id == "A2" |
| 79 | + manifest.close() |
| 80 | + |
| 81 | + def test_multi_resource_orphan(self, tmp_path: object) -> None: |
| 82 | + """An asset with multiple resources (HEIC + MOV) should return all rows.""" |
| 83 | + tmpdir = str(tmp_path) |
| 84 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 85 | + {"asset_id": "A1", "local_path": "2024-01/IMG_001.HEIC", "asset_resource": "resOriginal"}, |
| 86 | + {"asset_id": "A1", "local_path": "2024-01/IMG_001_HEVC.MOV", "asset_resource": "resOriginalVidCompl"}, |
| 87 | + {"asset_id": "A2", "local_path": "2024-01/IMG_002.HEIC", "asset_resource": "resOriginal"}, |
| 88 | + ]) |
| 89 | + seen = {"A2"} |
| 90 | + orphans = manifest.find_orphaned(seen) |
| 91 | + assert len(orphans) == 2 |
| 92 | + assert all(o.asset_id == "A1" for o in orphans) |
| 93 | + manifest.close() |
| 94 | + |
| 95 | + def test_empty_manifest(self, tmp_path: object) -> None: |
| 96 | + tmpdir = str(tmp_path) |
| 97 | + manifest = ManifestDB(tmpdir) |
| 98 | + manifest.open() |
| 99 | + orphans = manifest.find_orphaned({"A1", "A2"}) |
| 100 | + assert len(orphans) == 0 |
| 101 | + manifest.close() |
| 102 | + |
| 103 | + |
| 104 | +class TestDeleteOrphaned: |
| 105 | + """Tests for the delete_orphaned() function.""" |
| 106 | + |
| 107 | + def test_warning_mode_no_flag(self, tmp_path: object) -> None: |
| 108 | + """Without --delete-orphaned, logs warning but doesn't delete.""" |
| 109 | + tmpdir = str(tmp_path) |
| 110 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 111 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 112 | + ]) |
| 113 | + logger = logging.getLogger("test") |
| 114 | + # Call without delete flag |
| 115 | + delete_orphaned(logger, manifest, tmpdir, set(), delete=False, dry_run=False) |
| 116 | + # File should still exist |
| 117 | + assert os.path.exists(os.path.join(tmpdir, "2024-01/photo1.HEIC")) |
| 118 | + assert os.path.exists(os.path.join(tmpdir, "2024-01/photo1.HEIC.xmp")) |
| 119 | + # Manifest should still have the entry |
| 120 | + assert manifest.count() == 1 |
| 121 | + manifest.close() |
| 122 | + |
| 123 | + def test_delete_mode(self, tmp_path: object) -> None: |
| 124 | + """With --delete-orphaned, files and manifest entries are removed.""" |
| 125 | + tmpdir = str(tmp_path) |
| 126 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 127 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 128 | + {"asset_id": "A2", "local_path": "2024-01/photo2.HEIC"}, |
| 129 | + ]) |
| 130 | + logger = logging.getLogger("test") |
| 131 | + delete_orphaned(logger, manifest, tmpdir, {"A2"}, delete=True, dry_run=False) |
| 132 | + # A1 should be deleted |
| 133 | + assert not os.path.exists(os.path.join(tmpdir, "2024-01/photo1.HEIC")) |
| 134 | + assert not os.path.exists(os.path.join(tmpdir, "2024-01/photo1.HEIC.xmp")) |
| 135 | + # A2 should remain |
| 136 | + assert os.path.exists(os.path.join(tmpdir, "2024-01/photo2.HEIC")) |
| 137 | + assert os.path.exists(os.path.join(tmpdir, "2024-01/photo2.HEIC.xmp")) |
| 138 | + # Manifest should only have A2 |
| 139 | + assert manifest.count() == 1 |
| 140 | + manifest.close() |
| 141 | + |
| 142 | + def test_dry_run_skips_deletion(self, tmp_path: object) -> None: |
| 143 | + """Dry run logs but doesn't delete files or manifest entries.""" |
| 144 | + tmpdir = str(tmp_path) |
| 145 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 146 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 147 | + ]) |
| 148 | + logger = logging.getLogger("test") |
| 149 | + delete_orphaned(logger, manifest, tmpdir, set(), delete=True, dry_run=True) |
| 150 | + # File should still exist |
| 151 | + assert os.path.exists(os.path.join(tmpdir, "2024-01/photo1.HEIC")) |
| 152 | + assert os.path.exists(os.path.join(tmpdir, "2024-01/photo1.HEIC.xmp")) |
| 153 | + # Manifest should still have the entry |
| 154 | + assert manifest.count() == 1 |
| 155 | + manifest.close() |
| 156 | + |
| 157 | + def test_multi_resource_cleanup(self, tmp_path: object) -> None: |
| 158 | + """Deleting orphaned asset removes all resources (HEIC + MOV + XMPs).""" |
| 159 | + tmpdir = str(tmp_path) |
| 160 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 161 | + {"asset_id": "A1", "local_path": "2024-01/IMG.HEIC", "asset_resource": "resOriginal"}, |
| 162 | + {"asset_id": "A1", "local_path": "2024-01/IMG_HEVC.MOV", "asset_resource": "resOriginalVidCompl"}, |
| 163 | + ]) |
| 164 | + logger = logging.getLogger("test") |
| 165 | + delete_orphaned(logger, manifest, tmpdir, set(), delete=True, dry_run=False) |
| 166 | + assert not os.path.exists(os.path.join(tmpdir, "2024-01/IMG.HEIC")) |
| 167 | + assert not os.path.exists(os.path.join(tmpdir, "2024-01/IMG.HEIC.xmp")) |
| 168 | + assert not os.path.exists(os.path.join(tmpdir, "2024-01/IMG_HEVC.MOV")) |
| 169 | + assert not os.path.exists(os.path.join(tmpdir, "2024-01/IMG_HEVC.MOV.xmp")) |
| 170 | + assert manifest.count() == 0 |
| 171 | + manifest.close() |
| 172 | + |
| 173 | + def test_no_orphans_no_action(self, tmp_path: object) -> None: |
| 174 | + """When all assets are seen, no warning or deletion.""" |
| 175 | + tmpdir = str(tmp_path) |
| 176 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 177 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 178 | + ]) |
| 179 | + logger = logging.getLogger("test") |
| 180 | + delete_orphaned(logger, manifest, tmpdir, {"A1"}, delete=True, dry_run=False) |
| 181 | + assert os.path.exists(os.path.join(tmpdir, "2024-01/photo1.HEIC")) |
| 182 | + assert manifest.count() == 1 |
| 183 | + manifest.close() |
| 184 | + |
| 185 | + def test_file_already_missing(self, tmp_path: object) -> None: |
| 186 | + """Orphan in manifest but file already deleted from disk.""" |
| 187 | + tmpdir = str(tmp_path) |
| 188 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 189 | + {"asset_id": "A1", "local_path": "2024-01/photo1.HEIC"}, |
| 190 | + ]) |
| 191 | + # Remove the file manually |
| 192 | + os.remove(os.path.join(tmpdir, "2024-01/photo1.HEIC")) |
| 193 | + os.remove(os.path.join(tmpdir, "2024-01/photo1.HEIC.xmp")) |
| 194 | + logger = logging.getLogger("test") |
| 195 | + # Should not crash, just remove manifest entry |
| 196 | + delete_orphaned(logger, manifest, tmpdir, set(), delete=True, dry_run=False) |
| 197 | + assert manifest.count() == 0 |
| 198 | + manifest.close() |
| 199 | + |
| 200 | + def test_dedup_suffix_files(self, tmp_path: object) -> None: |
| 201 | + """Orphan with dedup suffix in path is handled correctly.""" |
| 202 | + tmpdir = str(tmp_path) |
| 203 | + manifest = _setup_manifest_with_files(tmpdir, [ |
| 204 | + {"asset_id": "A1", "local_path": "2024-01/IMG_001_aB3x.HEIC"}, |
| 205 | + ]) |
| 206 | + logger = logging.getLogger("test") |
| 207 | + delete_orphaned(logger, manifest, tmpdir, set(), delete=True, dry_run=False) |
| 208 | + assert not os.path.exists(os.path.join(tmpdir, "2024-01/IMG_001_aB3x.HEIC")) |
| 209 | + assert manifest.count() == 0 |
| 210 | + manifest.close() |
0 commit comments