def test_batch_annotate_images(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response
        )

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = vision_v1.ImageAnnotatorClient()

        # Setup Request
        requests = []

        response = client.batch_annotate_images(requests)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests
        )
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_annotate_multiple_results(self):
        from google.cloud.vision_v1.proto import image_annotator_pb2
        from google.cloud.vision.annotations import Annotations
        from google.cloud.vision.feature import Feature
        from google.cloud.vision.feature import FeatureTypes
        from google.cloud.vision.image import Image

        client = mock.Mock(spec_set=['_credentials'])
        feature = Feature(FeatureTypes.LABEL_DETECTION, 5)
        image_content = b'abc 1 2 3'
        image = Image(client, content=image_content)
        with mock.patch('google.cloud.vision._gax.image_annotator_client.'
                        'ImageAnnotatorClient'):
            gax_api = self._make_one(client)

        responses = [
            image_annotator_pb2.AnnotateImageResponse(),
            image_annotator_pb2.AnnotateImageResponse(),
        ]
        response = image_annotator_pb2.BatchAnnotateImagesResponse(
            responses=responses)

        gax_api._annotator_client = mock.Mock(
            spec_set=['batch_annotate_images'])
        gax_api._annotator_client.batch_annotate_images.return_value = response
        images = ((image, [feature]), )
        responses = gax_api.annotate(images)

        self.assertEqual(len(responses), 2)
        self.assertIsInstance(responses[0], Annotations)
        self.assertIsInstance(responses[1], Annotations)
        gax_api._annotator_client.batch_annotate_images.assert_called()
    def test_batch_annotate_images(self, mock_create_stub):
        # Mock gRPC layer
        grpc_stub = mock.Mock()
        mock_create_stub.return_value = grpc_stub

        client = vision_v1.ImageAnnotatorClient()

        # Mock request
        requests = []

        # Mock response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response)
        grpc_stub.BatchAnnotateImages.return_value = expected_response

        response = client.batch_annotate_images(requests)
        self.assertEqual(expected_response, response)

        grpc_stub.BatchAnnotateImages.assert_called_once()
        args, kwargs = grpc_stub.BatchAnnotateImages.call_args
        self.assertEqual(len(args), 2)
        self.assertEqual(len(kwargs), 1)
        self.assertIn('metadata', kwargs)
        actual_request = args[0]

        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        self.assertEqual(expected_request, actual_request)
    def test_batch_annotate_images(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = image_annotator_pb2.BatchAnnotateImagesResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        client = vision_v1.ImageAnnotatorClient(channel=channel)

        # Setup Request
        requests = []

        response = client.batch_annotate_images(requests)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = image_annotator_pb2.BatchAnnotateImagesRequest(
            requests=requests)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
    def test_annotate_with_pb_requests_results(self):
        from google.cloud.vision_v1.proto import image_annotator_pb2
        from google.cloud.vision.annotations import Annotations

        client = mock.Mock(spec_set=['_credentials'])

        feature_type = image_annotator_pb2.Feature.CROP_HINTS
        feature = image_annotator_pb2.Feature(type=feature_type, max_results=2)

        image_content = b'abc 1 2 3'
        image = image_annotator_pb2.Image(content=image_content)

        aspect_ratios = [1.3333, 1.7777]
        crop_hints_params = image_annotator_pb2.CropHintsParams(
            aspect_ratios=aspect_ratios)
        image_context = image_annotator_pb2.ImageContext(
            crop_hints_params=crop_hints_params)
        request = image_annotator_pb2.AnnotateImageRequest(
            image=image, features=[feature], image_context=image_context)

        with mock.patch('google.cloud.vision._gax.image_annotator_client.'
                        'ImageAnnotatorClient'):
            gax_api = self._make_one(client)

        responses = [
            image_annotator_pb2.AnnotateImageResponse(),
            image_annotator_pb2.AnnotateImageResponse(),
        ]
        response = image_annotator_pb2.BatchAnnotateImagesResponse(
            responses=responses)

        gax_api._annotator_client = mock.Mock(
            spec_set=['batch_annotate_images'])
        gax_api._annotator_client.batch_annotate_images.return_value = response
        responses = gax_api.annotate(requests_pb=[request])

        self.assertEqual(len(responses), 2)
        for annotation in responses:
            self.assertIsInstance(annotation, Annotations)
        gax_api._annotator_client.batch_annotate_images.assert_called()