From 0c66f9f6c2fd043c9bca611116eca4e8464af9db Mon Sep 17 00:00:00 2001 From: Thomas Schultz Date: Mon, 27 Feb 2017 22:34:00 -0500 Subject: [PATCH] Clean up Vision unit tests. --- vision/unit_tests/test__gax.py | 8 +- vision/unit_tests/test__http.py | 3 + vision/unit_tests/test_annotations.py | 2 + vision/unit_tests/test_batch.py | 1 + vision/unit_tests/test_client.py | 370 +++++++++++++------------- vision/unit_tests/test_color.py | 7 +- vision/unit_tests/test_crop_hint.py | 8 +- vision/unit_tests/test_entity.py | 13 +- vision/unit_tests/test_face.py | 72 +++-- vision/unit_tests/test_feature.py | 15 +- vision/unit_tests/test_geometry.py | 3 +- vision/unit_tests/test_image.py | 1 + vision/unit_tests/test_safe_search.py | 1 + 13 files changed, 265 insertions(+), 239 deletions(-) diff --git a/vision/unit_tests/test__gax.py b/vision/unit_tests/test__gax.py index 88877f2324ad..ee73cbdce0b2 100644 --- a/vision/unit_tests/test__gax.py +++ b/vision/unit_tests/test__gax.py @@ -19,12 +19,14 @@ def _make_credentials(): import google.auth.credentials + return mock.Mock(spec=google.auth.credentials.Credentials) class TestGAXClient(unittest.TestCase): def _get_target_class(self): from google.cloud.vision._gax import _GAPICVisionAPI + return _GAPICVisionAPI def _make_one(self, *args, **kwargs): @@ -40,6 +42,7 @@ def test_ctor(self): def test_gapic_credentials(self): from google.cloud.gapic.vision.v1.image_annotator_client import ( ImageAnnotatorClient) + from google.cloud.vision import Client # Mock the GAPIC ImageAnnotatorClient, whose arguments we @@ -214,8 +217,8 @@ def test_annotate_with_pb_requests_results(self): responses = gax_api.annotate(requests_pb=[request]) self.assertEqual(len(responses), 2) - self.assertIsInstance(responses[0], Annotations) - self.assertIsInstance(responses[1], Annotations) + for annotation in responses: + self.assertIsInstance(annotation, Annotations) gax_api._annotator_client.batch_annotate_images.assert_called() @@ -239,6 +242,7 @@ def test__to_gapic_feature(self): class Test__to_gapic_image(unittest.TestCase): def _call_fut(self, image): from google.cloud.vision._gax import _to_gapic_image + return _to_gapic_image(image) def test__to_gapic_image_content(self): diff --git a/vision/unit_tests/test__http.py b/vision/unit_tests/test__http.py index eae6a6506147..cfecc6d98b29 100644 --- a/vision/unit_tests/test__http.py +++ b/vision/unit_tests/test__http.py @@ -28,6 +28,7 @@ class TestConnection(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision._http import Connection + return Connection def _make_one(self, *args, **kw): @@ -72,6 +73,7 @@ def test_extra_headers(self): class Test_HTTPVisionAPI(unittest.TestCase): def _get_target_class(self): from google.cloud.vision._http import _HTTPVisionAPI + return _HTTPVisionAPI def _make_one(self, *args, **kwargs): @@ -172,6 +174,7 @@ class TestVisionRequest(unittest.TestCase): @staticmethod def _get_target_function(): from google.cloud.vision._http import _make_request + return _make_request def _call_fut(self, *args, **kw): diff --git a/vision/unit_tests/test_annotations.py b/vision/unit_tests/test_annotations.py index 56be200bfd33..89d03def13a5 100644 --- a/vision/unit_tests/test_annotations.py +++ b/vision/unit_tests/test_annotations.py @@ -128,6 +128,7 @@ def test_it(self): class Test__make_faces_from_pb(unittest.TestCase): def _call_fut(self, annotations): from google.cloud.vision.annotations import _make_faces_from_pb + return _make_faces_from_pb(annotations) def test_it(self): @@ -144,6 +145,7 @@ class Test__make_image_properties_from_pb(unittest.TestCase): def _call_fut(self, annotations): from google.cloud.vision.annotations import ( _make_image_properties_from_pb) + return _make_image_properties_from_pb(annotations) def test_it(self): diff --git a/vision/unit_tests/test_batch.py b/vision/unit_tests/test_batch.py index c9c59c1baf45..a1acaf4efe33 100644 --- a/vision/unit_tests/test_batch.py +++ b/vision/unit_tests/test_batch.py @@ -21,6 +21,7 @@ def _make_credentials(): import google.auth.credentials + return mock.Mock(spec=google.auth.credentials.Credentials) diff --git a/vision/unit_tests/test_client.py b/vision/unit_tests/test_client.py index 115758329b7a..7cd7fb21e229 100644 --- a/vision/unit_tests/test_client.py +++ b/vision/unit_tests/test_client.py @@ -26,6 +26,7 @@ def _make_credentials(): import google.auth.credentials + return mock.Mock(spec=google.auth.credentials.Credentials) @@ -61,8 +62,8 @@ def test_make_gax_client(self): from google.cloud.vision._gax import _GAPICVisionAPI credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=None) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=None) vision_api = client._vision_api vision_api._connection = _Connection() with mock.patch('google.cloud.vision.client._GAPICVisionAPI', @@ -73,8 +74,8 @@ def test_make_http_client(self): from google.cloud.vision._http import _HTTPVisionAPI credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) self.assertIsInstance(client._vision_api, _HTTPVisionAPI) def test_face_annotation(self): @@ -82,31 +83,32 @@ def test_face_annotation(self): from google.cloud.vision.feature import Feature, FeatureTypes from unit_tests._fixtures import FACE_DETECTION_RESPONSE - RETURNED = FACE_DETECTION_RESPONSE - REQUEST = { + returned = FACE_DETECTION_RESPONSE + request = { "requests": [ { "image": { - "content": B64_IMAGE_CONTENT + "content": B64_IMAGE_CONTENT, }, "features": [ { "maxResults": 3, - "type": "FACE_DETECTION" - } - ] - } - ] + "type": "FACE_DETECTION", + }, + ], + }, + ], } credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(returned) vision_api._connection = connection - features = [Feature(feature_type=FeatureTypes.FACE_DETECTION, - max_results=3)] + features = [ + Feature(feature_type=FeatureTypes.FACE_DETECTION, max_results=3), + ] image = client.image(content=IMAGE_CONTENT) images = ((image, features),) api_response = client._vision_api.annotate(images) @@ -114,15 +116,14 @@ def test_face_annotation(self): self.assertEqual(len(api_response), 1) response = api_response[0] self.assertEqual( - REQUEST, connection._requested[0]['data']) + request, connection._requested[0]['data']) self.assertIsInstance(response, Annotations) def test_image_with_client_gcs_source(self): from google.cloud.vision.image import Image credentials = _make_credentials() - client = self._make_one(project=PROJECT, - credentials=credentials) + client = self._make_one(project=PROJECT, credentials=credentials) gcs_image = client.image(source_uri=IMAGE_SOURCE) self.assertIsInstance(gcs_image, Image) self.assertEqual(gcs_image.source, IMAGE_SOURCE) @@ -131,8 +132,8 @@ def test_image_with_client_raw_content(self): from google.cloud.vision.image import Image credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) raw_image = client.image(content=IMAGE_CONTENT) self.assertIsInstance(raw_image, Image) self.assertEqual(raw_image.content, IMAGE_CONTENT) @@ -143,8 +144,8 @@ def test_image_with_client_filename(self): from google.cloud.vision.image import Image credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) with patch('google.cloud.vision.image.open', mock_open(read_data=IMAGE_CONTENT)) as m: file_image = client.image(filename='my_image.jpg') @@ -214,85 +215,88 @@ def test_multiple_detection_from_content(self): def test_detect_crop_hints_from_source(self): from google.cloud.vision.crop_hint import CropHint from unit_tests._fixtures import CROP_HINTS_RESPONSE - returned = CROP_HINTS_RESPONSE + credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) api = client._vision_api - api._connection = _Connection(returned) + api._connection = _Connection(CROP_HINTS_RESPONSE) image = client.image(source_uri=IMAGE_SOURCE) crop_hints = image.detect_crop_hints(aspect_ratios=[1.3333], limit=3) self.assertEqual(len(crop_hints), 2) self.assertIsInstance(crop_hints[0], CropHint) image_request = api._connection._requested[0]['data']['requests'][0] - self.assertEqual(image_request['image']['source']['gcsImageUri'], - IMAGE_SOURCE) - ar = image_request['imageContext']['cropHintsParams']['aspectRatios'] - self.assertAlmostEqual(ar[0], 1.3333, 4) - self.assertEqual(3, image_request['features'][0]['maxResults']) + self.assertEqual( + image_request['image']['source']['gcsImageUri'], IMAGE_SOURCE) + + crop_hints = image_request['imageContext']['cropHintsParams'] + ratios = crop_hints['aspectRatios'] + self.assertAlmostEqual(ratios[0], 1.3333, 4) + self.assertEqual(image_request['features'][0]['maxResults'], 3) def test_face_detection_from_source(self): from google.cloud.vision.face import Face from unit_tests._fixtures import FACE_DETECTION_RESPONSE - RETURNED = FACE_DETECTION_RESPONSE + credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(FACE_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(source_uri=IMAGE_SOURCE) faces = image.detect_faces(limit=3) - self.assertEqual(5, len(faces)) - self.assertIsInstance(faces[0], Face) + self.assertEqual(len(faces), 5) + for face in faces: + self.assertIsInstance(face, Face) + image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(IMAGE_SOURCE, - image_request['image']['source']['gcs_image_uri']) - self.assertEqual(3, image_request['features'][0]['maxResults']) + self.assertEqual( + IMAGE_SOURCE, image_request['image']['source']['gcs_image_uri']) + self.assertEqual(image_request['features'][0]['maxResults'], 3) def test_face_detection_from_content(self): from google.cloud.vision.face import Face from unit_tests._fixtures import FACE_DETECTION_RESPONSE - RETURNED = FACE_DETECTION_RESPONSE + credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(FACE_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(content=IMAGE_CONTENT) faces = image.detect_faces(limit=5) - self.assertEqual(5, len(faces)) - self.assertIsInstance(faces[0], Face) - image_request = connection._requested[0]['data']['requests'][0] + self.assertEqual(len(faces), 5) + for face in faces: + self.assertIsInstance(face, Face) - self.assertEqual(B64_IMAGE_CONTENT, - image_request['image']['content']) - self.assertEqual(5, image_request['features'][0]['maxResults']) + image_request = connection._requested[0]['data']['requests'][0] + self.assertEqual(B64_IMAGE_CONTENT, image_request['image']['content']) + self.assertEqual(image_request['features'][0]['maxResults'], 5) def test_face_detection_from_content_no_results(self): - RETURNED = { + returned = { 'responses': [{}] } credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(returned) vision_api._connection = connection image = client.image(content=IMAGE_CONTENT) faces = image.detect_faces(limit=5) self.assertEqual(faces, ()) self.assertEqual(len(faces), 0) - image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(B64_IMAGE_CONTENT, - image_request['image']['content']) - self.assertEqual(5, image_request['features'][0]['maxResults']) + image_request = connection._requested[0]['data']['requests'][0] + self.assertEqual(B64_IMAGE_CONTENT, image_request['image']['content']) + self.assertEqual(image_request['features'][0]['maxResults'], 5) def test_detect_full_text_annotation(self): from google.cloud.vision.text import TextAnnotation @@ -300,8 +304,8 @@ def test_detect_full_text_annotation(self): returned = FULL_TEXT_RESPONSE credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) api = client._vision_api api._connection = _Connection(returned) image = client.image(source_uri=IMAGE_SOURCE) @@ -319,46 +323,46 @@ def test_detect_full_text_annotation(self): self.assertEqual(len(page.blocks[0].paragraphs[0].words), 1) image_request = api._connection._requested[0]['data']['requests'][0] - self.assertEqual(image_request['image']['source']['gcs_image_uri'], - IMAGE_SOURCE) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) self.assertEqual(image_request['features'][0]['maxResults'], 2) - self.assertEqual(image_request['features'][0]['type'], - 'DOCUMENT_TEXT_DETECTION') + self.assertEqual( + image_request['features'][0]['type'], 'DOCUMENT_TEXT_DETECTION') def test_label_detection_from_source(self): from google.cloud.vision.entity import EntityAnnotation - from unit_tests._fixtures import ( - LABEL_DETECTION_RESPONSE as RETURNED) + from unit_tests._fixtures import LABEL_DETECTION_RESPONSE credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(LABEL_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(source_uri=IMAGE_SOURCE) labels = image.detect_labels(limit=3) - self.assertEqual(3, len(labels)) - self.assertIsInstance(labels[0], EntityAnnotation) + self.assertEqual(len(labels), 3) + for label in labels: + self.assertIsInstance(label, EntityAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(IMAGE_SOURCE, - image_request['image']['source']['gcs_image_uri']) - self.assertEqual(3, image_request['features'][0]['maxResults']) - self.assertEqual('automobile', labels[0].description) - self.assertEqual('vehicle', labels[1].description) - self.assertEqual('/m/0k4j', labels[0].mid) - self.assertEqual('/m/07yv9', labels[1].mid) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) + self.assertEqual(image_request['features'][0]['maxResults'], 3) + self.assertEqual(labels[0].description, 'automobile') + self.assertEqual(labels[1].description, 'vehicle') + self.assertEqual(labels[0].mid, '/m/0k4j') + self.assertEqual(labels[1].mid, '/m/07yv9') def test_label_detection_no_results(self): - RETURNED = { + returned = { 'responses': [{}] } credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - vision_api._connection = _Connection(RETURNED) + vision_api._connection = _Connection(returned) image = client.image(content=IMAGE_CONTENT) labels = image.detect_labels() @@ -367,59 +371,59 @@ def test_label_detection_no_results(self): def test_landmark_detection_from_source(self): from google.cloud.vision.entity import EntityAnnotation - from unit_tests._fixtures import ( - LANDMARK_DETECTION_RESPONSE as RETURNED) + from unit_tests._fixtures import LANDMARK_DETECTION_RESPONSE credentials = _make_credentials() client = self._make_one(project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(LANDMARK_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(source_uri=IMAGE_SOURCE) landmarks = image.detect_landmarks(limit=3) - self.assertEqual(2, len(landmarks)) - self.assertIsInstance(landmarks[0], EntityAnnotation) + self.assertEqual(len(landmarks), 2) + + for landmark in landmarks: + self.assertIsInstance(landmark, EntityAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(IMAGE_SOURCE, - image_request['image']['source']['gcs_image_uri']) - self.assertEqual(3, image_request['features'][0]['maxResults']) - self.assertEqual(48.861013, landmarks[0].locations[0].latitude) - self.assertEqual(2.335818, landmarks[0].locations[0].longitude) - self.assertEqual('/m/04gdr', landmarks[0].mid) - self.assertEqual('/m/094llg', landmarks[1].mid) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) + self.assertEqual(image_request['features'][0]['maxResults'], 3) + self.assertEqual(landmarks[0].locations[0].latitude, 48.861013) + self.assertEqual(landmarks[0].locations[0].longitude, 2.335818) + self.assertEqual(landmarks[0].mid, '/m/04gdr') + self.assertEqual(landmarks[1].mid, '/m/094llg') def test_landmark_detection_from_content(self): from google.cloud.vision.entity import EntityAnnotation - from unit_tests._fixtures import ( - LANDMARK_DETECTION_RESPONSE as RETURNED) + from unit_tests._fixtures import LANDMARK_DETECTION_RESPONSE credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(LANDMARK_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(content=IMAGE_CONTENT) landmarks = image.detect_landmarks(limit=5) - self.assertEqual(2, len(landmarks)) - self.assertIsInstance(landmarks[0], EntityAnnotation) + self.assertEqual(len(landmarks), 2) + for landmark in landmarks: + self.assertIsInstance(landmark, EntityAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(B64_IMAGE_CONTENT, - image_request['image']['content']) - self.assertEqual(5, image_request['features'][0]['maxResults']) + self.assertEqual(image_request['image']['content'], B64_IMAGE_CONTENT) + self.assertEqual(image_request['features'][0]['maxResults'], 5) def test_landmark_detection_no_results(self): - RETURNED = { + returned = { 'responses': [{}] } credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - vision_api._connection = _Connection(RETURNED) + vision_api._connection = _Connection(returned) image = client.image(content=IMAGE_CONTENT) landmarks = image.detect_landmarks() @@ -429,53 +433,53 @@ def test_landmark_detection_no_results(self): def test_logo_detection_from_source(self): from google.cloud.vision.entity import EntityAnnotation from unit_tests._fixtures import LOGO_DETECTION_RESPONSE - RETURNED = LOGO_DETECTION_RESPONSE + credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(LOGO_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(source_uri=IMAGE_SOURCE) logos = image.detect_logos(limit=3) - self.assertEqual(2, len(logos)) - self.assertIsInstance(logos[0], EntityAnnotation) + self.assertEqual(len(logos), 2) + for logo in logos: + self.assertIsInstance(logo, EntityAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(IMAGE_SOURCE, - image_request['image']['source']['gcs_image_uri']) - self.assertEqual(3, image_request['features'][0]['maxResults']) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) + self.assertEqual(image_request['features'][0]['maxResults'], 3) def test_logo_detection_from_content(self): from google.cloud.vision.entity import EntityAnnotation from unit_tests._fixtures import LOGO_DETECTION_RESPONSE - RETURNED = LOGO_DETECTION_RESPONSE + credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(LOGO_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(content=IMAGE_CONTENT) logos = image.detect_logos(limit=5) - self.assertEqual(2, len(logos)) - self.assertIsInstance(logos[0], EntityAnnotation) + self.assertEqual(len(logos), 2) + for logo in logos: + self.assertIsInstance(logo, EntityAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(B64_IMAGE_CONTENT, - image_request['image']['content']) - self.assertEqual(5, image_request['features'][0]['maxResults']) + self.assertEqual(image_request['image']['content'], B64_IMAGE_CONTENT) + self.assertEqual(image_request['features'][0]['maxResults'], 5) def test_text_detection_from_source(self): from google.cloud.vision.entity import EntityAnnotation - from unit_tests._fixtures import ( - TEXT_DETECTION_RESPONSE as RETURNED) + from unit_tests._fixtures import TEXT_DETECTION_RESPONSE credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(TEXT_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(source_uri=IMAGE_SOURCE) @@ -483,33 +487,33 @@ def test_text_detection_from_source(self): self.assertEqual(3, len(text)) self.assertIsInstance(text[0], EntityAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(IMAGE_SOURCE, - image_request['image']['source']['gcs_image_uri']) - self.assertEqual(3, image_request['features'][0]['maxResults']) - self.assertEqual('en', text[0].locale) - self.assertEqual('Google CloudPlatform\n', text[0].description) - self.assertEqual('Google', text[1].description) - self.assertEqual(694, text[0].bounds.vertices[0].y_coordinate) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) + self.assertEqual(image_request['features'][0]['maxResults'], 3) + self.assertEqual(text[0].locale, 'en') + self.assertEqual(text[0].description, 'Google CloudPlatform\n') + self.assertEqual(text[1].description, 'Google') + self.assertEqual(text[0].bounds.vertices[0].x_coordinate, 129) + self.assertEqual(text[0].bounds.vertices[0].y_coordinate, 694) def test_safe_search_detection_from_source(self): from google.cloud.vision.likelihood import Likelihood from google.cloud.vision.safe_search import SafeSearchAnnotation from unit_tests._fixtures import SAFE_SEARCH_DETECTION_RESPONSE - RETURNED = SAFE_SEARCH_DETECTION_RESPONSE credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(SAFE_SEARCH_DETECTION_RESPONSE) vision_api._connection = connection image = client.image(source_uri=IMAGE_SOURCE) safe_search = image.detect_safe_search() self.assertIsInstance(safe_search, SafeSearchAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(IMAGE_SOURCE, - image_request['image']['source']['gcs_image_uri']) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) self.assertIs(safe_search.adult, Likelihood.VERY_UNLIKELY) self.assertIs(safe_search.spoof, Likelihood.UNLIKELY) @@ -517,14 +521,14 @@ def test_safe_search_detection_from_source(self): self.assertIs(safe_search.violence, Likelihood.VERY_UNLIKELY) def test_safe_search_no_results(self): - RETURNED = { + returned = { 'responses': [{}] } credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - vision_api._connection = _Connection(RETURNED) + vision_api._connection = _Connection(returned) image = client.image(content=IMAGE_CONTENT) safe_search = image.detect_safe_search() @@ -535,37 +539,36 @@ def test_image_properties_detection_from_source(self): from google.cloud.vision.color import ImagePropertiesAnnotation from unit_tests._fixtures import IMAGE_PROPERTIES_RESPONSE - RETURNED = IMAGE_PROPERTIES_RESPONSE credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - connection = _Connection(RETURNED) + connection = _Connection(IMAGE_PROPERTIES_RESPONSE) vision_api._connection = connection image = client.image(source_uri=IMAGE_SOURCE) image_properties = image.detect_properties() self.assertIsInstance(image_properties, ImagePropertiesAnnotation) image_request = connection._requested[0]['data']['requests'][0] - self.assertEqual(IMAGE_SOURCE, - image_request['image']['source']['gcs_image_uri']) - self.assertEqual(0.42258179, image_properties.colors[0].score) - self.assertEqual(0.025376344, - image_properties.colors[0].pixel_fraction) - self.assertEqual(253, image_properties.colors[0].color.red) - self.assertEqual(203, image_properties.colors[0].color.green) - self.assertEqual(65, image_properties.colors[0].color.blue) - self.assertEqual(0.0, image_properties.colors[0].color.alpha) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) + self.assertEqual(image_properties.colors[0].score, 0.42258179) + self.assertEqual( + image_properties.colors[0].pixel_fraction, 0.025376344) + self.assertEqual(image_properties.colors[0].color.red, 253) + self.assertEqual(image_properties.colors[0].color.green, 203) + self.assertEqual(image_properties.colors[0].color.blue, 65) + self.assertEqual(image_properties.colors[0].color.alpha, 0.0) def test_image_properties_no_results(self): - RETURNED = { + returned = { 'responses': [{}] } credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) vision_api = client._vision_api - vision_api._connection = _Connection(RETURNED) + vision_api._connection = _Connection(returned) image = client.image(content=IMAGE_CONTENT) image_properties = image.detect_properties() @@ -578,12 +581,11 @@ def test_detect_web_detection(self): from google.cloud.vision.web import WebPage from unit_tests._fixtures import WEB_DETECTION_RESPONSE - returned = WEB_DETECTION_RESPONSE credentials = _make_credentials() - client = self._make_one(project=PROJECT, credentials=credentials, - use_gax=False) + client = self._make_one( + project=PROJECT, credentials=credentials, use_gax=False) api = client._vision_api - api._connection = _Connection(returned) + api._connection = _Connection(WEB_DETECTION_RESPONSE) image = client.image(source_uri=IMAGE_SOURCE) web_images = image.detect_web(limit=2) @@ -592,19 +594,24 @@ def test_detect_web_detection(self): self.assertEqual(len(web_images.web_entities), 2) self.assertEqual(len(web_images.pages_with_matching_images), 2) - self.assertIsInstance(web_images.partial_matching_images[0], - WebImage) - self.assertIsInstance(web_images.full_matching_images[0], WebImage) - self.assertIsInstance(web_images.web_entities[0], WebEntity) - self.assertIsInstance(web_images.pages_with_matching_images[0], - WebPage) + for partial_match in web_images.partial_matching_images: + self.assertIsInstance(partial_match, WebImage) + + for full_match in web_images.full_matching_images: + self.assertIsInstance(full_match, WebImage) + + for web_entity in web_images.web_entities: + self.assertIsInstance(web_entity, WebEntity) + + for page in web_images.pages_with_matching_images: + self.assertIsInstance(page, WebPage) image_request = api._connection._requested[0]['data']['requests'][0] - self.assertEqual(image_request['image']['source']['gcs_image_uri'], - IMAGE_SOURCE) + self.assertEqual( + image_request['image']['source']['gcs_image_uri'], IMAGE_SOURCE) self.assertEqual(image_request['features'][0]['maxResults'], 2) - self.assertEqual(image_request['features'][0]['type'], - 'WEB_DETECTION') + self.assertEqual( + image_request['features'][0]['type'], 'WEB_DETECTION') class _Connection(object): @@ -615,6 +622,7 @@ def __init__(self, *responses): def api_request(self, **kw): import json + json.dumps(kw.get('data', '')) # Simulate JSON encoding. self._requested.append(kw) response, self._responses = self._responses[0], self._responses[1:] diff --git a/vision/unit_tests/test_color.py b/vision/unit_tests/test_color.py index 6421fd5d3423..17541524dac9 100644 --- a/vision/unit_tests/test_color.py +++ b/vision/unit_tests/test_color.py @@ -19,6 +19,7 @@ class TestColor(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.color import Color + return Color def test_rgb_color_data(self): @@ -92,6 +93,7 @@ class TestImagePropertiesAnnotation(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.color import ImagePropertiesAnnotation + return ImagePropertiesAnnotation def test_image_properties_annotation_from_pb(self): @@ -101,9 +103,8 @@ def test_image_properties_annotation_from_pb(self): alpha = FloatValue(value=1.0) color_pb = Color(red=1.0, green=2.0, blue=3.0, alpha=alpha) - color_info_pb = image_annotator_pb2.ColorInfo(color=color_pb, - score=1.0, - pixel_fraction=1.0) + color_info_pb = image_annotator_pb2.ColorInfo( + color=color_pb, score=1.0, pixel_fraction=1.0) dominant_colors = image_annotator_pb2.DominantColorsAnnotation( colors=[color_info_pb]) diff --git a/vision/unit_tests/test_crop_hint.py b/vision/unit_tests/test_crop_hint.py index 21367d001564..c9449617b012 100644 --- a/vision/unit_tests/test_crop_hint.py +++ b/vision/unit_tests/test_crop_hint.py @@ -19,11 +19,12 @@ class TestCropHint(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.crop_hint import CropHint + return CropHint def test_crop_hint_annotation(self): - from google.cloud.vision.geometry import Bounds from unit_tests._fixtures import CROP_HINTS_RESPONSE + from google.cloud.vision.geometry import Bounds response = CROP_HINTS_RESPONSE['responses'][0]['cropHintsAnnotation'] crop_hints_dict = response['cropHints'][0] @@ -47,7 +48,8 @@ def test_crop_hint_annotation_pb(self): crop_hint = crop_hints_class.from_pb(crop_hint_pb) self.assertEqual(len(crop_hint.bounds.vertices), 1) - self.assertEqual(crop_hint.bounds.vertices[0].x_coordinate, 1) - self.assertEqual(crop_hint.bounds.vertices[0].y_coordinate, 2) + vertex = crop_hint.bounds.vertices[0] + self.assertEqual(vertex.x_coordinate, 1) + self.assertEqual(vertex.y_coordinate, 2) self.assertAlmostEqual(crop_hint.confidence, 1.23, 4) self.assertAlmostEqual(crop_hint.importance_fraction, 4.56, 4) diff --git a/vision/unit_tests/test_entity.py b/vision/unit_tests/test_entity.py index b812bb0b0682..bca67270226b 100644 --- a/vision/unit_tests/test_entity.py +++ b/vision/unit_tests/test_entity.py @@ -24,14 +24,15 @@ def _get_target_class(): def test_logo_annotation(self): from unit_tests._fixtures import LOGO_DETECTION_RESPONSE - LOGO = LOGO_DETECTION_RESPONSE['responses'][0]['logoAnnotations'][0] entity_class = self._get_target_class() - logo = entity_class.from_api_repr(LOGO) + logo = entity_class.from_api_repr( + LOGO_DETECTION_RESPONSE['responses'][0]['logoAnnotations'][0]) - self.assertEqual('/m/05b5c', logo.mid) - self.assertEqual('Brand1', logo.description) - self.assertEqual(0.63192177, logo.score) - self.assertEqual(162, logo.bounds.vertices[0].y_coordinate) + self.assertEqual(logo.mid, '/m/05b5c') + self.assertEqual(logo.description, 'Brand1') + self.assertEqual(logo.score, 0.63192177) + self.assertEqual(logo.bounds.vertices[0].x_coordinate, 78) + self.assertEqual(logo.bounds.vertices[0].y_coordinate, 162) def test_logo_pb_annotation(self): from google.cloud.proto.vision.v1 import image_annotator_pb2 diff --git a/vision/unit_tests/test_face.py b/vision/unit_tests/test_face.py index 55814e0cad15..6f7864e5f750 100644 --- a/vision/unit_tests/test_face.py +++ b/vision/unit_tests/test_face.py @@ -19,6 +19,7 @@ class TestFace(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.face import Face + return Face def _make_face_pb(self, *args, **kwargs): @@ -28,10 +29,11 @@ def _make_face_pb(self, *args, **kwargs): def setUp(self): from unit_tests._fixtures import FACE_DETECTION_RESPONSE - self.FACE_ANNOTATIONS = FACE_DETECTION_RESPONSE['responses'][0] + + self.face_annotations = FACE_DETECTION_RESPONSE['responses'][0] self.face_class = self._get_target_class() self.face = self.face_class.from_api_repr( - self.FACE_ANNOTATIONS['faceAnnotations'][0]) + self.face_annotations['faceAnnotations'][0]) def test_face_from_pb(self): from google.cloud.proto.vision.v1 import image_annotator_pb2 @@ -48,49 +50,45 @@ def test_face_from_pb(self): def test_face_landmarks(self): from google.cloud.vision.face import LandmarkTypes - self.assertEqual(0.54453093, self.face.landmarking_confidence) - self.assertEqual(0.9863683, self.face.detection_confidence) + self.assertEqual(self.face.landmarking_confidence, 0.54453093) + self.assertEqual(self.face.detection_confidence, 0.9863683) self.assertTrue(hasattr(self.face.landmarks, 'left_eye')) - self.assertEqual(1004.8003, - self.face.landmarks.left_eye.position.x_coordinate) - self.assertEqual(482.69385, - self.face.landmarks.left_eye.position.y_coordinate) - self.assertEqual(0.0016593217, - self.face.landmarks.left_eye.position.z_coordinate) - self.assertEqual(self.face.landmarks.left_eye.landmark_type, - LandmarkTypes.LEFT_EYE) + left_eye = self.face.landmarks.left_eye + self.assertEqual(left_eye.position.x_coordinate, 1004.8003) + self.assertEqual(left_eye.position.y_coordinate, 482.69385) + self.assertEqual(left_eye.position.z_coordinate, 0.0016593217) + self.assertEqual(left_eye.landmark_type, LandmarkTypes.LEFT_EYE) def test_facial_emotions(self): from google.cloud.vision.face import Likelihood - self.assertEqual(Likelihood.VERY_LIKELY, - self.face.joy) - self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.sorrow) - self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.surprise) - self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.anger) - - def test_faciale_angles(self): - self.assertEqual(-0.43419784, self.face.angles.roll) - self.assertEqual(6.027647, self.face.angles.pan) - self.assertEqual(-18.412321, self.face.angles.tilt) + + self.assertEqual(self.face.joy, Likelihood.VERY_LIKELY) + self.assertEqual(self.face.sorrow, Likelihood.VERY_UNLIKELY) + self.assertEqual(self.face.surprise, Likelihood.VERY_UNLIKELY) + self.assertEqual(self.face.anger, Likelihood.VERY_UNLIKELY) + + def test_facial_angles(self): + self.assertEqual(self.face.angles.roll, -0.43419784) + self.assertEqual(self.face.angles.pan, 6.027647) + self.assertEqual(self.face.angles.tilt, -18.412321) def test_face_headware_and_blur_and_underexposed(self): from google.cloud.vision.face import Likelihood - self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.image_properties.blurred) - self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.headwear) - self.assertEqual(Likelihood.VERY_UNLIKELY, - self.face.image_properties.underexposed) + + very_unlikely = Likelihood.VERY_UNLIKELY + image_properties = self.face.image_properties + self.assertEqual(image_properties.blurred, very_unlikely) + self.assertEqual(image_properties.underexposed, very_unlikely) + self.assertEqual(self.face.headwear, Likelihood.VERY_UNLIKELY) def test_face_bounds(self): - self.assertEqual(4, len(self.face.bounds.vertices)) - self.assertEqual(748, self.face.bounds.vertices[0].x_coordinate) - self.assertEqual(58, self.face.bounds.vertices[0].y_coordinate) + self.assertEqual(len(self.face.bounds.vertices), 4) + vertex = self.face.bounds.vertices[0] + self.assertEqual(vertex.x_coordinate, 748) + self.assertEqual(vertex.y_coordinate, 58) def test_facial_skin_bounds(self): - self.assertEqual(4, len(self.face.fd_bounds.vertices)) - self.assertEqual(845, self.face.fd_bounds.vertices[0].x_coordinate) - self.assertEqual(310, self.face.fd_bounds.vertices[0].y_coordinate) + self.assertEqual(len(self.face.fd_bounds.vertices), 4) + vertex = self.face.bounds.vertices[1] + self.assertEqual(vertex.x_coordinate, 1430) + self.assertEqual(vertex.y_coordinate, 58) diff --git a/vision/unit_tests/test_feature.py b/vision/unit_tests/test_feature.py index 736f2519fe1b..322b5c6f52ac 100644 --- a/vision/unit_tests/test_feature.py +++ b/vision/unit_tests/test_feature.py @@ -19,6 +19,7 @@ class TestFeature(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.feature import Feature + return Feature def _make_one(self, *args, **kw): @@ -26,22 +27,24 @@ def _make_one(self, *args, **kw): def test_construct_feature(self): from google.cloud.vision.feature import FeatureTypes + feature = self._make_one(FeatureTypes.LABEL_DETECTION) - self.assertEqual(1, feature.max_results) - self.assertEqual('LABEL_DETECTION', feature.feature_type) + self.assertEqual(feature.max_results, 1) + self.assertEqual(feature.feature_type, 'LABEL_DETECTION') feature = self._make_one(FeatureTypes.FACE_DETECTION, 3) - self.assertEqual(3, feature.max_results) - self.assertEqual('FACE_DETECTION', feature.feature_type) + self.assertEqual(feature.max_results, 3) + self.assertEqual(feature.feature_type, 'FACE_DETECTION') def test_feature_as_dict(self): from google.cloud.vision.feature import FeatureTypes + feature = self._make_one(FeatureTypes.FACE_DETECTION, max_results=5) - EXPECTED = { + expected = { 'type': 'FACE_DETECTION', 'maxResults': 5 } - self.assertEqual(EXPECTED, feature.as_dict()) + self.assertEqual(feature.as_dict(), expected) def test_bad_feature_type(self): with self.assertRaises(AttributeError): diff --git a/vision/unit_tests/test_geometry.py b/vision/unit_tests/test_geometry.py index 07964b7988a1..6b4e8c50e0d3 100644 --- a/vision/unit_tests/test_geometry.py +++ b/vision/unit_tests/test_geometry.py @@ -15,10 +15,11 @@ import unittest -class TestVertext(unittest.TestCase): +class TestVertex(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.geometry import Vertex + return Vertex def _make_one(self, x_coordinate, y_coordinate): diff --git a/vision/unit_tests/test_image.py b/vision/unit_tests/test_image.py index 5b8f3e74e166..2435bc39ac4b 100644 --- a/vision/unit_tests/test_image.py +++ b/vision/unit_tests/test_image.py @@ -28,6 +28,7 @@ class TestVisionImage(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.image import Image + return Image def _make_one(self, *args, **kw): diff --git a/vision/unit_tests/test_safe_search.py b/vision/unit_tests/test_safe_search.py index b2ee7593e87b..a1d4e49ce062 100644 --- a/vision/unit_tests/test_safe_search.py +++ b/vision/unit_tests/test_safe_search.py @@ -19,6 +19,7 @@ class TestSafeSearchAnnotation(unittest.TestCase): @staticmethod def _get_target_class(): from google.cloud.vision.safe_search import SafeSearchAnnotation + return SafeSearchAnnotation def test_safe_search_annotation(self):