|
| 1 | +# tests/test_deposit_zenodo.py |
| 2 | +import json |
| 3 | +import tempfile |
| 4 | +from pathlib import Path |
| 5 | +from copy import deepcopy |
| 6 | +from unittest import TestCase |
| 7 | +from unittest.mock import patch |
| 8 | + |
| 9 | +from django.core.management import call_command |
| 10 | +from django.test import override_settings |
| 11 | +from works.models import Publication, Source |
| 12 | + |
| 13 | + |
| 14 | +class DepositZenodoTest(TestCase): |
| 15 | + def setUp(self): |
| 16 | + self._tmpdir = tempfile.TemporaryDirectory() |
| 17 | + self.project_root = Path(self._tmpdir.name) |
| 18 | + self.templates_dir = self.project_root / "publications" / "templates" |
| 19 | + self.cmds_dir = self.project_root / "publications" / "management" / "commands" |
| 20 | + self.data_dir = self.project_root / "data" |
| 21 | + self.templates_dir.mkdir(parents=True, exist_ok=True) |
| 22 | + self.cmds_dir.mkdir(parents=True, exist_ok=True) |
| 23 | + self.data_dir.mkdir(parents=True, exist_ok=True) |
| 24 | + |
| 25 | + # Minimal README so description→HTML works |
| 26 | + (self.data_dir / "README.md").write_text("# Title\n\nSome text.", encoding="utf-8") |
| 27 | + (self.data_dir / "optimap-main.zip").write_bytes(b"ZIP") |
| 28 | + # dynamic JSON with new related identifiers and version |
| 29 | + (self.data_dir / "zenodo_dynamic.json").write_text(json.dumps({ |
| 30 | + "title": "OPTIMAP FAIR Data Package (test)", |
| 31 | + "version": "v999", |
| 32 | + "related_identifiers": [ |
| 33 | + {"relation": "describes", "identifier": "https://optimap.science", "scheme": "url"} |
| 34 | + ] |
| 35 | + }), encoding="utf-8") |
| 36 | + |
| 37 | + # Fake dump files to upload |
| 38 | + (self.data_dir / "optimap_data_dump_20250101.geojson").write_text("{}", encoding="utf-8") |
| 39 | + (self.data_dir / "optimap_data_dump_20250101.gpkg").write_bytes(b"GPKG") |
| 40 | + |
| 41 | + # Minimal DB so import paths work |
| 42 | + Publication.objects.create(title="A", publicationDate="2010-10-10") |
| 43 | + Source.objects.create(name="OPTIMAP", url_field="https://optimap.science") |
| 44 | + |
| 45 | + # Command import – prefer deposit_zenodo; fallback to deploy_zenodo if needed |
| 46 | + import importlib |
| 47 | + try: |
| 48 | + self.deposit_mod = importlib.import_module( |
| 49 | + "works.management.commands.deposit_zenodo" |
| 50 | + ) |
| 51 | + except ModuleNotFoundError: |
| 52 | + self.deposit_mod = importlib.import_module( |
| 53 | + "works.management.commands.deploy_zenodo" |
| 54 | + ) |
| 55 | + |
| 56 | + class FakePath(Path): |
| 57 | + _flavour = Path(".")._flavour |
| 58 | + def resolve(self): |
| 59 | + return self |
| 60 | + self.FakePath = FakePath |
| 61 | + self.deposit_file = str(self.cmds_dir / "deposit_zenodo.py") |
| 62 | + |
| 63 | + def tearDown(self): |
| 64 | + self._tmpdir.cleanup() |
| 65 | + |
| 66 | + def test_deposit_merges_metadata_and_uses_zenodo_client_for_uploads(self): |
| 67 | + # Fake Zenodo deposition (existing metadata) |
| 68 | + existing = { |
| 69 | + "submitted": False, |
| 70 | + "state": "unsubmitted", |
| 71 | + "links": {"edit": "http://edit", "bucket": "http://bucket"}, |
| 72 | + "metadata": { |
| 73 | + "title": "Existing Title", |
| 74 | + "upload_type": "dataset", |
| 75 | + "publication_date": "2025-07-14", |
| 76 | + "creators": [{"name": "OPTIMAP"}], |
| 77 | + "keywords": ["Open Science"], |
| 78 | + "related_identifiers": [ |
| 79 | + {"relation": "isSupplementTo", "identifier": "https://old.example", "scheme": "url"} |
| 80 | + ], |
| 81 | + "language": "eng", |
| 82 | + "description": "<p>Old</p>", |
| 83 | + "version": "v1", |
| 84 | + }, |
| 85 | + } |
| 86 | + |
| 87 | + put_payload = {} |
| 88 | + |
| 89 | + def _fake_get(url, params=None, **kwargs): |
| 90 | + class R: |
| 91 | + status_code = 200 |
| 92 | + text = "ok" |
| 93 | + def json(self): |
| 94 | + # whatever object your test expects (e.g., deepcopy(existing)) |
| 95 | + return deepcopy(existing) |
| 96 | + def raise_for_status(self): |
| 97 | + return None |
| 98 | + return R() |
| 99 | + |
| 100 | + def _fake_post(url, params=None, json=None, **kwargs): |
| 101 | + class R: |
| 102 | + status_code = 200 |
| 103 | + text = "ok" |
| 104 | + def json(self): |
| 105 | + # return what your code reads from POST responses, if anything |
| 106 | + return {"links": {"bucket": "https://example-bucket"}} |
| 107 | + def raise_for_status(self): |
| 108 | + return None |
| 109 | + return R() |
| 110 | + |
| 111 | + def _fake_put(url, params=None, data=None, headers=None, **kwargs): |
| 112 | + class R: |
| 113 | + status_code = 200 |
| 114 | + text = "ok" |
| 115 | + def raise_for_status(self): |
| 116 | + return None |
| 117 | + return R() |
| 118 | + |
| 119 | + uploaded = {} |
| 120 | + |
| 121 | + # zenodo-client upload shim: capture files that would be uploaded |
| 122 | + def _fake_update_zenodo(deposition_id, paths, sandbox=True, access_token=None, publish=False): |
| 123 | + self.assertEqual(deposition_id, "123456") |
| 124 | + self.assertTrue(sandbox) |
| 125 | + self.assertEqual(access_token, "tok") |
| 126 | + names = {Path(p).name for p in paths} |
| 127 | + self.assertIn("README.md", names) |
| 128 | + self.assertIn("optimap-main.zip", names) |
| 129 | + self.assertTrue(any(n.endswith(".geojson") for n in names)) |
| 130 | + self.assertTrue(any(n.endswith(".gpkg") for n in names)) |
| 131 | + uploaded["paths"] = [str(p) for p in paths] |
| 132 | + class R: |
| 133 | + def json(self): return {"links": {"html": f"https://sandbox.zenodo.org/deposit/{deposition_id}"}} |
| 134 | + return R() |
| 135 | + |
| 136 | + with patch.object(self.deposit_mod, "__file__", new=self.deposit_file), \ |
| 137 | + patch.object(self.deposit_mod, "Path", self.FakePath), \ |
| 138 | + patch.object(self.deposit_mod.requests, "get", _fake_get), \ |
| 139 | + patch.object(self.deposit_mod.requests, "put", _fake_put), \ |
| 140 | + patch.object(self.deposit_mod, "update_zenodo", _fake_update_zenodo), \ |
| 141 | + patch.object(self.deposit_mod, "_markdown_to_html", lambda s: "<p>HTML</p>"), \ |
| 142 | + override_settings(ZENODO_UPLOADS_ENABLED=True): |
| 143 | + |
| 144 | + call_command( |
| 145 | + "deposit_zenodo", |
| 146 | + "--deposition-id", "123456", |
| 147 | + ) |
| 148 | + |
| 149 | + # Merged metadata: required fields preserved, description/version updated, related merged |
| 150 | + merged = put_payload["metadata"] |
| 151 | + self.assertEqual(merged["title"], "Existing Title") |
| 152 | + self.assertEqual(merged["upload_type"], "dataset") |
| 153 | + self.assertEqual(merged["publication_date"], "2025-07-14") |
| 154 | + self.assertEqual(merged["creators"], [{"name": "OPTIMAP"}]) |
| 155 | + |
| 156 | + self.assertIn("description", merged) |
| 157 | + self.assertTrue(merged["description"].startswith("<p")) # from markdown->HTML |
| 158 | + |
| 159 | + self.assertIsInstance(merged.get("version"), str) |
| 160 | + rel = {(d["identifier"], d["relation"]) for d in merged.get("related_identifiers", [])} |
| 161 | + self.assertIn(("https://old.example", "isSupplementTo"), rel) |
| 162 | + self.assertIn(("https://optimap.science", "describes"), rel) |
| 163 | + |
| 164 | + # Uploader called with expected files |
| 165 | + self.assertIn("paths", uploaded) |
| 166 | + self.assertGreater(len(uploaded["paths"]), 0) |
0 commit comments