forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_operation.py
More file actions
120 lines (96 loc) · 4.52 KB
/
test_operation.py
File metadata and controls
120 lines (96 loc) · 4.52 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
# Copyright 2016 Google Inc.
#
# 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.
import unittest
class OperationTests(unittest.TestCase):
OPERATION_NAME = '123456789'
def _getTargetClass(self):
from google.cloud.speech.operation import Operation
return Operation
def _makeOne(self, *args, **kwargs):
return self._getTargetClass()(*args, **kwargs)
def test_ctor_defaults(self):
client = _Client()
operation = self._makeOne(client, self.OPERATION_NAME)
self.assertEqual(operation.name, '123456789')
self.assertFalse(operation.complete)
self.assertIsNone(operation.metadata)
self.assertIsNone(operation.results)
def test_from_api_repr(self):
from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE
from google.cloud.speech.operation import Transcript
from google.cloud.speech.metadata import Metadata
RESPONSE = OPERATION_COMPLETE_RESPONSE
client = _Client()
operation = self._getTargetClass().from_api_repr(client, RESPONSE)
self.assertEqual('123456789', operation.name)
self.assertTrue(operation.complete)
self.assertIsInstance(operation.results[0], Transcript)
self.assertEqual(operation.results[0].transcript,
'how old is the Brooklyn Bridge')
self.assertEqual(operation.results[0].confidence, 0.98267895)
self.assertTrue(operation.complete)
self.assertIsInstance(operation.metadata, Metadata)
self.assertEqual(operation.metadata.progress_percent, 100)
def test_update_response(self):
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE
RESPONSE = ASYNC_RECOGNIZE_RESPONSE
client = _Client()
operation = self._getTargetClass().from_api_repr(client, RESPONSE)
self.assertEqual(operation.name, '123456789')
operation._update(OPERATION_COMPLETE_RESPONSE)
self.assertTrue(operation.complete)
def test_poll(self):
from google.cloud.speech.operation import Metadata
from unit_tests._fixtures import ASYNC_RECOGNIZE_RESPONSE
from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE
RESPONSE = ASYNC_RECOGNIZE_RESPONSE
client = _Client()
connection = _Connection(OPERATION_COMPLETE_RESPONSE)
client.connection = connection
operation = self._getTargetClass().from_api_repr(client, RESPONSE)
self.assertFalse(operation.complete)
operation.poll()
self.assertTrue(operation.complete)
self.assertIsInstance(operation.metadata, Metadata)
self.assertEqual(operation.metadata.progress_percent, 100)
requested = client.connection._requested
self.assertEqual(requested[0]['method'], 'GET')
self.assertEqual(requested[0]['path'],
'operations/%s' % (operation.name,))
def test_poll_complete(self):
from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE
from unit_tests._fixtures import OPERATION_INCOMPLETE_RESPONSE
RESPONSE = OPERATION_INCOMPLETE_RESPONSE
client = _Client()
connection = _Connection(OPERATION_COMPLETE_RESPONSE)
client.connection = connection
operation = self._getTargetClass().from_api_repr(client, RESPONSE)
self.assertFalse(operation.complete)
operation.poll() # Update the operation with complete data.
with self.assertRaises(ValueError):
operation.poll()
requested = client.connection._requested
self.assertEqual(requested[0]['method'], 'GET')
self.assertEqual(requested[0]['path'],
'operations/%s' % (operation.name,))
class _Connection(object):
def __init__(self, response=None):
self.response = response
self._requested = []
def api_request(self, method, path):
self._requested.append({'method': method, 'path': path})
return self.response
class _Client(object):
connection = None