Skip to content

Commit

Permalink
feat: setup Google Docs functions (#394)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcharest authored Feb 2, 2024
1 parent 7504b42 commit 1b0cd56
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
59 changes: 59 additions & 0 deletions app/integrations/google_workspace/google_docs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
"""Google Docs module.
This module provides functions to create and manipulate Google Docs.
Functions:
create(title: str) -> str:
Creates a new document in Google Docs and returns the id of the new document.
batch_update(document_id: str, requests: list) -> None:
Applies a list of updates to a document in Google Docs.
get(document_id: str) -> dict:
Gets a document from Google Docs and returns the document resource.
"""
from integrations.google_workspace.google_service import get_google_service


def create(title: str) -> str:
"""Creates a new document in Google Docs.
Args:
title (str): The title of the new document.
Returns:
str: The id of the new document.
"""
# pylint: disable=no-member
service = get_google_service("docs", "v1")
result = service.documents().create(body={"title": title}).execute()
return result["documentId"]


def batch_update(document_id: str, requests: list) -> None:
"""Applies a list of updates to a document in Google Docs.
Args:
document_id (str): The id of the document to update.
requests (list): A list of update requests.
Returns:
None
"""
# pylint: disable=no-member
service = get_google_service("docs", "v1")
service.documents().batchUpdate(
documentId=document_id,
body={"requests": requests},
).execute()


def get(document_id: str) -> dict:
"""Gets a document from Google Docs.
Args:
document_id (str): The id of the document to get.
Returns:
dict: The document resource.
"""
# pylint: disable=no-member
service = get_google_service("docs", "v1")
result = service.documents().get(documentId=document_id).execute()
return result
54 changes: 54 additions & 0 deletions app/tests/integrations/google_workspace/test_google_docs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Unit tests for google_docs module."""
from unittest.mock import patch

import integrations.google_workspace.google_docs as google_docs


@patch("integrations.google_workspace.google_docs.get_google_service")
def test_create_returns_document_id(get_google_service_mock):
# the api states: Creates a blank document using the title given in the request. Other fields in the request, including any provided content, are ignored.
get_google_service_mock.return_value.documents.return_value.create.return_value.execute.return_value = {
"documentId": "test_document_id",
"title": "test_document",
"body": {"content": [{}]},
"headers": {},
}
assert google_docs.create("test_document") == "test_document_id"


@patch("integrations.google_workspace.google_docs.get_google_service")
def test_batch_update_with_valid_requests_succeeds(get_google_service_mock):
get_google_service_mock.return_value.documents.return_value.batchUpdate.return_value.execute.return_value = {
"responses": [{"headerId": "test_header_id"}, {}, {}]
}

requests = [
{"createHeader": {"type": "DEFAULT", "sectionBreakLocation": {"index": 1}}},
{"insertText": {"location": {"index": 2}, "text": "Hello world"}},
{"insertText": {"location": {"index": 3}, "text": "Foo"}},
]

assert google_docs.batch_update("test_document_id", requests) is None
get_google_service_mock.return_value.documents.return_value.batchUpdate.assert_called_once_with(
documentId="test_document_id", body={"requests": requests}
)


@patch("integrations.google_workspace.google_docs.get_google_service")
def test_get_returns_document_resource(get_google_service_mock):
get_google_service_mock.return_value.documents.return_value.get.return_value.execute.return_value = {
"documentId": "test_document_id",
"title": "test_document",
"body": {"content": [{}]},
"documentStyle": {},
"namedStyles": {},
"revisionId": "test_revision_id",
"suggestionsViewMode": "test_suggestions_view_mode",
"inlineObjects": {},
"lists": {},
}

document = google_docs.get("test_document_id")
assert document["documentId"] == "test_document_id"
assert document["title"] == "test_document"
assert document["body"] == {"content": [{}]}

0 comments on commit 1b0cd56

Please sign in to comment.