|
| 1 | +# Copyright 2022 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from unittest import mock |
| 16 | + |
| 17 | +import flask |
| 18 | +from google.cloud import vision_v1 |
| 19 | +import pytest |
| 20 | + |
| 21 | +import vision_function |
| 22 | + |
| 23 | + |
| 24 | +# Create a fake "app" for generating test request contexts. |
| 25 | +@pytest.fixture(scope="module") |
| 26 | +def app() -> flask.Flask: |
| 27 | + return flask.Flask(__name__) |
| 28 | + |
| 29 | + |
| 30 | +@mock.patch('vision_function.vision_v1') |
| 31 | +def test_vision_function(mock_vision_v1: object, app: flask.Flask) -> None: |
| 32 | + label_detection_mock = mock.Mock(side_effect=[ |
| 33 | + vision_v1.AnnotateImageResponse( |
| 34 | + {'label_annotations': [{'description': 'apple'}]}), |
| 35 | + vision_v1.AnnotateImageResponse( |
| 36 | + {'label_annotations': [{'description': 'banana'}]})]) |
| 37 | + mock_vision_v1.ImageAnnotatorClient = mock.Mock( |
| 38 | + return_value=mock.Mock(label_detection=label_detection_mock)) |
| 39 | + mock_vision_v1.AnnotateImageResponse = vision_v1.AnnotateImageResponse |
| 40 | + with app.test_request_context( |
| 41 | + json={'calls': [['https://storage.googleapis.com/bucket/apple'], |
| 42 | + ['https://storage.googleapis.com/bucket/banana']]}): |
| 43 | + response = vision_function.label_detection(flask.request) |
| 44 | + assert response.status_code == 200 |
| 45 | + assert len(response.get_json()['replies']) == 2 |
| 46 | + assert 'apple' in str(response.get_json()['replies'][0]) |
| 47 | + assert 'banana' in str(response.get_json()['replies'][1]) |
| 48 | + |
| 49 | + |
| 50 | +@mock.patch('vision_function.vision_v1') |
| 51 | +def test_vision_function_error( |
| 52 | + mock_vision_v1: object, app: flask.Flask) -> None: |
| 53 | + label_detection_mock = mock.Mock(side_effect=Exception('API error')) |
| 54 | + mock_vision_v1.ImageAnnotatorClient = mock.Mock( |
| 55 | + return_value=mock.Mock(label_detection=label_detection_mock)) |
| 56 | + with app.test_request_context( |
| 57 | + json={'calls': [['https://storage.googleapis.com/bucket/apple'], |
| 58 | + ['https://storage.googleapis.com/bucket/banana']]}): |
| 59 | + response = vision_function.label_detection(flask.request) |
| 60 | + assert response.status_code == 400 |
| 61 | + assert 'API error' in str(response.get_data()) |
0 commit comments