Skip to content

Commit

Permalink
Refactor example dataset download tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VeckoTheGecko committed Jan 17, 2025
1 parent 02eb9a7 commit 47e16ac
Showing 1 changed file with 25 additions and 34 deletions.
59 changes: 25 additions & 34 deletions tests/tools/test_exampledata_utils.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import unittest.mock
from pathlib import Path

import pytest
import requests

from parcels import (
from parcels.tools.exampledata_utils import (
download_example_dataset,
list_example_datasets,
)


@pytest.mark.skip(reason="too time intensive")
def test_download_example_dataset(tmp_path):
# test valid datasets
for dataset in list_example_datasets():
dataset_folder_path = download_example_dataset(dataset, data_home=tmp_path)
@pytest.fixture
def mock_download(monkeypatch):
"""Avoid the download, only check the status code and create empty file."""

assert dataset_folder_path.exists()
assert dataset_folder_path.name == dataset
assert str(dataset_folder_path.parent) == str(tmp_path)
def mock_urlretrieve(url, filename):
response = requests.head(url)

# test non-existing dataset
with pytest.raises(ValueError):
download_example_dataset("non_existing_dataset", data_home=tmp_path)
if 400 <= response.status_code < 600:
raise Exception(f"Failed to access URL: {url}. Status code: {response.status_code}")

Check warning on line 20 in tests/tools/test_exampledata_utils.py

View check run for this annotation

Codecov / codecov/patch

tests/tools/test_exampledata_utils.py#L20

Added line #L20 was not covered by tests

Path(filename).touch()

monkeypatch.setattr("parcels.tools.exampledata_utils.urlretrieve", mock_urlretrieve)


@pytest.mark.usefixtures("mock_download")
@pytest.mark.parametrize("dataset", list_example_datasets())
def test_download_example_dataset(tmp_path, dataset):
if dataset == "GlobCurrent_example_data":
pytest.skip(f"{dataset} too time consuming.")

dataset_folder_path = download_example_dataset(dataset, data_home=tmp_path)

def test_download_example_dataset_lite(tmp_path):
# test valid datasets
# avoids downloading the dataset (only verifying that the URL is responsive, and folders are created)
with unittest.mock.patch("urllib.request.urlretrieve", new=mock_urlretrieve) as mock_function: # noqa: F841
for dataset in list_example_datasets()[0:1]:
dataset_folder_path = download_example_dataset(dataset, data_home=tmp_path)
assert dataset_folder_path.exists()
assert dataset_folder_path.name == dataset
assert dataset_folder_path.parent == tmp_path

assert dataset_folder_path.exists()
assert dataset_folder_path.name == dataset
assert str(dataset_folder_path.parent) == str(tmp_path)

# test non-existing dataset
def test_download_non_existing_example_dataset(tmp_path):
with pytest.raises(ValueError):
download_example_dataset("non_existing_dataset", data_home=tmp_path)

Expand All @@ -47,14 +49,3 @@ def test_download_example_dataset_no_data_home():
dataset_folder_path = download_example_dataset(dataset)
assert dataset_folder_path.exists()
assert dataset_folder_path.name == dataset


def mock_urlretrieve(url, filename):
# send a HEAD request to the URL
response = requests.head(url)

# check the status code of the response
if 400 <= response.status_code < 600:
raise Exception(f"Failed to access URL: {url}. Status code: {response.status_code}")

print(f"Pinged URL successfully: {url}")

0 comments on commit 47e16ac

Please sign in to comment.