forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decorators.py
More file actions
103 lines (82 loc) · 3.81 KB
/
test_decorators.py
File metadata and controls
103 lines (82 loc) · 3.81 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
# Copyright 2017, Google LLC All rights reserved.
#
# 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 absolute_import
import unittest
import mock
from google.auth.credentials import Credentials
from google.cloud import vision
from google.cloud import vision_helpers
class DecoratorTests(unittest.TestCase):
def test_noop_without_enums(self):
class A(object):
pass
APrime = vision_helpers.decorators.add_single_feature_methods(A)
# It should be the same class object.
assert A is APrime
# Nothing should have been added.
assert not hasattr(A, 'face_detection')
assert not hasattr(A, 'logo_detection')
def test_with_enums(self):
class A(object):
enums = vision.enums
# There should not be detection methods yet.
assert not hasattr(A, 'face_detection')
# Add the detection methods.
APrime = vision_helpers.decorators.add_single_feature_methods(A)
assert A is APrime
# There should be detection methods now.
assert hasattr(A, 'face_detection')
assert callable(A.face_detection)
class SingleFeatureMethodTests(unittest.TestCase):
@mock.patch.object(vision.ImageAnnotatorClient, 'annotate_image')
def test_runs_generic_single_image(self, ai):
ai.return_value = vision.types.AnnotateImageResponse()
# Prove that other aspects of the AnnotateImageRequest, such as the
# image context, will be preserved.
SENTINEL = mock.sentinel.image_context
# Make a face detection request.
client = vision.ImageAnnotatorClient(
credentials=mock.Mock(spec=Credentials),
)
image = {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}}
max_results = 50
response = client.face_detection(image, image_context=SENTINEL,
max_results=max_results)
assert isinstance(response, vision.types.AnnotateImageResponse)
# Assert that the single-image method was called as expected.
ai.assert_called_once_with({
'features': [{'type': vision.enums.Feature.Type.FACE_DETECTION,
'max_results': max_results}],
'image': image,
'image_context': SENTINEL,
}, retry=None, timeout=None)
@mock.patch.object(vision.ImageAnnotatorClient, 'annotate_image')
def test_runs_generic_single_image_without_max_results(self, ai):
ai.return_value = vision.types.AnnotateImageResponse()
# Prove that other aspects of the AnnotateImageRequest, such as the
# image context, will be preserved.
SENTINEL = mock.sentinel.image_context
# Make a face detection request.
client = vision.ImageAnnotatorClient(
credentials=mock.Mock(spec=Credentials),
)
image = {'source': {'image_uri': 'gs://my-test-bucket/image.jpg'}}
response = client.face_detection(image, image_context=SENTINEL)
assert isinstance(response, vision.types.AnnotateImageResponse)
# Assert that the single-image method was called as expected.
ai.assert_called_once_with({
'features': [{'type': vision.enums.Feature.Type.FACE_DETECTION}],
'image': image,
'image_context': SENTINEL,
}, retry=None, timeout=None)