From f6c29788c47eccb73d05799d1ab08cda083a3036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20M=C3=BCller?= Date: Thu, 5 Nov 2026 12:43:52 +0100 Subject: [PATCH] tests: upload resources to S3 in package_revise tests Uploading resources via CKAN is rejected since ckanext-dcor_schemas 1.3.0, before the checks these tests are about. Upload the resources to S3 first, so that the tests check that resources cannot be added to active datasets and that resource names must be unique again. Co-Authored-By: Claude Opus 5.5 --- tests/helper.py | 19 ++++++++++++++ tests/test_schemas_basic.py | 51 ++++++++++++++++++++----------------- 2 files changed, 47 insertions(+), 23 deletions(-) diff --git a/tests/helper.py b/tests/helper.py index 26393b7..eb1478a 100644 --- a/tests/helper.py +++ b/tests/helper.py @@ -5,6 +5,7 @@ import time from dcoraid.api import CKANAPI, create_dcor_dev_user, is_valid_api_key +from dcoraid.api import dataset, s3_api SERVER = "dcor-dev.mpl.mpg.de" @@ -71,3 +72,21 @@ def make_dataset_dict(circle=None, hint=None): "authors": "Guybrush Ulysses Threepwood", } return dataset_dict + + +def upload_resource_to_s3(api, dataset_id, path): + """Upload a file to S3 without adding it to the dataset + + Returns the resource ID, which can then be used to add the + resource to the dataset with `package_revise`. + """ + path = pathlib.Path(path) + org_id = dataset.get_organization_id_for_dataset(api=api, + dataset_id=dataset_id) + upload_info = api.get("resource_upload_s3_urls", + organization_id=org_id, + file_size=path.stat().st_size) + s3_api.upload_s3_presigned(path=path, + upload_urls=upload_info["upload_urls"], + complete_url=upload_info["complete_url"]) + return upload_info["resource_id"] diff --git a/tests/test_schemas_basic.py b/tests/test_schemas_basic.py index 081cff7..d50f269 100644 --- a/tests/test_schemas_basic.py +++ b/tests/test_schemas_basic.py @@ -7,7 +7,9 @@ import dcoraid from dcoraid.api import dataset -from helper import CKANAPI, get_api, make_dataset_dict +from helper import ( + CKANAPI, get_api, make_dataset_dict, upload_resource_to_s3 +) data_path = pathlib.Path(__file__).parent / "data" @@ -211,16 +213,18 @@ def test_package_revise_upload_not_allowed_active_dataset(): ) dataset.dataset_activate(dataset_id=data["id"], api=get_api()) - # now try to add a resource with package_revise - path = data_path / "calibration_beads_47.rtdc" - with path.open("rb") as fd: - req = requests.post( - api.api_url + "package_revise", - data={"match__id": data["id"], - "update__resources__extend": '[{"name":"peter.rtdc"}]', - }, - files=[("update__resources__-1__upload", ("peter.rtdc", fd))], - headers=api.headers) + # now try to add a resource (uploaded to S3) with package_revise + rid = upload_resource_to_s3(api=api, + dataset_id=data["id"], + path=data_path / "calibration_beads_47.rtdc") + req = requests.post( + api.api_url + "package_revise", + json={"match": {"id": data["id"]}, + "update__resources__extend": [{"id": rid, + "name": "peter.rtdc", + "s3_available": True}], + }, + headers=api.headers) assert not req.ok assert not req.json()["success"] error = req.json()["error"] @@ -275,18 +279,19 @@ def test_package_revise_upload_not_allowed_same_resource_name(): api=api, ) - # now try to add a resource with package_revise - path = data_path / "calibration_beads_47.rtdc" - with path.open("rb") as fd: - req = requests.post( - api.api_url + "package_revise", - data={"match__id": data["id"], - "update__resources__extend": - '[{"name":"calibration_beads_47.rtdc"}]', - }, - files=[("update__resources__-1__upload", - ("calibration_beads_47.rtdc", fd))], - headers=api.headers) + # now try to add a resource (uploaded to S3) with the same name + rid = upload_resource_to_s3(api=api, + dataset_id=data["id"], + path=data_path / "calibration_beads_47.rtdc") + req = requests.post( + api.api_url + "package_revise", + json={"match": {"id": data["id"]}, + "update__resources__extend": [ + {"id": rid, + "name": "calibration_beads_47.rtdc", + "s3_available": True}], + }, + headers=api.headers) assert not req.ok assert not req.json()["success"] assert req.reason == "CONFLICT"