Skip to content

Commit a04316f

Browse files
committed
PR #828 simplify download_results unit test #744
DummyBackend already covers all the mocking needs
1 parent d90f06b commit a04316f

File tree

3 files changed

+31
-74
lines changed

3 files changed

+31
-74
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10-
### Added'
10+
### Added
11+
1112
- `MultiBackendJobManager`: add `download_results` option to enable/disable the automated download of job results once completed by the job manager ([#744](https://github.com/Open-EO/openeo-python-client/issues/744))
1213

1314
### Changed

openeo/extra/job_management/_manager.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def start_job(
158158
- get_job_metadata_path
159159
160160
:param download_results:
161-
Boolean to enable the automated download of job results once completed by the job manager.
161+
Whether to download job results automatically once the job is completed.
162162
163163
:param cancel_running_job_after:
164164
Optional temporal limit (in seconds) after which running jobs should be canceled

tests/extra/job_management/test_manager.py

Lines changed: 28 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
)
4040
from openeo.rest._testing import DummyBackend
4141
from openeo.rest.auth.testing import OidcMock
42-
from openeo.util import rfc3339
42+
from openeo.util import load_json, rfc3339
4343

4444

4545
def _job_id_from_year(process_graph) -> Union[str, None]:
@@ -898,76 +898,32 @@ def test_refresh_bearer_token_before_start(
898898
# we should have 2 additional token requests now
899899
assert len(oidc_mock.grant_request_history) == 4
900900

901-
def test_on_job_done_boolean_download(
902-
self, tmp_path, job_manager_root_dir, requests_mock
901+
@pytest.mark.parametrize(
902+
["download_results"],
903+
[
904+
(True,),
905+
(False,),
906+
],
907+
)
908+
def test_download_results_toggle(
909+
self, tmp_path, job_manager_root_dir, dummy_backend_foo, download_results, sleep_mock
903910
):
904-
"""Test that job results are only downloaded when download_results=True"""
911+
job_manager = MultiBackendJobManager(root_dir=job_manager_root_dir, download_results=download_results)
912+
job_manager.add_backend("foo", connection=dummy_backend_foo.connection)
905913

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"
914+
df = pd.DataFrame({"year": [2018, 2019]})
915+
job_db = CsvJobDatabase(tmp_path / "jobs.csv").initialize_from_df(df)
916+
run_stats = job_manager.run_jobs(job_db=job_db, start_job=self._create_year_job)
917+
assert run_stats == dirty_equals.IsPartialDict({"job finished": 2})
918+
919+
if download_results:
920+
assert (job_manager_root_dir / "job_job-2018/result.data").read_bytes() == DummyBackend.DEFAULT_RESULT
921+
assert load_json(job_manager_root_dir / "job_job-2018/job_job-2018.json") == dirty_equals.IsPartialDict(
922+
id="job-2018", status="finished"
923+
)
924+
assert (job_manager_root_dir / "job_job-2019/result.data").read_bytes() == DummyBackend.DEFAULT_RESULT
925+
assert load_json(job_manager_root_dir / "job_job-2019/job_job-2019.json") == dirty_equals.IsPartialDict(
926+
id="job-2019", status="finished"
927+
)
928+
else:
929+
assert not job_manager_root_dir.exists() or list(job_manager_root_dir.iterdir()) == []

0 commit comments

Comments
 (0)