Skip to content

Commit

Permalink
raise errors when removing site/conn not in top
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjonesBSU committed Sep 8, 2023
1 parent 7a8272a commit 59248ea
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
8 changes: 8 additions & 0 deletions gmso/core/topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,10 @@ def remove_site(self, site):
gmso.core.topology.Topology.iter_connections_by_site
The method that shows all connections belonging to a specific site
"""
if site not in self._sites:
raise ValueError(
f"Site {site} is not currently part of this topology."
)
for connection in self.iter_connections_by_site(site):
self.remove_connection(connection)
self._sites.remove(site)
Expand All @@ -682,6 +686,10 @@ def remove_connection(self, connection):
The sites that belong to this connection are
not removed from the topology.
"""
if connection not in self.connections:
raise ValueError(
f"Connection {connection} is not currently part of this topology."
)
if isinstance(connection, gmso.core.bond.Bond):
self._bonds.remove(connection)
elif isinstance(connection, gmso.core.angle.Angle):
Expand Down
14 changes: 14 additions & 0 deletions gmso/tests/test_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ def test_remove_site(self, ethane):
assert ethane.n_sites == 2
assert ethane.n_connections == 1

def test_remove_site_not_in_top(self, ethane):
top = Topology()
site = Atom(name="site")
with pytest.raises(ValueError):
top.remove_site(site)

def test_add_connection(self):
top = Topology()
atom1 = Atom(name="atom1")
Expand All @@ -83,6 +89,14 @@ def test_remove_connection(self):
top.remove_connection(connect)
assert top.n_connections == 0

def test_remove_connection(self):
top = Topology()
atom1 = Atom(name="atom1")
atom2 = Atom(name="atom2")
connect = Bond(connection_members=[atom1, atom2])
with pytest.raises(ValueError):
top.remove_connection(connect)

def test_add_box(self):
top = Topology()
box = Box(2 * u.nm * np.ones(3))
Expand Down

0 comments on commit 59248ea

Please sign in to comment.