1
1
# 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
-
7
2
from datetime import datetime
8
3
import os
4
+ from pathlib import Path
9
5
from urllib .parse import urlparse
10
6
import re
11
7
from unittest .mock import Mock , MagicMock , patch
12
8
13
9
from astropy import coordinates
14
10
from astropy import units as u
11
+ import numpy as np
12
+ import pytest
15
13
16
14
from astroquery .exceptions import CorruptDataWarning
17
15
from astroquery .utils .commons import ASTROPY_LT_4_1
@@ -181,7 +179,7 @@ def test_data_info(self, tmp_path, alma):
181
179
break
182
180
assert file_url
183
181
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
185
183
186
184
# mock downloading an entire program
187
185
download_files_mock = Mock ()
@@ -252,7 +250,7 @@ def test_download_and_extract(self, tmp_path, alma):
252
250
[asdm_url ], include_asdm = True , regex = r'.*\.py' )
253
251
delete_mock .assert_called_once_with (
254
252
'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' )]
256
254
257
255
def test_doc_example (self , tmp_path , alma ):
258
256
alma .cache_location = tmp_path
@@ -536,44 +534,34 @@ def test_big_download_regression(alma):
536
534
537
535
538
536
@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
540
539
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )])
541
540
assert result
542
541
543
542
544
543
@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
557
546
558
547
# download the file
559
548
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )])
560
549
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result [0 ]
561
550
562
551
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )], verify_only = True )
563
552
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
567
556
568
557
# manipulate the file
569
558
with open (local_filepath , 'ab' ) as fh :
570
559
fh .write (b"Extra Text" )
571
560
572
561
caplog .clear ()
573
- length = 66336
574
- existing_file_length = length + 10
562
+ new_file_length = expected_file_length + 10
575
563
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." ):
577
565
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )], verify_only = True )
578
566
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result [0 ]
579
567
@@ -584,10 +572,5 @@ def test_verify_html_file(alma, caplog):
584
572
caplog .clear ()
585
573
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )], verify_only = True )
586
574
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result [0 ]
587
- length = 66336
588
575
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