From 237199eed8f6be88a2d31d82a56b5f59ce0d3d59 Mon Sep 17 00:00:00 2001
From: alaskowski <alaskowski@ethz.ch>
Date: Fri, 8 Dec 2023 13:25:49 +0100
Subject: [PATCH] SSDM-14138: Refactored get_project() method to fail when
 multiple projects with the same code are found. PyBIS-1.36.4-rc6

---
 .../src/python/CHANGELOG.md                   |  1 +
 .../src/python/pybis/__init__.py              |  2 +-
 .../src/python/pybis/pybis.py                 |  2 ++
 .../src/python/setup.cfg                      |  2 +-
 api-openbis-python3-pybis/src/python/setup.py |  2 +-
 .../src/python/tests/test_project.py          | 36 +++++++++++++++++++
 6 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/api-openbis-python3-pybis/src/python/CHANGELOG.md b/api-openbis-python3-pybis/src/python/CHANGELOG.md
index 0e29be33aa5..a3f4e48a180 100644
--- a/api-openbis-python3-pybis/src/python/CHANGELOG.md
+++ b/api-openbis-python3-pybis/src/python/CHANGELOG.md
@@ -6,6 +6,7 @@
 - Fixed v1 dataset upload
 - Fixed PropertyAssignment data frame creation
 - Improved property data type validation 
+- Improved get_project method 
 
 ## Changes with pybis-1.36.3
 
diff --git a/api-openbis-python3-pybis/src/python/pybis/__init__.py b/api-openbis-python3-pybis/src/python/pybis/__init__.py
index ad8be093f1f..2e2203057b4 100644
--- a/api-openbis-python3-pybis/src/python/pybis/__init__.py
+++ b/api-openbis-python3-pybis/src/python/pybis/__init__.py
@@ -15,7 +15,7 @@
 name = "pybis"
 __author__ = "ID SIS • ETH Zürich"
 __email__ = "openbis-support@id.ethz.ch"
-__version__ = "1.36.4-rc5"
+__version__ = "1.36.4-rc6"
 
 from . import pybis
 from .pybis import DataSet
diff --git a/api-openbis-python3-pybis/src/python/pybis/pybis.py b/api-openbis-python3-pybis/src/python/pybis/pybis.py
index c9b71dd4659..69b6b7a8ff0 100644
--- a/api-openbis-python3-pybis/src/python/pybis/pybis.py
+++ b/api-openbis-python3-pybis/src/python/pybis/pybis.py
@@ -3206,6 +3206,8 @@ class Openbis:
             resp = self._post_request(self.as_v3, request)
             if len(resp["objects"]) == 0:
                 raise ValueError("No such project: %s" % projectId)
+            elif len(resp["objects"]) > 1:
+                raise ValueError("There is more than one project with code '%s'" % projectId)
             if only_data:
                 return resp["objects"][0]
 
diff --git a/api-openbis-python3-pybis/src/python/setup.cfg b/api-openbis-python3-pybis/src/python/setup.cfg
index aad3a926520..899d293121a 100644
--- a/api-openbis-python3-pybis/src/python/setup.cfg
+++ b/api-openbis-python3-pybis/src/python/setup.cfg
@@ -1,6 +1,6 @@
 [metadata]
 name = PyBIS
-version = 1.36.4-rc5
+version = 1.36.4-rc6
 author = ID SIS • ETH Zürich
 author_email = openbis-support@id.ethz.ch
 license = Apache Software License Version 2.0
diff --git a/api-openbis-python3-pybis/src/python/setup.py b/api-openbis-python3-pybis/src/python/setup.py
index 84bab4b8d89..f5fdb5b6c52 100644
--- a/api-openbis-python3-pybis/src/python/setup.py
+++ b/api-openbis-python3-pybis/src/python/setup.py
@@ -26,7 +26,7 @@ with open("README.md", "r", encoding="utf-8") as fh:
 
 setup(
     name="PyBIS",
-    version="1.36.4-rc5",
+    version="1.36.4-rc6",
     author="ID SIS • ETH Zürich",
     author_email="openbis-support@id.ethz.ch",
     description="openBIS connection and interaction, optimized for using with Jupyter",
diff --git a/api-openbis-python3-pybis/src/python/tests/test_project.py b/api-openbis-python3-pybis/src/python/tests/test_project.py
index 82a67c1582b..9f5daf5ca1a 100644
--- a/api-openbis-python3-pybis/src/python/tests/test_project.py
+++ b/api-openbis-python3-pybis/src/python/tests/test_project.py
@@ -63,3 +63,39 @@ def test_create_project_with_attachment(space):
     project_exists = o.get_project(project_name)
     assert project_exists is not None
     assert project_exists.attachments is not None
+
+
+def test_get_project_by_code(space):
+    o = space.openbis
+
+    timestamp = time.strftime("%a_%y%m%d_%H%M%S").upper()
+
+    space_code_1 = "space_1_" + timestamp
+    project_code = "project_" + timestamp
+
+    o.new_space(code=space_code_1).save()
+
+    o.new_project(space=space_code_1, code=project_code).save()
+    project_exists = o.get_project(project_code)
+    assert project_exists is not None
+
+
+def test_get_project_fail_because_of_multiple_projects_existing(space):
+    o = space.openbis
+
+    timestamp = time.strftime("%a_%y%m%d_%H%M%S").upper()
+
+    space_code_1 = "space_1_" + timestamp
+    space_code_2 = "space_2_" + timestamp
+    project_code = "project_" + timestamp
+
+    o.new_space(code=space_code_1).save()
+    o.new_space(code=space_code_2).save()
+
+    o.new_project(space=space_code_1, code=project_code).save()
+    o.new_project(space=space_code_2, code=project_code).save()
+
+    with pytest.raises(ValueError):
+        project_exists = o.get_project(project_code)
+
+
-- 
GitLab