forked from GoogleCloudPlatform/python-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.py
More file actions
107 lines (85 loc) · 2.98 KB
/
main_test.py
File metadata and controls
107 lines (85 loc) · 2.98 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
# 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 collections import UserDict
import uuid
from mock import MagicMock, patch
import main
@patch('main.__blur_image')
@patch('main.vision_client')
@patch('main.storage_client')
def test_process_offensive_image(
storage_client,
vision_client,
__blur_image,
capsys):
result = UserDict()
result.safe_search_annotation = UserDict()
result.safe_search_annotation.adult = 5
result.safe_search_annotation.violence = 5
vision_client.safe_search_detection = MagicMock(return_value=result)
filename = str(uuid.uuid4())
data = {
'bucket': 'my-bucket',
'name': filename
}
main.blur_offensive_images(data, None)
out, _ = capsys.readouterr()
assert 'Analyzing %s.' % filename in out
assert 'The image %s was detected as inappropriate.' % filename in out
assert main.__blur_image.called
@patch('main.__blur_image')
@patch('main.vision_client')
@patch('main.storage_client')
def test_process_safe_image(
storage_client,
vision_client,
__blur_image,
capsys):
result = UserDict()
result.safe_search_annotation = UserDict()
result.safe_search_annotation.adult = 1
result.safe_search_annotation.violence = 1
vision_client.safe_search_detection = MagicMock(return_value=result)
filename = str(uuid.uuid4())
data = {
'bucket': 'my-bucket',
'name': filename
}
main.blur_offensive_images(data, None)
out, _ = capsys.readouterr()
assert 'Analyzing %s.' % filename in out
assert 'The image %s was detected as OK.' % filename in out
assert __blur_image.called is False
@patch('main.os')
@patch('main.Image')
def test_blur_image(image_mock, os_mock, capsys):
filename = str(uuid.uuid4())
os_mock.remove = MagicMock()
os_mock.path = MagicMock()
os_mock.path.basename = MagicMock(side_effect=(lambda x: x))
image_mock.return_value = image_mock
image_mock.__enter__.return_value = image_mock
blob = UserDict()
blob.name = filename
blob.bucket = UserDict()
blob.bucket.blob = MagicMock(return_value=blob)
blob.download_to_filename = MagicMock()
blob.upload_from_filename = MagicMock()
main.__blur_image(blob)
out, _ = capsys.readouterr()
assert f'Image {filename} was downloaded to' in out
assert f'Image {filename} was blurred.' in out
assert f'Blurred image was uploaded to blurred-{filename}.' in out
assert os_mock.remove.called
assert image_mock.resize.called