1- import unittest . mock
1+ from pathlib import Path
22
33import pytest
44import requests
55
6- from parcels import (
6+ from parcels . tools . exampledata_utils import (
77 download_example_dataset ,
88 list_example_datasets ,
99)
1010
1111
12- @pytest .mark .skip (reason = "too time intensive" )
13- def test_download_example_dataset (tmp_path ):
14- # test valid datasets
15- for dataset in list_example_datasets ():
16- dataset_folder_path = download_example_dataset (dataset , data_home = tmp_path )
12+ @pytest .fixture
13+ def mock_download (monkeypatch ):
14+ """Avoid the download, only check the status code and create empty file."""
1715
18- assert dataset_folder_path .exists ()
19- assert dataset_folder_path .name == dataset
20- assert str (dataset_folder_path .parent ) == str (tmp_path )
16+ def mock_urlretrieve (url , filename ):
17+ response = requests .head (url )
2118
22- # test non-existing dataset
23- with pytest .raises (ValueError ):
24- download_example_dataset ("non_existing_dataset" , data_home = tmp_path )
19+ if 400 <= response .status_code < 600 :
20+ raise Exception (f"Failed to access URL: { url } . Status code: { response .status_code } " )
21+
22+ Path (filename ).touch ()
23+
24+ monkeypatch .setattr ("parcels.tools.exampledata_utils.urlretrieve" , mock_urlretrieve )
25+
26+
27+ @pytest .mark .usefixtures ("mock_download" )
28+ @pytest .mark .parametrize ("dataset" , list_example_datasets ())
29+ def test_download_example_dataset (tmp_path , dataset ):
30+ if dataset == "GlobCurrent_example_data" :
31+ pytest .skip ("GlobCurrent_example_data too time consuming." )
2532
33+ dataset_folder_path = download_example_dataset (dataset , data_home = tmp_path )
2634
27- def test_download_example_dataset_lite (tmp_path ):
28- # test valid datasets
29- # avoids downloading the dataset (only verifying that the URL is responsive, and folders are created)
30- with unittest .mock .patch ("urllib.request.urlretrieve" , new = mock_urlretrieve ) as mock_function : # noqa: F841
31- for dataset in list_example_datasets ()[0 :1 ]:
32- dataset_folder_path = download_example_dataset (dataset , data_home = tmp_path )
35+ assert dataset_folder_path .exists ()
36+ assert dataset_folder_path .name == dataset
37+ assert dataset_folder_path .parent == tmp_path
3338
34- assert dataset_folder_path .exists ()
35- assert dataset_folder_path .name == dataset
36- assert str (dataset_folder_path .parent ) == str (tmp_path )
3739
38- # test non-existing dataset
40+ def test_download_non_existing_example_dataset ( tmp_path ):
3941 with pytest .raises (ValueError ):
4042 download_example_dataset ("non_existing_dataset" , data_home = tmp_path )
4143
@@ -47,14 +49,3 @@ def test_download_example_dataset_no_data_home():
4749 dataset_folder_path = download_example_dataset (dataset )
4850 assert dataset_folder_path .exists ()
4951 assert dataset_folder_path .name == dataset
50-
51-
52- def mock_urlretrieve (url , filename ):
53- # send a HEAD request to the URL
54- response = requests .head (url )
55-
56- # check the status code of the response
57- if 400 <= response .status_code < 600 :
58- raise Exception (f"Failed to access URL: { url } . Status code: { response .status_code } " )
59-
60- print (f"Pinged URL successfully: { url } " )
0 commit comments