diff --git a/oopnet/elements/__init__.py b/oopnet/elements/__init__.py index 0bdfa52..5e31df5 100644 --- a/oopnet/elements/__init__.py +++ b/oopnet/elements/__init__.py @@ -25,3 +25,7 @@ Controlcondition, Energy, ) +from .component_registry import ( + ComponentNotExistingError, + IdenticalIDError +) diff --git a/oopnet/elements/component_registry.py b/oopnet/elements/component_registry.py index 545360a..2e85bee 100644 --- a/oopnet/elements/component_registry.py +++ b/oopnet/elements/component_registry.py @@ -21,9 +21,9 @@ class ComponentRegistry(dict): def __setitem__(self, key, value: NetworkComponent): if self.super_registry and self.super_registry.check_id_exists(key): - raise ComponentExistsError(key) + raise IdenticalIDError(key) elif not self.super_registry and key in self: - raise ComponentExistsError(key) + raise IdenticalIDError(key) else: super().__setitem__(key, value) @@ -100,12 +100,12 @@ def __new__(cls, *args, **kwargs): return SuperComponentRegistry(["pipes", "pumps", "valves"]) -class ComponentExistsError(Exception): +class IdenticalIDError(Exception): """Raised when a component with the same ID already exists in the network.""" def __init__(self, id, message=None): if not message: - self.message = f'A conflicting component with the ID "{id}" already exists in the network.' + self.message = f'A conflicting component with the ID {id} already exists in the network.' super().__init__(self.message) @@ -114,5 +114,5 @@ class ComponentNotExistingError(Exception): def __init__(self, id, message=None): if not message: - self.message = f'No Component with ID "{id}" found in the network.' + self.message = f'No Component with ID {id} found in the network.' super().__init__(self.message) diff --git a/testing/test_add_element.py b/testing/test_add_element.py index eb02e59..398ac13 100644 --- a/testing/test_add_element.py +++ b/testing/test_add_element.py @@ -3,7 +3,7 @@ from oopnet.elements.network import Network from oopnet.elements.network_components import Junction, Reservoir, Tank, Pipe, Pump, PRV, Link, Node, TCV from oopnet.elements.system_operation import Pattern, Curve -from oopnet.elements.component_registry import ComponentExistsError +from oopnet.elements.component_registry import IdenticalIDError from oopnet.utils.getters import * from oopnet.utils.adders import * @@ -88,27 +88,27 @@ def setUp(self) -> None: # todo: add tests that include patterns + curves def test_add_existing_junction(self): - with self.assertRaises(ComponentExistsError): + with self.assertRaises(IdenticalIDError): add_junction(self.network, Junction(id='J-1')) def test_add_existing_reservoir(self): - with self.assertRaises(ComponentExistsError): + with self.assertRaises(IdenticalIDError): add_reservoir(self.network, Reservoir(id='R-1')) def test_add_existing_tank(self): - with self.assertRaises(ComponentExistsError): + with self.assertRaises(IdenticalIDError): add_tank(self.network, Tank(id='T-1')) def test_add_existing_pipe(self): - with self.assertRaises(ComponentExistsError): + with self.assertRaises(IdenticalIDError): add_pipe(self.network, Pipe(id='P-1')) def test_add_existing_pump(self): - with self.assertRaises(ComponentExistsError): + with self.assertRaises(IdenticalIDError): add_pump(self.network, Pump(id='PU-1')) def test_add_existing_valve(self): - with self.assertRaises(ComponentExistsError): + with self.assertRaises(IdenticalIDError): add_valve(self.network, PRV(id='V-1')) def test_invalid_node(self):