Skip to content

Commit 4887b6a

Browse files
committed
add unit and integration tests
1 parent 1ecb7a9 commit 4887b6a

16 files changed

Lines changed: 652 additions & 0 deletions

tests/conftest.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import os
2+
import pytest
3+
import requests
4+
5+
6+
@pytest.fixture
7+
def credentials():
8+
"""Returns the credentials for the Dataverse server."""
9+
BASE_URL = os.environ.get("BASE_URL")
10+
API_TOKEN = os.environ.get("API_TOKEN")
11+
12+
assert BASE_URL, "BASE_URL environment variable must be set"
13+
assert API_TOKEN, "API_TOKEN environment variable must be set"
14+
15+
return BASE_URL, API_TOKEN
16+
17+
18+
def create_dataset(
19+
parent: str,
20+
server_url: str,
21+
api_token: str,
22+
):
23+
"""
24+
Creates a dataset in a Dataverse.
25+
26+
Args:
27+
parent (str): The parent Dataverse identifier.
28+
server_url (str): The URL of the Dataverse server.
29+
api_token (str): The API token for authentication.
30+
31+
Returns:
32+
str: The persistent identifier of the created dataset.
33+
"""
34+
if server_url.endswith("/"):
35+
server_url = server_url[:-1]
36+
37+
url = f"{server_url}/api/dataverses/{parent}/datasets"
38+
response = requests.post(
39+
url=url,
40+
headers={"X-Dataverse-key": api_token},
41+
data=open("./tests/fixtures/create_dataset.json", "rb"),
42+
)
43+
44+
response.raise_for_status()
45+
46+
return response.json()["data"]["persistentId"]
47+
48+
49+
def create_mock_file(
50+
directory: str,
51+
name: str,
52+
size: int = 100,
53+
):
54+
"""Create a file with the specified size in megabytes.
55+
56+
Args:
57+
directory (str): The directory where the file will be created.
58+
name (str): The name of the file.
59+
size (int, optional): Size of the file in megabytes. Defaults to 100.
60+
"""
61+
62+
path = os.path.join(directory, name)
63+
size = size * 1024 * 1024
64+
65+
with open(path, "wb") as f:
66+
f.seek(size - 1) # 1 GB
67+
f.write(b"\0")
68+
69+
return path

tests/fixtures/add_dir_files/.hidden

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This file is part of an ignored dir
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is another test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is a subfile

tests/fixtures/add_dir_files/to_ignore.txt

Whitespace-only changes.

tests/fixtures/cli_input.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
persistent_id: doi:10.70122/XXX/XXXXX
2+
dataverse_url: https://demo.dataverse.org/
3+
api_token: XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
4+
files:
5+
- filepath: ./tests/fixtures/add_dir_files/somefile.txt
6+
- filepath: ./tests/fixtures/add_dir_files/anotherfile.txt
7+
directoryLabel: some/dir

tests/fixtures/create_dataset.json

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
{
2+
"datasetVersion": {
3+
"metadataBlocks": {
4+
"citation": {
5+
"fields": [
6+
{
7+
"multiple": false,
8+
"typeClass": "primitive",
9+
"typeName": "title",
10+
"value": "My dataset"
11+
},
12+
{
13+
"multiple": true,
14+
"typeClass": "controlledVocabulary",
15+
"typeName": "subject",
16+
"value": [
17+
"Other"
18+
]
19+
},
20+
{
21+
"multiple": true,
22+
"typeClass": "compound",
23+
"typeName": "author",
24+
"value": [
25+
{
26+
"authorName": {
27+
"multiple": false,
28+
"typeClass": "primitive",
29+
"typeName": "authorName",
30+
"value": "John Doe"
31+
}
32+
}
33+
]
34+
},
35+
{
36+
"multiple": true,
37+
"typeClass": "compound",
38+
"typeName": "datasetContact",
39+
"value": [
40+
{
41+
"datasetContactName": {
42+
"multiple": false,
43+
"typeClass": "primitive",
44+
"typeName": "datasetContactName",
45+
"value": "John Doe"
46+
},
47+
"datasetContactEmail": {
48+
"multiple": false,
49+
"typeClass": "primitive",
50+
"typeName": "datasetContactEmail",
51+
"value": "john@doe.com"
52+
}
53+
}
54+
]
55+
},
56+
{
57+
"multiple": true,
58+
"typeClass": "compound",
59+
"typeName": "dsDescription",
60+
"value": [
61+
{
62+
"dsDescriptionValue": {
63+
"multiple": false,
64+
"typeClass": "primitive",
65+
"typeName": "dsDescriptionValue",
66+
"value": "This is a description of the dataset"
67+
}
68+
}
69+
]
70+
}
71+
]
72+
}
73+
}
74+
}
75+
}
76+

tests/integration/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)