Skip to content

Commit 937c3bc

Browse files
committed
rephrase and include a unit test
1 parent 83f0945 commit 937c3bc

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

openeo/extra/job_management/_manager.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,8 @@ def start_job(
157157
- get_error_log_path
158158
- get_job_metadata_path
159159
160-
:param auto_download_results:
161-
Optional disable the automated download of job results.
160+
:param download_results:
161+
Boolean to enable the automated download of job results once completed by the job manager.
162162
163163
:param cancel_running_job_after:
164164
Optional temporal limit (in seconds) after which running jobs should be canceled
@@ -197,7 +197,7 @@ def __init__(
197197
poll_sleep: int = 60,
198198
root_dir: Optional[Union[str, Path]] = ".",
199199
*,
200-
auto_download_results: bool = True,
200+
download_results: bool = True,
201201
cancel_running_job_after: Optional[int] = None,
202202
):
203203
"""Create a MultiBackendJobManager."""
@@ -209,7 +209,7 @@ def __init__(
209209
# An explicit None or "" should also default to "."
210210
self._root_dir = Path(root_dir or ".")
211211

212-
self.auto_download_results = auto_download_results
212+
self._download_results = download_results
213213

214214
self._cancel_running_job_after = (
215215
datetime.timedelta(seconds=cancel_running_job_after) if cancel_running_job_after is not None else None
@@ -719,7 +719,7 @@ def on_job_done(self, job: BatchJob, row):
719719
:param row: DataFrame row containing the job's metadata.
720720
"""
721721
# TODO: param `row` is never accessed in this method. Remove it? Is this intended for future use?
722-
if self.auto_download_results:
722+
if self._download_results:
723723
job_metadata = job.describe()
724724
job_dir = self.get_job_dir(job.job_id)
725725
metadata_path = self.get_job_metadata_path(job.job_id)

tests/extra/job_management/test_manager.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,3 +897,77 @@ def test_refresh_bearer_token_before_start(
897897
# Because of proactive+throttled token refreshing,
898898
# we should have 2 additional token requests now
899899
assert len(oidc_mock.grant_request_history) == 4
900+
901+
def test_on_job_done_boolean_download(
902+
self, tmp_path, job_manager_root_dir, requests_mock
903+
):
904+
"""Test that job results are only downloaded when download_results=True"""
905+
906+
# Setup backend and connection
907+
backend = "http://foo.test"
908+
job_id = "job-test-123"
909+
910+
requests_mock.get(backend, json={"api_version": "1.1.0"})
911+
requests_mock.get(f"{backend}/jobs/{job_id}", json={
912+
"id": job_id,
913+
"status": "finished",
914+
"title": "Test Job"
915+
})
916+
requests_mock.get(f"{backend}/jobs/{job_id}/results", json={
917+
"assets": {
918+
"result.tif": {
919+
"href": f"{backend}/jobs/{job_id}/results/result.tif",
920+
"type": "image/tiff"
921+
}
922+
}
923+
})
924+
# Mock the actual file download
925+
requests_mock.head(f"{backend}/jobs/{job_id}/results/result.tif", headers={"Content-Length": "100"})
926+
requests_mock.get(f"{backend}/jobs/{job_id}/results/result.tif", content=b"fake_tiff_data")
927+
928+
# Test with auto_download_results=False
929+
manager_no_download = MultiBackendJobManager(
930+
root_dir=job_manager_root_dir,
931+
download_results=False
932+
)
933+
connection = openeo.connect(backend)
934+
manager_no_download.add_backend("foo", connection=connection)
935+
936+
df = pd.DataFrame({"year": [2020]})
937+
job = BatchJob(job_id=job_id, connection=connection)
938+
row = df.loc[0]
939+
940+
# Call on_job_done
941+
manager_no_download.on_job_done(job=job, row=row)
942+
943+
# Verify no files were downloaded and no directory was created
944+
job_dir = manager_no_download.get_job_dir(job_id)
945+
metadata_path = manager_no_download.get_job_metadata_path(job_id)
946+
947+
assert not job_dir.exists(), "Job directory should not exist when auto_download_results=False"
948+
assert not metadata_path.exists(), "Metadata file should not exist when auto_download_results=False"
949+
950+
# Now test with auto_download_results=True
951+
manager_with_download = MultiBackendJobManager(
952+
root_dir=job_manager_root_dir,
953+
download_results=True
954+
)
955+
manager_with_download.add_backend("foo", connection=connection)
956+
957+
# Call on_job_done
958+
manager_with_download.on_job_done(job=job, row=row)
959+
960+
# Verify files were downloaded and directory was created
961+
assert job_dir.exists(), "Job directory should exist when auto_download_results=True"
962+
assert metadata_path.exists(), "Metadata file should exist when auto_download_results=True"
963+
964+
# Verify metadata content
965+
with metadata_path.open("r", encoding="utf-8") as f:
966+
metadata = json.load(f)
967+
assert metadata["id"] == job_id
968+
assert metadata["status"] == "finished"
969+
970+
# Verify result file was downloaded
971+
result_file = job_dir / "result.tif"
972+
assert result_file.exists(), "Result file should be downloaded"
973+
assert result_file.read_bytes() == b"fake_tiff_data"

0 commit comments

Comments
 (0)