Skip to content

Commit 3bdd57e

Browse files
committed
Use mocks, and breakout tests.
1 parent 2eec0d9 commit 3bdd57e

3 files changed

Lines changed: 62 additions & 35 deletions

File tree

vision/google/cloud/vision/_gax.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,22 +28,41 @@ class _GAPICVisionAPI(object):
2828
"""
2929
def __init__(self, client=None):
3030
self._client = client
31-
self._api = image_annotator_client.ImageAnnotatorClient
31+
self._api = image_annotator_client.ImageAnnotatorClient()
3232

3333

3434
def _to_gapic_feature(feature):
35-
"""Helper function to convert a ``Feature`` to a gRPC ``Feature``."""
36-
feature_pb = image_annotator_pb2.Feature
37-
feature_type = getattr(feature_pb, feature.feature_type)
38-
return feature_pb(type=feature_type, max_results=feature.max_results)
35+
"""Helper function to convert a ``Feature`` to a gRPC ``Feature``.
36+
37+
:type feature: :class:`~google.cloud.vision.feature.Feature`
38+
:param feature: Local ``Feature`` class to be converted to gRPC ``Feature``
39+
instance.
40+
41+
:rtype: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.Feature`
42+
:returns: gRPC ``Feature`` converted from
43+
:class:`~google.cloud.vision.feature.Feature`.
44+
"""
45+
return image_annotator_pb2.Feature(
46+
type=getattr(image_annotator_pb2.Feature, feature.feature_type),
47+
max_results=feature.max_results)
3948

4049

4150
def _to_gapic_image(image):
42-
"""Helper function to convert an ``Image`` to a gRPC ``Image``."""
43-
image_pb = image_annotator_pb2.Image
51+
"""Helper function to convert an ``Image`` to a gRPC ``Image``.
52+
53+
:type image: :class:`~google.cloud.vision.image.Image`
54+
:param image: Local ``Image`` class to be converted to gRPC ``Image``.
55+
56+
:rtype: :class:`~google.cloud.grpc.vision.v1.image_annotator_pb2.Image`
57+
:returns: gRPC ``Image`` converted from
58+
:class:`~google.cloud.vision.image.Image`.
59+
"""
4460
if image.content is not None:
45-
return image_pb(content=_to_bytes(image.content))
61+
return image_annotator_pb2.Image(content=_to_bytes(image.content))
4662
if image.source is not None:
47-
image_source_pb = image_annotator_pb2.ImageSource
48-
image_source = image_source_pb(gcs_image_uri=image.source)
49-
return image_pb(source=image_source)
63+
return image_annotator_pb2.Image(
64+
source=image_annotator_pb2.ImageSource(
65+
gcs_image_uri=image.source
66+
),
67+
)
68+
raise ValueError('No image content or source found.')

vision/unit_tests/test__gax.py

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
import unittest
1616

17+
import mock
18+
19+
20+
def _make_credentials():
21+
import google.auth.credentials
22+
return mock.Mock(spec=google.auth.credentials.Credentials)
23+
1724

1825
class TestGAXClient(unittest.TestCase):
1926
def _get_target_class(self):
@@ -26,60 +33,61 @@ def _make_one(self, *args, **kwargs):
2633
def test_ctor(self):
2734
from google.cloud.gapic.vision.v1 import image_annotator_client
2835

29-
client = object()
36+
client = mock.Mock(credentials=_make_credentials())
3037
api = self._make_one(client)
31-
self.assertEqual(api._client, client)
32-
self.assertIsInstance(api._api(),
38+
self.assertIs(api._client, client)
39+
self.assertIsInstance(api._api,
3340
image_annotator_client.ImageAnnotatorClient)
3441

35-
def test__to_gapic_feature(self):
42+
43+
class TestToGAPICFeature(unittest.TestCase):
44+
def _call_fut(self, feature):
3645
from google.cloud.vision._gax import _to_gapic_feature
46+
return _to_gapic_feature(feature)
47+
48+
def test__to_gapic_feature(self):
3749
from google.cloud.vision.feature import Feature
3850
from google.cloud.vision.feature import FeatureTypes
3951
from google.cloud.grpc.vision.v1 import image_annotator_pb2
4052

4153
feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
42-
feature_pb = _to_gapic_feature(feature)
54+
feature_pb = self._call_fut(feature)
4355
self.assertIsInstance(feature_pb, image_annotator_pb2.Feature)
4456
self.assertEqual(feature_pb.type, 4)
4557
self.assertEqual(feature_pb.max_results, 5)
4658

59+
60+
class TestToGAPICImage(unittest.TestCase):
61+
def _call_fut(self, image):
62+
from google.cloud.vision._gax import _to_gapic_image
63+
return _to_gapic_image(image)
64+
4765
def test__to_gapic_image_content(self):
4866
import base64
49-
from google.cloud.vision._gax import _to_gapic_image
5067
from google.cloud.vision.image import Image
5168
from google.cloud.grpc.vision.v1 import image_annotator_pb2
5269

5370
image_content = b'abc 1 2 3'
5471
b64_content = base64.b64encode(image_content)
5572
client = object()
5673
image = Image(client, content=image_content)
57-
image_pb = _to_gapic_image(image)
74+
image_pb = self._call_fut(image)
5875
self.assertIsInstance(image_pb, image_annotator_pb2.Image)
5976
self.assertEqual(image_pb.content, b64_content)
6077

6178
def test__to_gapic_image_uri(self):
62-
from google.cloud.vision._gax import _to_gapic_image
6379
from google.cloud.vision.image import Image
6480
from google.cloud.grpc.vision.v1 import image_annotator_pb2
6581

6682
image_uri = b'gs://1234/34.jpg'
6783
client = object()
6884
image = Image(client, source_uri=image_uri)
69-
image_pb = _to_gapic_image(image)
85+
image_pb = self._call_fut(image)
7086
self.assertIsInstance(image_pb, image_annotator_pb2.Image)
7187
self.assertEqual(image_pb.source.gcs_image_uri, image_uri)
7288

73-
def test__to_gapic_image(self):
74-
from google.cloud.vision._gax import _to_gapic_image
75-
76-
class MockImage(object):
77-
@property
78-
def content(self):
79-
return None
80-
81-
@property
82-
def source(self):
83-
return None
84-
image = MockImage()
85-
self.assertIsNone(_to_gapic_image(image))
89+
def test__to_gapic_with_empty_image(self):
90+
image = mock.Mock(
91+
content=None, source=None, spec=['content', 'source'])
92+
with self.assertRaises(ValueError):
93+
self._call_fut(image)

vision/unit_tests/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_annotate_with_preset_api(self):
5555
client._vision_api.annotate()
5656
api.annotate.assert_called_once_with()
5757

58-
def test_annotate_with_gax(self):
58+
def test_make_gax_client(self):
5959
from google.cloud.vision._gax import _GAPICVisionAPI
6060

6161
credentials = _make_credentials()
@@ -64,7 +64,7 @@ def test_annotate_with_gax(self):
6464
client._connection = _Connection()
6565
self.assertIsInstance(client._vision_api, _GAPICVisionAPI)
6666

67-
def test_annotate_with_http(self):
67+
def test_make_http_client(self):
6868
from google.cloud.vision._http import _HTTPVisionAPI
6969

7070
credentials = _make_credentials()

0 commit comments

Comments
 (0)