This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 173
Expand file tree
/
Copy pathtest_async_write_object_stream.py
More file actions
396 lines (324 loc) · 13.4 KB
/
test_async_write_object_stream.py
File metadata and controls
396 lines (324 loc) · 13.4 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# Copyright 2025 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.
import pytest
from unittest import mock
from unittest.mock import AsyncMock
from google.cloud.storage._experimental.asyncio.async_write_object_stream import (
_AsyncWriteObjectStream,
)
from google.cloud import _storage_v2
BUCKET = "my-bucket"
OBJECT = "my-object"
GENERATION = 12345
WRITE_HANDLE = b"test-handle"
@pytest.fixture
def mock_client():
"""Mock the async gRPC client."""
mock_transport = mock.AsyncMock()
mock_transport.bidi_write_object = mock.sentinel.bidi_write_object
mock_transport._wrapped_methods = {
mock.sentinel.bidi_write_object: mock.sentinel.wrapped_bidi_write_object
}
mock_gapic_client = mock.AsyncMock()
mock_gapic_client._transport = mock_transport
client = mock.AsyncMock()
client._client = mock_gapic_client
return client
async def instantiate_write_obj_stream(mock_client, mock_cls_async_bidi_rpc, open=True):
"""Helper to create an instance of _AsyncWriteObjectStream and open it by default."""
socket_like_rpc = AsyncMock()
mock_cls_async_bidi_rpc.return_value = socket_like_rpc
socket_like_rpc.open = AsyncMock()
socket_like_rpc.send = AsyncMock()
socket_like_rpc.close = AsyncMock()
mock_response = mock.MagicMock(spec=_storage_v2.BidiWriteObjectResponse)
mock_response.resource = mock.MagicMock(spec=_storage_v2.Object)
mock_response.resource.generation = GENERATION
mock_response.resource.size = 0
mock_response.write_handle = WRITE_HANDLE
socket_like_rpc.recv = AsyncMock(return_value=mock_response)
write_obj_stream = _AsyncWriteObjectStream(mock_client, BUCKET, OBJECT)
if open:
await write_obj_stream.open()
return write_obj_stream
def test_async_write_object_stream_init(mock_client):
"""Test the constructor of _AsyncWriteObjectStream."""
stream = _AsyncWriteObjectStream(mock_client, BUCKET, OBJECT)
assert stream.client == mock_client
assert stream.bucket_name == BUCKET
assert stream.object_name == OBJECT
assert stream.generation_number is None
assert stream.write_handle is None
assert stream._full_bucket_name == f"projects/_/buckets/{BUCKET}"
assert stream.rpc == mock.sentinel.wrapped_bidi_write_object
assert stream.metadata == (
("x-goog-request-params", f"bucket=projects/_/buckets/{BUCKET}"),
)
assert stream.socket_like_rpc is None
assert not stream._is_stream_open
assert stream.first_bidi_write_req is None
assert stream.persisted_size == 0
assert stream.object_resource is None
def test_async_write_object_stream_init_with_generation_and_handle(mock_client):
"""Test the constructor with optional arguments."""
generation = 12345
write_handle = b"test-handle"
stream = _AsyncWriteObjectStream(
mock_client,
BUCKET,
OBJECT,
generation_number=generation,
write_handle=write_handle,
)
assert stream.generation_number == generation
assert stream.write_handle == write_handle
def test_async_write_object_stream_init_raises_value_error():
"""Test that the constructor raises ValueError for missing arguments."""
with pytest.raises(ValueError, match="client must be provided"):
_AsyncWriteObjectStream(None, BUCKET, OBJECT)
with pytest.raises(ValueError, match="bucket_name must be provided"):
_AsyncWriteObjectStream(mock.Mock(), None, OBJECT)
with pytest.raises(ValueError, match="object_name must be provided"):
_AsyncWriteObjectStream(mock.Mock(), BUCKET, None)
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_open_for_new_object(mock_async_bidi_rpc, mock_client):
"""Test opening a stream for a new object."""
# Arrange
socket_like_rpc = mock.AsyncMock()
mock_async_bidi_rpc.return_value = socket_like_rpc
socket_like_rpc.open = mock.AsyncMock()
mock_response = mock.MagicMock(spec=_storage_v2.BidiWriteObjectResponse)
mock_response.resource = mock.MagicMock(spec=_storage_v2.Object)
mock_response.resource.generation = GENERATION
mock_response.resource.size = 0
mock_response.write_handle = WRITE_HANDLE
socket_like_rpc.recv = mock.AsyncMock(return_value=mock_response)
stream = _AsyncWriteObjectStream(mock_client, BUCKET, OBJECT)
# Act
await stream.open()
# Assert
assert stream._is_stream_open
socket_like_rpc.open.assert_called_once()
socket_like_rpc.recv.assert_called_once()
assert stream.generation_number == GENERATION
assert stream.write_handle == WRITE_HANDLE
assert stream.persisted_size == 0
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_open_for_existing_object(mock_async_bidi_rpc, mock_client):
"""Test opening a stream for an existing object."""
# Arrange
socket_like_rpc = mock.AsyncMock()
mock_async_bidi_rpc.return_value = socket_like_rpc
socket_like_rpc.open = mock.AsyncMock()
mock_response = mock.MagicMock(spec=_storage_v2.BidiWriteObjectResponse)
mock_response.resource = mock.MagicMock(spec=_storage_v2.Object)
mock_response.resource.size = 1024
mock_response.resource.generation = GENERATION
mock_response.write_handle = WRITE_HANDLE
socket_like_rpc.recv = mock.AsyncMock(return_value=mock_response)
stream = _AsyncWriteObjectStream(
mock_client, BUCKET, OBJECT, generation_number=GENERATION
)
# Act
await stream.open()
# Assert
assert stream._is_stream_open
socket_like_rpc.open.assert_called_once()
socket_like_rpc.recv.assert_called_once()
assert stream.generation_number == GENERATION
assert stream.write_handle == WRITE_HANDLE
assert stream.persisted_size == 1024
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_open_when_already_open_raises_error(mock_async_bidi_rpc, mock_client):
"""Test that opening an already open stream raises a ValueError."""
# Arrange
socket_like_rpc = mock.AsyncMock()
mock_async_bidi_rpc.return_value = socket_like_rpc
socket_like_rpc.open = mock.AsyncMock()
mock_response = mock.MagicMock(spec=_storage_v2.BidiWriteObjectResponse)
mock_response.resource = mock.MagicMock(spec=_storage_v2.Object)
mock_response.resource.generation = GENERATION
mock_response.resource.size = 0
mock_response.write_handle = WRITE_HANDLE
socket_like_rpc.recv = mock.AsyncMock(return_value=mock_response)
stream = _AsyncWriteObjectStream(mock_client, BUCKET, OBJECT)
await stream.open()
# Act & Assert
with pytest.raises(ValueError, match="Stream is already open"):
await stream.open()
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_open_raises_error_on_missing_object_resource(
mock_async_bidi_rpc, mock_client
):
"""Test that open raises ValueError if object_resource is not in the response."""
socket_like_rpc = mock.AsyncMock()
mock_async_bidi_rpc.return_value = socket_like_rpc
mock_reponse = mock.AsyncMock()
type(mock_reponse).resource = mock.PropertyMock(return_value=None)
socket_like_rpc.recv.return_value = mock_reponse
# Note: Don't use below code as unittest library automatically assigns an
# `AsyncMock` object to an attribute, if not set.
# socket_like_rpc.recv.return_value = mock.AsyncMock(
# return_value=_storage_v2.BidiWriteObjectResponse(resource=None)
# )
stream = _AsyncWriteObjectStream(mock_client, BUCKET, OBJECT)
with pytest.raises(
ValueError, match="Failed to obtain object resource after opening the stream"
):
await stream.open()
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_open_raises_error_on_missing_generation(
mock_async_bidi_rpc, mock_client
):
"""Test that open raises ValueError if generation is not in the response."""
socket_like_rpc = mock.AsyncMock()
mock_async_bidi_rpc.return_value = socket_like_rpc
# Configure the mock response object
mock_response = mock.AsyncMock()
type(mock_response.resource).generation = mock.PropertyMock(return_value=None)
socket_like_rpc.recv.return_value = mock_response
stream = _AsyncWriteObjectStream(mock_client, BUCKET, OBJECT)
with pytest.raises(
ValueError, match="Failed to obtain object generation after opening the stream"
):
await stream.open()
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_open_raises_error_on_missing_write_handle(
mock_async_bidi_rpc, mock_client
):
"""Test that open raises ValueError if write_handle is not in the response."""
socket_like_rpc = mock.AsyncMock()
mock_async_bidi_rpc.return_value = socket_like_rpc
socket_like_rpc.recv = mock.AsyncMock(
return_value=_storage_v2.BidiWriteObjectResponse(
resource=_storage_v2.Object(generation=GENERATION), write_handle=None
)
)
stream = _AsyncWriteObjectStream(mock_client, BUCKET, OBJECT)
with pytest.raises(ValueError, match="Failed to obtain write_handle"):
await stream.open()
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_close(mock_cls_async_bidi_rpc, mock_client):
"""Test that close successfully closes the stream."""
# Arrange
write_obj_stream = await instantiate_write_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=True
)
# Act
await write_obj_stream.close()
# Assert
write_obj_stream.socket_like_rpc.close.assert_called_once()
assert not write_obj_stream.is_stream_open
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_close_without_open_should_raise_error(
mock_cls_async_bidi_rpc, mock_client
):
"""Test that closing a stream that is not open raises a ValueError."""
# Arrange
write_obj_stream = await instantiate_write_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=False
)
# Act & Assert
with pytest.raises(ValueError, match="Stream is not open"):
await write_obj_stream.close()
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_send(mock_cls_async_bidi_rpc, mock_client):
"""Test that send calls the underlying rpc's send method."""
# Arrange
write_obj_stream = await instantiate_write_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=True
)
# Act
bidi_write_object_request = _storage_v2.BidiWriteObjectRequest()
await write_obj_stream.send(bidi_write_object_request)
# Assert
write_obj_stream.socket_like_rpc.send.assert_called_once_with(
bidi_write_object_request
)
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_send_without_open_should_raise_error(
mock_cls_async_bidi_rpc, mock_client
):
"""Test that sending on a stream that is not open raises a ValueError."""
# Arrange
write_obj_stream = await instantiate_write_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=False
)
# Act & Assert
with pytest.raises(ValueError, match="Stream is not open"):
await write_obj_stream.send(_storage_v2.BidiWriteObjectRequest())
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_recv(mock_cls_async_bidi_rpc, mock_client):
"""Test that recv calls the underlying rpc's recv method."""
# Arrange
write_obj_stream = await instantiate_write_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=True
)
bidi_write_object_response = _storage_v2.BidiWriteObjectResponse()
write_obj_stream.socket_like_rpc.recv = AsyncMock(
return_value=bidi_write_object_response
)
# Act
response = await write_obj_stream.recv()
# Assert
write_obj_stream.socket_like_rpc.recv.assert_called_once()
assert response == bidi_write_object_response
@pytest.mark.asyncio
@mock.patch(
"google.cloud.storage._experimental.asyncio.async_write_object_stream.AsyncBidiRpc"
)
async def test_recv_without_open_should_raise_error(
mock_cls_async_bidi_rpc, mock_client
):
"""Test that receiving on a stream that is not open raises a ValueError."""
# Arrange
write_obj_stream = await instantiate_write_obj_stream(
mock_client, mock_cls_async_bidi_rpc, open=False
)
# Act & Assert
with pytest.raises(ValueError, match="Stream is not open"):
await write_obj_stream.recv()