Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rjambrecic committed Jun 18, 2024
1 parent 57d2dba commit 94e6328
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 4 additions & 2 deletions google_sheets/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"],
Expand All @@ -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[
Expand All @@ -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."
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
23 changes: 23 additions & 0 deletions tests/app/test_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import datetime
from unittest.mock import patch

from fastapi.testclient import TestClient

Expand All @@ -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
Expand Down

0 comments on commit 94e6328

Please sign in to comment.