Skip to content

Commit b23ec07

Browse files
committed
Add integration test for dataset update with multiple fields
Introduces a new integration test to verify updating a dataset with multiple fields, including authors, description, contact, and other IDs. Also updates unit tests to include PrivateAttr _changed in test models for change tracking.
1 parent 988f3b9 commit b23ec07

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

tests/integration/test_dataset_update.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,69 @@ def test_dataset_update(
6363
"The updated dataset title does not match the expected title."
6464
)
6565

66+
@pytest.mark.integration
67+
def test_dataset_update_with_multiple_fields(
68+
self,
69+
credentials,
70+
):
71+
# Arrange
72+
base_url, api_token = credentials
73+
dataverse = Dataverse(
74+
server_url=base_url,
75+
api_token=api_token,
76+
)
77+
78+
# Create a dataset
79+
dataset = dataverse.create_dataset()
80+
dataset.citation.title = "My dataset"
81+
dataset.citation.subject = ["Other"]
82+
dataset.citation.add_author(name="John Doe")
83+
dataset.citation.add_ds_description(
84+
value="This is a description of the dataset",
85+
date="2024",
86+
)
87+
dataset.citation.add_dataset_contact(
88+
name="John Doe",
89+
90+
)
91+
92+
pid = dataset.upload("Root")
93+
94+
# Act
95+
# Re-fetch the dataset and add other ID
96+
dataset = dataverse.load_dataset(pid)
97+
dataset.citation.add_other_id(agency="DOI", value="10.5072/easy-dataverse")
98+
dataset.update()
99+
100+
# Re-fetch the dataset to verify the update
101+
url = (
102+
f"{base_url}/api/datasets/:persistentId/versions/:draft?persistentId={pid}"
103+
)
104+
105+
response = httpx.get(
106+
url,
107+
headers={"X-Dataverse-key": api_token},
108+
)
109+
110+
response.raise_for_status()
111+
updated_dataset = response.json()
112+
other_id_field = next(
113+
filter(
114+
lambda x: x["typeName"] == "otherId",
115+
updated_dataset["data"]["metadataBlocks"]["citation"]["fields"],
116+
),
117+
None,
118+
)
119+
120+
# Assert
121+
assert other_id_field is not None, "Other ID field should be present"
122+
assert len(other_id_field["value"]) > 0, "Other ID field should have values"
123+
assert any(
124+
item["otherIdAgency"]["value"] == "DOI"
125+
and item["otherIdValue"]["value"] == "10.5072/easy-dataverse"
126+
for item in other_id_field["value"]
127+
), "The DOI other ID should be present in the updated dataset"
128+
66129
@staticmethod
67130
def sort_citation(dataset: Dict):
68131
citation = dataset["datasetVersion"]["metadataBlocks"]["citation"]

tests/unit/test_connect.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from enum import Enum
2-
from typing import List, Optional, Union, get_args
2+
from typing import List, Optional, Set, Union, get_args
33

44
import pytest
5-
from pydantic import BaseModel, Field
5+
from pydantic import BaseModel, Field, PrivateAttr
66

77
from easyDataverse.classgen import (
88
camel_to_snake,
@@ -451,9 +451,11 @@ class TestClass(BaseModel):
451451
name: str
452452
value: int = 42
453453
optional: Optional[str] = None
454+
_changed: Set = PrivateAttr(default_factory=set)
454455

455456
class ParentClass(BaseModel):
456457
to_add_to: List[TestClass] = []
458+
_changed: Set = PrivateAttr(default_factory=set)
457459

458460
# Act
459461
result = generate_add_function(
@@ -480,6 +482,7 @@ class ParentClass(BaseModel):
480482
"name": str,
481483
"value": int,
482484
"optional": Optional[str],
485+
"_changed": Set,
483486
}
484487

485488
expected_object = TestClass(

0 commit comments

Comments
 (0)