Skip to content

Commit

Permalink
fix use of mapping exception
Browse files Browse the repository at this point in the history
  • Loading branch information
lmichel committed Dec 1, 2024
1 parent c7cfe7b commit 7e0c918
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 33 deletions.
22 changes: 11 additions & 11 deletions pyvo/mivot/tests/test_mivot_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from astropy.io.votable import parse
from pyvo.utils import activate_features
from pyvo.mivot.version_checker import check_astropy_version
from pyvo.mivot.utils.exceptions import MappingException
from pyvo.mivot.utils.exceptions import MappingError
from pyvo.mivot.utils.dict_utils import DictUtils
from pyvo.mivot.writer.annotations import MivotAnnotations
from pyvo.mivot.writer.instance import MivotInstance
Expand Down Expand Up @@ -48,25 +48,25 @@ def test_MivotInstance():
"""
Test the MivotInstance class for various operations including attribute addition,
reference addition, and XML generation. Verifies that invalid operations raise
the expected MappingException.
the expected MappingError.
"""
with pytest.raises(MappingException):
with pytest.raises(MappingError):
MivotInstance(dmid="model:type.inst")

instance1 = MivotInstance(dmtype="model:type.inst", dmid="id1")
with pytest.raises(MappingException):
with pytest.raises(MappingError):
instance1.add_attribute(
dmrole="model:type.inst.role1", value="value1", unit="m/s"
)
with pytest.raises(MappingException):
with pytest.raises(MappingError):
instance1.add_attribute(
dmtype="model:type.att1", dmrole="model:type.inst.role1"
)
with pytest.raises(MappingException):
with pytest.raises(MappingError):
instance1.add_reference(dmref="dmreference")
with pytest.raises(MappingException):
with pytest.raises(MappingError):
instance1.add_reference(dmrole="model:type.inst.role2")
with pytest.raises(MappingException):
with pytest.raises(MappingError):
instance1.add_instance("azerty")

instance1.add_reference(dmrole="model:type.inst.role2", dmref="dmreference")
Expand All @@ -88,13 +88,13 @@ def test_MivotInstance():
def test_MivotAnnotations():
"""
Test the MivotAnnotations class for template and global instance addition. Verifies
that invalid operations raise the expected MappingException.
that invalid operations raise the expected MappingError.
"""
mb = MivotAnnotations()

with pytest.raises(MappingException):
with pytest.raises(MappingError):
mb.add_templates(12)
with pytest.raises(MappingException):
with pytest.raises(MappingError):
mb.add_globals(12)


Expand Down
20 changes: 10 additions & 10 deletions pyvo/mivot/writer/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
from astropy import version
from pyvo.utils.prototype import prototype_feature
from pyvo.mivot.utils.xml_utils import XmlUtils
from pyvo.mivot.utils.exceptions import MappingException, AstropyVersionException
from pyvo.mivot.utils.exceptions import MappingError, AstropyVersionException
from pyvo.mivot.writer.instance import MivotInstance
from pyvo.mivot.version_checker import check_astropy_version

Expand Down Expand Up @@ -251,15 +251,15 @@ def add_templates(self, templates_instance):
Raises
------
MappingException
MappingError
If `templates_instance` is neither a string nor an instance of `MivotInstance`.
"""
if isinstance(templates_instance, MivotInstance):
self._templates.append(templates_instance.xml_string())
elif isinstance(templates_instance, str):
self._templates.append(templates_instance)
else:
raise MappingException(
raise MappingError(
"Instance added to templates must be a string or MivotInstance."
)

Expand All @@ -274,15 +274,15 @@ def add_globals(self, globals_instance):
Raises
------
MappingException
MappingError
If `globals_instance` is neither a string nor an instance of `MivotInstance`.
"""
if isinstance(globals_instance, MivotInstance):
self._globals.append(globals_instance.xml_string())
elif isinstance(globals_instance, str):
self._globals.append(globals_instance)
else:
raise MappingException(
raise MappingError(
"Instance added to globals must be a string or MivotInstance."
)

Expand Down Expand Up @@ -327,7 +327,7 @@ def check_xml(self):
Raises
------
MappingException
MappingError
If the validation fails.
Notes
Expand All @@ -349,7 +349,7 @@ def check_xml(self):
try:
schema.validate(mivot_block)
except Exception as excep:
raise MappingException(f"Validation failed: {excep}") from excep
raise MappingError(f"Validation failed: {excep}") from excep

def insert_into_votable(self, votable_file, template_id=None, override=False):
"""
Expand All @@ -366,7 +366,7 @@ def insert_into_votable(self, votable_file, template_id=None, override=False):
Raises
------
MappingException
MappingError
If a mapping block already exists and `override` is False.
"""
if not check_astropy_version():
Expand All @@ -377,7 +377,7 @@ def insert_into_votable(self, votable_file, template_id=None, override=False):
elif isinstance(votable_file, VOTableFile):
votable = votable_file
else:
raise MappingException(
raise MappingError(
"votable_file must be a file path string or a VOTableFile instance."
)

Expand All @@ -386,7 +386,7 @@ def insert_into_votable(self, votable_file, template_id=None, override=False):
for subresource in resource.resources:
if subresource.type == "meta":
if not override:
raise MappingException(
raise MappingError(
"A type='meta' resource already exists in the first 'result' resource."
)
else:
Expand Down
24 changes: 12 additions & 12 deletions pyvo/mivot/writer/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"""

from pyvo.utils.prototype import prototype_feature
from pyvo.mivot.utils.exceptions import MappingException
from pyvo.mivot.utils.exceptions import MappingError


@prototype_feature("MIVOT")
Expand All @@ -65,11 +65,11 @@ def __init__(self, dmtype=None, dmrole=None, dmid=None):
Raises
------
MappingException
MappingError
If `dmtype` is not provided.
"""
if not dmtype:
raise MappingException("Cannot build an instance without dmtype")
raise MappingError("Cannot build an instance without dmtype")
self._dmtype = dmtype
self._dmrole = dmrole
self._dmid = dmid
Expand All @@ -94,15 +94,15 @@ def add_attribute(self, dmtype=None, dmrole=None, ref=None, value=None, unit=Non
Raises
------
MappingException
MappingError
If `dmtype` or `dmrole` is not provided, or if both `ref` and `value` are not defined.
"""
if not dmtype:
raise MappingException("Cannot add an attribute without dmtype")
raise MappingError("Cannot add an attribute without dmtype")
if not dmrole:
raise MappingException("Cannot add an attribute without dmrole")
raise MappingError("Cannot add an attribute without dmrole")
if not ref and not value:
raise MappingException("Cannot add an attribute without ref or value")
raise MappingError("Cannot add an attribute without ref or value")

xml_string = f'<ATTRIBUTE dmtype="{dmtype}" dmrole="{dmrole}" '
if unit:
Expand All @@ -127,13 +127,13 @@ def add_reference(self, dmrole=None, dmref=None):
Raises
------
MappingException
MappingError
If `dmrole` or `dmref` is not provided.
"""
if not dmref:
raise MappingException("Cannot add a reference without dmref")
raise MappingError("Cannot add a reference without dmref")
if not dmrole:
raise MappingException("Cannot add a reference without dmrole")
raise MappingError("Cannot add a reference without dmrole")

xml_string = f'<REFERENCE dmrole="{dmrole}" dmref="{dmref}" />'
self._content.append(xml_string)
Expand All @@ -149,11 +149,11 @@ def add_instance(self, mivot_instance):
Raises
------
MappingException
MappingError
If `mivot_instance` is not of type `MivotInstance`.
"""
if not isinstance(mivot_instance, MivotInstance):
raise MappingException("Instance added must be of type MivotInstance")
raise MappingError("Instance added must be of type MivotInstance")
self._content.append(mivot_instance.xml_string())

def xml_string(self):
Expand Down

0 comments on commit 7e0c918

Please sign in to comment.