Skip to content

Commit a7b55c7

Browse files
committed
[JTH] updated tests for downloaders
1 parent fb577ed commit a7b55c7

File tree

3 files changed

+316
-71
lines changed

3 files changed

+316
-71
lines changed

tests/downloaders/test_copernicus_downloader.py

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import tempfile
22
import unittest
33

4+
from bluemath_tk.downloaders._download_result import DownloadResult
45
from bluemath_tk.downloaders.copernicus.copernicus_downloader import (
56
CopernicusDownloader,
67
)
@@ -13,10 +14,10 @@ def setUp(self):
1314
product="ERA5",
1415
base_path_to_download=self.temp_dir,
1516
token=None,
16-
check=True, # Just check paths to download
1717
)
1818

1919
def test_download_data_era5(self):
20+
"""Test downloading ERA5 data."""
2021
result = self.downloader.download_data_era5(
2122
variables=["spectra"],
2223
years=[f"{year:04d}" for year in range(2020, 2025)],
@@ -36,14 +37,108 @@ def test_download_data_era5(self):
3637
],
3738
area=[43.4, 350.4, 43.6, 350.6], # [lat_min, lon_min, lat_max, lon_max]
3839
)
40+
self.assertIsInstance(result, DownloadResult)
3941
print(result)
4042

43+
def test_download_data_era5_dry_run(self):
44+
"""Test dry_run functionality for ERA5."""
45+
result = self.downloader.download_data_era5(
46+
variables=["spectra"],
47+
years=["2020"],
48+
months=["01"],
49+
area=[43.4, 350.4, 43.6, 350.6],
50+
dry_run=True,
51+
)
52+
self.assertIsInstance(result, DownloadResult)
53+
self.assertTrue(
54+
len(result.skipped_files) > 0 or len(result.downloaded_files) > 0
55+
)
56+
print(f"\nDry run result: {result}")
4157

42-
if __name__ == "__main__":
43-
unittest.main()
58+
def test_download_data_routing(self):
59+
"""Test that download_data routes to product-specific methods."""
60+
result = self.downloader.download_data(
61+
variables=["spectra"],
62+
years=["2020"],
63+
months=["01"],
64+
dry_run=True,
65+
)
66+
self.assertIsInstance(result, DownloadResult)
67+
68+
def test_product_parameter(self):
69+
"""Test that product parameter is required and validated."""
70+
# Test with valid product ERA5
71+
downloader = CopernicusDownloader(
72+
product="ERA5",
73+
base_path_to_download=self.temp_dir,
74+
)
75+
self.assertEqual(downloader.product, "ERA5")
76+
77+
# Test with valid product CERRA
78+
downloader = CopernicusDownloader(
79+
product="CERRA",
80+
base_path_to_download=self.temp_dir,
81+
)
82+
self.assertEqual(downloader.product, "CERRA")
83+
84+
# Test with invalid product
85+
with self.assertRaises(ValueError):
86+
CopernicusDownloader(
87+
product="INVALID",
88+
base_path_to_download=self.temp_dir,
89+
)
4490

91+
def test_list_variables(self):
92+
"""Test listing available variables."""
93+
variables = self.downloader.list_variables()
94+
self.assertIsInstance(variables, list)
95+
self.assertTrue(len(variables) > 0)
96+
print(f"\nAvailable variables: {variables}")
4597

46-
# mean_wave_period_based_on_first_moment/
47-
# wave_spectral_directional_width/
48-
# wave_spectral_directional_width_for_swell/
49-
# wave_spectral_directional_width_for_wind_waves/
98+
def test_list_datasets(self):
99+
"""Test listing available datasets."""
100+
datasets = self.downloader.list_datasets()
101+
self.assertIsInstance(datasets, list)
102+
self.assertTrue(len(datasets) > 0)
103+
print(f"\nAvailable datasets: {datasets}")
104+
105+
def test_download_data_cerra(self):
106+
"""Test downloading CERRA data."""
107+
cerra_downloader = CopernicusDownloader(
108+
product="CERRA",
109+
base_path_to_download=self.temp_dir,
110+
token=None,
111+
)
112+
result = cerra_downloader.download_data_cerra(
113+
variables=["10m_wind_speed"],
114+
years=["2020"],
115+
months=["01"],
116+
days=["01"],
117+
dry_run=True,
118+
)
119+
self.assertIsInstance(result, DownloadResult)
120+
print(f"\nCERRA download result: {result}")
121+
122+
def test_download_data_cerra_dry_run(self):
123+
"""Test dry_run functionality for CERRA."""
124+
cerra_downloader = CopernicusDownloader(
125+
product="CERRA",
126+
base_path_to_download=self.temp_dir,
127+
token=None,
128+
)
129+
result = cerra_downloader.download_data_cerra(
130+
variables=["10m_wind_direction"],
131+
years=["2020"],
132+
months=["01"],
133+
days=["01"],
134+
dry_run=True,
135+
)
136+
self.assertIsInstance(result, DownloadResult)
137+
self.assertTrue(
138+
len(result.skipped_files) > 0 or len(result.downloaded_files) > 0
139+
)
140+
print(f"\nCERRA dry run result: {result}")
141+
142+
143+
if __name__ == "__main__":
144+
unittest.main()

tests/downloaders/test_ecmwf_downloader.py

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ def setUp(self):
1010
self.downloader = ECMWFDownloader(
1111
product="OpenData",
1212
base_path_to_download=self.temp_dir,
13-
check=True, # Just check paths to download, do not actually download
1413
)
1514

1615
def test_list_datasets(self):
16+
"""Test listing available datasets."""
1717
datasets = self.downloader.list_datasets()
1818
self.assertIsInstance(datasets, list)
1919
self.assertTrue(len(datasets) > 0)
2020
print(f"Available datasets: {datasets}")
2121

2222
def test_download_data(self):
23+
"""Test downloading data."""
2324
dataset = self.downloader.download_data(
2425
load_data=False,
2526
param=["msl"],
@@ -30,6 +31,45 @@ def test_download_data(self):
3031
self.assertIsInstance(dataset, str)
3132
print(dataset)
3233

34+
def test_download_data_dry_run(self):
35+
"""Test dry_run functionality."""
36+
dataset = self.downloader.download_data(
37+
load_data=False,
38+
param=["msl"],
39+
step=[0, 240],
40+
type="fc",
41+
dry_run=True,
42+
)
43+
self.assertIsInstance(dataset, str)
44+
print(f"\nDry run result: {dataset}")
45+
46+
def test_download_data_open_data(self):
47+
"""Test product-specific download method."""
48+
dataset = self.downloader.download_data_open_data(
49+
param=["msl"],
50+
step=[0, 240],
51+
type="fc",
52+
dry_run=True,
53+
)
54+
self.assertIsInstance(dataset, str)
55+
print(f"\nOpenData download result: {dataset}")
56+
57+
def test_product_parameter(self):
58+
"""Test that product parameter is required and validated."""
59+
# Test with valid product
60+
downloader = ECMWFDownloader(
61+
product="OpenData",
62+
base_path_to_download=self.temp_dir,
63+
)
64+
self.assertEqual(downloader.product, "OpenData")
65+
66+
# Test with invalid product
67+
with self.assertRaises(ValueError):
68+
ECMWFDownloader(
69+
product="INVALID",
70+
base_path_to_download=self.temp_dir,
71+
)
72+
3373

3474
if __name__ == "__main__":
3575
unittest.main()

0 commit comments

Comments
 (0)