Skip to content

Commit 0f8fea7

Browse files
committed
fix tests
1 parent 942255a commit 0f8fea7

2 files changed

Lines changed: 55 additions & 26 deletions

File tree

deep_code/tests/tools/test_publish.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -126,11 +126,22 @@ def test_environment_repo_selection(self, mock_gp):
126126
== "open-science-catalog-metadata-testing"
127127
)
128128

129+
@patch.object(Publisher, "_write_stac_catalog_to_s3")
129130
@patch.object(Publisher, "publish_dataset", return_value={"a": {}})
130131
@patch.object(
131132
Publisher, "generate_workflow_experiment_records", return_value={"b": {}}
132133
)
133-
def test_publish_mode_routing(self, mock_wf, mock_ds):
134+
def test_publish_mode_routing(self, mock_wf, mock_ds, mock_s3):
135+
mock_generator = MagicMock()
136+
mock_generator.build_zarr_stac_catalog_file_dict.return_value = {}
137+
self.publisher._last_generator = mock_generator
138+
self.publisher.dataset_config = {
139+
"stac_catalog_s3_root": "s3://bucket/stac/",
140+
"collection_id": "test-collection",
141+
"dataset_id": "test-dataset",
142+
}
143+
self.publisher.gh_publisher.publish_files.return_value = "PR_URL"
144+
134145
# dataset only
135146
self.publisher.publish(write_to_file=True, mode="dataset")
136147
mock_ds.assert_called()
@@ -142,28 +153,41 @@ def test_publish_mode_routing(self, mock_wf, mock_ds):
142153
mock_ds.assert_not_called()
143154
mock_wf.assert_called()
144155

156+
@patch.object(Publisher, "_write_stac_catalog_to_s3")
145157
@patch.object(Publisher, "generate_workflow_experiment_records", return_value={})
146158
@patch.object(Publisher, "publish_dataset", return_value={})
147159
def test_publish_nothing_to_publish_raises(
148-
self, mock_publish_dataset, mock_generate_workflow_experiment_records
160+
self, mock_publish_dataset, mock_generate_workflow_experiment_records, mock_s3
149161
):
162+
mock_generator = MagicMock()
163+
mock_generator.build_zarr_stac_catalog_file_dict.return_value = {}
164+
self.publisher._last_generator = mock_generator
165+
self.publisher.dataset_config = {"stac_catalog_s3_root": "s3://bucket/stac/"}
166+
150167
with pytest.raises(ValueError):
151168
self.publisher.publish(write_to_file=False, mode="dataset")
152169
mock_publish_dataset.assert_called_once()
153170
mock_generate_workflow_experiment_records.assert_not_called()
154171

172+
@patch.object(Publisher, "_write_stac_catalog_to_s3")
155173
@patch.object(Publisher, "publish_dataset", return_value={"x": {}})
156174
@patch.object(
157175
Publisher, "generate_workflow_experiment_records", return_value={"y": {}}
158176
)
159-
def test_publish_builds_pr_params(self, mock_wf, mock_ds):
177+
def test_publish_builds_pr_params(self, mock_wf, mock_ds, mock_s3):
160178
# Make PR creation return a fixed URL
161179
self.publisher.gh_publisher.publish_files.return_value = "PR_URL"
162180

163181
# Provide IDs for commit/PR labels
164182
self.publisher.collection_id = "col"
165183
self.publisher.workflow_id = "wf"
166184

185+
# _last_generator is set by publish_dataset; since that's mocked, stub it
186+
mock_generator = MagicMock()
187+
mock_generator.build_zarr_stac_catalog_file_dict.return_value = {}
188+
self.publisher._last_generator = mock_generator
189+
self.publisher.dataset_config = {"stac_catalog_s3_root": "s3://bucket/stac/"}
190+
167191
url = self.publisher.publish(write_to_file=False, mode="all")
168192
assert url == "PR_URL"
169193

@@ -309,6 +333,7 @@ def test_publish_dataset_creates_project_collection_when_missing(
309333
"dataset_id": "test-dataset",
310334
"collection_id": "test-collection",
311335
"license_type": "CC-BY-4.0",
336+
"stac_catalog_s3_root": "s3://bucket/stac/test-collection/",
312337
}
313338
self.publisher.collection_id = "test-collection"
314339

@@ -343,6 +368,7 @@ def test_publish_dataset_updates_project_collection_when_exists(
343368
"dataset_id": "test-dataset",
344369
"collection_id": "test-collection",
345370
"license_type": "CC-BY-4.0",
371+
"stac_catalog_s3_root": "s3://bucket/stac/test-collection/",
346372
}
347373
self.publisher.collection_id = "test-collection"
348374

@@ -359,20 +385,15 @@ def test_publish_dataset_updates_project_collection_when_exists(
359385
update_methods = [call.args[2] for call in mock_update.call_args_list]
360386
self.assertIn(mock_gen.update_deepesdl_collection, update_methods)
361387

362-
@patch.object(Publisher, "publish_dataset", return_value={"github_file.json": {}})
363-
def test_publish_skips_zarr_stac_when_not_configured(self, mock_publish_ds):
364-
# No stac_catalog_s3_root in config
388+
def test_publish_dataset_raises_when_stac_root_missing(self):
389+
# stac_catalog_s3_root is mandatory; publish_dataset must raise ValueError
365390
self.publisher.dataset_config = {
366391
"collection_id": "test-collection",
367392
"dataset_id": "test-dataset",
393+
"license_type": "CC-BY-4.0",
368394
}
369-
self.publisher.gh_publisher.publish_files.return_value = "PR_URL"
370-
371-
with patch.object(
372-
self.publisher, "_write_stac_catalog_to_s3"
373-
) as mock_write:
374-
self.publisher.publish(mode="dataset")
375-
mock_write.assert_not_called()
395+
with pytest.raises(ValueError, match="stac_catalog_s3_root"):
396+
self.publisher.publish_dataset(write_to_file=False)
376397

377398

378399
class TestParseGithubNotebookUrl:

deep_code/tests/utils/test_dataset_stac_generator.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -509,28 +509,36 @@ def test_build_zarr_stac_catalog_file_dict_content(self):
509509
self.assertIn("zarr-consolidated-metadata", item_dict["assets"])
510510

511511
def test_build_dataset_stac_collection_adds_s3_catalog_via_link(self):
512-
"""A 'via' link to the S3 catalog is added when stac_catalog_s3_root is provided.
512+
"""A 'via' link (STAC browser) and a 'child' link (HTTPS catalog) are added
513+
when stac_catalog_s3_root is provided.
513514
514-
rel='via' is used (not 'child') because the OSC validator requires every
515-
'child' link to resolve to a file inside the metadata repository.
515+
The OSC convention uses:
516+
- rel='via' → STAC browser URL
517+
- rel='child' → direct HTTPS catalog URL (s3:// converted to HTTPS)
516518
"""
517519
s3_root = "s3://test-bucket/stac/my-collection/"
518520
collection = self.generator.build_dataset_stac_collection(
519521
mode="dataset", stac_catalog_s3_root=s3_root
520522
)
521-
s3_via = next(
522-
(
523-
lnk
524-
for lnk in collection.links
525-
if lnk.rel == "via" and "catalog.json" in str(lnk.target)
526-
),
523+
https_catalog = "https://test-bucket.s3.amazonaws.com/stac/my-collection/catalog.json"
524+
stac_browser_href = (
525+
"https://opensciencedata.esa.int/stac-browser/#/external/"
526+
+ https_catalog.replace("https://", "")
527+
)
528+
529+
via_link = next(
530+
(lnk for lnk in collection.links if lnk.rel == "via" and "stac-browser" in str(lnk.target)),
527531
None,
528532
)
529-
self.assertIsNotNone(s3_via, "Expected a 'via' link pointing to S3 catalog")
530-
self.assertEqual(
531-
s3_via.target,
532-
"s3://test-bucket/stac/my-collection/catalog.json",
533+
self.assertIsNotNone(via_link, "Expected a 'via' STAC browser link")
534+
self.assertEqual(via_link.target, stac_browser_href)
535+
536+
child_link = next(
537+
(lnk for lnk in collection.links if lnk.rel == "child" and "catalog.json" in str(lnk.target)),
538+
None,
533539
)
540+
self.assertIsNotNone(child_link, "Expected a 'child' HTTPS catalog link")
541+
self.assertEqual(child_link.target, https_catalog)
534542

535543
def test_build_dataset_stac_collection_no_s3_via_link_by_default(self):
536544
"""No S3 catalog 'via' link is added when stac_catalog_s3_root is absent."""

0 commit comments

Comments
 (0)