Skip to content
This repository was archived by the owner on Mar 31, 2026. It is now read-only.

Commit dfe4566

Browse files
feat(zb-experimental): implement open in writer (#1618)
feat(zb-experimental): implement open in writer --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent ec470a2 commit dfe4566

3 files changed

Lines changed: 64 additions & 5 deletions

File tree

‎google/cloud/storage/_experimental/asyncio/async_appendable_object_writer.py‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,17 @@ async def state_lookup(self) -> int:
126126

127127
asyncdefopen(self) ->None:
128128
"""Opens the underlying bidi-gRPC stream."""
129-
raiseNotImplementedError("open is not implemented yet.")
129+
ifself._is_stream_open:
130+
raiseValueError("Underlying bidi-gRPC stream is already open")
131+
132+
awaitself.write_obj_stream.open()
133+
self._is_stream_open=True
134+
ifself.generationisNone:
135+
self.generation=self.write_obj_stream.generation_number
136+
self.write_handle=self.write_obj_stream.write_handle
137+
138+
# Update self.persisted_size
139+
_=awaitself.state_lookup()
130140

131141
asyncdefappend(self, data: bytes):
132142
raiseNotImplementedError("append is not implemented yet.")

‎tests/unit/asyncio/test_async_appendable_object_writer.py‎

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,62 @@ async def test_state_lookup(mock_write_object_stream, mock_client):
112112

113113

114114
@pytest.mark.asyncio
115-
asyncdeftest_unimplemented_methods_raise_error(mock_client):
116-
"""Test that all currently unimplemented methods raise NotImplementedError."""
115+
@mock.patch(
116+
"google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream"
117+
)
118+
asyncdeftest_open_appendable_object_writer(mock_write_object_stream, mock_client):
119+
"""Test the open method."""
120+
# Arrange
117121
writer=AsyncAppendableObjectWriter(mock_client, BUCKET, OBJECT)
122+
mock_stream=mock_write_object_stream.return_value
123+
mock_stream.open=mock.AsyncMock()
124+
mock_stream.send=mock.AsyncMock()
125+
mock_stream.recv=mock.AsyncMock()
118126

119-
withpytest.raises(NotImplementedError):
127+
mock_state_response=mock.MagicMock()
128+
mock_state_response.persisted_size=1024
129+
mock_stream.recv.return_value=mock_state_response
130+
131+
mock_stream.generation_number=GENERATION
132+
mock_stream.write_handle=WRITE_HANDLE
133+
134+
# Act
135+
awaitwriter.open()
136+
137+
# Assert
138+
mock_stream.open.assert_awaited_once()
139+
assertwriter._is_stream_open
140+
assertwriter.generation==GENERATION
141+
assertwriter.write_handle==WRITE_HANDLE
142+
143+
expected_request=_storage_v2.BidiWriteObjectRequest(state_lookup=True)
144+
mock_stream.send.assert_awaited_once_with(expected_request)
145+
mock_stream.recv.assert_awaited_once()
146+
assertwriter.persisted_size==1024
147+
148+
149+
@pytest.mark.asyncio
150+
@mock.patch(
151+
"google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream"
152+
)
153+
asyncdeftest_open_when_already_open_raises_error(
154+
mock_write_object_stream, mock_client
155+
):
156+
"""Test that opening an already open writer raises a ValueError."""
157+
# Arrange
158+
writer=AsyncAppendableObjectWriter(mock_client, BUCKET, OBJECT)
159+
writer._is_stream_open=True# Manually set to open
160+
161+
# Act & Assert
162+
withpytest.raises(ValueError, match="Underlying bidi-gRPC stream is already open"):
120163
awaitwriter.open()
121164

165+
166+
@pytest.mark.asyncio
167+
asyncdeftest_unimplemented_methods_raise_error(mock_client):
168+
"""Test that all currently unimplemented methods raise NotImplementedError."""
169+
writer=AsyncAppendableObjectWriter(mock_client, BUCKET, OBJECT)
170+
122171
withpytest.raises(NotImplementedError):
123172
awaitwriter.append(b"data")
124173

‎tests/unit/test_bucket.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ def test_move_blob_needs_url_encoding(self):
23502350
timeout=30,
23512351
retry=None,
23522352
_target_object=new_blob,
2353-
)
2353+
)
23542354

23552355
deftest_move_blob_w_user_project(self):
23562356
source_name="source"

0 commit comments

Comments
 (0)