Skip to content

Commit

Permalink
fix duplicate param and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Jan 15, 2025
1 parent 693d0ff commit d682149
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
14 changes: 10 additions & 4 deletions pyDataverse/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Dataverse API wrapper for all it's API's."""

from io import StringIO
import json
from typing import Any, Dict, Optional
import httpx
Expand Down Expand Up @@ -275,7 +276,9 @@ def post_request(
**request_params,
)

def put_request(self, url, data=None, auth=DEPRECATION_GUARD, params=None):
def put_request(
self, url, data=None, auth=DEPRECATION_GUARD, params=None, files=None
):
"""Make a PUT request.
Parameters
Expand Down Expand Up @@ -326,7 +329,6 @@ def put_request(self, url, data=None, auth=DEPRECATION_GUARD, params=None):
return self._sync_request(
method=httpx.put,
url=url,
json=data,
headers=headers,
params=params,
**request_params,
Expand All @@ -335,7 +337,6 @@ def put_request(self, url, data=None, auth=DEPRECATION_GUARD, params=None):
return self._async_request(
method=self.client.put,
url=url,
json=data,
headers=headers,
params=params,
**request_params,
Expand Down Expand Up @@ -1545,7 +1546,12 @@ def edit_dataset_metadata(
self.base_url_api_native, identifier
)
params = {"replace": True} if replace else {}
resp = self.put_request(url, metadata, auth, params)
resp = self.put_request(
url=url,
data=metadata,
auth=auth,
params=params,
)

if resp.status_code == 401:
error_msg = resp.json()["message"]
Expand Down
52 changes: 52 additions & 0 deletions tests/api/test_edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import json
import os

from pyDataverse.api import NativeApi


class TestEditDatasetMetadata:
def test_edit_dataset_metadata(self):
"""
Test case for uploading a file to a dataset.
This test case performs the following steps:
1. Creates a dataset using the provided metadata.
2. Edits the dataset metadata.
3. Asserts that the metadata was edited successfully.
Raises:
AssertionError: If the file upload fails.
"""
# Arrange
BASE_URL = os.getenv("BASE_URL").rstrip("/")
API_TOKEN = os.getenv("API_TOKEN")

# Create dataset
metadata = json.load(open("tests/data/file_upload_ds_minimum.json"))
pid = self._create_dataset(BASE_URL, API_TOKEN, metadata)
api = NativeApi(BASE_URL, API_TOKEN)

# Prepare file upload
edit_metadata = {
"fields": [
{
"typeName": "title",
"value": "New Title",
}
]
}

# Act
response = api.edit_dataset_metadata(
identifier=pid,
metadata=edit_metadata,
)

# Assert
dataset = api.get_dataset(pid)
assert (
dataset.json()["data"]["metadataBlocks"]["citation"]["fields"][0]["value"]
== "New Title"
), "Metadata edit failed."
assert response.status_code == 200, "Metadata edit failed."

0 comments on commit d682149

Please sign in to comment.