Skip to content

Commit

Permalink
fix: added ComponentExistsError to __init__.py and renamed to Identic…
Browse files Browse the repository at this point in the history
…alIDError
  • Loading branch information
tiuri101 committed Feb 14, 2022
1 parent 4f5d9bd commit cde55a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
4 changes: 4 additions & 0 deletions oopnet/elements/__init__.py
Expand Up @@ -25,3 +25,7 @@
Controlcondition,
Energy,
)
from .component_registry import (
ComponentNotExistingError,
IdenticalIDError
)
10 changes: 5 additions & 5 deletions oopnet/elements/component_registry.py
Expand Up @@ -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)

Expand Down Expand Up @@ -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)


Expand All @@ -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)
14 changes: 7 additions & 7 deletions testing/test_add_element.py
Expand Up @@ -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 *

Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit cde55a0

Please sign in to comment.