Skip to content

Commit

Permalink
adapt tests to httpx
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed May 11, 2024
1 parent 67b8def commit a57a7d9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 96 deletions.
56 changes: 12 additions & 44 deletions tests/unit/test_directupload.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from urllib.parse import urljoin
import aiohttp
import httpx
import pytest
from rich.progress import Progress
from dvuploader.directupload import (
UPLOAD_ENDPOINT,
REPLACE_ENDPOINT,
_add_files_to_ds,
_validate_ticket_response,
)
Expand All @@ -13,49 +10,26 @@


class Test_AddFileToDs:
# Should successfully add a file to a Dataverse dataset with a valid file path
# Should successfully add files to a Dataverse dataset with a valid file path
@pytest.mark.asyncio
async def test_successfully_add_file_with_valid_filepath(self, mocker):
async def test_successfully_add_replace_file_with_valid_filepath(self, httpx_mock):
# Mock the session.post method to return a response with status code 200
mock_post = mocker.patch("aiohttp.ClientSession.post")
mock_post.return_value.__aenter__.return_value.status = 200

# Initialize the necessary variables
session = aiohttp.ClientSession()
dataverse_url = "https://example.com"
pid = "persistent_id"
fpath = "tests/fixtures/add_dir_files/somefile.txt"
files = [File(filepath=fpath)]
progress = Progress()
pbar = progress.add_task("Uploading", total=1)

# Invoke the function
await _add_files_to_ds(
session=session,
dataverse_url=dataverse_url,
pid=pid,
files=files,
progress=progress,
pbar=pbar,
httpx_mock.add_response(
method="post",
url="https://example.com/api/datasets/:persistentId/addFiles?persistentId=pid",
)

# Assert that the response status is 200 and the result is True
assert mock_post.called_with(
urljoin(dataverse_url, UPLOAD_ENDPOINT + pid), data=mocker.ANY
httpx_mock.add_response(
method="post",
url="https://example.com/api/datasets/:persistentId/replaceFiles?persistentId=pid",
)

@pytest.mark.asyncio
async def test_successfully_replace_file_with_valid_filepath(self, mocker):
# Mock the session.post method to return a response with status code 200
mock_post = mocker.patch("aiohttp.ClientSession.post")
mock_post.return_value.__aenter__.return_value.status = 200

# Initialize the necessary variables
session = aiohttp.ClientSession()
session = httpx.AsyncClient()
dataverse_url = "https://example.com"
pid = "persistent_id"
pid = "pid"
fpath = "tests/fixtures/add_dir_files/somefile.txt"
files = [File(filepath=fpath, file_id="0")]
files = [File(filepath=fpath)]
progress = Progress()
pbar = progress.add_task("Uploading", total=1)

Expand All @@ -69,12 +43,6 @@ async def test_successfully_replace_file_with_valid_filepath(self, mocker):
pbar=pbar,
)

# Assert that the response status is 200 and the result is True
assert mock_post.called_with(
urljoin(dataverse_url, REPLACE_ENDPOINT + pid),
data=mocker.ANY,
)


class Test_ValidateTicketResponse:
# Function does not raise any exceptions when all necessary fields are present
Expand Down
68 changes: 16 additions & 52 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from io import BytesIO
import requests
import pytest
import httpx

from rich.progress import Progress
from dvuploader.file import File
Expand Down Expand Up @@ -141,55 +141,22 @@ def test_raises_TypeError_if_query_parameter_keys_not_strings(self):


class TestRetrieveDatasetFiles:
# Retrieve files of a dataset with valid dataverse_url, persistent_id, and api_token.
def test_valid_parameters_with_dotted_dict(self, mocker):
# Mock the requests.get function
mocker.patch("requests.get")

# Set up mock response
mock_response = mocker.Mock()
mock_response.json.return_value = {
"data": {
"latestVersion": {
"files": [
{"file_name": "file1.txt"},
{"file_name": "file2.txt"},
]
}
}
}
requests.get.return_value = mock_response

# Call the function under test
result = retrieve_dataset_files("http://example.com", "12345", "token")

# Assert the result
assert result == [
{"file_name": "file1.txt"},
{"file_name": "file2.txt"},
]

# Assert that requests.get was called with the correct parameters
requests.get.assert_called_once_with(
"http://example.com/api/datasets/:persistentId/?persistentId=12345",
headers={"X-Dataverse-key": "token"},
)

# Return a list of files in the dataset.
def test_return_files_list(self, mocker):
def test_return_files_list(self, httpx_mock):
# Mock the requests.get function
mocker.patch("requests.get")

# Set up mock response
mock_response = mocker.Mock()
mock_response.json.return_value = {
"data": {
"latestVersion": {
"files": [{"file_name": "file1.txt"}, {"file_name": "file2.txt"}]
httpx_mock.add_response(
url="http://example.com/api/datasets/:persistentId/?persistentId=12345",
json={
"data": {
"latestVersion": {
"files": [
{"file_name": "file1.txt"},
{"file_name": "file2.txt"},
]
}
}
}
}
requests.get.return_value = mock_response
},
)

# Call the function under test
result = retrieve_dataset_files("http://example.com", "12345", "token")
Expand All @@ -198,12 +165,9 @@ def test_return_files_list(self, mocker):
assert result == [{"file_name": "file1.txt"}, {"file_name": "file2.txt"}]

# Raise HTTPError if the request to the Dataverse repository fails.
def test_raise_http_error(self, mocker):
# Mock the requests.get function to raise an exception
mocker.patch("requests.get", side_effect=requests.exceptions.HTTPError)

def test_raise_http_error(self):
# Call the function under test and assert that it raises an HTTPError
with pytest.raises(requests.exceptions.HTTPError):
with pytest.raises(httpx.HTTPStatusError):
retrieve_dataset_files("http://example.com", "12345", "token")


Expand Down

0 comments on commit a57a7d9

Please sign in to comment.