|
4 | 4 | import os
|
5 | 5 | import re
|
6 | 6 | from shutil import copyfile
|
| 7 | +from unittest.mock import patch |
7 | 8 |
|
8 | 9 | import pytest
|
9 | 10 |
|
|
16 | 17 |
|
17 | 18 | from astroquery.mast.services import _json_to_table
|
18 | 19 | from astroquery.utils.mocks import MockResponse
|
19 |
| -from astroquery.exceptions import InvalidQueryError, InputWarning, MaxResultsWarning |
| 20 | +from astroquery.exceptions import (InvalidQueryError, InputWarning, MaxResultsWarning, NoResultsWarning, |
| 21 | + RemoteServiceError) |
20 | 22 |
|
21 | 23 | from astroquery import mast
|
22 | 24 |
|
|
48 | 50 | 'Mast.HscMatches.Db.v3': 'matchid.json',
|
49 | 51 | 'Mast.HscMatches.Db.v2': 'matchid.json',
|
50 | 52 | 'Mast.HscSpectra.Db.All': 'spectra.json',
|
| 53 | + 'mast_relative_path': 'mast_relative_path.json', |
51 | 54 | 'panstarrs': 'panstarrs.json',
|
52 | 55 | 'panstarrs_columns': 'panstarrs_columns.json',
|
53 | 56 | 'tess_cutout': 'astrocut_107.27_-70.0_5x5.zip',
|
@@ -142,6 +145,8 @@ def request_mockreturn(url, params={}):
|
142 | 145 | filename = data_path(DATA_FILES["Mast.Name.Lookup"])
|
143 | 146 | elif 'panstarrs' in url:
|
144 | 147 | filename = data_path(DATA_FILES['panstarrs_columns'])
|
| 148 | + elif 'path_lookup' in url: |
| 149 | + filename = data_path(DATA_FILES['mast_relative_path']) |
145 | 150 | with open(filename, 'rb') as infile:
|
146 | 151 | content = infile.read()
|
147 | 152 | return MockResponse(content)
|
@@ -678,6 +683,95 @@ def test_observations_download_file(patch_post, tmpdir):
|
678 | 683 | assert result == ('COMPLETE', None, None)
|
679 | 684 |
|
680 | 685 |
|
| 686 | +@patch('boto3.client') |
| 687 | +def test_observations_get_cloud_uri(mock_client, patch_post): |
| 688 | + pytest.importorskip("boto3") |
| 689 | + |
| 690 | + mast_uri = 'mast:HST/product/u9o40504m_c3m.fits' |
| 691 | + expected = 's3://stpubdata/hst/public/u9o4/u9o40504m/u9o40504m_c3m.fits' |
| 692 | + |
| 693 | + # Error without cloud connection |
| 694 | + with pytest.raises(RemoteServiceError): |
| 695 | + mast.Observations.get_cloud_uri('mast:HST/product/u9o40504m_c3m.fits') |
| 696 | + |
| 697 | + # Enable access to public AWS S3 bucket |
| 698 | + mast.Observations.enable_cloud_dataset() |
| 699 | + |
| 700 | + # Row input |
| 701 | + product = Table() |
| 702 | + product['dataURI'] = [mast_uri] |
| 703 | + uri = mast.Observations.get_cloud_uri(product[0]) |
| 704 | + assert isinstance(uri, str) |
| 705 | + assert uri == expected |
| 706 | + |
| 707 | + # String input |
| 708 | + uri = mast.Observations.get_cloud_uri(mast_uri) |
| 709 | + assert uri == expected |
| 710 | + |
| 711 | + mast.Observations.disable_cloud_dataset() |
| 712 | + |
| 713 | + |
| 714 | +@patch('boto3.client') |
| 715 | +def test_observations_get_cloud_uris(mock_client, patch_post): |
| 716 | + pytest.importorskip("boto3") |
| 717 | + |
| 718 | + mast_uri = 'mast:HST/product/u9o40504m_c3m.fits' |
| 719 | + expected = 's3://stpubdata/hst/public/u9o4/u9o40504m/u9o40504m_c3m.fits' |
| 720 | + |
| 721 | + # Error without cloud connection |
| 722 | + with pytest.raises(RemoteServiceError): |
| 723 | + mast.Observations.get_cloud_uris(['mast:HST/product/u9o40504m_c3m.fits']) |
| 724 | + |
| 725 | + # Enable access to public AWS S3 bucket |
| 726 | + mast.Observations.enable_cloud_dataset() |
| 727 | + |
| 728 | + # Get the cloud URIs |
| 729 | + # Table input |
| 730 | + product = Table() |
| 731 | + product['dataURI'] = [mast_uri] |
| 732 | + uris = mast.Observations.get_cloud_uris([mast_uri]) |
| 733 | + assert isinstance(uris, list) |
| 734 | + assert len(uris) == 1 |
| 735 | + assert uris[0] == expected |
| 736 | + |
| 737 | + # List input |
| 738 | + uris = mast.Observations.get_cloud_uris([mast_uri]) |
| 739 | + assert isinstance(uris, list) |
| 740 | + assert len(uris) == 1 |
| 741 | + assert uris[0] == expected |
| 742 | + |
| 743 | + # Warn if attempting to filter with list input |
| 744 | + with pytest.warns(InputWarning, match='Filtering is not supported'): |
| 745 | + mast.Observations.get_cloud_uris([mast_uri], |
| 746 | + extension='png') |
| 747 | + |
| 748 | + # Warn if not found |
| 749 | + with pytest.warns(NoResultsWarning, match='Failed to retrieve MAST relative path'): |
| 750 | + mast.Observations.get_cloud_uris(['mast:HST/product/does_not_exist.fits']) |
| 751 | + |
| 752 | + |
| 753 | +@patch('boto3.client') |
| 754 | +def test_observations_get_cloud_uris_query(mock_client, patch_post): |
| 755 | + pytest.importorskip("boto3") |
| 756 | + |
| 757 | + # enable access to public AWS S3 bucket |
| 758 | + mast.Observations.enable_cloud_dataset() |
| 759 | + |
| 760 | + # get uris with streamlined function |
| 761 | + uris = mast.Observations.get_cloud_uris(target_name=234295610, |
| 762 | + filter_products={'productSubGroupDescription': 'C3M'}) |
| 763 | + assert isinstance(uris, list) |
| 764 | + |
| 765 | + # check that InvalidQueryError is thrown if neither data_products or **criteria are defined |
| 766 | + with pytest.raises(InvalidQueryError): |
| 767 | + mast.Observations.get_cloud_uris(filter_products={'productSubGroupDescription': 'C3M'}) |
| 768 | + |
| 769 | + # warn if no data products match filters |
| 770 | + with pytest.warns(NoResultsWarning, match='No matching products'): |
| 771 | + mast.Observations.get_cloud_uris(target_name=234295610, |
| 772 | + filter_products={'productSubGroupDescription': 'LC'}) |
| 773 | + |
| 774 | + |
681 | 775 | ######################
|
682 | 776 | # CatalogClass tests #
|
683 | 777 | ######################
|
|
0 commit comments