diff --git a/google_sheets/app.py b/google_sheets/app.py index e45c386..3a7a520 100644 --- a/google_sheets/app.py +++ b/google_sheets/app.py @@ -6,6 +6,7 @@ from typing import Annotated, Any, List, Union import python_weather +from asyncify import asyncify from fastapi import FastAPI, HTTPException, Query from google.oauth2.credentials import Credentials from googleapiclient.discovery import build @@ -118,6 +119,7 @@ async def load_user_credentials(user_id: Union[int, str]) -> Any: return data.creds +@asyncify # type: ignore[misc] def _get_sheet(user_credentials: Any, spreadshit_id: str, range: str) -> Any: sheets_credentials = { "refresh_token": user_credentials["refresh_token"], @@ -143,7 +145,7 @@ async def get_sheet( user_id: Annotated[ int, Query(description="The user ID for which the data is requested") ], - spreadshit_id: Annotated[ + spreadsheet_id: Annotated[ str, Query(description="ID of the Google Sheet to fetch data from") ], range: Annotated[ @@ -152,7 +154,7 @@ async def get_sheet( ], ) -> Union[str, List[List[str]]]: user_credentials = await load_user_credentials(user_id) - values = _get_sheet(user_credentials, spreadshit_id, range) + values = await _get_sheet(user_credentials, spreadsheet_id, range) if not values: return "No data found." diff --git a/pyproject.toml b/pyproject.toml index 4f9943c..5536ba4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -49,6 +49,7 @@ dependencies = [ "python-weather==2.0.3", "prisma==0.13.1", "google-api-python-client==2.133.0", + "asyncify==0.10.0", ] [project.optional-dependencies] diff --git a/tests/app/test_app.py b/tests/app/test_app.py index 3a27068..4e67a79 100644 --- a/tests/app/test_app.py +++ b/tests/app/test_app.py @@ -1,4 +1,5 @@ import datetime +from unittest.mock import patch from fastapi.testclient import TestClient @@ -9,6 +10,28 @@ class TestRoutes: + def test_get_sheet(self) -> None: + with patch( + "google_sheets.app.load_user_credentials", + return_value={"refresh_token": "abcdf"}, + ) as mock_load_user_credentials: + excepted = [ + ["Campaign", "Ad Group", "Keyword"], + ["Campaign A", "Ad group A", "Keyword A"], + ["Campaign A", "Ad group A", "Keyword B"], + ["Campaign A", "Ad group A", "Keyword C"], + ] + with patch( + "google_sheets.app._get_sheet", return_value=excepted + ) as mock_get_sheet: + response = client.get( + "/sheet?user_id=123&spreadsheet_id=abc&range=Sheet1" + ) + mock_load_user_credentials.assert_called_once() + mock_get_sheet.assert_called_once() + assert response.status_code == 200 + assert response.json() == excepted + def test_weather_route(self) -> None: response = client.get("/?city=Chennai") assert response.status_code == 200