correct import in test file

This commit is contained in:
Daniel Roth 2026-06-15 12:21:32 +00:00
parent a6050fc1c7
commit b9cbea367d

View file

@ -1,10 +1,12 @@
from typing import Any
from unittest.mock import MagicMock, call, patch
from unittest.mock import MagicMock
import pytest
import scripts.rename_sharepoint_files as module
from scripts.rename_sharepoint_files import build_canonical_filename, process_folder
from orchestration.sharepoint_renamer_orchestrator import (
SharepointRenamerOrchestrator,
build_canonical_filename,
)
def _make_file(name: str, item_id: str = "id-1") -> dict[str, Any]:
@ -19,6 +21,12 @@ def _make_package(name: str) -> dict[str, Any]:
return {"name": name, "package": {}}
def _make_orchestrator(sp: MagicMock) -> SharepointRenamerOrchestrator:
orchestrator = SharepointRenamerOrchestrator.__new__(SharepointRenamerOrchestrator)
orchestrator._sp_client = sp
return orchestrator
# ---------------------------------------------------------------------------
# build_canonical_filename
# ---------------------------------------------------------------------------
@ -39,7 +47,7 @@ def test_no_prefix_still_canonical() -> None:
# ---------------------------------------------------------------------------
# process_folder — files only at root level
# _process_folder — files only at root level
# ---------------------------------------------------------------------------
@ -52,8 +60,7 @@ def test_renames_top_level_files(caplog: pytest.LogCaptureFixture) -> None:
]
}
with patch.object(module, "DRY_RUN", False):
process_folder(sp, "some/path", "100", "1 High St", "AB1 2CD")
_make_orchestrator(sp)._process_folder("some/path", "100", "1 High St", "AB1 2CD")
assert sp.rename_file.call_count == 2
sp.rename_file.assert_any_call("id-1", "100_1 High St AB1 2CD_Survey.pdf")
@ -61,7 +68,7 @@ def test_renames_top_level_files(caplog: pytest.LogCaptureFixture) -> None:
# ---------------------------------------------------------------------------
# process_folder — recursive two-level hierarchy
# _process_folder — recursive two-level hierarchy
# ---------------------------------------------------------------------------
@ -84,8 +91,7 @@ def test_recurses_into_subfolders_and_renames_all_files() -> None:
root_contents if path == "base/path" else suba_contents
)
with patch.object(module, "DRY_RUN", False):
process_folder(sp, "base/path", "200", "2 Main Rd", "XY9 8ZW")
_make_orchestrator(sp)._process_folder("base/path", "200", "2 Main Rd", "XY9 8ZW")
assert sp.rename_file.call_count == 2
sp.rename_file.assert_any_call("root-file", "200_2 Main Rd XY9 8ZW_Root.pdf")
@ -95,25 +101,22 @@ def test_recurses_into_subfolders_and_renames_all_files() -> None:
# ---------------------------------------------------------------------------
# process_folder — non-file, non-folder items are skipped
# _process_folder — non-file, non-folder items are skipped
# ---------------------------------------------------------------------------
def test_ignores_package_items() -> None:
sp = MagicMock()
sp.get_folders_in_path.return_value = {
"value": [_make_package("Notebook")]
}
sp.get_folders_in_path.return_value = {"value": [_make_package("Notebook")]}
with patch.object(module, "DRY_RUN", False):
process_folder(sp, "some/path", "300", "3 Oak Ave", "ZZ1 1ZZ")
_make_orchestrator(sp)._process_folder("some/path", "300", "3 Oak Ave", "ZZ1 1ZZ")
sp.rename_file.assert_not_called()
assert sp.get_folders_in_path.call_count == 1
# ---------------------------------------------------------------------------
# process_folder — missing folder
# _process_folder — missing folder
# ---------------------------------------------------------------------------
@ -121,31 +124,14 @@ def test_missing_folder_logs_warning_and_returns(caplog: pytest.LogCaptureFixtur
sp = MagicMock()
sp.get_folders_in_path.side_effect = ValueError("not found")
with patch.object(module, "DRY_RUN", False):
process_folder(sp, "missing/path", "400", "4 Elm St", "AA2 2BB")
_make_orchestrator(sp)._process_folder("missing/path", "400", "4 Elm St", "AA2 2BB")
sp.rename_file.assert_not_called()
assert any("Missing folder" in r.message and "400" in r.message for r in caplog.records)
# ---------------------------------------------------------------------------
# process_folder — dry run
# ---------------------------------------------------------------------------
def test_dry_run_logs_without_renaming(caplog: pytest.LogCaptureFixture) -> None:
sp = MagicMock()
sp.get_folders_in_path.return_value = {"value": [_make_file("Doc.pdf", "id-x")]}
with patch.object(module, "DRY_RUN", True):
process_folder(sp, "some/path", "500", "5 Pine Ln", "BB3 3CC")
sp.rename_file.assert_not_called()
assert any("[DRY RUN]" in r.message for r in caplog.records)
# ---------------------------------------------------------------------------
# process_folder — already-canonical files are skipped
# _process_folder — already-canonical files are skipped
# ---------------------------------------------------------------------------
@ -155,7 +141,6 @@ def test_skips_already_canonical_files() -> None:
"value": [_make_file("500_Pine Ln BB3 3CC_Doc.pdf", "id-y")]
}
with patch.object(module, "DRY_RUN", False):
process_folder(sp, "some/path", "500", "5 Pine Ln", "BB3 3CC")
_make_orchestrator(sp)._process_folder("some/path", "500", "5 Pine Ln", "BB3 3CC")
sp.rename_file.assert_not_called()