Repository: PMEAL/OpenPNM

Issue:
Object names must be unique within a project
Currently, this is not enforced.

Discussion:

Patch:
diff --git a/openpnm/core/Base.py b/openpnm/core/Base.py
index f99efe03c..7a19fa281 100644
--- a/openpnm/core/Base.py
+++ b/openpnm/core/Base.py
@@ -1,7 +1,6 @@
 import warnings
 import uuid
 import numpy as np
-import scipy as sp
 from collections import namedtuple
 from openpnm.models.misc import from_neighbor_throats, from_neighbor_pores
 from openpnm.utils import Workspace, logging
@@ -156,6 +155,7 @@ class Base(dict):
             project = ws.new_project()
         if name is None:
             name = project._generate_name(self)
+        project._validate_name(name)
         project.extend(self)
         self.settings['name'] = name
         self.update({'pore.all': np.ones(shape=(Np, ), dtype=bool)})
@@ -282,6 +282,8 @@ class Base(dict):
 
     def _set_name(self, name, validate=True):
         old_name = self.settings['name']
+        if name == old_name:
+            return
         if name is None:
             name = self.project._generate_name(self)
         if validate:
diff --git a/openpnm/utils/Workspace.py b/openpnm/utils/Workspace.py
index 9aa0e22de..609a0dee6 100644
--- a/openpnm/utils/Workspace.py
+++ b/openpnm/utils/Workspace.py
@@ -53,7 +53,9 @@ class Workspace(dict):
         if name is None:
             name = self._gen_name()
         if name in self.keys():
-            raise Exception("A project named " + name + " already exists")
+            # To ensure renaming a Project to its current name is allowed
+            if self.__instance__[name] is not project:
+                raise Exception(f"A project named {name} already exists")
         if project in self.values():
             self.pop(project.name, None)
         if not isinstance(project, openpnm.utils.Project):


Test patch:
diff --git a/tests/unit/core/BaseTest.py b/tests/unit/core/BaseTest.py
index e61e51791..d333fbfd6 100644
--- a/tests/unit/core/BaseTest.py
+++ b/tests/unit/core/BaseTest.py
@@ -958,6 +958,15 @@ class BaseTest:
         pn.set_label(label='tester', mode='purge')
         # Should only issue warning
 
+    def test_renaming_to_current_name_is_allowed(self):
+        obj = op.core.Base(name="temp")
+        obj.name = "temp"
+
+    def test_object_names_must_be_unique_within_project(self):
+        obj = op.core.Base(name="temp")
+        with pytest.raises(Exception):
+            op.core.Base(name="temp", project=obj.project)
+
 
 if __name__ == '__main__':
 
diff --git a/tests/unit/utils/WorkspaceTest.py b/tests/unit/utils/WorkspaceTest.py
index 99fcf5bb3..364e61ca9 100644
--- a/tests/unit/utils/WorkspaceTest.py
+++ b/tests/unit/utils/WorkspaceTest.py
@@ -1,8 +1,5 @@
 import os
 import pytest
-import pickle
-import numpy as np
-import scipy as sp
 import openpnm as op
 
 
@@ -34,7 +31,7 @@ class WorkspaceTest:
     def test_assign_project(self):
         proj = self.ws.new_project()
         with pytest.raises(Exception):
-            self.ws[proj.name] = proj
+            self.ws.new_project(name=proj.name)
         old_name = proj.name
         new_name = self.ws._gen_name()
         self.ws[new_name] = proj