9
9
import glob
10
10
import os
11
11
import shutil
12
+ from datetime import datetime
12
13
from pathlib import Path
13
14
from unittest .mock import patch
14
15
@@ -50,6 +51,25 @@ def data_path(filename):
50
51
return os .path .join (data_dir , filename )
51
52
52
53
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
+
53
73
@pytest .fixture (scope = "module" )
54
74
def mock_querier_async ():
55
75
conn_handler = DummyConnHandler ()
@@ -657,7 +677,7 @@ def test_get_product_list_errors():
657
677
assert str (exc_info .value ).startswith ("Invalid product type DpdMerBksMosaic." )
658
678
659
679
660
- def test_get_product_by_product_id ():
680
+ def test_get_product_by_product_id (tmp_path_factory ):
661
681
conn_handler = DummyConnHandler ()
662
682
tap_plus = TapPlus (url = "http://test:1111/tap" , data_context = 'data' , client_id = 'ASTROQUERY' ,
663
683
connhandler = conn_handler )
@@ -669,12 +689,24 @@ def test_get_product_by_product_id():
669
689
670
690
tap = EuclidClass (tap_plus_conn_handler = conn_handler , datalink_handler = tap_plus , show_server_messages = False )
671
691
672
- result = tap .get_product (product_id = 123456789 , output_file = None )
692
+ result = tap .get_product (product_id = ' 123456789' , output_file = None )
673
693
674
694
assert result is not None
675
695
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
+
676
702
remove_temp_dir ()
677
703
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
+
678
710
679
711
def test_get_product ():
680
712
conn_handler = DummyConnHandler ()
@@ -693,6 +725,12 @@ def test_get_product():
693
725
694
726
assert result is not None
695
727
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
+
696
734
remove_temp_dir ()
697
735
698
736
@@ -711,7 +749,7 @@ def test_get_product_exceptions():
711
749
with pytest .raises (ValueError ) as exc_info :
712
750
tap .get_product (file_name = None , product_id = None , output_file = None )
713
751
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" )
715
753
716
754
717
755
@patch .object (TapPlus , 'load_data' )
@@ -741,8 +779,16 @@ def test_get_product_exceptions_2(mock_load_data, caplog):
741
779
mssg = "Cannot retrieve products for file_name hola.fits or product_id None: launch_job_async Exception"
742
780
assert caplog .records [1 ].msg == mssg
743
781
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
+
744
790
745
- def test_get_obs_products ( ):
791
+ def test_get_observation_products ( tmp_path_factory ):
746
792
conn_handler = DummyConnHandler ()
747
793
tap_plus = TapPlus (url = "http://test:1111/tap" , data_context = 'data' , client_id = 'ASTROQUERY' ,
748
794
connhandler = conn_handler )
@@ -762,8 +808,22 @@ def test_get_obs_products():
762
808
763
809
assert result is not None
764
810
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
+
765
825
766
- def test_get_obs_products_exceptions ():
826
+ def test_get_observation_products_exceptions ():
767
827
conn_handler = DummyConnHandler ()
768
828
tap_plus = TapPlus (url = "http://test:1111/tap" , data_context = 'data' , client_id = 'ASTROQUERY' ,
769
829
connhandler = conn_handler )
@@ -781,18 +841,18 @@ def test_get_obs_products_exceptions():
781
841
assert str (exc_info .value ).startswith ("Missing required argument: 'observation_id'" )
782
842
783
843
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 )
785
845
786
846
assert str (exc_info .value ).startswith ("Missing required argument: 'product_type'" )
787
847
788
848
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 )
790
850
791
851
assert str (exc_info .value ).startswith ("Invalid product type XXXXXXXX. Valid values: ['observation', 'mosaic']" )
792
852
793
853
794
854
@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 ):
796
856
conn_handler = DummyConnHandler ()
797
857
tap_plus = TapPlus (url = "http://test:1111/tap" , data_context = 'data' , client_id = 'ASTROQUERY' ,
798
858
connhandler = conn_handler )
@@ -806,18 +866,20 @@ def test_get_obs_products_exceptions_2(mock_load_data, caplog):
806
866
807
867
tap = EuclidClass (tap_plus_conn_handler = conn_handler , datalink_handler = tap_plus , show_server_messages = False )
808
868
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 )
810
870
811
871
mssg = "Cannot retrieve products for observation 12345. HTTP error: launch_job_async HTTPError"
812
872
assert caplog .records [0 ].msg == mssg
813
873
814
874
mock_load_data .side_effect = Exception ("launch_job_async Exception" )
815
875
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 )
817
877
818
878
mssg = "Cannot retrieve products for observation 12345: launch_job_async Exception"
819
879
assert caplog .records [1 ].msg == mssg
820
880
881
+ remove_temp_dir ()
882
+
821
883
822
884
def test_get_cutout ():
823
885
conn_handler = DummyConnHandler ()
@@ -845,6 +907,8 @@ def test_get_cutout():
845
907
846
908
assert result is not None
847
909
910
+ remove_temp_dir ()
911
+
848
912
849
913
def test_get_cutout_exception ():
850
914
conn_handler = DummyConnHandler ()
@@ -936,7 +1000,7 @@ def test_get_cutout_exceptions_2(mock_load_data, caplog):
936
1000
assert caplog .records [1 ].msg == mssg
937
1001
938
1002
939
- def test_get_spectrum ():
1003
+ def test_get_spectrum (tmp_path_factory ):
940
1004
conn_handler = DummyConnHandler ()
941
1005
tap_plus = TapPlus (url = "http://test:1111/tap" , data_context = 'data' , client_id = 'ASTROQUERY' ,
942
1006
connhandler = conn_handler )
@@ -952,8 +1016,20 @@ def test_get_spectrum():
952
1016
953
1017
assert result is not None
954
1018
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
+
955
1025
remove_temp_dir ()
956
1026
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
+
957
1033
958
1034
@patch .object (TapPlus , 'load_data' )
959
1035
def test_get_spectrum_exceptions_2 (mock_load_data , caplog ):
@@ -984,15 +1060,6 @@ def test_get_spectrum_exceptions_2(mock_load_data, caplog):
984
1060
assert caplog .records [1 ].msg == mssg
985
1061
986
1062
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
-
996
1063
def test_get_spectrum_exceptions ():
997
1064
conn_handler = DummyConnHandler ()
998
1065
tap_plus = TapPlus (url = "http://test:1111/tap" , data_context = 'data' , client_id = 'ASTROQUERY' ,
@@ -1018,16 +1085,6 @@ def test_get_spectrum_exceptions():
1018
1085
assert str (exc_info .value ).startswith ('Missing required argument' )
1019
1086
1020
1087
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
-
1031
1088
@patch .object (TapPlus , 'login' )
1032
1089
def test_login (mock_login ):
1033
1090
conn_handler = DummyConnHandler ()
@@ -1065,14 +1122,21 @@ def test_logout(mock_logout):
1065
1122
assert (mock_logout .call_count == 4 )
1066
1123
1067
1124
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 )
1072
1129
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
1077
1131
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