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
@@ -49,15 +47,6 @@ def alma(request):
49
47
50
48
@pytest .mark .remote_data
51
49
class TestAlma :
52
- @pytest .fixture ()
53
- def temp_dir (self , request ):
54
- my_temp_dir = tempfile .mkdtemp ()
55
-
56
- def fin ():
57
- shutil .rmtree (my_temp_dir )
58
- request .addfinalizer (fin )
59
- return my_temp_dir
60
-
61
50
def test_public (self , alma ):
62
51
results = alma .query (payload = None , public = True , maxrec = 100 )
63
52
assert len (results ) == 100
@@ -68,8 +57,8 @@ def test_public(self, alma):
68
57
for row in results :
69
58
assert row ['data_rights' ] == 'Proprietary'
70
59
71
- def test_SgrAstar (self , temp_dir , alma ):
72
- alma .cache_location = temp_dir
60
+ def test_SgrAstar (self , tmp_path , alma ):
61
+ alma .cache_location = tmp_path
73
62
74
63
result_s = alma .query_object ('Sgr A*' , legacy_columns = True )
75
64
@@ -122,9 +111,9 @@ def test_ra_dec(self, alma):
122
111
assert len (result ) > 0
123
112
124
113
@pytest .mark .skipif ("SKIP_SLOW" )
125
- def test_m83 (self , temp_dir , alma ):
114
+ def test_m83 (self , tmp_path , alma ):
126
115
# Runs for over 9 minutes
127
- alma .cache_location = temp_dir
116
+ alma .cache_location = tmp_path
128
117
129
118
m83_data = alma .query_object ('M83' , science = True , legacy_columns = True )
130
119
uids = np .unique (m83_data ['Member ous id' ])
@@ -149,20 +138,20 @@ def test_data_proprietary(self, alma):
149
138
with pytest .raises (AttributeError ):
150
139
alma .is_proprietary ('uid://NON/EXI/STING' )
151
140
152
- def test_retrieve_data (self , temp_path , alma ):
141
+ def test_retrieve_data (self , tmp_path , alma ):
153
142
"""
154
143
Regression test for issue 2490 (the retrieval step will simply fail if
155
144
given a blank line, so all we're doing is testing that it runs)
156
145
"""
157
- alma .cache_location = temp_path
146
+ alma .cache_location = tmp_path
158
147
159
148
# small solar TP-only data set (<1 GB)
160
149
uid = 'uid://A001/X87c/X572'
161
150
162
151
alma .retrieve_data_from_uid ([uid ])
163
152
164
- def test_data_info (self , temp_dir , alma ):
165
- alma .cache_location = temp_dir
153
+ def test_data_info (self , tmp_path , alma ):
154
+ alma .cache_location = tmp_path
166
155
167
156
uid = 'uid://A001/X12a3/Xe9'
168
157
data_info = alma .get_data_info (uid , expand_tarfiles = True )
@@ -189,8 +178,8 @@ def test_data_info(self, temp_dir, alma):
189
178
file_url = url
190
179
break
191
180
assert file_url
192
- alma .download_files ([file_url ], savedir = temp_dir )
193
- assert os . stat ( os . path . join ( temp_dir , file )).st_size
181
+ alma .download_files ([file_url ], savedir = tmp_path )
182
+ assert Path ( tmp_path , file ). stat ( ).st_size
194
183
195
184
# mock downloading an entire program
196
185
download_files_mock = Mock ()
@@ -200,9 +189,9 @@ def test_data_info(self, temp_dir, alma):
200
189
comparison = download_files_mock .mock_calls [0 ][1 ] == data_info_tar ['access_url' ]
201
190
assert comparison .all ()
202
191
203
- def test_download_data (self , temp_dir , alma ):
192
+ def test_download_data (self , tmp_path , alma ):
204
193
# test only fits files from a program
205
- alma .cache_location = temp_dir
194
+ alma .cache_location = tmp_path
206
195
207
196
uid = 'uid://A001/X12a3/Xe9'
208
197
data_info = alma .get_data_info (uid , expand_tarfiles = True )
@@ -214,14 +203,14 @@ def test_download_data(self, temp_dir, alma):
214
203
alma ._download_file = download_mock
215
204
urls = [x ['access_url' ] for x in data_info
216
205
if fitsre .match (x ['access_url' ])]
217
- results = alma .download_files (urls , savedir = temp_dir )
206
+ results = alma .download_files (urls , savedir = tmp_path )
218
207
alma ._download_file .call_count == len (results )
219
208
assert len (results ) == len (urls )
220
209
221
- def test_download_and_extract (self , temp_dir , alma ):
210
+ def test_download_and_extract (self , tmp_path , alma ):
222
211
# TODO: slowish, runs for ~90s
223
212
224
- alma .cache_location = temp_dir
213
+ alma .cache_location = tmp_path
225
214
alma ._cycle0_tarfile_content_table = {'ID' : '' }
226
215
227
216
uid = 'uid://A001/X12a3/Xe9'
@@ -261,10 +250,10 @@ def test_download_and_extract(self, temp_dir, alma):
261
250
[asdm_url ], include_asdm = True , regex = r'.*\.py' )
262
251
delete_mock .assert_called_once_with (
263
252
'cache_path/' + asdm_url .split ('/' )[- 1 ])
264
- assert downloaded_asdm == [os . path . join ( temp_dir , 'foo.py' )]
253
+ assert downloaded_asdm == [Path ( tmp_path , 'foo.py' )]
265
254
266
- def test_doc_example (self , temp_dir , alma ):
267
- alma .cache_location = temp_dir
255
+ def test_doc_example (self , tmp_path , alma ):
256
+ alma .cache_location = tmp_path
268
257
m83_data = alma .query_object ('M83' , legacy_columns = True )
269
258
# the order can apparently sometimes change
270
259
# These column names change too often to keep testing.
@@ -299,8 +288,8 @@ def test_doc_example(self, temp_dir, alma):
299
288
# file sizes are replaced with -1
300
289
assert (totalsize_mous .to (u .GB ).value > 52 )
301
290
302
- def test_query (self , temp_dir , alma ):
303
- alma .cache_location = temp_dir
291
+ def test_query (self , tmp_path , alma ):
292
+ alma .cache_location = tmp_path
304
293
305
294
result = alma .query (payload = {'start_date' : '<11-11-2011' },
306
295
public = False , legacy_columns = True , science = True )
@@ -395,9 +384,9 @@ def test_user(self, alma):
395
384
# This has been reported, as it is definitely a bug.
396
385
@pytest .mark .xfail
397
386
@pytest .mark .bigdata
398
- def test_cycle1 (self , temp_dir , alma ):
387
+ def test_cycle1 (self , tmp_path , alma ):
399
388
# About 500 MB
400
- alma .cache_location = temp_dir
389
+ alma .cache_location = tmp_path
401
390
target = 'NGC4945'
402
391
project_code = '2012.1.00912.S'
403
392
payload = {'project_code' : project_code ,
@@ -442,9 +431,9 @@ def test_cycle1(self, temp_dir, alma):
442
431
443
432
@pytest .mark .skipif ("SKIP_SLOW" )
444
433
@pytest .mark .xfail (reason = "Not working anymore" )
445
- def test_cycle0 (self , temp_dir , alma ):
434
+ def test_cycle0 (self , tmp_path , alma ):
446
435
# About 20 MB
447
- alma .cache_location = temp_dir
436
+ alma .cache_location = tmp_path
448
437
449
438
target = 'NGC4945'
450
439
project_code = '2011.0.00121.S'
@@ -477,7 +466,7 @@ def test_cycle0(self, temp_dir, alma):
477
466
# There are 10 small files, but only 8 unique
478
467
assert len (data ) == 8
479
468
480
- def test_keywords (self , temp_dir , alma ):
469
+ def test_keywords (self , tmp_path , alma ):
481
470
482
471
alma .help_tap ()
483
472
result = alma .query_tap (
@@ -545,44 +534,34 @@ def test_big_download_regression(alma):
545
534
546
535
547
536
@pytest .mark .remote_data
548
- def test_download_html_file (alma ):
537
+ def test_download_html_file (alma , tmp_path ):
538
+ alma .cache_location = tmp_path
549
539
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )])
550
540
assert result
551
541
552
542
553
543
@pytest .mark .remote_data
554
- def test_verify_html_file (alma , caplog ):
555
- # first, make sure the file is not cached (in case this test gets called repeatedly)
556
- # (we are hacking the file later in this test to trigger different failure modes so
557
- # we need it fresh)
558
- try :
559
- result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )], verify_only = True )
560
- local_filepath = result [0 ]
561
- os .remove (local_filepath )
562
- except FileNotFoundError :
563
- pass
564
-
565
- caplog .clear ()
544
+ def test_verify_html_file (alma , caplog , tmp_path ):
545
+ alma .cache_location = tmp_path
566
546
567
547
# download the file
568
548
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )])
569
549
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result [0 ]
570
550
571
551
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )], verify_only = True )
572
552
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result [0 ]
573
- local_filepath = result [0 ]
574
- existing_file_length = 66336
575
- 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
576
556
577
557
# manipulate the file
578
558
with open (local_filepath , 'ab' ) as fh :
579
559
fh .write (b"Extra Text" )
580
560
581
561
caplog .clear ()
582
- length = 66336
583
- existing_file_length = length + 10
562
+ new_file_length = expected_file_length + 10
584
563
with pytest .warns (expected_warning = CorruptDataWarning ,
585
- 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." ):
586
565
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )], verify_only = True )
587
566
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result [0 ]
588
567
@@ -593,10 +572,5 @@ def test_verify_html_file(alma, caplog):
593
572
caplog .clear ()
594
573
result = alma .download_files (['https://{}/dataPortal/member.uid___A001_X1284_X1353.qa2_report.html' .format (download_hostname )], verify_only = True )
595
574
assert 'member.uid___A001_X1284_X1353.qa2_report.html' in result [0 ]
596
- length = 66336
597
575
existing_file_length = 10
598
- assert f"Found cached file { local_filepath } with size { existing_file_length } < expected size { length } . The download should be continued." in caplog .text
599
-
600
- # cleanup: we don't want `test_download_html_file` to fail if this test is re-run
601
- if os .path .exists (local_filepath ):
602
- 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