Skip to content

Commit 2facaed

Browse files
authored
Merge pull request #2689 from eerovaher/esa-hsa-remote-tests
Fix and refactor `esa.hsa` remote tests
2 parents a4716fd + d6ecf97 commit 2facaed

File tree

1 file changed

+50
-155
lines changed

1 file changed

+50
-155
lines changed

astroquery/esa/hsa/tests/test_hsa_remote.py

Lines changed: 50 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -3,168 +3,63 @@
33
import tarfile
44

55
import pytest
6-
from requests.exceptions import ChunkedEncodingError
76

87
from ..core import HSAClass
98

10-
spire_chksum = [10233, 10762, 9019, 10869, 3944, 11328, 3921, 10999, 10959,
11-
11342, 10974, 3944, 11335, 11323, 11078, 11321, 11089, 11314, 11108, 6281]
129

13-
pacs_chksum = [10208, 10755, 8917, 10028, 3924, 3935, 6291]
10+
pytestmark = pytest.mark.remote_data
1411

1512

16-
@pytest.mark.remote_data
17-
class TestHSARemote:
18-
retries = 2
13+
PACS_ENDINGS = ["571.xml", "571.jpg", "214.fits.gz", "008.fits.gz",
14+
"674.fits.gz", "350.fits.gz", "README.pdf"]
15+
SPIRE_ENDINGS = ["898.xml", "898.jpg", "141.fits.gz", "045.fits.gz", "952.fits.gz",
16+
"974.fits.gz", "715.fits.gz", "547.fits.gz", "770.fits.gz",
17+
"856.fits.gz", "148.fits.gz", "025.fits.gz", "538.fits.gz",
18+
"070.fits.gz", "434.fits.gz", "637.fits.gz", "835.fits.gz",
19+
"372.fits.gz", "248.fits.gz", "README.pdf"]
1920

20-
def access_archive_with_retries(self, f, params):
21-
for _ in range(self.retries):
22-
try:
23-
res = f(**params)
24-
return res
25-
except ChunkedEncodingError:
26-
pass
27-
return None
2821

29-
def test_download_data_observation_pacs(self, tmp_path):
30-
obs_id = "1342191813"
31-
parameters = {'retrieval_type': "OBSERVATION",
32-
'observation_id': obs_id,
33-
'instrument_name': "PACS",
34-
'product_level': 'LEVEL3',
35-
'cache': False,
36-
'download_dir': tmp_path}
37-
expected_res = Path(tmp_path, obs_id + ".tar")
38-
hsa = HSAClass()
39-
res = self.access_archive_with_retries(hsa.download_data, parameters)
40-
if res is None:
41-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
42-
assert Path(res) == expected_res
43-
assert Path(res).is_file()
44-
with tarfile.open(res) as tar:
45-
chksum = [m.chksum for m in tar.getmembers()]
46-
assert chksum.sort() == pacs_chksum.sort()
22+
@pytest.mark.parametrize(
23+
"method,kwargs,expected_filename,expected_endings",
24+
[("download_data", {}, "1342191813.tar", PACS_ENDINGS),
25+
("download_data", {"filename": "output_file"}, "output_file.tar", PACS_ENDINGS),
26+
("download_data", {"compress": "true"}, "1342191813.tgz", PACS_ENDINGS),
27+
("download_data", {"observation_id": "1342191188", "instrument_name": "SPIRE", "product_level": "LEVEL2", },
28+
"1342191188.tar", SPIRE_ENDINGS),
29+
("get_observation", {}, "1342191813.tar", PACS_ENDINGS)])
30+
def test_download_data_observation(method, kwargs, expected_filename, expected_endings, tmp_path):
31+
parameters = {"observation_id": "1342191813",
32+
'instrument_name': "PACS",
33+
'product_level': 'LEVEL3',
34+
'cache': False,
35+
'download_dir': tmp_path}
36+
parameters.update(kwargs)
37+
if method == "download_data":
38+
res = HSAClass().download_data(**parameters, retrieval_type="OBSERVATION")
39+
elif method == "get_observation":
40+
res = HSAClass().get_observation(**parameters)
41+
assert Path(res) == tmp_path / expected_filename
42+
assert Path(res).is_file()
43+
with tarfile.open(res) as tar:
44+
names = tar.getnames()
45+
assert len(names) == len(expected_endings)
46+
for name, ending in zip(names, expected_endings):
47+
assert name.endswith(ending)
4748

48-
def test_download_data_observation_pacs_filename(self, tmp_path):
49-
obs_id = "1342191813"
50-
fname = "output_file"
51-
parameters = {'retrieval_type': "OBSERVATION",
52-
'observation_id': obs_id,
53-
'instrument_name': "PACS",
54-
'product_level': 'LEVEL3',
55-
'filename': fname,
56-
'cache': False,
57-
'download_dir': tmp_path}
58-
expected_res = Path(tmp_path, fname + ".tar")
59-
hsa = HSAClass()
60-
res = self.access_archive_with_retries(hsa.download_data, parameters)
61-
if res is None:
62-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
63-
assert Path(res) == expected_res
64-
assert Path(res).is_file()
65-
with tarfile.open(res) as tar:
66-
chksum = [m.chksum for m in tar.getmembers()]
67-
assert chksum.sort() == pacs_chksum.sort()
6849

69-
def test_download_data_observation_pacs_compressed(self, tmp_path):
70-
obs_id = "1342191813"
71-
parameters = {'retrieval_type': "OBSERVATION",
72-
'observation_id': obs_id,
73-
'instrument_name': "PACS",
74-
'product_level': 'LEVEL3',
75-
'compress': 'true',
76-
'cache': False,
77-
'download_dir': tmp_path}
78-
expected_res = Path(tmp_path, obs_id + ".tgz")
79-
hsa = HSAClass()
80-
res = self.access_archive_with_retries(hsa.download_data, parameters)
81-
if res is None:
82-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
83-
assert Path(res) == expected_res
84-
assert Path(res).is_file()
85-
with tarfile.open(res) as tar:
86-
chksum = [m.chksum for m in tar.getmembers()]
87-
assert chksum.sort() == pacs_chksum.sort()
88-
89-
def test_download_data_observation_spire(self, tmp_path):
90-
obs_id = "1342191188"
91-
parameters = {'retrieval_type': "OBSERVATION",
92-
'observation_id': obs_id,
93-
'instrument_name': "SPIRE",
94-
'product_level': 'LEVEL2',
95-
'cache': False,
96-
'download_dir': tmp_path}
97-
expected_res = Path(tmp_path, obs_id + ".tar")
98-
hsa = HSAClass()
99-
res = self.access_archive_with_retries(hsa.download_data, parameters)
100-
if res is None:
101-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
102-
assert Path(res) == expected_res
103-
assert Path(res).is_file()
104-
with tarfile.open(res) as tar:
105-
chksum = [m.chksum for m in tar.getmembers()]
106-
assert chksum.sort() == spire_chksum.sort()
107-
108-
def test_download_data_postcard_pacs(self, tmp_path):
109-
obs_id = "1342191813"
110-
parameters = {'retrieval_type': "POSTCARD",
111-
'observation_id': obs_id,
112-
'instrument_name': "PACS",
113-
'cache': False,
114-
'download_dir': tmp_path}
115-
expected_res = Path(tmp_path, obs_id + ".jpg")
116-
hsa = HSAClass()
117-
res = self.access_archive_with_retries(hsa.download_data, parameters)
118-
if res is None:
119-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
120-
assert Path(res) == expected_res
121-
assert Path(res).is_file()
122-
123-
def test_download_data_postcard_pacs_filename(self, tmp_path):
124-
obs_id = "1342191813"
125-
fname = "output_file"
126-
parameters = {'retrieval_type': "POSTCARD",
127-
'observation_id': obs_id,
128-
'instrument_name': "PACS",
129-
'filename': fname,
130-
'cache': False,
131-
'download_dir': tmp_path}
132-
expected_res = Path(tmp_path, fname + ".jpg")
133-
hsa = HSAClass()
134-
res = self.access_archive_with_retries(hsa.download_data, parameters)
135-
if res is None:
136-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
137-
assert Path(res) == expected_res
138-
assert Path(res).is_file()
139-
140-
def test_get_observation(self, tmp_path):
141-
obs_id = "1342191813"
142-
parameters = {'observation_id': obs_id,
143-
'instrument_name': "PACS",
144-
'product_level': 'LEVEL3',
145-
'cache': False,
146-
'download_dir': tmp_path}
147-
expected_res = Path(tmp_path, obs_id + ".tar")
148-
hsa = HSAClass()
149-
res = self.access_archive_with_retries(hsa.get_observation, parameters)
150-
if res is None:
151-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
152-
assert Path(res) == expected_res
153-
assert Path(res).is_file()
154-
with tarfile.open(res) as tar:
155-
chksum = [m.chksum for m in tar.getmembers()]
156-
assert chksum.sort() == pacs_chksum.sort()
157-
158-
def test_get_postcard(self, tmp_path):
159-
obs_id = "1342191813"
160-
parameters = {'observation_id': obs_id,
161-
'instrument_name': "PACS",
162-
'cache': False,
163-
'download_dir': tmp_path}
164-
expected_res = Path(tmp_path, obs_id + ".jpg")
165-
hsa = HSAClass()
166-
res = self.access_archive_with_retries(hsa.get_postcard, parameters)
167-
if res is None:
168-
pytest.xfail(f"Archive broke the connection {self.retries} times, unable to test")
169-
assert Path(res) == expected_res
170-
assert Path(res).is_file()
50+
@pytest.mark.parametrize(
51+
"method,kwargs,expected_filename",
52+
[("download_data", {}, "1342191813.jpg"),
53+
("download_data", {"filename": "output_file"}, "output_file.jpg"),
54+
("get_postcard", {}, "1342191813.jpg")])
55+
def test_download_data_postcard(method, kwargs, expected_filename, tmp_path):
56+
parameters = {"observation_id": "1342191813",
57+
'instrument_name': "PACS",
58+
'cache': False,
59+
'download_dir': tmp_path}
60+
if method == "download_data":
61+
res = HSAClass().download_data(**parameters, **kwargs, retrieval_type="POSTCARD")
62+
elif method == "get_postcard":
63+
res = HSAClass().get_postcard(**parameters, **kwargs)
64+
assert Path(res) == tmp_path / expected_filename
65+
assert Path(res).is_file()

0 commit comments

Comments
 (0)