Skip to content

Commit

Permalink
basic test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kecnry committed Oct 7, 2022
1 parent c9fd027 commit d30072f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 6 deletions.
20 changes: 15 additions & 5 deletions jdaviz/configs/imviz/plugins/catalogs/catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ def __init__(self, *args, **kwargs):

def _on_file_path_changed(self, event):
self.from_file_message = 'Checking if file is valid'
if (self._file_upload.file_path is not None
and not os.path.exists(self._file_upload.file_path)
or not os.path.isfile(self._file_upload.file_path)):
path = event['new']
if (path is not None
and not os.path.exists(path)
or not os.path.isfile(path)):
self.from_file_message = 'File path does not exist'
return

try:
table = QTable.read(self._file_upload.file_path)
table = QTable.read(path)
except Exception:
self.from_file_message = 'Could not parse file with astropy.table.QTable.read'
return
Expand All @@ -71,7 +72,7 @@ def _on_file_path_changed(self, event):
# since we loaded the file already to check if its valid, we might as well cache the table
# so we don't have to re-load it when clicking search. We'll only keep the latest entry
# though, but store in a dict so we can catch if the file path was changed from the API
self._cached_table_from_file = {self._file_upload.file_path: table}
self._cached_table_from_file = {path: table}
self.from_file_message = ''

@observe('from_file')
Expand Down Expand Up @@ -130,6 +131,12 @@ def search(self):
# finds all the sources in that region
query_region_result = SDSS.query_region(skycoord_center, radius=zoom_radius,
data_release=17)
if query_region_result is None:
self.results_available = True
self.number_of_results = 0
self.app._catalog_source_table = None
return

# TODO: Filter this table the same way as the actual displayed markers.
# attach the table to the app for Python extraction
self.app._catalog_source_table = query_region_result
Expand All @@ -141,16 +148,19 @@ def search(self):
# all exceptions when going through the UI should have prevented setting this path
# but this exceptions might be raised here if setting from_file from the UI
table = self._cached_table_from_file.get(self.from_file, QTable.read(self.from_file))
self.app._catalog_source_table = table
skycoord_table = table['sky_centroid']

else:
self.results_available = False
self.number_of_results = 0
self.app._catalog_source_table = None
raise NotImplementedError(f"{self.catalog_selected} not a supported catalog")

self.results_available = True
if not len(skycoord_table):
self.number_of_results = 0
self.app._catalog_source_table = None
return

# coordinates found are converted to pixel coordinates
Expand Down
48 changes: 47 additions & 1 deletion jdaviz/configs/imviz/tests/test_catalogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

from astropy.io import fits
from astropy.nddata import NDData
from astropy.coordinates import SkyCoord
from astropy.table import QTable


@pytest.mark.remote_data
Expand Down Expand Up @@ -63,7 +65,7 @@ def test_plugin_image_no_result(self, imviz_helper, image_2d_wcs):
# data used: information based on this image -
# https://dr12.sdss.org/fields/runCamcolField?field=76&camcol=5&run=7674
# the z-band FITS image was downloaded and used
def test_plugin_image_with_result(self, imviz_helper):
def test_plugin_image_with_result(self, imviz_helper, tmp_path):
arr = np.ones((1489, 2048))

# header is based on the data provided above
Expand Down Expand Up @@ -99,3 +101,47 @@ def test_plugin_image_with_result(self, imviz_helper):
catalogs_plugin.vue_do_clear()

assert not catalogs_plugin.results_available

# test loading from file
table = imviz_helper.app._catalog_source_table
skycoord_table = SkyCoord(table['ra'],
table['dec'],
unit='deg')
qtable = QTable({'sky_centroid': skycoord_table})
tmp_file = tmp_path / 'test.ecsv'
qtable.write(tmp_file, overwrite=True)

catalogs_plugin.from_file = str(tmp_file)
# setting filename from API will automatically set catalog to 'From File...'
assert catalogs_plugin.catalog.selected == 'From File...'
catalogs_plugin.vue_do_search()
assert catalogs_plugin.results_available
assert catalogs_plugin.number_of_results == 2473

def test_from_file_parsing(self, imviz_helper, tmp_path):
catalogs_plugin = imviz_helper.app.get_tray_item_from_name('imviz-catalogs')

# _on_file_path_changed is fired when changing the selection in the file dialog
catalogs_plugin._on_file_path_changed({'new': './invalid_path'})
assert catalogs_plugin.from_file_message == 'File path does not exist'

# observe('from_file') is fired when setting from_file from the API (or after clicking
# select in the file dialog)
with pytest.raises(ValueError, match='./invalid_path does not exist'):
catalogs_plugin.from_file = './invalid_path'

# setting to a blank string from the API resets the catalog selection to the
# default/first entry
catalogs_plugin.from_file = ''
assert catalogs_plugin.catalog.selected == catalogs_plugin.catalog.choices[0]

not_table_file = tmp_path / 'not_table.tst'
not_table_file.touch()
catalogs_plugin._on_file_path_changed({'new': not_table_file})
assert catalogs_plugin.from_file_message == 'Could not parse file with astropy.table.QTable.read' # noqa

qtable = QTable({'not_sky_centroid': [1, 2, 3]})
not_valid_table = tmp_path / 'not_valid_table.ecsv'
qtable.write(not_valid_table, overwrite=True)
catalogs_plugin._on_file_path_changed({'new': not_valid_table})
assert catalogs_plugin.from_file_message == 'Table does not contain required sky_centroid column' # noqa

0 comments on commit d30072f

Please sign in to comment.