Skip to content

Commit

Permalink
Feat 224 project names (#225)
Browse files Browse the repository at this point in the history
* feat: adds endpoint for project names

* feat: returns sorted list
  • Loading branch information
jtyoung84 authored Apr 29, 2024
1 parent e81d637 commit da0ed52
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/aind_metadata_service/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,21 @@ async def retrieve_funding(project_name, pickle: bool = False):
return model_response.map_to_json_response()


@app.get("/project_names")
async def retrieve_project_names():
"""Retrieves funding information from smartsheet"""

# TODO: We can probably cache the response if it's 200
smart_sheet_response = await run_in_threadpool(
funding_smart_sheet_client.get_sheet
)
mapper = FundingMapper(
smart_sheet_response=smart_sheet_response, input_id=""
)
json_response = mapper.get_project_names()
return json_response


@app.get("/subject/{subject_id}")
async def retrieve_subject(subject_id, pickle: bool = False):
"""
Expand Down
22 changes: 22 additions & 0 deletions src/aind_metadata_service/smartsheet/funding/mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from aind_data_schema.core.data_description import Funding
from aind_data_schema.models.organizations import Organization
from pydantic import ValidationError
from starlette.responses import JSONResponse

from aind_metadata_service.client import StatusCodes
from aind_metadata_service.response_handler import ModelResponse
Expand Down Expand Up @@ -128,3 +129,24 @@ def _get_model_response(self) -> ModelResponse:
except Exception as e:
logging.error(repr(e))
return ModelResponse.internal_server_error_response()

def get_project_names(self) -> JSONResponse:
"""Get list of project names from funding sheet"""
try:
project_names = set()
for row in self.model.rows:
row_dict = self.map_row_to_dict(row)
project_name = row_dict.get(FundingColumnNames.PROJECT_NAME)
if project_name is not None:
project_names.add(project_name)
return JSONResponse(
status_code=200,
content=(
{"message": "Success", "data": sorted(list(project_names))}
),
)
except Exception as e:
return JSONResponse(
status_code=500,
content=({"message": f"Error: {e}", "data": None}),
)
51 changes: 51 additions & 0 deletions tests/smartsheet/test_funding.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,57 @@ def test_mapping_error(
]
)

@patch("smartsheet.sheets.Sheets.get_sheet")
def test_get_project_names_success(self, mock_get_sheet: MagicMock):
"""Tests successful sheet return response of project names"""
mock_get_sheet.return_value.to_json.return_value = self.example_sheet
settings = SmartsheetSettings(
access_token="abc-123", sheet_id=7478444220698500
)
client = SmartSheetClient(smartsheet_settings=settings)
smart_sheet_response = client.get_sheet()
mapper = FundingMapper(
smart_sheet_response=smart_sheet_response,
input_id="",
)
json_response = mapper.get_project_names()
expected_response = {
"message": "Success",
"data": ["AIND Scientific Activities", "v1omFISH"],
}
self.assertEqual(
json.loads(json_response.body.decode("utf-8")), expected_response
)

@patch("smartsheet.sheets.Sheets.get_sheet")
@patch("logging.error")
def test_get_project_names_failure(
self, mock_log_error: MagicMock, mock_get_sheet: MagicMock
):
"""Tests failure of sheet return response of project names"""

def mock_get_sheet_error(_):
"""Mock the get_sheet so that it returns an error."""
raise Exception("Something went wrong")

type(mock_get_sheet.return_value).to_json = mock_get_sheet_error
settings = SmartsheetSettings(
access_token="abc-123", sheet_id=7478444220698500
)
client = SmartSheetClient(smartsheet_settings=settings)
smart_sheet_response = client.get_sheet()
mapper = FundingMapper(
smart_sheet_response=smart_sheet_response,
input_id="",
)
json_response = mapper.get_project_names()
self.assertEqual(500, json_response.status_code)
mock_log_error.assert_has_calls(
[
call("Exception('Something went wrong',)"),
]
)


class TestFundingModels(unittest.TestCase):
"""Test methods in models package"""
Expand Down

0 comments on commit da0ed52

Please sign in to comment.