Skip to content

Commit 45c1804

Browse files
committed
Improve directupload tests with dataset and lock mocks
Added mock responses for dataset retrieval and lock checks in directupload unit tests to better simulate Dataverse API interactions. Also set base_url for AsyncClient to ensure consistent request URLs.
1 parent 8ee65b2 commit 45c1804

1 file changed

Lines changed: 43 additions & 17 deletions

File tree

tests/unit/test_directupload.py

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,43 @@
11
import httpx
22
import pytest
33
from rich.progress import Progress
4+
45
from dvuploader.directupload import (
56
_add_files_to_ds,
6-
_validate_ticket_response,
77
_prepare_registration,
8+
_validate_ticket_response,
89
)
9-
1010
from dvuploader.file import File
1111

1212

1313
class Test_AddFileToDs:
14-
# Should successfully add files to a Dataverse dataset with a valid file path
1514
@pytest.mark.asyncio
1615
async def test_successfully_add_file_with_valid_filepath(self, httpx_mock):
17-
# Mock the session.post method to return a response with status code 200
16+
httpx_mock.add_response(
17+
method="get",
18+
url="https://example.com/api/datasets/:persistentId/?persistentId=pid",
19+
json={"status": "OK", "data": {"id": 123}},
20+
)
21+
22+
httpx_mock.add_response(
23+
method="get",
24+
url="https://example.com/api/datasets/123/locks",
25+
json={"status": "OK", "data": []},
26+
)
27+
1828
httpx_mock.add_response(
1929
method="post",
2030
url="https://example.com/api/datasets/:persistentId/addFiles?persistentId=pid",
2131
)
2232

23-
# Initialize the necessary variables
24-
session = httpx.AsyncClient()
33+
session = httpx.AsyncClient(base_url="https://example.com")
2534
dataverse_url = "https://example.com"
2635
pid = "pid"
2736
fpath = "tests/fixtures/add_dir_files/somefile.txt"
2837
files = [File(filepath=fpath)]
2938
progress = Progress()
3039
pbar = progress.add_task("Uploading", total=1)
3140

32-
# Invoke the function
3341
await _add_files_to_ds(
3442
session=session,
3543
dataverse_url=dataverse_url,
@@ -41,22 +49,31 @@ async def test_successfully_add_file_with_valid_filepath(self, httpx_mock):
4149

4250
@pytest.mark.asyncio
4351
async def test_successfully_replace_file_with_valid_filepath(self, httpx_mock):
44-
# Mock the session.post method to return a response with status code 200
52+
httpx_mock.add_response(
53+
method="get",
54+
url="https://example.com/api/datasets/:persistentId/?persistentId=pid",
55+
json={"status": "OK", "data": {"id": 123}},
56+
)
57+
58+
httpx_mock.add_response(
59+
method="get",
60+
url="https://example.com/api/datasets/123/locks",
61+
json={"status": "OK", "data": []},
62+
)
63+
4564
httpx_mock.add_response(
4665
method="post",
4766
url="https://example.com/api/datasets/:persistentId/replaceFiles?persistentId=pid",
4867
)
4968

50-
# Initialize the necessary variables
51-
session = httpx.AsyncClient()
69+
session = httpx.AsyncClient(base_url="https://example.com")
5270
dataverse_url = "https://example.com"
5371
pid = "pid"
5472
fpath = "tests/fixtures/add_dir_files/somefile.txt"
5573
files = [File(filepath=fpath, to_replace=True)]
5674
progress = Progress()
5775
pbar = progress.add_task("Uploading", total=1)
5876

59-
# Invoke the function
6077
await _add_files_to_ds(
6178
session=session,
6279
dataverse_url=dataverse_url,
@@ -70,19 +87,29 @@ async def test_successfully_replace_file_with_valid_filepath(self, httpx_mock):
7087
async def test_successfully_add_and_replace_file_with_valid_filepath(
7188
self, httpx_mock
7289
):
73-
# Mock the session.post method to return a response with status code 200
7490
httpx_mock.add_response(
75-
method="post",
76-
url="https://example.com/api/datasets/:persistentId/replaceFiles?persistentId=pid",
91+
method="get",
92+
url="https://example.com/api/datasets/:persistentId/?persistentId=pid",
93+
json={"status": "OK", "data": {"id": 123}},
94+
)
95+
96+
httpx_mock.add_response(
97+
method="get",
98+
url="https://example.com/api/datasets/123/locks",
99+
json={"status": "OK", "data": []},
77100
)
78101

79102
httpx_mock.add_response(
80103
method="post",
81104
url="https://example.com/api/datasets/:persistentId/addFiles?persistentId=pid",
82105
)
83106

84-
# Initialize the necessary variables
85-
session = httpx.AsyncClient()
107+
httpx_mock.add_response(
108+
method="post",
109+
url="https://example.com/api/datasets/:persistentId/replaceFiles?persistentId=pid",
110+
)
111+
112+
session = httpx.AsyncClient(base_url="https://example.com")
86113
dataverse_url = "https://example.com"
87114
pid = "pid"
88115
fpath = "tests/fixtures/add_dir_files/somefile.txt"
@@ -93,7 +120,6 @@ async def test_successfully_add_and_replace_file_with_valid_filepath(
93120
progress = Progress()
94121
pbar = progress.add_task("Uploading", total=1)
95122

96-
# Invoke the function
97123
await _add_files_to_ds(
98124
session=session,
99125
dataverse_url=dataverse_url,

0 commit comments

Comments
 (0)