Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set timeout to None to avoid timeout errors #188

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyDataverse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
__email__ = "[email protected]"
__copyright__ = "Copyright (c) 2019 Stefan Kasberger"
__license__ = "MIT License"
__version__ = "0.3.2"
__version__ = "0.3.3"
__url__ = "https://github.com/GDCC/pyDataverse"
__download_url__ = "https://pypi.python.org/pypi/pyDataverse"
__description__ = "A Python module for Dataverse."
2 changes: 1 addition & 1 deletion pyDataverse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ def _sync_request(
kwargs = self._filter_kwargs(kwargs)

try:
resp = method(**kwargs, follow_redirects=True)
resp = method(**kwargs, follow_redirects=True, timeout=None)
if resp.status_code == 401:
error_msg = resp.json()["message"]
raise ApiAuthorizationError(
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyDataverse"
version = "0.3.2"
version = "0.3.3"
description = "A Python module for Dataverse."
authors = [
"Stephan Kasberger <[email protected]>",
Expand Down
56 changes: 53 additions & 3 deletions tests/api/test_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class TestFileUpload:

def test_file_upload(self):
"""
Test case for uploading a file to a dataset.
Expand Down Expand Up @@ -46,6 +45,58 @@ def test_file_upload(self):
# Assert
assert response.status_code == 200, "File upload failed."

def test_bulk_file_upload(self, create_mock_file):
"""
Test case for uploading bulk files to a dataset.

This test is meant to check the performance of the file upload feature
and that nothing breaks when uploading multiple files in line.

This test case performs the following steps:
0. Create 50 mock files.
1. Creates a dataset using the provided metadata.
2. Prepares a file for upload.
3. Uploads the file to the dataset.
4. Asserts that the file upload was successful.

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)

with tempfile.TemporaryDirectory() as tmp_dir:
# Create mock files
mock_files = [
create_mock_file(
filename=f"mock_file_{i}.txt",
dir=tmp_dir,
size=1024**2, # 1MB
)
for i in range(50)
]

for mock_file in mock_files:
# Prepare file upload
df = Datafile({"pid": pid, "filename": os.path.basename(mock_file)})

# Act
response = api.upload_datafile(
identifier=pid,
filename=mock_file,
json_str=df.json(),
)

# Assert
assert response.status_code == 200, "File upload failed."

def test_file_replacement(self):
"""
Test case for replacing a file in a dataset.
Expand All @@ -56,7 +107,7 @@ def test_file_replacement(self):
3. Replace the uploaded datafile with a mutated version.
4. Verify that the file replacement was successful and the content matches the expected content.
"""

# Arrange
BASE_URL = os.getenv("BASE_URL").rstrip("/")
API_TOKEN = os.getenv("API_TOKEN")
Expand All @@ -79,7 +130,6 @@ def test_file_replacement(self):

# Act
with tempfile.TemporaryDirectory() as tempdir:

original = open("tests/data/replace.xyz").read()
mutated = "Z" + original[1::]
mutated_path = os.path.join(tempdir, "replace.xyz")
Expand Down
24 changes: 24 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,27 @@ def import_datafile_full_dict():
"description": "Test datafile",
"restrict": False,
}


@pytest.fixture
def create_mock_file():
"""Returns a function that creates a mock file."""

def _create_mock_file(filename: str, dir: str, size: int):
"""Create a mock file.

Args:
filename (str): Filename.
dir (str): Directory.
size (int): Size.

Returns:
str: Path to the file.
"""
path = os.path.join(dir, filename)
with open(path, "wb") as f:
f.write(os.urandom(size))

return path

return _create_mock_file
Loading