Skip to content

Commit 7b95bcb

Browse files
committed
moved util into a utils file
1 parent 677cdb5 commit 7b95bcb

5 files changed

Lines changed: 69 additions & 76 deletions

File tree

synapseclient/extensions/curator/file_based_metadata_task.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from synapseclient import Synapse # type: ignore
1111
from synapseclient import Wiki # type: ignore
1212
from synapseclient.core.exceptions import SynapseHTTPError # type: ignore
13+
from synapseclient.extensions.curator.utils import project_id_from_entity_id
1314
from synapseclient.models import ( # type: ignore
1415
Column,
1516
ColumnType,
@@ -430,9 +431,6 @@ def create_file_based_metadata_task(
430431
synapse_client.logger.info(
431432
"Attempting to get the Synapse ID of the provided folders project."
432433
)
433-
from synapseclient.extensions.curator.record_based_metadata_task import (
434-
project_id_from_entity_id,
435-
)
436434

437435
project_id = project_id_from_entity_id(folder_id, synapse_client=synapse_client)
438436
synapse_client.logger.info("Got the Synapse ID of the provided folders project.")

synapseclient/extensions/curator/record_based_metadata_task.py

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111
from synapseclient import Synapse
1212
from synapseclient.core.typing_utils import DataFrame as DATA_FRAME_TYPE
1313
from synapseclient.core.utils import test_import_pandas
14+
from synapseclient.extensions.curator.utils import project_id_from_entity_id
1415
from synapseclient.models import (
1516
CurationTask,
1617
Grid,
1718
JSONSchema,
18-
Project,
1919
RecordBasedMetadataTaskProperties,
2020
RecordSet,
2121
)
22-
from synapseclient.operations import get
2322

2423

2524
def extract_property_titles(schema_data: Dict[str, Any]) -> List[str]:
@@ -294,29 +293,3 @@ def create_record_based_metadata_task(
294293
raise e
295294

296295
return record_set_with_data, curation_task, curation_grid
297-
298-
299-
def project_id_from_entity_id(entity_id: str, synapse_client: Synapse) -> str:
300-
"""
301-
Retrieves the project ID from a given entity ID by traversing up the folder hierarchy
302-
303-
Args:
304-
entity_id: The Synapse ID of the entity (e.g., folder, file) to start from.
305-
synapse_client: Authenticated Synapse client instance
306-
307-
Returns:
308-
The Synapse ID of the project that the entity belongs to.
309-
310-
Raises:
311-
ValueError: If the project ID cannot be found within 1000 iterations.
312-
"""
313-
314-
# Get the project ID from the folder ID
315-
current_obj = get(entity_id, synapse_client=synapse_client)
316-
iterations = 0
317-
while not isinstance(current_obj, Project):
318-
current_obj = get(current_obj.parent_id, synapse_client=synapse_client)
319-
iterations += 1
320-
if iterations > 1000:
321-
raise ValueError("Could not find project ID in folder hierarchy")
322-
return current_obj.id
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from synapseclient import Synapse
2+
from synapseclient.models import Project
3+
from synapseclient.operations import get
4+
5+
6+
def project_id_from_entity_id(entity_id: str, synapse_client: Synapse) -> str:
7+
"""
8+
Retrieves the project ID from a given entity ID by traversing up the folder hierarchy
9+
10+
Args:
11+
entity_id: The Synapse ID of the entity (e.g., folder, file) to start from.
12+
synapse_client: Authenticated Synapse client instance
13+
14+
Returns:
15+
The Synapse ID of the project that the entity belongs to.
16+
17+
Raises:
18+
ValueError: If the project ID cannot be found within 1000 iterations.
19+
"""
20+
21+
# Get the project ID from the folder ID
22+
current_obj = get(entity_id, synapse_client=synapse_client)
23+
iterations = 0
24+
while not isinstance(current_obj, Project):
25+
current_obj = get(current_obj.parent_id, synapse_client=synapse_client)
26+
iterations += 1
27+
if iterations > 1000:
28+
raise ValueError("Could not find project ID in folder hierarchy")
29+
return current_obj.id

tests/integration/synapseclient/extensions/curator/test_record_based_metadata_task.py

Lines changed: 0 additions & 44 deletions
This file was deleted.

tests/integration/synapseclient/test_schema_management.py renamed to tests/integration/synapseclient/extensions/curator/test_schema_management.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
from synapseclient import Synapse
1010
from synapseclient.extensions.curator import bind_jsonschema, register_jsonschema
11-
from synapseclient.models import File, Folder, Project, SchemaOrganization
11+
from synapseclient.extensions.curator.record_based_metadata_task import (
12+
project_id_from_entity_id,
13+
)
14+
from synapseclient.models import Folder, Project, SchemaOrganization
1215

1316

1417
def create_test_name():
@@ -259,3 +262,37 @@ def test_complete_workflow(
259262
# Cleanup: unbind schema before deleting folder
260263
folder.unbind_schema(synapse_client=syn)
261264
syn.delete(folder.id)
265+
266+
267+
class TestProjectIDFromEntityID:
268+
@pytest.fixture(scope="module")
269+
def temp_hierarchy(self, syn: Synapse, request) -> tuple[str, str, str]:
270+
"""Creates a Project -> Folder -> Folder hierarchy for testing."""
271+
project = Project(name=create_test_name()).store(synapse_client=syn)
272+
folder1 = Folder(name=create_test_name(), parent_id=project.id).store(
273+
synapse_client=syn
274+
)
275+
folder2 = Folder(name=create_test_name(), parent_id=folder1.id).store(
276+
synapse_client=syn
277+
)
278+
279+
def delete_project():
280+
project.delete(synapse_client=syn)
281+
282+
request.addfinalizer(delete_project)
283+
return project.id, folder1.id, folder2.id
284+
285+
def test_project_id_from_folder(self, syn, temp_hierarchy):
286+
"""Test finding project id when input id is from a nested folder."""
287+
folder_id = temp_hierarchy[2]
288+
expected_project_id = temp_hierarchy[0]
289+
290+
result = project_id_from_entity_id(folder_id, syn)
291+
assert result == expected_project_id
292+
293+
def test_project_id_from_project(self, syn, temp_hierarchy):
294+
"""Test finding project id when input id is for a project"""
295+
project_id = temp_hierarchy[0]
296+
297+
result = project_id_from_entity_id(project_id, syn)
298+
assert result == project_id

0 commit comments

Comments
 (0)