Skip to content

Commit

Permalink
handle no id
Browse files Browse the repository at this point in the history
  • Loading branch information
BWMac committed Jan 30, 2025
1 parent b512968 commit bde0e55
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 34 deletions.
15 changes: 9 additions & 6 deletions src/agoradatatools/process.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import logging
from typing import Union
from typing import Optional, Union

import synapseclient
from pandas import DataFrame
Expand Down Expand Up @@ -73,25 +73,28 @@ def upload_dataversion_metadata(
syn: synapseclient.Synapse,
file_id: str,
file_version: str,
team_images_id: str,
staging_path: str,
destination: str,
team_images_id: Optional[str] = None,
) -> None:
"""Uploads dataversion.json file to Synapse with metadata about the manifest file
"""Uploads dataversion.json file to Synapse with metadata about the manifest file.
Model-AD runs do not have a team_images_id, which will be left out of the dataversion.json file.
Args:
syn (synapseclient.Synapse): Synapse client session
file_id (str): Synapse ID of the manifest file
file_version (str): Version number of the manifest file
team_images_id (str): Synapse ID of the team_images folder
staging_path (str): Path to the staging directory
destination (str): Synapse ID of the destination folder
team_images_id (str, optional): Synapse ID of the team_images folder if provided. Defaults to None.
"""
dataversion_dict = {
"data_file": file_id,
"data_version": file_version,
"team_images_id": team_images_id,
}
if team_images_id:
dataversion_dict["team_images_id"] = team_images_id

dataversion_json_path = load.dict_to_json(
df=dataversion_dict, staging_path=staging_path, filename="dataversion.json"
)
Expand Down Expand Up @@ -340,7 +343,7 @@ def process_all_files(
syn=syn,
file_id=file_id,
file_version=file_version,
team_images_id=config["team_images_id"],
team_images_id=config.get("team_images_id", None),
staging_path=staging_path,
destination=destination,
)
Expand Down
86 changes: 58 additions & 28 deletions tests/test_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,70 @@ class TestUploadDataversionMetadata:
file_version = "1"
team_images_id = "syn12861877"
destination = "syn1111113"
dataversion_dict = {
dataversion_dict_with_team_images_id = {
"data_file": file_id,
"data_version": file_version,
"team_images_id": team_images_id,
}
dataversion_dict_without_team_images_id = {
"data_file": file_id,
"data_version": file_version,
}

def test_upload_dataversion_metadata(self, syn: Any):
with patch.object(
@pytest.fixture(scope="function", autouse=True)
def setup_method(self):
self.patch_dict_to_json = patch.object(
load, "dict_to_json", return_value="path/to/json"
) as patch_dict_to_json, patch.object(
load, "load", return_value=("syn123", 1)
) as patch_load:
# WHEN I call upload_dataversion_metadata with the correct arguments
process.upload_dataversion_metadata(
syn=syn,
file_id=self.file_id,
file_version=self.file_version,
team_images_id=self.team_images_id,
staging_path=STAGING_PATH,
destination=self.destination,
)
# THEN I expect the dict_to_json function to be called with the correct arguments
patch_dict_to_json.assert_called_once_with(
df=self.dataversion_dict,
staging_path=STAGING_PATH,
filename="dataversion.json",
)
# AND I expect the load function to be called with the correct arguments
patch_load.assert_called_once_with(
file_path="path/to/json",
provenance=[self.file_id],
destination=self.destination,
syn=syn,
)
).start()
self.patch_load = patch.object(load, "load", return_value=("syn123", 1)).start()

def test_upload_dataversion_metadata_with_team_images_id(self, syn: Any):
# WHEN I call upload_dataversion_metadata with a team_images_id
process.upload_dataversion_metadata(
syn=syn,
file_id=self.file_id,
file_version=self.file_version,
team_images_id=self.team_images_id,
staging_path=STAGING_PATH,
destination=self.destination,
)
# THEN I expect the dict_to_json function to be called with the correct arguments
self.patch_dict_to_json.assert_called_once_with(
df=self.dataversion_dict_with_team_images_id,
staging_path=STAGING_PATH,
filename="dataversion.json",
)
# AND I expect the load function to be called with the correct arguments
self.patch_load.assert_called_once_with(
file_path="path/to/json",
provenance=[self.file_id],
destination=self.destination,
syn=syn,
)

def test_upload_dataversion_metadata_without_team_images_id(self, syn: Any):
# WHEN I call upload_dataversion_metadata without a team_images_id
process.upload_dataversion_metadata(
syn=syn,
file_id=self.file_id,
file_version=self.file_version,
staging_path=STAGING_PATH,
destination=self.destination,
team_images_id=None,
)
# THEN I expect the dict_to_json function to be called with the correct arguments
self.patch_dict_to_json.assert_called_once_with(
df=self.dataversion_dict_without_team_images_id,
staging_path=STAGING_PATH,
filename="dataversion.json",
)
# AND I expect the load function to be called with the correct arguments
self.patch_load.assert_called_once_with(
file_path="path/to/json",
provenance=[self.file_id],
destination=self.destination,
syn=syn,
)


class TestProcessDataset:
Expand Down

0 comments on commit bde0e55

Please sign in to comment.