|
39 | 39 | ) |
40 | 40 | from openeo.rest._testing import DummyBackend |
41 | 41 | from openeo.rest.auth.testing import OidcMock |
42 | | -from openeo.util import rfc3339 |
| 42 | +from openeo.util import load_json, rfc3339 |
43 | 43 |
|
44 | 44 |
|
45 | 45 | def _job_id_from_year(process_graph) -> Union[str, None]: |
@@ -898,76 +898,32 @@ def test_refresh_bearer_token_before_start( |
898 | 898 | # we should have 2 additional token requests now |
899 | 899 | assert len(oidc_mock.grant_request_history) == 4 |
900 | 900 |
|
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 |
903 | 910 | ): |
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) |
905 | 913 |
|
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