diff --git a/tests/tools/test_exampledata_utils.py b/tests/tools/test_exampledata_utils.py index 3c1f88c6ea..613ec1485a 100644 --- a/tests/tools/test_exampledata_utils.py +++ b/tests/tools/test_exampledata_utils.py @@ -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}") + + 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) @@ -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}")