Luke Sneeringer | acb6e3e | 2017-10-31 08:57:09 -0700 | [diff] [blame^] | 1 | # Copyright 2017 Google LLC |
Jon Wayne Parrott | 77fb0f2 | 2017-10-18 12:52:35 -0700 | [diff] [blame] | 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 | import mock |
| 16 | |
| 17 | from google.api_core import operations_v1 |
| 18 | from google.api_core import page_iterator |
| 19 | from google.longrunning import operations_pb2 |
| 20 | |
| 21 | |
| 22 | def make_operations_stub(channel): |
| 23 | return mock.Mock( |
| 24 | spec=[ |
| 25 | 'GetOperation', 'DeleteOperation', 'ListOperations', |
| 26 | 'CancelOperation']) |
| 27 | |
| 28 | |
| 29 | operations_stub_patch = mock.patch( |
| 30 | 'google.longrunning.operations_pb2.OperationsStub', |
| 31 | autospec=True, |
| 32 | side_effect=make_operations_stub) |
| 33 | |
| 34 | |
| 35 | @operations_stub_patch |
| 36 | def test_constructor(operations_stub): |
| 37 | stub = make_operations_stub(None) |
| 38 | operations_stub.side_effect = None |
| 39 | operations_stub.return_value = stub |
| 40 | |
| 41 | client = operations_v1.OperationsClient(mock.sentinel.channel) |
| 42 | |
| 43 | assert client.operations_stub == stub |
| 44 | operations_stub.assert_called_once_with(mock.sentinel.channel) |
| 45 | |
| 46 | |
| 47 | @operations_stub_patch |
| 48 | def test_get_operation(operations_stub): |
| 49 | client = operations_v1.OperationsClient(mock.sentinel.channel) |
| 50 | client.operations_stub.GetOperation.return_value = mock.sentinel.operation |
| 51 | |
| 52 | response = client.get_operation('name') |
| 53 | |
| 54 | request = client.operations_stub.GetOperation.call_args[0][0] |
| 55 | assert isinstance(request, operations_pb2.GetOperationRequest) |
| 56 | assert request.name == 'name' |
| 57 | |
| 58 | assert response == mock.sentinel.operation |
| 59 | |
| 60 | |
| 61 | @operations_stub_patch |
| 62 | def test_list_operations(operations_stub): |
| 63 | client = operations_v1.OperationsClient(mock.sentinel.channel) |
| 64 | operations = [ |
| 65 | operations_pb2.Operation(name='1'), |
| 66 | operations_pb2.Operation(name='2')] |
| 67 | list_response = operations_pb2.ListOperationsResponse( |
| 68 | operations=operations) |
| 69 | client.operations_stub.ListOperations.return_value = list_response |
| 70 | |
| 71 | response = client.list_operations('name', 'filter') |
| 72 | |
| 73 | assert isinstance(response, page_iterator.Iterator) |
| 74 | assert list(response) == operations |
| 75 | |
| 76 | request = client.operations_stub.ListOperations.call_args[0][0] |
| 77 | assert isinstance(request, operations_pb2.ListOperationsRequest) |
| 78 | assert request.name == 'name' |
| 79 | assert request.filter == 'filter' |
| 80 | |
| 81 | |
| 82 | @operations_stub_patch |
| 83 | def test_delete_operation(operations_stub): |
| 84 | client = operations_v1.OperationsClient(mock.sentinel.channel) |
| 85 | |
| 86 | client.delete_operation('name') |
| 87 | |
| 88 | request = client.operations_stub.DeleteOperation.call_args[0][0] |
| 89 | assert isinstance(request, operations_pb2.DeleteOperationRequest) |
| 90 | assert request.name == 'name' |
| 91 | |
| 92 | |
| 93 | @operations_stub_patch |
| 94 | def test_cancel_operation(operations_stub): |
| 95 | client = operations_v1.OperationsClient(mock.sentinel.channel) |
| 96 | |
| 97 | client.cancel_operation('name') |
| 98 | |
| 99 | request = client.operations_stub.CancelOperation.call_args[0][0] |
| 100 | assert isinstance(request, operations_pb2.CancelOperationRequest) |
| 101 | assert request.name == 'name' |