forked from smooth80/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.py
89 lines (75 loc) · 2.89 KB
/
main_test.py
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
# 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.
import base64
import concurrent.futures
import json
import mock
import main
class TestGCFPyOCRSample():
@mock.patch.object(main, 'publisher')
@mock.patch.object(main, 'translate_client')
@mock.patch.object(main, 'vision_client')
def test_detect_text(self, mock_vision_client, mock_translate_client,
mock_publisher):
mock_annotation = mock.MagicMock()
mock_annotation.description = 'sample text'
mock_annotations = mock.MagicMock()
mock_annotations.text_annotations = [mock_annotation]
mock_vision_client.text_detection.return_value = mock_annotations
mock_translate_client.detect_language.return_value = {'language': 'en'}
mock_future = concurrent.futures.Future()
mock_future.set_result(True)
mock_publisher.publish.return_value = mock_future
main.detect_text('sample-bucket', 'sample-file')
@mock.patch.object(main, 'detect_text')
def test_process_image(self, m):
m.return_value = None
event = {
'bucket': 'sample-bucket',
'name': 'sample-file'
}
context = {}
main.process_image(event, context)
@mock.patch.object(main, 'publisher')
@mock.patch.object(main, 'translate_client')
def test_translate_text(self, mock_translate_client, mock_publisher):
mock_translate_client.translate.return_value = {'translatedText': ''}
mock_future = concurrent.futures.Future()
mock_future.set_result(True)
mock_publisher.publish.return_value = mock_future
data = base64.b64encode(json.dumps({
'text': 'menu',
'filename': 'sample-file',
'lang': 'es',
'src_lang': 'en'
}).encode('utf-8'))
event = {
'data': data
}
context = {}
main.translate_text(event, context)
@mock.patch.object(main, 'storage_client')
def test_save_result(self, m):
bucket = m.bucket.return_value
file = bucket.file.return_value
file.save.return_value = None
data = base64.b64encode(json.dumps({
'text': 'menu',
'filename': 'sample-file',
'lang': 'fr',
}).encode('utf-8'))
event = {
'data': data
}
context = {}
main.save_result(event, context)