Skip to content

Commit db6b28c

Browse files
authored
Merge pull request #3227 from esdc-esac-esa-int/ESA_euclid_creates_temp_directory_in_the_repo
Euclid: PR for issue #3226
2 parents e2f7bd5 + 1f45a44 commit db6b28c

File tree

2 files changed

+105
-39
lines changed

2 files changed

+105
-39
lines changed

CHANGES.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ esa.euclid
1414

1515
- New module to access the ESA Euclid Archive. [#3216]
1616

17+
- Tests remove temp directories. [#3226]
18+
1719
mast
1820
^^^^
1921

astroquery/esa/euclid/tests/test_euclidtap.py

Lines changed: 103 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import glob
1010
import os
1111
import shutil
12+
from datetime import datetime
1213
from pathlib import Path
1314
from unittest.mock import patch
1415

@@ -50,6 +51,25 @@ def data_path(filename):
5051
return os.path.join(data_dir, filename)
5152

5253

54+
@pytest.fixture(autouse=True)
55+
def run_before_and_after_tests():
56+
yield
57+
remove_temp_dir()
58+
59+
60+
@pytest.fixture(scope="module")
61+
def mock_querier():
62+
conn_handler = DummyConnHandler()
63+
tap_plus = TapPlus(url="http://test:1111/tap", connhandler=conn_handler)
64+
65+
launch_response = DummyResponse(200)
66+
launch_response.set_data(method="POST", body=JOB_DATA)
67+
# The query contains decimals: default response is more robust.
68+
conn_handler.set_default_response(launch_response)
69+
70+
return EuclidClass(tap_plus_conn_handler=conn_handler, datalink_handler=tap_plus, show_server_messages=False)
71+
72+
5373
@pytest.fixture(scope="module")
5474
def mock_querier_async():
5575
conn_handler = DummyConnHandler()
@@ -657,7 +677,7 @@ def test_get_product_list_errors():
657677
assert str(exc_info.value).startswith("Invalid product type DpdMerBksMosaic.")
658678

659679

660-
def test_get_product_by_product_id():
680+
def test_get_product_by_product_id(tmp_path_factory):
661681
conn_handler = DummyConnHandler()
662682
tap_plus = TapPlus(url="http://test:1111/tap", data_context='data', client_id='ASTROQUERY',
663683
connhandler=conn_handler)
@@ -669,12 +689,24 @@ def test_get_product_by_product_id():
669689

670690
tap = EuclidClass(tap_plus_conn_handler=conn_handler, datalink_handler=tap_plus, show_server_messages=False)
671691

672-
result = tap.get_product(product_id=123456789, output_file=None)
692+
result = tap.get_product(product_id='123456789', output_file=None)
673693

674694
assert result is not None
675695

696+
now = datetime.now()
697+
dirs = glob.glob(os.path.join(os.getcwd(), "temp_" + now.strftime("%Y%m%d") + '_*'))
698+
699+
assert len(dirs) == 1
700+
assert dirs[0] is not None
701+
676702
remove_temp_dir()
677703

704+
fits_file = os.path.join(tmp_path_factory.mktemp("euclid_tmp"), 'my_fits_file.fits')
705+
706+
result = tap.get_product(product_id='123456789', output_file=fits_file)
707+
708+
assert result is not None
709+
678710

679711
def test_get_product():
680712
conn_handler = DummyConnHandler()
@@ -693,6 +725,12 @@ def test_get_product():
693725

694726
assert result is not None
695727

728+
now = datetime.now()
729+
dirs = glob.glob(os.path.join(os.getcwd(), "temp_" + now.strftime("%Y%m%d") + '_*'))
730+
731+
assert len(dirs) == 1
732+
assert dirs[0] is not None
733+
696734
remove_temp_dir()
697735

698736

@@ -711,7 +749,7 @@ def test_get_product_exceptions():
711749
with pytest.raises(ValueError) as exc_info:
712750
tap.get_product(file_name=None, product_id=None, output_file=None)
713751

714-
assert str(exc_info.value).startswith("'file_name' and 'product_id' are both None")
752+
str(exc_info.value).startswith("'file_name' and 'product_id' are both None")
715753

716754

717755
@patch.object(TapPlus, 'load_data')
@@ -741,8 +779,16 @@ def test_get_product_exceptions_2(mock_load_data, caplog):
741779
mssg = "Cannot retrieve products for file_name hola.fits or product_id None: launch_job_async Exception"
742780
assert caplog.records[1].msg == mssg
743781

782+
now = datetime.now()
783+
dirs = glob.glob(os.path.join(os.getcwd(), "temp_" + now.strftime("%Y%m%d") + '_*'))
784+
785+
assert len(dirs) == 1
786+
assert dirs[0] is not None
787+
788+
remove_temp_dir()
789+
744790

745-
def test_get_obs_products():
791+
def test_get_observation_products(tmp_path_factory):
746792
conn_handler = DummyConnHandler()
747793
tap_plus = TapPlus(url="http://test:1111/tap", data_context='data', client_id='ASTROQUERY',
748794
connhandler=conn_handler)
@@ -762,8 +808,22 @@ def test_get_obs_products():
762808

763809
assert result is not None
764810

811+
now = datetime.now()
812+
dirs = glob.glob(os.path.join(os.getcwd(), "temp_" + now.strftime("%Y%m%d") + '_*'))
813+
814+
assert len(dirs) == 1
815+
assert dirs[0] is not None
816+
817+
remove_temp_dir()
818+
819+
fits_file = os.path.join(tmp_path_factory.mktemp("euclid_tmp"), 'my_fits_file.fits')
820+
821+
result = tap.get_observation_products(id='13', product_type='mosaic', filter='VIS', output_file=fits_file)
822+
823+
assert result is not None
824+
765825

766-
def test_get_obs_products_exceptions():
826+
def test_get_observation_products_exceptions():
767827
conn_handler = DummyConnHandler()
768828
tap_plus = TapPlus(url="http://test:1111/tap", data_context='data', client_id='ASTROQUERY',
769829
connhandler=conn_handler)
@@ -781,18 +841,18 @@ def test_get_obs_products_exceptions():
781841
assert str(exc_info.value).startswith("Missing required argument: 'observation_id'")
782842

783843
with pytest.raises(ValueError) as exc_info:
784-
tap.get_observation_products(id=12, product_type=None, filter='VIS', output_file=None)
844+
tap.get_observation_products(id='12', product_type=None, filter='VIS', output_file=None)
785845

786846
assert str(exc_info.value).startswith("Missing required argument: 'product_type'")
787847

788848
with pytest.raises(ValueError) as exc_info:
789-
tap.get_observation_products(id=12, product_type='XXXXXXXX', filter='VIS', output_file=None)
849+
tap.get_observation_products(id='12', product_type='XXXXXXXX', filter='VIS', output_file=None)
790850

791851
assert str(exc_info.value).startswith("Invalid product type XXXXXXXX. Valid values: ['observation', 'mosaic']")
792852

793853

794854
@patch.object(TapPlus, 'load_data')
795-
def test_get_obs_products_exceptions_2(mock_load_data, caplog):
855+
def test_get_observation_products_exceptions_2(mock_load_data, caplog):
796856
conn_handler = DummyConnHandler()
797857
tap_plus = TapPlus(url="http://test:1111/tap", data_context='data', client_id='ASTROQUERY',
798858
connhandler=conn_handler)
@@ -806,18 +866,20 @@ def test_get_obs_products_exceptions_2(mock_load_data, caplog):
806866

807867
tap = EuclidClass(tap_plus_conn_handler=conn_handler, datalink_handler=tap_plus, show_server_messages=False)
808868

809-
tap.get_observation_products(id=12345, product_type='observation', filter='VIS', output_file=None)
869+
tap.get_observation_products(id='12345', product_type='observation', filter='VIS', output_file=None)
810870

811871
mssg = "Cannot retrieve products for observation 12345. HTTP error: launch_job_async HTTPError"
812872
assert caplog.records[0].msg == mssg
813873

814874
mock_load_data.side_effect = Exception("launch_job_async Exception")
815875

816-
tap.get_observation_products(id=12345, product_type='observation', filter='VIS', output_file=None)
876+
tap.get_observation_products(id='12345', product_type='observation', filter='VIS', output_file=None)
817877

818878
mssg = "Cannot retrieve products for observation 12345: launch_job_async Exception"
819879
assert caplog.records[1].msg == mssg
820880

881+
remove_temp_dir()
882+
821883

822884
def test_get_cutout():
823885
conn_handler = DummyConnHandler()
@@ -845,6 +907,8 @@ def test_get_cutout():
845907

846908
assert result is not None
847909

910+
remove_temp_dir()
911+
848912

849913
def test_get_cutout_exception():
850914
conn_handler = DummyConnHandler()
@@ -936,7 +1000,7 @@ def test_get_cutout_exceptions_2(mock_load_data, caplog):
9361000
assert caplog.records[1].msg == mssg
9371001

9381002

939-
def test_get_spectrum():
1003+
def test_get_spectrum(tmp_path_factory):
9401004
conn_handler = DummyConnHandler()
9411005
tap_plus = TapPlus(url="http://test:1111/tap", data_context='data', client_id='ASTROQUERY',
9421006
connhandler=conn_handler)
@@ -952,8 +1016,20 @@ def test_get_spectrum():
9521016

9531017
assert result is not None
9541018

1019+
now = datetime.now()
1020+
dirs = glob.glob(os.path.join(os.getcwd(), "temp_" + now.strftime("%Y%m%d") + '_*'))
1021+
1022+
assert len(dirs) == 1
1023+
assert dirs[0] is not None
1024+
9551025
remove_temp_dir()
9561026

1027+
fits_file = os.path.join(tmp_path_factory.mktemp("euclid_tmp"), 'my_fits_file.fits')
1028+
1029+
result = tap.get_spectrum(source_id='2417660845403252054', schema='sedm_sc8', output_file=fits_file)
1030+
1031+
assert result is not None
1032+
9571033

9581034
@patch.object(TapPlus, 'load_data')
9591035
def test_get_spectrum_exceptions_2(mock_load_data, caplog):
@@ -984,15 +1060,6 @@ def test_get_spectrum_exceptions_2(mock_load_data, caplog):
9841060
assert caplog.records[1].msg == mssg
9851061

9861062

987-
def remove_temp_dir():
988-
dirs = glob.glob('./temp_*')
989-
for dir_path in dirs:
990-
try:
991-
shutil.rmtree(dir_path)
992-
except OSError as e:
993-
print("Error: %s : %s" % (dir_path, e.strerror))
994-
995-
9961063
def test_get_spectrum_exceptions():
9971064
conn_handler = DummyConnHandler()
9981065
tap_plus = TapPlus(url="http://test:1111/tap", data_context='data', client_id='ASTROQUERY',
@@ -1018,16 +1085,6 @@ def test_get_spectrum_exceptions():
10181085
assert str(exc_info.value).startswith('Missing required argument')
10191086

10201087

1021-
def test_load_async_job(mock_querier_async):
1022-
jobid = '1479386030738O'
1023-
name = None
1024-
job = mock_querier_async.load_async_job(jobid=jobid, name=name, verbose=False)
1025-
1026-
assert job is not None
1027-
1028-
assert job.jobid == jobid
1029-
1030-
10311088
@patch.object(TapPlus, 'login')
10321089
def test_login(mock_login):
10331090
conn_handler = DummyConnHandler()
@@ -1065,14 +1122,21 @@ def test_logout(mock_logout):
10651122
assert (mock_logout.call_count == 4)
10661123

10671124

1068-
@pytest.fixture(scope="module")
1069-
def mock_querier():
1070-
conn_handler = DummyConnHandler()
1071-
tap_plus = TapPlus(url="http://test:1111/tap", connhandler=conn_handler)
1125+
def test_load_async_job(mock_querier_async):
1126+
jobid = '1479386030738O'
1127+
name = None
1128+
job = mock_querier_async.load_async_job(jobid=jobid, name=name, verbose=False)
10721129

1073-
launch_response = DummyResponse(200)
1074-
launch_response.set_data(method="POST", body=JOB_DATA)
1075-
# The query contains decimals: default response is more robust.
1076-
conn_handler.set_default_response(launch_response)
1130+
assert job is not None
10771131

1078-
return EuclidClass(tap_plus_conn_handler=conn_handler, datalink_handler=tap_plus, show_server_messages=False)
1132+
assert job.jobid == jobid
1133+
1134+
1135+
def remove_temp_dir():
1136+
dirs = glob.glob('./temp_*')
1137+
for dir_path in dirs:
1138+
try:
1139+
print(dir_path)
1140+
shutil.rmtree(dir_path)
1141+
except OSError as e:
1142+
print("Error: %s : %s" % (dir_path, e.strerror))

0 commit comments

Comments
 (0)