Skip to content

Commit 4827362

Browse files
committed
Clean up paths in alma tests
1 parent 932fb79 commit 4827362

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

astroquery/alma/tests/test_alma_remote.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
# Licensed under a 3-clause BSD style license - see LICENSE.rst
2-
import tempfile
3-
import shutil
4-
import numpy as np
5-
import pytest
6-
72
from datetime import datetime
83
import os
4+
from pathlib import Path
95
from urllib.parse import urlparse
106
import re
117
from unittest.mock import Mock, MagicMock, patch
128

139
from astropy import coordinates
1410
from astropy import units as u
11+
import numpy as np
12+
import pytest
1513

1614
from astroquery.exceptions import CorruptDataWarning
1715
from astroquery.utils.commons import ASTROPY_LT_4_1
@@ -181,7 +179,7 @@ def test_data_info(self, tmp_path, alma):
181179
break
182180
assert file_url
183181
alma.download_files([file_url], savedir=tmp_path)
184-
assert os.stat(os.path.join(tmp_path, file)).st_size
182+
assert Path(tmp_path, file).stat().st_size
185183

186184
# mock downloading an entire program
187185
download_files_mock = Mock()
@@ -252,7 +250,7 @@ def test_download_and_extract(self, tmp_path, alma):
252250
[asdm_url], include_asdm=True, regex=r'.*\.py')
253251
delete_mock.assert_called_once_with(
254252
'cache_path/' + asdm_url.split('/')[-1])
255-
assert downloaded_asdm == [os.path.join(tmp_path, 'foo.py')]
253+
assert downloaded_asdm == [Path(tmp_path, 'foo.py')]
256254

257255
def test_doc_example(self, tmp_path, alma):
258256
alma.cache_location = tmp_path
@@ -536,44 +534,34 @@ def test_big_download_regression(alma):
536534

537535

538536
@pytest.mark.remote_data
539-
def test_download_html_file(alma):
537+
def test_download_html_file(alma, tmp_path):
538+
alma.cache_location = tmp_path
540539
result = alma.download_files(['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'.format(download_hostname)])
541540
assert result
542541

543542

544543
@pytest.mark.remote_data
545-
def test_verify_html_file(alma, caplog):
546-
# first, make sure the file is not cached (in case this test gets called repeatedly)
547-
# (we are hacking the file later in this test to trigger different failure modes so
548-
# we need it fresh)
549-
try:
550-
result = alma.download_files(['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'.format(download_hostname)], verify_only=True)
551-
local_filepath = result[0]
552-
os.remove(local_filepath)
553-
except FileNotFoundError:
554-
pass
555-
556-
caplog.clear()
544+
def test_verify_html_file(alma, caplog, tmp_path):
545+
alma.cache_location = tmp_path
557546

558547
# download the file
559548
result = alma.download_files(['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'.format(download_hostname)])
560549
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
561550

562551
result = alma.download_files(['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'.format(download_hostname)], verify_only=True)
563552
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
564-
local_filepath = result[0]
565-
existing_file_length = 66336
566-
assert f"Found cached file {local_filepath} with expected size {existing_file_length}." in caplog.text
553+
local_filepath = Path(result[0])
554+
expected_file_length = local_filepath.stat().st_size
555+
assert f"Found cached file {local_filepath} with expected size {expected_file_length}." in caplog.text
567556

568557
# manipulate the file
569558
with open(local_filepath, 'ab') as fh:
570559
fh.write(b"Extra Text")
571560

572561
caplog.clear()
573-
length = 66336
574-
existing_file_length = length + 10
562+
new_file_length = expected_file_length + 10
575563
with pytest.warns(expected_warning=CorruptDataWarning,
576-
match=f"Found cached file {local_filepath} with size {existing_file_length} > expected size {length}. The download is likely corrupted."):
564+
match=f"Found cached file {local_filepath} with size {new_file_length} > expected size {expected_file_length}. The download is likely corrupted."):
577565
result = alma.download_files(['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'.format(download_hostname)], verify_only=True)
578566
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
579567

@@ -584,10 +572,5 @@ def test_verify_html_file(alma, caplog):
584572
caplog.clear()
585573
result = alma.download_files(['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html'.format(download_hostname)], verify_only=True)
586574
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result[0]
587-
length = 66336
588575
existing_file_length = 10
589-
assert f"Found cached file {local_filepath} with size {existing_file_length} < expected size {length}. The download should be continued." in caplog.text
590-
591-
# cleanup: we don't want `test_download_html_file` to fail if this test is re-run
592-
if os.path.exists(local_filepath):
593-
os.remove(local_filepath)
576+
assert f"Found cached file {local_filepath} with size {existing_file_length} < expected size {expected_file_length}. The download should be continued." in caplog.text

0 commit comments

Comments
 (0)