/
test_operations_async_client.py
97 lines (75 loc) · 3.62 KB
/
test_operations_async_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Copyright 2017 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from grpc.experimental import aio
import mock
import pytest
from google.api_core import (grpc_helpers_async, operations_v1,
page_iterator_async)
from google.longrunning import operations_pb2
from google.protobuf import empty_pb2
def _mock_grpc_objects(response):
fake_call = grpc_helpers_async.FakeUnaryUnaryCall(response)
method = mock.Mock(spec=aio.UnaryUnaryMultiCallable, return_value=fake_call)
mocked_channel = mock.Mock()
mocked_channel.unary_unary = mock.Mock(return_value=method)
return mocked_channel, method, fake_call
@pytest.mark.asyncio
async def test_get_operation():
mocked_channel, method, fake_call = _mock_grpc_objects(
operations_pb2.Operation(name="meep"))
client = operations_v1.OperationsAsyncClient(mocked_channel)
response = await client.get_operation("name", metadata=[("x-goog-request-params", "foo")])
assert method.call_count == 1
assert tuple(method.call_args_list[0])[0][0].name == "name"
assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"]
assert response == fake_call.response
@pytest.mark.asyncio
async def test_list_operations():
operations = [
operations_pb2.Operation(name="1"),
operations_pb2.Operation(name="2"),
]
list_response = operations_pb2.ListOperationsResponse(operations=operations)
mocked_channel, method, fake_call = _mock_grpc_objects(list_response)
client = operations_v1.OperationsAsyncClient(mocked_channel)
pager = await client.list_operations("name", "filter", metadata=[("x-goog-request-params", "foo")])
assert isinstance(pager, page_iterator_async.AsyncIterator)
responses = []
async for response in pager:
responses.append(response)
assert responses == operations
assert method.call_count == 1
assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"]
request = tuple(method.call_args_list[0])[0][0]
assert isinstance(request, operations_pb2.ListOperationsRequest)
assert request.name == "name"
assert request.filter == "filter"
@pytest.mark.asyncio
async def test_delete_operation():
mocked_channel, method, fake_call = _mock_grpc_objects(
empty_pb2.Empty())
client = operations_v1.OperationsAsyncClient(mocked_channel)
await client.delete_operation("name", metadata=[("x-goog-request-params", "foo")])
assert method.call_count == 1
assert tuple(method.call_args_list[0])[0][0].name == "name"
assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"]
@pytest.mark.asyncio
async def test_cancel_operation():
mocked_channel, method, fake_call = _mock_grpc_objects(
empty_pb2.Empty())
client = operations_v1.OperationsAsyncClient(mocked_channel)
await client.cancel_operation("name", metadata=[("x-goog-request-params", "foo")])
assert method.call_count == 1
assert tuple(method.call_args_list[0])[0][0].name == "name"
assert ("x-goog-request-params", "foo") in tuple(method.call_args_list[0])[1]["metadata"]