generated from cds-snc/project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: setup Google Docs functions (#394)
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
54
app/tests/integrations/google_workspace/test_google_docs.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": [{}]} |