blob: bd7f3736d01635dc1cee69de8b7cc3de869b15cd [file] [log] [blame]
Luke Sneeringeracb6e3e2017-10-31 08:57:09 -07001# Copyright 2017 Google LLC
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -07002#
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
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080015from google.api_core import grpc_helpers
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070016from google.api_core import operations_v1
17from google.api_core import page_iterator
18from google.longrunning import operations_pb2
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080019from google.protobuf import empty_pb2
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070020
21
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080022def test_get_operation():
23 channel = grpc_helpers.ChannelStub()
24 client = operations_v1.OperationsClient(channel)
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080025 channel.GetOperation.response = operations_pb2.Operation(name="meep")
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070026
Bu Sun Kim73854e82021-01-14 14:22:58 -070027 response = client.get_operation("name", metadata=[("x-goog-request-params", "foo")])
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070028
Bu Sun Kim73854e82021-01-14 14:22:58 -070029 assert ("x-goog-request-params", "foo") in channel.GetOperation.calls[0].metadata
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080030 assert len(channel.GetOperation.requests) == 1
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080031 assert channel.GetOperation.requests[0].name == "name"
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080032 assert response == channel.GetOperation.response
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070033
34
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080035def test_list_operations():
36 channel = grpc_helpers.ChannelStub()
37 client = operations_v1.OperationsClient(channel)
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070038 operations = [
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080039 operations_pb2.Operation(name="1"),
40 operations_pb2.Operation(name="2"),
41 ]
42 list_response = operations_pb2.ListOperationsResponse(operations=operations)
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080043 channel.ListOperations.response = list_response
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070044
Bu Sun Kim73854e82021-01-14 14:22:58 -070045 response = client.list_operations("name", "filter", metadata=[("x-goog-request-params", "foo")])
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070046
47 assert isinstance(response, page_iterator.Iterator)
48 assert list(response) == operations
49
Bu Sun Kim73854e82021-01-14 14:22:58 -070050 assert ("x-goog-request-params", "foo") in channel.ListOperations.calls[0].metadata
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080051 assert len(channel.ListOperations.requests) == 1
52 request = channel.ListOperations.requests[0]
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070053 assert isinstance(request, operations_pb2.ListOperationsRequest)
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080054 assert request.name == "name"
55 assert request.filter == "filter"
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070056
57
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080058def test_delete_operation():
59 channel = grpc_helpers.ChannelStub()
60 client = operations_v1.OperationsClient(channel)
61 channel.DeleteOperation.response = empty_pb2.Empty()
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070062
Bu Sun Kim73854e82021-01-14 14:22:58 -070063 client.delete_operation("name", metadata=[("x-goog-request-params", "foo")])
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070064
Bu Sun Kim73854e82021-01-14 14:22:58 -070065 assert ("x-goog-request-params", "foo") in channel.DeleteOperation.calls[0].metadata
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080066 assert len(channel.DeleteOperation.requests) == 1
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080067 assert channel.DeleteOperation.requests[0].name == "name"
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070068
69
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080070def test_cancel_operation():
71 channel = grpc_helpers.ChannelStub()
72 client = operations_v1.OperationsClient(channel)
73 channel.CancelOperation.response = empty_pb2.Empty()
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070074
Bu Sun Kim73854e82021-01-14 14:22:58 -070075 client.cancel_operation("name", metadata=[("x-goog-request-params", "foo")])
Jon Wayne Parrott77fb0f22017-10-18 12:52:35 -070076
Bu Sun Kim73854e82021-01-14 14:22:58 -070077 assert ("x-goog-request-params", "foo") in channel.CancelOperation.calls[0].metadata
Jon Wayne Parrott37658c12018-01-05 14:54:49 -080078 assert len(channel.CancelOperation.requests) == 1
Christopher Wilcox6f4070d2018-11-29 11:02:52 -080079 assert channel.CancelOperation.requests[0].name == "name"