Skip to content

Commit

Permalink
ephemeris component validation (#78)
Browse files Browse the repository at this point in the history
* ephemeris component validation to avoid breaking assumptions when parsing viewer names
* jdaviz version condition in test coverage
  • Loading branch information
kecnry authored Jan 19, 2024
1 parent f8f4e41 commit 8f5d02e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lcviz/plugins/ephemeris/ephemeris.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ def __init__(self, *args, **kwargs):
manual_options=['default'],
on_add=self._on_component_add,
on_rename_after_selection=self._on_component_rename, # noqa
on_remove_after_selection=self._on_component_remove) # noqa
on_remove_after_selection=self._on_component_remove, # noqa
validate_choice=self._validate_component)

# force the original entry in ephemerides with defaults
self._change_component()
Expand Down Expand Up @@ -311,6 +312,13 @@ def _check_if_phase_viewer_exists(self, *args):
viewer_base_refs = [id.split('[')[0] for id in self.app.get_viewer_ids()]
self.phase_viewer_exists = self.phase_viewer_id in viewer_base_refs

def _validate_component(self, lbl):
if '[' in lbl or ']' in lbl:
return 'cannot contain square brackets'
if ':' in lbl:
return 'cannot contain colon'
return ''

def _on_component_add(self, lbl):
self.hub.broadcast(EphemerisComponentChangedMessage(old_lbl=None, new_lbl=lbl,
sender=self))
Expand Down
14 changes: 14 additions & 0 deletions lcviz/tests/test_plugin_ephemeris.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import pytest
from packaging.version import Version

import jdaviz
JDAVIZ_LT_3_9_0 = Version(jdaviz.__version__) < Version('3.9.0')


def test_docs_snippets(helper, light_curve_like_kepler_quarter):
lcviz, lc = helper, light_curve_like_kepler_quarter
Expand Down Expand Up @@ -43,6 +49,14 @@ def test_plugin_ephemeris(helper, light_curve_like_kepler_quarter):
assert len(ephem.ephemerides) == 2
assert 'custom component' in ephem.ephemerides

if not JDAVIZ_LT_3_9_0:
with pytest.raises(ValueError):
# brackets interfere with cloned viewer label logic
ephem.rename_component('custom component', 'custom component[blah]')
with pytest.raises(ValueError):
# colons interfere with viewer ephemeris logic
ephem.rename_component('custom component', 'custom component:blah')

ephem.rename_component('custom component', 'renamed custom component')
assert len(ephem.ephemerides) == 2
assert 'renamed custom component' in ephem.ephemerides
Expand Down

0 comments on commit 8f5d02e

Please sign in to comment.