Skip to content

Commit 4f73f02

Browse files
committed
fixed the test
1 parent 0f7f442 commit 4f73f02

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

astroquery/alma/core.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
_gen_science_sql, _gen_spec_res_sql, ALMA_DATE_FORMAT
3232
from . import conf, auth_urls
3333
from astroquery.utils.commons import ASTROPY_LT_4_1
34+
from astroquery.exceptions import CorruptDataWarning
3435

3536
__all__ = {'AlmaClass', 'ALMA_BANDS'}
3637

@@ -750,7 +751,6 @@ def download_files(self, files, savedir=None, cache=True,
750751

751752
if verify_only:
752753
existing_file_length = os.stat(filename).st_size
753-
print(f"{filename}: {existing_file_length}")
754754
if 'content-length' in check_filename.headers:
755755
length = int(check_filename.headers['content-length'])
756756
if length == 0:
@@ -761,9 +761,9 @@ def download_files(self, files, savedir=None, cache=True,
761761
log.info(f"Found cached file {filename} with size {existing_file_length} < expected "
762762
f"size {length}. The download should be continued.")
763763
elif existing_file_length > length:
764-
print("Issuing a warning")
765764
warnings.warn(f"Found cached file {filename} with size {existing_file_length} > expected "
766-
f"size {length}. The download is likely corrupted.")
765+
f"size {length}. The download is likely corrupted.",
766+
CorruptDataWarning)
767767
else:
768768
raise ValueError("It should not be possible to reach this state.")
769769
else:

astroquery/alma/tests/test_alma_remote.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from astropy import coordinates
1414
from astropy import units as u
1515

16+
from astroquery.exceptions import CorruptDataWarning
1617
from astroquery.utils.commons import ASTROPY_LT_4_1
1718
from .. import Alma
1819

@@ -674,10 +675,10 @@ def test_verify_html_file(alma, caplog):
674675

675676
# download the file
676677
result = alma.download_files(['https://almascience.nao.ac.jp/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'])
677-
assert result
678+
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
678679

679680
result = alma.download_files(['https://almascience.nao.ac.jp/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'], verify_only=True)
680-
assert result
681+
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
681682
local_filepath = result[0]
682683
existing_file_length = 66336
683684
assert f"Found cached file {local_filepath} with expected size {existing_file_length}." in caplog.text
@@ -686,24 +687,25 @@ def test_verify_html_file(alma, caplog):
686687
with open(local_filepath, 'ab') as fh:
687688
fh.write(b"Extra Text")
688689

689-
print("Trying the failed version now")
690690
caplog.clear()
691-
with warnings.catch_warnings(record=True) as ww:
691+
length = 66336
692+
existing_file_length = length + 10
693+
with pytest.warns(expected_warning=CorruptDataWarning,
694+
match=f"Found cached file {local_filepath} with size {existing_file_length} > expected size {length}. The download is likely corrupted."):
692695
result = alma.download_files(['https://almascience.nao.ac.jp/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'], verify_only=True)
693-
assert result
694-
length = 66336
695-
existing_file_length = length + 10
696-
print(f"WARNING: {str(ww)}")
697-
print(f"caplog: {caplog.text}")
698-
assert f"Found cached file {local_filepath} with size {existing_file_length} > expected size {length}. The download is likely corrupted." in str(ww)
696+
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
699697

700698
# manipulate the file: make it small
701699
with open(local_filepath, 'wb') as fh:
702700
fh.write(b"Empty Text")
703701

704702
caplog.clear()
705703
result = alma.download_files(['https://almascience.nao.ac.jp/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'], verify_only=True)
706-
assert result
704+
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
707705
length = 66336
708706
existing_file_length = 10
709707
assert f"Found cached file {local_filepath} with size {existing_file_length} < expected size {length}. The download should be continued." in caplog.text
708+
709+
# cleanup: we don't want `test_download_html_file` to fail if this test is re-run
710+
if os.path.exists(local_filepath):
711+
os.remove(local_filepath)

astroquery/exceptions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
__all__ = ['TimeoutError', 'InvalidQueryError', 'RemoteServiceError',
99
'TableParseError', 'LoginError', 'ResolverError',
1010
'NoResultsWarning', 'LargeQueryWarning', 'InputWarning',
11-
'AuthenticationWarning', 'MaxResultsWarning']
11+
'AuthenticationWarning', 'MaxResultsWarning', 'CorruptDataWarning']
1212

1313

1414
class TimeoutError(Exception):
@@ -98,6 +98,14 @@ class MaxResultsWarning(AstropyWarning):
9898
pass
9999

100100

101+
class CorruptDataWarning(AstropyWarning):
102+
"""
103+
Astroquery warning class to be issued when there is a sign that the
104+
(partially) downloaded data are corrupt.
105+
"""
106+
pass
107+
108+
101109
class EmptyResponseError(ValueError):
102110
"""
103111
Astroquery error class to be raised when the query returns an empty result

0 commit comments

Comments
 (0)