Skip to content

Commit e8489d3

Browse files
authored
Merge pull request #2224 from andamian/almafix
ALMA integration tests fix
2 parents cbec82e + b2bd97c commit e8489d3

File tree

7 files changed

+124
-137
lines changed

7 files changed

+124
-137
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ sdss
1616
Infrastructure, Utility and Other Changes and Additions
1717
-------------------------------------------------------
1818

19+
- Adding ``--alma-site`` pytest option for testing to have a control over
20+
which specific site to test. [#2224]
1921

2022

2123
0.4.4 (2021-11-17)

astroquery/alma/__init__.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
'https://almascience.nrao.edu',
1212
'https://almascience.nao.ac.jp']
1313

14-
_test_url_list = ['https://beta.cadc-ccda.hia-ha.nrc-cnrc.gc.ca']
15-
1614
auth_urls = ['asa.alma.cl', 'rh-cas.alma.cl']
1715

1816

@@ -27,11 +25,6 @@ class Conf(_config.ConfigNamespace):
2725
_url_list,
2826
'The ALMA Archive mirror to use.')
2927

30-
test_archive_url = _config.ConfigItem(
31-
_test_url_list,
32-
'ALMA Archive Test Mirrors (temporary)'
33-
)
34-
3528
auth_url = _config.ConfigItem(
3629
auth_urls,
3730
'ALMA Central Authentication Service URLs'

astroquery/alma/core.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@
110110
},
111111
'Energy': {
112112
'Frequency (GHz)': ['frequency', 'frequency', _gen_numeric_sql],
113-
'Bandwidth (GHz)': ['bandwidth', 'bandwidth', _gen_numeric_sql],
113+
'Bandwidth (Hz)': ['bandwidth', 'bandwidth', _gen_numeric_sql],
114114
'Spectral resolution (KHz)': ['spectral_resolution',
115115
'em_resolution', _gen_spec_res_sql],
116116
'Band': ['band_list', 'band_list', _gen_band_list_sql]
@@ -677,7 +677,7 @@ def _HEADER_data_size(self, files):
677677
return data_sizes, totalsize.to(u.GB)
678678

679679
def download_files(self, files, savedir=None, cache=True,
680-
continuation=True, skip_unauthorized=True):
680+
continuation=True, skip_unauthorized=True,):
681681
"""
682682
Given a list of file URLs, download them
683683
@@ -708,22 +708,21 @@ def download_files(self, files, savedir=None, cache=True,
708708
downloaded_files = []
709709
if savedir is None:
710710
savedir = self.cache_location
711-
for fileLink in unique(files):
712-
log.debug("Downloading {0} to {1}".format(fileLink, savedir))
711+
for file_link in unique(files):
712+
log.debug("Downloading {0} to {1}".format(file_link, savedir))
713713
try:
714-
check_filename = self._request('HEAD', fileLink, auth=auth,
715-
stream=True)
714+
check_filename = self._request('HEAD', file_link, auth=auth)
716715
check_filename.raise_for_status()
717716
except requests.HTTPError as ex:
718717
if ex.response.status_code == 401:
719718
if skip_unauthorized:
720719
log.info("Access denied to {url}. Skipping to"
721-
" next file".format(url=fileLink))
720+
" next file".format(url=file_link))
722721
continue
723722
else:
724723
raise(ex)
725724

726-
if 'text/html' in check_filename.headers['Content-Type']:
725+
if 'text/html' in check_filename.headers.get('Content-Type', ''):
727726
raise ValueError("Bad query. This can happen if you "
728727
"attempt to download proprietary "
729728
"data when not logged in")
@@ -732,7 +731,7 @@ def download_files(self, files, savedir=None, cache=True,
732731
filename = re.search("filename=(.*)",
733732
check_filename.headers['Content-Disposition']).groups()[0]
734733
except KeyError:
735-
log.info(f"Unable to find filename for {fileLink} "
734+
log.info(f"Unable to find filename for {file_link} "
736735
"(missing Content-Disposition in header). "
737736
"Skipping to next file.")
738737
continue
@@ -742,42 +741,42 @@ def download_files(self, files, savedir=None, cache=True,
742741
filename)
743742

744743
try:
745-
self._download_file(fileLink,
744+
self._download_file(file_link,
746745
filename,
747746
timeout=self.TIMEOUT,
748747
auth=auth,
749748
cache=cache,
750749
method='GET',
751-
head_safe=True,
750+
head_safe=False,
752751
continuation=continuation)
753752

754753
downloaded_files.append(filename)
755754
except requests.HTTPError as ex:
756755
if ex.response.status_code == 401:
757756
if skip_unauthorized:
758757
log.info("Access denied to {url}. Skipping to"
759-
" next file".format(url=fileLink))
758+
" next file".format(url=file_link))
760759
continue
761760
else:
762761
raise(ex)
763762
elif ex.response.status_code == 403:
764-
log.error("Access denied to {url}".format(url=fileLink))
765-
if 'dataPortal' in fileLink and 'sso' not in fileLink:
763+
log.error("Access denied to {url}".format(url=file_link))
764+
if 'dataPortal' in file_link and 'sso' not in file_link:
766765
log.error("The URL may be incorrect. Try using "
767766
"{0} instead of {1}"
768-
.format(fileLink.replace('dataPortal/',
769-
'dataPortal/sso/'),
770-
fileLink))
767+
.format(file_link.replace('dataPortal/',
768+
'dataPortal/sso/'),
769+
file_link))
771770
raise ex
772771
elif ex.response.status_code == 500:
773772
# empirically, this works the second time most of the time...
774-
self._download_file(fileLink,
773+
self._download_file(file_link,
775774
filename,
776775
timeout=self.TIMEOUT,
777776
auth=auth,
778777
cache=cache,
779778
method='GET',
780-
head_safe=True,
779+
head_safe=False,
781780
continuation=continuation)
782781

783782
downloaded_files.append(filename)
@@ -1107,7 +1106,7 @@ def download_and_extract_files(self, urls, delete=True, regex=r'.*\.fits$',
11071106
expanded_files += [x for x in files['access_url'] if
11081107
filere.match(x.split('/')[-1])]
11091108
else:
1110-
tar_files.append(tar_file)
1109+
tar_files.append(url)
11111110

11121111
try:
11131112
# get the tar files

astroquery/alma/tests/test_alma.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,3 +410,34 @@ def test_galactic_query():
410410
radius=1*u.deg, get_query_payload=True)
411411

412412
assert result['ra_dec'] == SkyCoord(0*u.deg, 0*u.deg, frame='galactic').icrs.to_string() + ", 1.0"
413+
414+
415+
def test_download_files():
416+
def _requests_mock(method, url, **kwargs):
417+
response = Mock()
418+
response.headers = {
419+
'Content-Disposition': 'attachment; '
420+
'filename={}'.format(url.split('/')[-1])}
421+
return response
422+
423+
def _download_file_mock(url, file_name, **kwargs):
424+
return file_name
425+
alma = Alma()
426+
alma._request = Mock(side_effect=_requests_mock)
427+
alma._download_file = Mock(side_effect=_download_file_mock)
428+
downloaded_files = alma.download_files(['https://location/file1'])
429+
assert len(downloaded_files) == 1
430+
assert downloaded_files[0].endswith('file1')
431+
432+
alma._request.reset_mock()
433+
alma._download_file.reset_mock()
434+
downloaded_files = alma.download_files(['https://location/file1',
435+
'https://location/file2'])
436+
assert len(downloaded_files) == 2
437+
438+
# error cases
439+
alma._request = Mock()
440+
# no Content-Disposition results in no downloaded file
441+
alma._request.return_value = Mock(headers={})
442+
result = alma.download_files(['https://location/file1'])
443+
assert not result

0 commit comments

Comments
 (0)