diff --git a/app/integrations/google_workspace/google_docs.py b/app/integrations/google_workspace/google_docs.py index 275ae601..7fc2d0be 100644 --- a/app/integrations/google_workspace/google_docs.py +++ b/app/integrations/google_workspace/google_docs.py @@ -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 diff --git a/app/tests/integrations/google_workspace/test_google_docs.py b/app/tests/integrations/google_workspace/test_google_docs.py new file mode 100644 index 00000000..e7f5e579 --- /dev/null +++ b/app/tests/integrations/google_workspace/test_google_docs.py @@ -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": [{}]}