Skip to content

Commit 06e0c20

Browse files
feat: only valid smartsheet responses get cached (#14)
* feat: only valid smartsheet responses get cached Also adds new pytest for checking that invalid smartsheet responses don't get cached * feat: updates get_smartsheet method to check the Smartsheet SDK response for errors Also updates test_get_smartsheet_fail logic and adds new test for raising HTTP exceptions for smartsheet API errors * test: removes unused import * feat: edits get_smartsheet to raise Exception on instance of Error model * style: removes spacing * test: update get_smartsheet tests to match new error handling - Added SmartsheetError mocking - Adjusted generic exception test - Restored to_json assertions in successful case
1 parent 22abdc9 commit 06e0c20

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

aind-smartsheet-service-server/src/aind_smartsheet_service_server/route.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
from asyncio import to_thread
44
from typing import List, Optional
55

6-
from fastapi import APIRouter, Query, status
6+
from fastapi import APIRouter, HTTPException, Query, status
77
from fastapi_cache.decorator import cache
88
from smartsheet import Smartsheet
9+
from smartsheet.models.error import Error as SmartsheetError
910

1011
from aind_smartsheet_service_server.configs import settings
1112
from aind_smartsheet_service_server.handler import SheetHandler
@@ -29,8 +30,7 @@ async def get_smartsheet(sheet_id: int) -> str:
2930
3031
Returns
3132
-------
32-
str
33-
33+
str or raises Exception
3434
"""
3535

3636
client = Smartsheet(
@@ -39,6 +39,12 @@ async def get_smartsheet(sheet_id: int) -> str:
3939
access_token=(settings.access_token.get_secret_value()),
4040
)
4141
sheet = await to_thread(client.Sheets.get_sheet, sheet_id)
42+
43+
if isinstance(sheet, SmartsheetError):
44+
status = sheet.result.status_code
45+
message = sheet.result.message or "Smartsheet error"
46+
raise HTTPException(status_code=status, detail=message)
47+
4248
return sheet.to_json()
4349

4450

aind-smartsheet-service-server/tests/test_route.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from unittest.mock import AsyncMock, MagicMock, call, patch
44

55
import pytest
6+
from fastapi import HTTPException
7+
from smartsheet.models.error import Error as SmartsheetError
68
from starlette.testclient import TestClient
79

810
from aind_smartsheet_service_server.route import get_smartsheet
@@ -28,6 +30,28 @@ async def test_get_smartsheet(self, mock_get_sheet: MagicMock):
2830
mock_get_sheet.assert_has_calls([call(0), call().to_json()])
2931
assert '{"a": "b"}' == sheet
3032

33+
@patch("smartsheet.sheets.Sheets.get_sheet")
34+
async def test_get_smartsheet_fail(self, mock_get_sheet: MagicMock):
35+
"""Tests generic exception handling"""
36+
mock_get_sheet.side_effect = Exception("Fail")
37+
with pytest.raises(Exception) as e:
38+
_ = await get_smartsheet(sheet_id=0)
39+
assert "Fail" in str(e.value)
40+
41+
@patch("smartsheet.sheets.Sheets.get_sheet")
42+
async def test_get_smartsheet_error(self, mock_get_sheet: MagicMock):
43+
"""Tests SmartsheetError triggers HTTPException"""
44+
error_obj = SmartsheetError(MagicMock())
45+
error_obj.result = MagicMock()
46+
error_obj.result.status_code = 404
47+
error_obj.result.message = "Not Found"
48+
mock_get_sheet.return_value = error_obj
49+
50+
with pytest.raises(HTTPException) as e:
51+
_ = await get_smartsheet(sheet_id=0)
52+
assert e.value.status_code == 404
53+
assert "Not Found" in str(e.value.detail)
54+
3155
@patch("aind_smartsheet_service_server.route.get_smartsheet")
3256
async def test_get_funding(
3357
self,

0 commit comments

Comments
 (0)