|
| 1 | +"""Tests for ZonalFile write operations.""" |
| 2 | + |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from gcsfs.extended_gcsfs import BucketType |
| 8 | +from gcsfs.tests.settings import TEST_BUCKET |
| 9 | + |
| 10 | +file_path = f"{TEST_BUCKET}/zonal-file-test" |
| 11 | +test_data = b"hello world" |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def zonal_write_mocks(): |
| 16 | + """A fixture for mocking Zonal bucket write functionality.""" |
| 17 | + |
| 18 | + patch_target_get_bucket_type = ( |
| 19 | + "gcsfs.extended_gcsfs.ExtendedGcsFileSystem._get_bucket_type" |
| 20 | + ) |
| 21 | + patch_target_init_aaow = "gcsfs.zb_hns_utils.init_aaow" |
| 22 | + |
| 23 | + mock_aaow = mock.AsyncMock() |
| 24 | + mock_init_aaow = mock.AsyncMock(return_value=mock_aaow) |
| 25 | + with ( |
| 26 | + mock.patch( |
| 27 | + patch_target_get_bucket_type, |
| 28 | + return_value=BucketType.ZONAL_HIERARCHICAL, |
| 29 | + ), |
| 30 | + mock.patch(patch_target_init_aaow, mock_init_aaow), |
| 31 | + ): |
| 32 | + mocks = { |
| 33 | + "aaow": mock_aaow, |
| 34 | + "init_aaow": mock_init_aaow, |
| 35 | + } |
| 36 | + yield mocks |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.parametrize( |
| 40 | + "setup_action, error_match", |
| 41 | + [ |
| 42 | + (lambda f: setattr(f, "mode", "rb"), "File not in write mode"), |
| 43 | + (lambda f: setattr(f, "closed", True), "I/O operation on closed file"), |
| 44 | + ( |
| 45 | + lambda f: setattr(f, "forced", True), |
| 46 | + "This file has been force-flushed, can only close", |
| 47 | + ), |
| 48 | + ], |
| 49 | + ids=["not_writable", "closed", "force_flushed"], |
| 50 | +) |
| 51 | +def test_zonal_file_write_value_errors( |
| 52 | + extended_gcsfs, zonal_write_mocks, setup_action, error_match # noqa: F841 |
| 53 | +): |
| 54 | + """Test ZonalFile.write raises ValueError for invalid states.""" |
| 55 | + with extended_gcsfs.open(file_path, "wb") as f: |
| 56 | + setup_action(f) |
| 57 | + with pytest.raises(ValueError, match=error_match): |
| 58 | + f.write(test_data) |
| 59 | + |
| 60 | + |
| 61 | +def test_zonal_file_write_success(extended_gcsfs, zonal_write_mocks): |
| 62 | + """Test that writing to a ZonalFile calls the underlying writer's append method.""" |
| 63 | + with extended_gcsfs.open(file_path, "wb") as f: |
| 64 | + f.write(test_data) |
| 65 | + |
| 66 | + zonal_write_mocks["aaow"].append.assert_awaited_once_with(test_data) |
| 67 | + |
| 68 | + |
| 69 | +def test_zonal_file_open_write_mode(extended_gcsfs, zonal_write_mocks): |
| 70 | + """Test that opening a ZonalFile in write mode initializes the writer.""" |
| 71 | + bucket, key, _ = extended_gcsfs.split_path(file_path) |
| 72 | + with extended_gcsfs.open(file_path, "wb"): |
| 73 | + pass |
| 74 | + |
| 75 | + zonal_write_mocks["init_aaow"].assert_called_once_with( |
| 76 | + extended_gcsfs.grpc_client, bucket, key |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +def test_zonal_file_flush(extended_gcsfs, zonal_write_mocks): |
| 81 | + """Test that flush calls the underlying writer's flush method.""" |
| 82 | + with extended_gcsfs.open(file_path, "wb") as f: |
| 83 | + f.flush() |
| 84 | + |
| 85 | + zonal_write_mocks["aaow"].flush.assert_awaited() |
| 86 | + |
| 87 | + |
| 88 | +def test_zonal_file_commit(extended_gcsfs, zonal_write_mocks): |
| 89 | + """Test that commit finalizes the write and sets autocommit to True.""" |
| 90 | + with extended_gcsfs.open(file_path, "wb") as f: |
| 91 | + f.commit() |
| 92 | + |
| 93 | + zonal_write_mocks["aaow"].finalize.assert_awaited_once() |
| 94 | + assert f.autocommit is True |
| 95 | + |
| 96 | + |
| 97 | +def test_zonal_file_discard(extended_gcsfs, zonal_write_mocks): # noqa: F841 |
| 98 | + """Test that discard on a ZonalFile logs a warning.""" |
| 99 | + with mock.patch("gcsfs.zonal_file.logger") as mock_logger: |
| 100 | + with extended_gcsfs.open(file_path, "wb") as f: |
| 101 | + f.discard() |
| 102 | + mock_logger.warning.assert_called_once() |
| 103 | + assert ( |
| 104 | + "Discard is unavailable for Zonal Buckets" |
| 105 | + in mock_logger.warning.call_args[0][0] |
| 106 | + ) |
| 107 | + |
| 108 | + |
| 109 | +def test_zonal_file_close(extended_gcsfs, zonal_write_mocks): |
| 110 | + """Test that close finalizes the write by default (autocommit=True).""" |
| 111 | + with extended_gcsfs.open(file_path, "wb"): |
| 112 | + pass |
| 113 | + zonal_write_mocks["aaow"].close.assert_awaited_once_with(finalize_on_close=True) |
| 114 | + |
| 115 | + |
| 116 | +def test_zonal_file_close_with_autocommit_false(extended_gcsfs, zonal_write_mocks): |
| 117 | + """Test that close does not finalize the write when autocommit is False.""" |
| 118 | + |
| 119 | + with extended_gcsfs.open(file_path, "wb", autocommit=False): |
| 120 | + pass # close is called on exit |
| 121 | + |
| 122 | + zonal_write_mocks["aaow"].close.assert_awaited_once_with(finalize_on_close=False) |
| 123 | + |
| 124 | + |
| 125 | +@pytest.mark.parametrize( |
| 126 | + "method_name", |
| 127 | + [ |
| 128 | + ("_initiate_upload"), |
| 129 | + ("_simple_upload"), |
| 130 | + ("_upload_chunk"), |
| 131 | + ], |
| 132 | +) |
| 133 | +def test_zonal_file_not_implemented_methods( |
| 134 | + extended_gcsfs, zonal_write_mocks, method_name # noqa: F841 |
| 135 | +): |
| 136 | + """Test that some GCSFile methods are not implemented for ZonalFile.""" |
| 137 | + with extended_gcsfs.open(file_path, "wb") as f: |
| 138 | + method_to_call = getattr(f, method_name) |
| 139 | + with pytest.raises(NotImplementedError): |
| 140 | + method_to_call() |
0 commit comments