11
11
12
12
13
13
"""
14
+ import numpy as np
14
15
import pytest
15
16
import os
17
+ from unittest .mock import patch
16
18
17
19
from requests .models import Response
18
20
from astroquery .esa .hubble import ESAHubbleClass
@@ -64,6 +66,13 @@ def ehst_cone_search(request):
64
66
return mp
65
67
66
68
69
+ class MockResponse :
70
+ observation_id = 'test'
71
+
72
+ @staticmethod
73
+ def pformat ():
74
+ return True
75
+
67
76
class TestESAHubble :
68
77
69
78
def get_dummy_tap_handler (self ):
@@ -178,7 +187,7 @@ def test_cone_search_coords(self):
178
187
parameters ['verbose' ])
179
188
with pytest .raises (ValueError ) as err :
180
189
ehst ._getCoordInput (1234 )
181
- assert "Coordinates must be either a string or " \
190
+ assert "Coordinates must be either a string or " \
182
191
"astropy.coordinates" in err .value .args [0 ]
183
192
184
193
def test_query_hst_tap (self ):
@@ -349,29 +358,29 @@ def test_cone_search_criteria(self):
349
358
'filename' : "output_test_query_by_criteria.vot.gz" ,
350
359
'output_format' : "votable" ,
351
360
'verbose' : True }
352
- test_query = "select o.*, p.calibration_level, p.data_product_type, " \
353
- "pos.ra, pos.dec from ehst.observation AS o JOIN " \
354
- "ehst.plane as p on o.observation_uuid=p.observation_" \
355
- "uuid JOIN ehst.position as pos on p.plane_id = " \
356
- "pos.plane_id where((o.collection LIKE '%HST%') AND " \
357
- "(o.instrument_name LIKE '%WFPC2%') AND " \
358
- "(o.instrument_configuration LIKE '%F606W%') AND " \
359
- "1=CONTAINS(POINT('ICRS', pos.ra, pos.dec)," \
360
- "CIRCLE('ICRS', 10.6847083, 41.26875, " \
361
+ test_query = "select o.*, p.calibration_level, p.data_product_type, " \
362
+ "pos.ra, pos.dec from ehst.observation AS o JOIN " \
363
+ "ehst.plane as p on o.observation_uuid=p.observation_" \
364
+ "uuid JOIN ehst.position as pos on p.plane_id = " \
365
+ "pos.plane_id where((o.collection LIKE '%HST%') AND " \
366
+ "(o.instrument_name LIKE '%WFPC2%') AND " \
367
+ "(o.instrument_configuration LIKE '%F606W%') AND " \
368
+ "1=CONTAINS(POINT('ICRS', pos.ra, pos.dec)," \
369
+ "CIRCLE('ICRS', 10.6847083, 41.26875, " \
361
370
"0.11666666666666667)))"
362
371
parameters3 = {'query' : test_query ,
363
372
'output_file' : "output_test_query_by_criteria.vot.gz" ,
364
373
'output_format' : "votable" ,
365
374
'verbose' : False }
366
375
ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
367
- query_criteria_query = "select o.*, p.calibration_level, " \
368
- "p.data_product_type, pos.ra, pos.dec from " \
369
- "ehst.observation AS o JOIN ehst.plane as p " \
370
- "on o.observation_uuid=p.observation_uuid " \
371
- "JOIN ehst.position as pos on p.plane_id = " \
372
- "pos.plane_id where((o.collection LIKE " \
373
- "'%HST%') AND (o.instrument_name LIKE " \
374
- "'%WFPC2%') AND (o.instrument_configuration " \
376
+ query_criteria_query = "select o.*, p.calibration_level, " \
377
+ "p.data_product_type, pos.ra, pos.dec from " \
378
+ "ehst.observation AS o JOIN ehst.plane as p " \
379
+ "on o.observation_uuid=p.observation_uuid " \
380
+ "JOIN ehst.position as pos on p.plane_id = " \
381
+ "pos.plane_id where((o.collection LIKE " \
382
+ "'%HST%') AND (o.instrument_name LIKE " \
383
+ "'%WFPC2%') AND (o.instrument_configuration " \
375
384
"LIKE '%F606W%'))"
376
385
ehst .query_criteria = MagicMock (return_value = query_criteria_query )
377
386
target = {'RA_DEGREES' : '10.6847083' , 'DEC_DEGREES' : '41.26875' }
@@ -419,14 +428,14 @@ def test_cone_search_criteria(self):
419
428
output_format = parameters1
420
429
['output_format' ],
421
430
verbose = parameters1 ['verbose' ])
422
- assert "Please use only target or coordinates as" \
423
- "parameter." in err .value .args [0 ]
431
+ assert "Please use only target or coordinates as" \
432
+ "parameter." in err .value .args [0 ]
424
433
425
434
def test_query_criteria_no_params (self ):
426
435
ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
427
436
ehst .query_criteria (async_job = False ,
428
437
output_file = "output_test_query_"
429
- "by_criteria.vot.gz" ,
438
+ "by_criteria.vot.gz" ,
430
439
output_format = "votable" ,
431
440
verbose = True )
432
441
parameters = {'query' : "select o.*, p.calibration_level, "
@@ -445,8 +454,173 @@ def test_empty_list(self):
445
454
ehst .query_criteria (instrument_name = [1 ],
446
455
async_job = False ,
447
456
output_file = "output_test_query_"
448
- "by_criteria.vot.gz" ,
457
+ "by_criteria.vot.gz" ,
449
458
output_format = "votable" ,
450
459
verbose = True )
451
- assert "One of the lists is empty or there are " \
460
+ assert "One of the lists is empty or there are " \
452
461
"elements that are not strings" in err .value .args [0 ]
462
+
463
+ def test_get_decoded_string (self ):
464
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
465
+ dummy = '\x74 \x65 \x73 \x74 '
466
+ decoded_string = ehst .get_decoded_string (dummy )
467
+ assert decoded_string == 'test'
468
+
469
+ def test_get_decoded_string_unicodedecodeerror (self ):
470
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
471
+ dummy = '\xd0 \x91 '
472
+ decoded_string = ehst .get_decoded_string (dummy )
473
+ assert decoded_string == dummy
474
+
475
+ def test_get_decoded_string_attributeerror (self ):
476
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
477
+ dummy = True
478
+ decoded_string = ehst .get_decoded_string (dummy )
479
+ assert decoded_string == dummy
480
+
481
+ @patch .object (ESAHubbleClass , 'query_hst_tap' )
482
+ def test__select_composite (self , mock_query ):
483
+ arr = {'a' : np .array ([1 , 4 ], dtype = np .int32 ),
484
+ 'b' : [2.0 , 5.0 ],
485
+ 'observation_id' : ['x' , 'y' ]}
486
+ data_table = Table (arr )
487
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
488
+ mock_query .return_value = data_table
489
+ dummy_obs_id = "1234"
490
+ oids = ehst ._select_composite (dummy_obs_id )
491
+ assert oids == ['x' , 'y' ]
492
+
493
+ @patch .object (ESAHubbleClass , 'query_hst_tap' )
494
+ def test__select_members (self , mock_query ):
495
+ arr = {'a' : np .array ([1 , 4 ], dtype = np .int32 ),
496
+ 'b' : [2.0 , 5.0 ],
497
+ 'members' : ['caom:HST/test' , 'y' ]}
498
+ data_table = Table (arr )
499
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
500
+ mock_query .return_value = data_table
501
+ dummy_obs_id = "1234"
502
+ oids = ehst ._select_members (dummy_obs_id )
503
+ assert oids == ['test' ]
504
+
505
+ @patch .object (ESAHubbleClass , 'query_hst_tap' )
506
+ def test_get_observation_type (self , mock_query ):
507
+ arr = {'a' : np .array ([1 , 4 ], dtype = np .int32 ),
508
+ 'b' : [2.0 , 5.0 ],
509
+ 'obs_type' : ['HST Test' , 'y' ]}
510
+ data_table = Table (arr )
511
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
512
+ mock_query .return_value = data_table
513
+ dummy_obs_id = "1234"
514
+ oids = ehst .get_observation_type (dummy_obs_id )
515
+ assert oids == 'HST Test'
516
+
517
+ def test_get_observation_type_attributeerror (self ):
518
+ with pytest .raises (AttributeError ):
519
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
520
+ dummy_obs_id = None
521
+ ehst .get_observation_type (dummy_obs_id )
522
+
523
+ @patch .object (ESAHubbleClass , 'query_hst_tap' )
524
+ def test_get_observation_type_valueerror (self , mock_query ):
525
+ with pytest .raises (ValueError ):
526
+ arr = {'a' : np .array ([], dtype = np .int32 ),
527
+ 'b' : [],
528
+ 'obs_type' : []}
529
+ data_table = Table (arr )
530
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
531
+ mock_query .return_value = data_table
532
+ dummy_obs_id = '1234'
533
+ ehst .get_observation_type (dummy_obs_id )
534
+
535
+ @patch .object (ESAHubbleClass , 'query_hst_tap' )
536
+ @patch .object (ESAHubbleClass , 'get_observation_type' )
537
+ def test_get_hst_link (self , mock_observation_type , mock_query ):
538
+ mock_observation_type .return_value = "HST"
539
+ arr = {'a' : np .array ([1 ], dtype = np .int32 ),
540
+ 'b' : [2.0 ],
541
+ 'observation_id' : ['1234' ]}
542
+ data_table = Table (arr )
543
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
544
+ mock_query .return_value = data_table
545
+ dummy_obs_id = "1234"
546
+ oids = ehst .get_hap_hst_link (dummy_obs_id )
547
+ assert oids == ['1234' ]
548
+
549
+ @patch .object (ESAHubbleClass , 'get_observation_type' )
550
+ @patch .object (ESAHubbleClass , '_select_members' )
551
+ def test_get_hap_link (self , mock_select_members , mock_observation_type ):
552
+ mock_select_members .return_value = 'test'
553
+ mock_observation_type .return_value = "HAP"
554
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
555
+ dummy_obs_id = "1234"
556
+ oids = ehst .get_hap_hst_link (dummy_obs_id )
557
+ assert oids == 'test'
558
+
559
+ @patch .object (ESAHubbleClass , 'get_observation_type' )
560
+ def test_get_hap_hst_link_valueerror (self , mock_observation_type ):
561
+ with pytest .raises (ValueError ):
562
+ mock_observation_type .return_value = "valueerror"
563
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
564
+ dummy_obs_id = "1234"
565
+ ehst .get_hap_hst_link (dummy_obs_id )
566
+
567
+ def test_get_hap_hst_link_attributeerror (self ):
568
+ with pytest .raises (AttributeError ):
569
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
570
+ dummy_obs_id = None
571
+ ehst .get_hap_hst_link (dummy_obs_id )
572
+
573
+ @patch .object (ESAHubbleClass , '_select_members' )
574
+ @patch .object (ESAHubbleClass , 'get_observation_type' )
575
+ def test_get_member_observations_composite (self , mock_observation_type , mock_select_members ):
576
+ mock_observation_type .return_value = "Composite"
577
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
578
+ mock_select_members .return_value = 'test'
579
+ dummy_obs_id = "1234"
580
+ oids = ehst .get_member_observations (dummy_obs_id )
581
+ assert oids == 'test'
582
+
583
+ @patch .object (ESAHubbleClass , '_select_composite' )
584
+ @patch .object (ESAHubbleClass , 'get_observation_type' )
585
+ def test_get_member_observations_simple (self , mock_observation_type , mock_select_composite ):
586
+ mock_observation_type .return_value = "Simple"
587
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
588
+ mock_select_composite .return_value = 'test'
589
+ dummy_obs_id = "1234"
590
+ oids = ehst .get_member_observations (dummy_obs_id )
591
+ assert oids == 'test'
592
+
593
+ @patch .object (ESAHubbleClass , 'get_observation_type' )
594
+ def test_get_member_observations_valueerror (self , mock_observation_type ):
595
+ with pytest .raises (ValueError ):
596
+ mock_observation_type .return_value = "valueerror"
597
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
598
+ dummy_obs_id = "1234"
599
+ ehst .get_member_observations (dummy_obs_id )
600
+
601
+ def test_get_member_observations_attributeerror (self ):
602
+ with pytest .raises (AttributeError ):
603
+ ehst = ESAHubbleClass (self .get_dummy_tap_handler ())
604
+ dummy_obs_id = None
605
+ ehst .get_member_observations (dummy_obs_id )
606
+
607
+
608
+ """
609
+ def test_get_member_observations(self):
610
+ parameters = {}
611
+ obs = 'dummyObs'
612
+ parameters['query'] = f"select observation_id from ehst.observation where members like '%{obs}%'"
613
+ parameters['name'] = None
614
+ parameters['output_file'] = None
615
+ parameters['output_format'] = 'votable'
616
+ parameters['verbose'] = False
617
+ parameters['dump_to_file'] = False
618
+ parameters['upload_resource'] = None
619
+ parameters['upload_table_name'] = None
620
+
621
+ dummyTapHandler = DummyHubbleTapHandler("launch_job", parameters)
622
+ tap = ESAHubbleClass(dummyTapHandler)
623
+
624
+ tap.get_member_observations(observation_id=obs)
625
+ dummyTapHandler.check_call('launch_job', parameters)
626
+ """
0 commit comments