Skip to content

Commit df42160

Browse files
committed
add unit tests and idomatic
1 parent b285a40 commit df42160

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ def __init__(
137137
raise exceptions.OutOfRange(
138138
f"flush_interval must be >= {_MAX_CHUNK_SIZE_BYTES} , but provided {self.flush_interval}"
139139
)
140-
if not self.flush_interval % _MAX_CHUNK_SIZE_BYTES == 0:
140+
if self.flush_interval % _MAX_CHUNK_SIZE_BYTES != 0:
141141
raise exceptions.OutOfRange(
142-
f"flush interval - {self.flush_interval} should be multiple of {_MAX_CHUNK_SIZE_BYTES}"
142+
f"flush_interval must be a multiple of {_MAX_CHUNK_SIZE_BYTES}, but provided {self.flush_interval}"
143143
)
144144
self.bytes_appended_since_last_flush = 0
145145

@@ -210,7 +210,6 @@ async def append(self, data: bytes) -> None:
210210
self.offset = self.persisted_size
211211

212212
start_idx = 0
213-
# bytes_to_flush = 0
214213
while start_idx < total_bytes:
215214
end_idx = min(start_idx + _MAX_CHUNK_SIZE_BYTES, total_bytes)
216215
data_chunk = data[start_idx:end_idx]

tests/unit/asyncio/test_async_appendable_object_writer.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
from google.cloud.storage._experimental.asyncio.async_appendable_object_writer import (
2020
AsyncAppendableObjectWriter,
2121
)
22+
from google.cloud.storage._experimental.asyncio.async_appendable_object_writer import (
23+
_MAX_CHUNK_SIZE_BYTES,
24+
)
2225
from google.cloud import _storage_v2
2326

2427

@@ -113,6 +116,36 @@ def test_init_with_writer_options(mock_write_object_stream, mock_client):
113116
)
114117

115118

119+
@mock.patch(
120+
"google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream"
121+
)
122+
def test_init_with_flush_interval_less_than_chunk_size_raises_error(mock_client):
123+
"""Test that an OutOfRange error is raised if flush_interval is less than the chunk size."""
124+
125+
with pytest.raises(exceptions.OutOfRange):
126+
AsyncAppendableObjectWriter(
127+
mock_client,
128+
BUCKET,
129+
OBJECT,
130+
writer_options={"FLUSH_INTERVAL_BYTES": _MAX_CHUNK_SIZE_BYTES - 1},
131+
)
132+
133+
134+
@mock.patch(
135+
"google.cloud.storage._experimental.asyncio.async_appendable_object_writer._AsyncWriteObjectStream"
136+
)
137+
def test_init_with_flush_interval_not_multiple_of_chunk_size_raises_error(mock_client):
138+
"""Test that an OutOfRange error is raised if flush_interval is not a multiple of the chunk size."""
139+
140+
with pytest.raises(exceptions.OutOfRange):
141+
AsyncAppendableObjectWriter(
142+
mock_client,
143+
BUCKET,
144+
OBJECT,
145+
writer_options={"FLUSH_INTERVAL_BYTES": _MAX_CHUNK_SIZE_BYTES + 1},
146+
)
147+
148+
116149
@mock.patch("google.cloud.storage._experimental.asyncio._utils.google_crc32c")
117150
@mock.patch(
118151
"google.cloud.storage._experimental.asyncio.async_grpc_client.AsyncGrpcClient.grpc_client"

0 commit comments

Comments
 (0)