Skip to content

Commit 77dcba4

Browse files
committed
EHSTSWRQ-54: tests with temporary paths
1 parent 1a58a5c commit 77dcba4

File tree

4 files changed

+112
-33
lines changed

4 files changed

+112
-33
lines changed

astroquery/esa/integral/core.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ def download_science_windows(self, *, science_windows=None, observation_id=None,
348348
349349
Returns
350350
-------
351-
The path and filename of the file with science windows
351+
If read_fits=True, a list with objects containing filename, path and FITS file opened with the
352+
science windows. If read_fits=False, the path of the downloaded file
352353
"""
353354

354355
# Validate and retrieve the correct value
@@ -469,7 +470,7 @@ def get_long_term_timeseries(self, target_name, *, instrument=None, band=None, p
469470
Returns
470471
-------
471472
If read_fits=True, a list with objects containing filename, path and FITS file opened with long
472-
term timeseries. If read_fits=False, return the path of the downloaded file
473+
term timeseries. If read_fits=False, the path of the downloaded file
473474
"""
474475

475476
value = self.__get_instrument_or_band(instrument=instrument, band=band)
@@ -516,7 +517,7 @@ def get_short_term_timeseries(self, target_name, epoch, instrument=None, band=No
516517
Returns
517518
-------
518519
If read_fits=True, a list with objects containing filename, path and FITS file opened with short
519-
term timeseries. If read_fits=False, return the path of the downloaded file
520+
term timeseries. If read_fits=False, the path of the downloaded file
520521
521522
"""
522523

@@ -569,7 +570,7 @@ def get_spectra(self, target_name, epoch, instrument=None, band=None, *, path=''
569570
Returns
570571
-------
571572
If read_fits=True, a list with objects containing filename, path and FITS file opened with spectra.
572-
If read_fits=False, return a list of paths of the downloaded files
573+
If read_fits=False, a list of paths of the downloaded files
573574
"""
574575

575576
value = self.__get_instrument_or_band(instrument=instrument, band=band)
@@ -633,7 +634,7 @@ def get_mosaic(self, epoch, instrument=None, band=None, *, path='', filename=Non
633634
Returns
634635
-------
635636
If read_fits=True, a list with objects containing filename, path and FITS file opened with mosaics.
636-
If read_fits=False, return a list of paths of the downloaded files
637+
If read_fits=False, a list of paths of the downloaded files
637638
"""
638639

639640
self.__validate_epoch(epoch=epoch,

astroquery/esa/integral/tests/test_isla_remote.py

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
1010
"""
1111
import os
12+
import tempfile
1213

14+
from astropy.coordinates import SkyCoord
1315
from astroquery.esa.integral import IntegralClass
1416
import pytest
1517
from pyvo import DALQueryError
@@ -29,6 +31,10 @@ def close_files(file_list):
2931
close_file(file['fits'])
3032

3133

34+
def create_temp_folder():
35+
return tempfile.TemporaryDirectory()
36+
37+
3238
@pytest.mark.remote_data
3339
class TestIntegralRemote:
3440

@@ -54,10 +60,13 @@ def test_query_tap(self):
5460
assert 'Unknown table' in err.value.args[0]
5561

5662
# Store the result in a file
57-
filename = 'query_tap.votable'
63+
temp_folder = create_temp_folder()
64+
filename = os.path.join(temp_folder.name, 'query_tap.votable')
5865
isla.query_tap('select top 10 * from ivoa.obscore;', output_file=filename)
5966
assert os.path.exists(filename)
6067

68+
temp_folder.cleanup()
69+
6170
def test_get_sources(self):
6271
isla = IntegralClass()
6372
sources = isla.get_sources(target_name='crab')
@@ -118,9 +127,13 @@ def test_get_observations(self):
118127
def test_download_science_windows(self):
119128
# Simple download
120129
isla = IntegralClass()
121-
sc = isla.download_science_windows(science_windows='008100430010')
130+
temp_folder = create_temp_folder()
131+
output_file = os.path.join(temp_folder.name, 'sc')
132+
sc = isla.download_science_windows(science_windows='008100430010', output_file=output_file)
122133
assert len(sc) > 1
123134

135+
close_files(sc)
136+
124137
# Only one parameter is allowed
125138
with pytest.raises(ValueError) as err:
126139
isla.download_science_windows(science_windows='008100430010', observation_id='03200490001')
@@ -131,17 +144,21 @@ def test_download_science_windows(self):
131144
isla.download_science_windows(revolution=12)
132145
assert 'Input parameters are wrong' in err.value.args[0]
133146

147+
temp_folder.cleanup()
148+
134149
def test_get_timeline(self):
135150
isla = IntegralClass()
136-
timeline = isla.get_timeline(coordinates='83.63320922851562 22.01447105407715')
151+
coords = SkyCoord(ra=83.63320922851562, dec=22.01447105407715, unit="deg")
152+
timeline = isla.get_timeline(coordinates=coords)
137153

138154
assert timeline is not None
139155
assert 'timeline' in timeline
140156
assert timeline['total_items'] == len(timeline['timeline'])
141157

142158
# No timeline has been found
159+
zero_coords = SkyCoord(ra=0, dec=0, unit="deg")
143160
with pytest.raises(ValueError) as err:
144-
isla.get_timeline(coordinates='0 0', radius=0.8)
161+
isla.get_timeline(coordinates=zero_coords, radius=0.8)
145162
assert 'No timeline is available for the current coordinates and radius.' in err.value.args[0]
146163

147164
def test_get_epochs(self):
@@ -167,12 +184,16 @@ def test_get_epochs(self):
167184
assert len(epochs) == 0
168185

169186
def test_get_long_term_timeseries(self):
187+
temp_folder = create_temp_folder()
188+
170189
isla = IntegralClass()
171-
ltt = isla.get_long_term_timeseries(target_name='J174537.0-290107', instrument='jem-x')
190+
ltt = isla.get_long_term_timeseries(target_name='J174537.0-290107', instrument='jem-x', path=temp_folder.name)
172191

173192
assert len(ltt) > 0
174193
assert 'fits' in ltt[0]
175194

195+
close_files(ltt)
196+
176197
# No correct instrument or band
177198
with pytest.raises(ValueError) as err:
178199
isla.get_long_term_timeseries(target_name='J174537.0-290107', instrument='test')
@@ -182,21 +203,26 @@ def test_get_long_term_timeseries(self):
182203
ltt = isla.get_long_term_timeseries(target_name='star', instrument='jem-x')
183204
assert ltt is None
184205

206+
temp_folder.cleanup()
207+
185208
def test_get_short_term_timeseries(self):
209+
temp_folder = create_temp_folder()
210+
186211
isla = IntegralClass()
187-
stt = isla.get_short_term_timeseries(target_name='J011705.1-732636', band='28_40', epoch='0745_06340000001')
212+
stt = isla.get_short_term_timeseries(target_name='J011705.1-732636', band='28_40', epoch='0745_06340000001',
213+
path=temp_folder.name)
188214

189215
assert len(stt) > 0
190216
assert 'fits' in stt[0]
191217

218+
close_files(stt)
219+
192220
# No correct instrument or band
193-
print('error instrument')
194221
with pytest.raises(ValueError) as err:
195222
isla.get_short_term_timeseries(target_name='J011705.1-732636', band='1234', epoch='0745_06340000001')
196223
assert 'This is not a valid value for instrument or band.' in err.value.args[0]
197224

198225
# No correct epoch
199-
print('error band')
200226
with pytest.raises(ValueError) as err:
201227
isla.get_short_term_timeseries(target_name='J011705.1-732636', band='28_40', epoch='123456')
202228
assert 'Epoch 123456 is not available for this target and instrument/band.' in err.value.args[0]
@@ -206,13 +232,20 @@ def test_get_short_term_timeseries(self):
206232
isla.get_short_term_timeseries(target_name='star', band='28_40', epoch='0745_06340000001')
207233
assert 'Epoch 0745_06340000001 is not available for this target and instrument/band.' in err.value.args[0]
208234

235+
temp_folder.cleanup()
236+
209237
def test_get_spectra(self):
238+
temp_folder = create_temp_folder()
239+
210240
isla = IntegralClass()
211-
spectra = isla.get_spectra(target_name='J011705.1-732636', instrument='ibis', epoch='0745_06340000001')
241+
spectra = isla.get_spectra(target_name='J011705.1-732636', instrument='ibis', epoch='0745_06340000001',
242+
path=temp_folder.name)
212243

213244
assert len(spectra) > 0
214245
assert 'fits' in spectra[0]
215246

247+
close_files(spectra)
248+
216249
# No correct instrument or band
217250
with pytest.raises(ValueError) as err:
218251
isla.get_spectra(target_name='J011705.1-732636', instrument='camera', epoch='0745_06340000001')
@@ -228,13 +261,19 @@ def test_get_spectra(self):
228261
isla.get_spectra(target_name='star', instrument='ibis', epoch='0745_06340000001')
229262
assert 'Epoch 0745_06340000001 is not available for this target and instrument/band.' in err.value.args[0]
230263

264+
temp_folder.cleanup()
265+
231266
def test_get_mosaics(self):
267+
temp_folder = create_temp_folder()
268+
232269
isla = IntegralClass()
233-
mosaics = isla.get_mosaic(epoch='0727_88601650001', instrument='ibis')
270+
mosaics = isla.get_mosaic(epoch='0727_88601650001', instrument='ibis', path=temp_folder.name)
234271

235272
assert len(mosaics) > 0
236273
assert 'fits' in mosaics[0]
237274

275+
close_files(mosaics)
276+
238277
# No correct instrument or band
239278
with pytest.raises(ValueError) as err:
240279
isla.get_mosaic(epoch='0727_88601650001', instrument='camera')
@@ -244,3 +283,5 @@ def test_get_mosaics(self):
244283
with pytest.raises(ValueError) as err:
245284
isla.get_mosaic(epoch='123456', instrument='ibis')
246285
assert 'Epoch 123456 is not available for this target and instrument/band.' in err.value.args[0]
286+
287+
temp_folder.cleanup()

astroquery/esa/integral/tests/test_isla_tap.py

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
1010
"""
1111
import os
12+
import shutil
13+
import tempfile
1214

1315
from astropy.coordinates import SkyCoord
1416
from astroquery.esa.integral import IntegralClass
@@ -30,6 +32,16 @@ def data_path(filename):
3032
return os.path.join(data_dir, filename)
3133

3234

35+
def create_temp_folder():
36+
return tempfile.TemporaryDirectory()
37+
38+
39+
def copy_to_temporal_path(data_path, temp_folder, filename):
40+
temp_data_dir = os.path.join(temp_folder.name, filename)
41+
shutil.copy(data_path, temp_data_dir)
42+
return temp_data_dir
43+
44+
3345
def close_file(file):
3446
file.close()
3547

@@ -325,7 +337,6 @@ def test_download_science_windows_error(self, instrument_band_mock, download_moc
325337
@patch('astroquery.esa.integral.core.IntegralClass.get_instrument_band_map')
326338
def test_download_science_windows(self, instrument_band_mock, download_mock):
327339
instrument_band_mock.return_value = mocks.get_instrument_bands()
328-
download_mock.return_value = 'file.test'
329340

330341
isla = IntegralClass()
331342
with pytest.raises(ValueError) as err:
@@ -336,11 +347,19 @@ def test_download_science_windows(self, instrument_band_mock, download_mock):
336347
isla.download_science_windows(science_windows='sc', observation_id='obs')
337348
assert 'Only one parameter can be provided at a time.' in err.value.args[0]
338349

339-
isla.download_science_windows(science_windows='sc')
350+
temp_path = create_temp_folder()
351+
temp_file = copy_to_temporal_path(data_path=data_path('zip_file.zip'), temp_folder=temp_path,
352+
filename='zip_file.zip')
353+
download_mock.return_value = temp_file
354+
355+
sc = isla.download_science_windows(science_windows='sc')
340356

341357
args, kwargs = download_mock.call_args
342358
assert kwargs['params']['RETRIEVAL_TYPE'] == 'SCW'
343359

360+
close_files(sc)
361+
temp_path.cleanup()
362+
344363
@patch('astroquery.esa.integral.core.pyvo.dal.TAPService.capabilities', [])
345364
@patch('astroquery.esa.utils.utils.execute_servlet_request')
346365
@patch('astroquery.esa.integral.core.IntegralClass.get_instrument_band_map')
@@ -349,7 +368,8 @@ def test_get_timeline(self, instrument_band_mock, servlet_mock):
349368
servlet_mock.return_value = mocks.get_mock_timeline()
350369

351370
isla = IntegralClass()
352-
timeline = isla.get_timeline(coordinates='83.63320922851562 22.01447105407715')
371+
coords = SkyCoord(ra=83.63320922851562, dec=22.01447105407715, unit="deg")
372+
timeline = isla.get_timeline(coordinates=coords)
353373

354374
assert len(timeline['timeline']['scwRevs']) > 0
355375

@@ -412,7 +432,11 @@ def test_get_long_term_timeseries_exception(self, instrument_band_mock, log_mock
412432
@patch('astroquery.esa.integral.core.IntegralClass.get_instrument_band_map')
413433
def test_get_long_term_timeseries(self, instrument_band_mock, download_mock):
414434
instrument_band_mock.return_value = mocks.get_instrument_bands()
415-
download_mock.return_value = data_path('zip_file.zip')
435+
436+
temp_path = create_temp_folder()
437+
temp_file = copy_to_temporal_path(data_path=data_path('zip_file.zip'), temp_folder=temp_path,
438+
filename='zip_file.zip')
439+
download_mock.return_value = temp_file
416440

417441
isla = IntegralClass()
418442
mock_instrument_bands(isla_module=isla)
@@ -423,6 +447,7 @@ def test_get_long_term_timeseries(self, instrument_band_mock, download_mock):
423447
read_fits=False)
424448
assert type(lt_timeseries_list_compressed) is str
425449
close_files(lt_timeseries_list_extracted)
450+
temp_path.cleanup()
426451

427452
@patch('astroquery.esa.integral.core.pyvo.dal.TAPService.capabilities', [])
428453
@patch('astroquery.esa.integral.core.log')
@@ -482,7 +507,10 @@ def test_get_short_term_timeseries(self, instrument_band_mock, epoch_mock, downl
482507

483508
instrument_band_mock.return_value = mocks.get_instrument_bands()
484509
epoch_mock.return_value = {'epoch': ['time']}
485-
download_mock.return_value = data_path('tar_file.tar')
510+
temp_path = create_temp_folder()
511+
temp_file = copy_to_temporal_path(data_path=data_path('tar_file.tar'), temp_folder=temp_path,
512+
filename='tar_file.tar')
513+
download_mock.return_value = temp_file
486514

487515
isla = IntegralClass()
488516
mock_instrument_bands(isla_module=isla)
@@ -495,6 +523,7 @@ def test_get_short_term_timeseries(self, instrument_band_mock, epoch_mock, downl
495523
assert type(st_timeseries_list_compressed) is str
496524

497525
close_files(st_timeseries_list_extracted)
526+
temp_path.cleanup()
498527

499528
@patch('astroquery.esa.integral.core.pyvo.dal.TAPService.capabilities', [])
500529
@patch('astroquery.esa.integral.core.log')
@@ -558,7 +587,10 @@ def test_get_spectra(self, instrument_band_mock, servlet_mock, epoch_mock, downl
558587
instrument_band_mock.return_value = mocks.get_instrument_bands()
559588
servlet_mock.return_value = mocks.get_mock_spectra()
560589
epoch_mock.return_value = {'epoch': ['today']}
561-
download_mock.return_value = data_path('tar_file.tar')
590+
temp_path = create_temp_folder()
591+
temp_file = copy_to_temporal_path(data_path=data_path('tar_file.tar'), temp_folder=temp_path,
592+
filename='tar_file.tar')
593+
download_mock.return_value = temp_file
562594

563595
isla = IntegralClass()
564596
mock_instrument_bands(isla_module=isla)
@@ -571,6 +603,7 @@ def test_get_spectra(self, instrument_band_mock, servlet_mock, epoch_mock, downl
571603
assert type(spectra_list_compressed) is list
572604

573605
close_files(spectra_list_extracted)
606+
temp_path.cleanup()
574607

575608
@patch('astroquery.esa.integral.core.pyvo.dal.TAPService.capabilities', [])
576609
@patch('astroquery.esa.integral.core.log')
@@ -614,7 +647,10 @@ def test_get_mosaic(self, instrument_band_mock, servlet_mock, epoch_mock, downlo
614647
instrument_band_mock.return_value = mocks.get_instrument_bands()
615648
servlet_mock.return_value = mocks.get_mock_mosaic()
616649
epoch_mock.return_value = {'epoch': ['today']}
617-
download_mock.return_value = data_path('tar_gz_file.gz')
650+
temp_path = create_temp_folder()
651+
temp_file = copy_to_temporal_path(data_path=data_path('tar_gz_file.gz'), temp_folder=temp_path,
652+
filename='tar_gz_file.gz')
653+
download_mock.return_value = temp_file
618654

619655
isla = IntegralClass()
620656
mock_instrument_bands(isla_module=isla)
@@ -625,6 +661,7 @@ def test_get_mosaic(self, instrument_band_mock, servlet_mock, epoch_mock, downlo
625661

626662
assert type(mosaics_compressed) is list
627663
close_files(mosaics_extracted)
664+
temp_path.cleanup()
628665

629666
@patch('astroquery.esa.integral.core.pyvo.dal.TAPService.capabilities', [])
630667
@patch('astroquery.esa.utils.utils.execute_servlet_request')

0 commit comments

Comments
 (0)