1414
1515import 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
1825class 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 )
0 commit comments