|
| 1 | +# Copyright 2023 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 documentai |
| 19 | +import pytest |
| 20 | + |
| 21 | +import document_function |
| 22 | + |
| 23 | +_BIGQUERY_REQUEST_JSON = { |
| 24 | + 'calls': |
| 25 | + [ |
| 26 | + ['https://storage.googleapis.com/bucket/apple', |
| 27 | + 'application/pdf'], |
| 28 | + ['https://storage.googleapis.com/bucket/banana', |
| 29 | + 'application/pdf'], |
| 30 | + ] |
| 31 | +} |
| 32 | +_BIGQUERY_RESPONSE_JSON = { |
| 33 | + 'replies': |
| 34 | + [ |
| 35 | + {'text': 'apple'}, |
| 36 | + {'text': 'banana'}, |
| 37 | + ] |
| 38 | +} |
| 39 | + |
| 40 | + |
| 41 | +# Create a fake "app" for generating test request contexts. |
| 42 | +@pytest.fixture(scope="module") |
| 43 | +def app() -> flask.Flask: |
| 44 | + return flask.Flask(__name__) |
| 45 | + |
| 46 | + |
| 47 | +@mock.patch('document_function.urllib.request') |
| 48 | +@mock.patch('document_function.documentai') |
| 49 | +def test_document_function( |
| 50 | + mock_documentai: object, |
| 51 | + mock_request: object, |
| 52 | + app: flask.Flask, |
| 53 | +) -> None: |
| 54 | + mock_request.urlopen = mock.Mock(read=mock.Mock(return_value=b'filedata')) |
| 55 | + process_document_mock = mock.Mock(side_effect=[ |
| 56 | + documentai.ProcessResponse( |
| 57 | + {'document': {'text': 'apple'}}), |
| 58 | + documentai.ProcessResponse( |
| 59 | + {'document': {'text': 'banana'}})]) |
| 60 | + mock_documentai.DocumentProcessorServiceClient = mock.Mock( |
| 61 | + return_value=mock.Mock(process_document=process_document_mock)) |
| 62 | + with app.test_request_context(json=_BIGQUERY_REQUEST_JSON): |
| 63 | + response = document_function.document_ocr(flask.request) |
| 64 | + assert response.status_code == 200 |
| 65 | + assert response.get_json() == _BIGQUERY_RESPONSE_JSON |
| 66 | + |
| 67 | + |
| 68 | +@mock.patch('document_function.urllib.request') |
| 69 | +@mock.patch('document_function.documentai') |
| 70 | +def test_document_function_error( |
| 71 | + mock_documentai: object, |
| 72 | + mock_request: object, |
| 73 | + app: flask.Flask, |
| 74 | +) -> None: |
| 75 | + mock_request.urlopen = mock.Mock(read=mock.Mock(return_value=b'filedata')) |
| 76 | + process_document_mock = mock.Mock(side_effect=Exception('API error')) |
| 77 | + mock_documentai.DocumentProcessorServiceClient = mock.Mock( |
| 78 | + return_value=mock.Mock(process_document=process_document_mock)) |
| 79 | + with app.test_request_context(json=_BIGQUERY_REQUEST_JSON): |
| 80 | + response = document_function.document_ocr(flask.request) |
| 81 | + assert response.status_code == 400 |
| 82 | + assert 'API error' in str(response.get_data()) |
0 commit comments