Skip to content

Commit

Permalink
Mock tmp storage for submission RO-Crate creation
Browse files Browse the repository at this point in the history
  • Loading branch information
JLoveUOA committed Dec 11, 2024
1 parent a40a943 commit 12d6cf6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 25 deletions.
46 changes: 26 additions & 20 deletions src/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ async def append_drive_info(
input_submission: InputDriveOffboardSubmission,
session: SessionDep,
response: Response,
background_tasks: BackgroundTasks,
api_key: ApiKey = Security(validate_api_key),
) -> dict[str, str]:
"""Handle requests to create new form submission."""
Expand Down Expand Up @@ -178,11 +179,34 @@ async def append_drive_info(
session.add(related_project)
session.add(submission)
session.commit()

# generate the RO-Crate now that db has been updated
# for now assume "Real" research drive has been mounted somewhere on VM home directory
drive_path = get_resdrive_path(drive.name)
background_tasks.add_task(
generate_ro_crate,
drive_name=drive.name,
session=session,
drive_location=drive_path / "Vault",
output_location=drive_path / "Archive",
)

return {
"message": f"Received additional RO-Crate metadata for {drive.name}.",
}


def get_resdrive_path(drive_name: str) -> Path:
"""Get a path for a research drive.
Please update when service acc logic is finalized"""
drive_path = Path.home() / "mnt" / drive_name
if not drive_path.is_dir():
raise FileNotFoundError(
"Research Drive must be mounted in order to generate RO-Crate"
)
return drive_path


def build_crate_contents(
drive_name: ResearchDriveID,
session: SessionDep,
Expand Down Expand Up @@ -227,7 +251,6 @@ def build_crate_contents(
return ro_crate_loader



async def generate_ro_crate(
drive_name: ResearchDriveID,
session: SessionDep,
Expand All @@ -248,7 +271,6 @@ async def generate_ro_crate(
async def get_drive_info(
drive_id: ResearchDriveID,
session: SessionDep,
background_tasks: BackgroundTasks,
api_key: ApiKey = Security(validate_api_key),
) -> Project:
"""Retrieve information about the specified Research Drive."""
Expand All @@ -266,28 +288,12 @@ async def get_drive_info(
detail=f"Research Drive ID {drive_id} not found in local database.",
)

project_ids = [
project.id for project in drive_found.projects if project.id is not None
]
if len(project_ids) == 0:
projects = drive_found.projects
if len(projects) == 0:
raise HTTPException(
status_code=404,
detail=f"No Projects associated with {drive_id} in local database",
)
# generate the RO-Crate now that db has been updated
# for now assume "Real" research drive has been mounted somewhere on VM home directory
drive_path = Path.home() / "mnt" / drive_found.name
if not drive_path.is_dir():
raise FileNotFoundError(
"Research Drive must be mounted in order to generate RO-Crate"
)
background_tasks.add_task(
generate_ro_crate,
drive_name=drive_found.name,
session=session,
drive_location=drive_path / "Vault",
output_location=drive_path / "Archive",
)

return projects[0]

Expand Down
3 changes: 2 additions & 1 deletion src/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ class ProjectWithDriveMember(BaseProject):
codes: list[Code]
research_drives: list[ResearchDriveServicePublic]
members: list[MemberPublic]



class ROCrateProject(BaseProject):
"""Project model to be serialized as part of an RO-Crate"""

Expand Down
29 changes: 25 additions & 4 deletions tests/test_submission_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from pathlib import Path

from fastapi.testclient import TestClient
from mock import MagicMock, patch
from sqlmodel import Session, select

from models.common import DataClassification
Expand All @@ -7,9 +10,15 @@
from models.submission import DriveOffboardSubmission


@patch("api.main.get_resdrive_path")
def test_post_submission_can_create(
session: Session, client: TestClient, project: Project
):
mock_get_resdrive_path: MagicMock,
session: Session,
client: TestClient,
project: Project,
tmpdir: Path,
) -> None:
mock_get_resdrive_path.return_value = tmpdir
session.add(project)
session.commit()
response = client.post(
Expand Down Expand Up @@ -41,9 +50,15 @@ def test_post_submission_can_create(
assert not saved_submission.is_project_updated


@patch("api.main.get_resdrive_path")
def test_post_submission_can_update_project(
session: Session, client: TestClient, project: Project
mock_get_resdrive_path: MagicMock,
session: Session,
client: TestClient,
project: Project,
tmpdir: Path,
):
mock_get_resdrive_path.return_value = tmpdir
session.add(project)
session.commit()
response = client.post(
Expand Down Expand Up @@ -75,9 +90,15 @@ def test_post_submission_can_update_project(
assert submission.is_project_updated


@patch("api.main.get_resdrive_path")
def test_post_submission_reject_already_submitted(
session: Session, client: TestClient, project: Project
mock_get_resdrive_path: MagicMock,
session: Session,
client: TestClient,
project: Project,
tmpdir: Path,
):
mock_get_resdrive_path.return_value = tmpdir
session.add(project)
session.commit()
response = client.post(
Expand Down

0 comments on commit 12d6cf6

Please sign in to comment.