forked from googleworkspace/python-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_test.py
More file actions
175 lines (160 loc) Β· 5.63 KB
/
base_test.py
File metadata and controls
175 lines (160 loc) Β· 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function
import sys
import unittest
import httplib2
from oauth2client.client import GoogleCredentials
from googleapiclient import errors
from googleapiclient.discovery import build
class BaseTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.credentials = cls.create_credentials()
http = cls.credentials.authorize(httplib2.Http())
cls.credentials.refresh(http)
cls.service = build('slides', 'v1', http=http)
cls.drive_service = build('drive', 'v3', http=http)
cls.sheets_service = build('sheets', 'v4', http=http)
# Hide STDOUT output generated by snippets.
cls.stdout = sys.stdout
sys.stdout = None
@classmethod
def tearDownClass(cls):
# Restore STDOUT.
sys.stdout = cls.stdout
@classmethod
def create_credentials(cls):
credentials = GoogleCredentials.get_application_default()
scope = [
'https://www.googleapis.com/auth/drive',
]
return credentials.create_scoped(scope)
def setUp(self):
self.files_to_delete = []
def tearDown(self):
for file_id in self.files_to_delete:
try:
self.drive_service.files().delete(fileId=file_id).execute()
except errors.HttpError:
print('Unable to delete file %s' % file_id, file=sys.stderr)
def delete_file_on_cleanup(self, file_id):
self.files_to_delete.append(file_id)
def create_test_presentation(self):
presentation = {
'title': 'Test Preso'
}
presentation = self.service.presentations().create(
body=presentation).execute()
self.delete_file_on_cleanup(presentation.get('presentationId'))
return presentation.get('presentationId')
def add_slides(self, presentation_id, num, layout='TITLE_AND_TWO_COLUMNS'):
requests = []
slide_ids = []
for i in range(num):
slide_id = 'slide_{0}'.format(i)
slide_ids.append(slide_id)
requests.append({
'createSlide': {
'objectId': slide_ids[i],
'slideLayoutReference': {
'predefinedLayout': layout
}
}
})
body = {
'requests': requests
}
response = self.service.presentations().batchUpdate(
presentationId=presentation_id, body=body).execute()
return slide_ids
def create_test_textbox(self, presentation_id, page_id):
box_id = 'MyTextBox_01'
pt350 = {
'magnitude': 350,
'unit': 'PT'
}
requests = []
requests.append({
'createShape': {
'objectId': box_id,
'shapeType': 'TEXT_BOX',
'elementProperties': {
'pageObjectId': page_id,
'size': {
'height': pt350,
'width': pt350
},
'transform': {
'scaleX': 1,
'scaleY': 1,
'translateX': 350,
'translateY': 100,
'unit': 'PT'
}
}
}
})
requests.append({
'insertText': {
'objectId': box_id,
'insertionIndex': 0,
'text': 'New Box Text Inserted'
}
})
body = {
'requests': requests
}
response = self.service.presentations().batchUpdate(
presentationId=presentation_id, body=body).execute()
return response.get('replies')[0].get('createShape').get('objectId')
def create_test_sheets_chart(
self, presentation_id, page_id, spreadsheet_id, sheet_chart_id):
chart_id = 'MyChart_01'
emu4M = {
'magnitude': 4000000,
'unit': 'EMU'
}
requests = []
requests.append({
'createSheetsChart': {
'objectId': chart_id,
'spreadsheetId': spreadsheet_id,
'chartId': sheet_chart_id,
'linkingMode': 'LINKED',
'elementProperties': {
'pageObjectId': page_id,
'size': {
'height': emu4M,
'width': emu4M
},
'transform': {
'scaleX': 1,
'scaleY': 1,
'translateX': 100000,
'translateY': 100000,
'unit': 'EMU'
}
}
}
})
body = {
'requests': requests
}
response = self.service.presentations().batchUpdate(
presentationId=presentation_id, body=body).execute()
return response.get('replies')[0] \
.get('createSheetsChart').get('objectId')
if __name__ == '__main__':
unittest.main()