|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +from io import BytesIO |
15 | 16 | import pytest |
16 | 17 | from unittest import mock |
17 | 18 |
|
|
21 | 22 | ) |
22 | 23 | from google.cloud.storage._experimental.asyncio.async_appendable_object_writer import ( |
23 | 24 | _MAX_CHUNK_SIZE_BYTES, |
| 25 | + _DEFAULT_FLUSH_INTERVAL_BYTES, |
24 | 26 | ) |
25 | 27 | from google.cloud import _storage_v2 |
26 | 28 |
|
@@ -285,9 +287,6 @@ async def test_unimplemented_methods_raise_error(mock_client): |
285 | 287 | with pytest.raises(NotImplementedError): |
286 | 288 | await writer.append_from_stream(mock.Mock()) |
287 | 289 |
|
288 | | - with pytest.raises(NotImplementedError): |
289 | | - await writer.append_from_file("file.txt") |
290 | | - |
291 | 290 |
|
292 | 291 | @pytest.mark.asyncio |
293 | 292 | @mock.patch( |
@@ -529,9 +528,6 @@ async def test_append_flushes_when_buffer_is_full( |
529 | 528 | mock_write_object_stream, mock_client |
530 | 529 | ): |
531 | 530 | """Test that append flushes the stream when the buffer size is reached.""" |
532 | | - from google.cloud.storage._experimental.asyncio.async_appendable_object_writer import ( |
533 | | - _DEFAULT_FLUSH_INTERVAL_BYTES, |
534 | | - ) |
535 | 531 |
|
536 | 532 | writer = AsyncAppendableObjectWriter(mock_client, BUCKET, OBJECT) |
537 | 533 | writer._is_stream_open = True |
@@ -595,3 +591,29 @@ async def test_append_data_two_times(mock_write_object_stream, mock_client): |
595 | 591 | total_data_length = len(data1) + len(data2) |
596 | 592 | assert writer.offset == total_data_length |
597 | 593 | assert writer.simple_flush.await_count == 0 |
| 594 | + |
| 595 | + |
| 596 | +@pytest.mark.asyncio |
| 597 | +@pytest.mark.parametrize( |
| 598 | + "file_size, block_size", |
| 599 | + [ |
| 600 | + (10, 4 * 1024), |
| 601 | + (20 * 1024 * 1024, _DEFAULT_FLUSH_INTERVAL_BYTES), |
| 602 | + (16 * 1024 * 1024, _DEFAULT_FLUSH_INTERVAL_BYTES), |
| 603 | + ], |
| 604 | +) |
| 605 | +async def test_append_from_file(file_size, block_size, mock_client): |
| 606 | + # arrange |
| 607 | + fp = BytesIO(b"a" * file_size) |
| 608 | + writer = AsyncAppendableObjectWriter(mock_client, BUCKET, OBJECT) |
| 609 | + writer._is_stream_open = True |
| 610 | + writer.append = mock.AsyncMock() |
| 611 | + |
| 612 | + # act |
| 613 | + await writer.append_from_file(fp, block_size=block_size) |
| 614 | + |
| 615 | + # assert |
| 616 | + if file_size % block_size == 0: |
| 617 | + writer.append.await_count == file_size // block_size |
| 618 | + else: |
| 619 | + writer.append.await_count == file_size // block_size + 1 |
0 commit comments