|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +import capnp |
| 4 | +import sys |
| 5 | +import gc |
| 6 | + |
| 7 | + |
| 8 | +@pytest.fixture(scope="module") |
| 9 | +def all_types(): |
| 10 | + """Load the standard all_types.capnp schema.""" |
| 11 | + directory = os.path.dirname(__file__) |
| 12 | + return capnp.load(os.path.join(directory, "all_types.capnp")) |
| 13 | + |
| 14 | + |
| 15 | +def test_set_bytes_get_bytes(all_types): |
| 16 | + """ |
| 17 | + Scenario 1: Set Byte -> Get Byte |
| 18 | + Verify standard behavior: writing bytes results in reading bytes. |
| 19 | + """ |
| 20 | + msg = all_types.TestAllTypes.new_message() |
| 21 | + input_data = b"hello_world" |
| 22 | + |
| 23 | + # Set |
| 24 | + msg.dataField = input_data |
| 25 | + |
| 26 | + # Get |
| 27 | + output_data = msg.dataField |
| 28 | + |
| 29 | + # Verify |
| 30 | + assert isinstance(output_data, bytes) |
| 31 | + assert output_data == input_data |
| 32 | + |
| 33 | + |
| 34 | +def test_set_view_get_bytes(all_types): |
| 35 | + """ |
| 36 | + Scenario 2: Set View -> Get Byte |
| 37 | + Verify compatibility: Passing a memoryview sets the data, |
| 38 | + but standard attribute access returns a bytes copy. |
| 39 | + """ |
| 40 | + msg = all_types.TestAllTypes.new_message() |
| 41 | + |
| 42 | + # Create a memoryview source |
| 43 | + raw_source = bytearray(b"view_source") |
| 44 | + view = memoryview(raw_source) |
| 45 | + |
| 46 | + # Set via memoryview |
| 47 | + msg.dataField = view |
| 48 | + |
| 49 | + # Get via standard attribute |
| 50 | + output_data = msg.dataField |
| 51 | + |
| 52 | + # Verify |
| 53 | + assert isinstance(output_data, bytes) |
| 54 | + assert output_data == b"view_source" |
| 55 | + |
| 56 | + |
| 57 | +def test_set_bytes_get_view_and_modify(all_types): |
| 58 | + """ |
| 59 | + Scenario 3: Set Byte -> Get View |
| 60 | + Verify the high-performance API get_data_as_view. |
| 61 | + The view must be writable and modifications must reflect in the message. |
| 62 | + """ |
| 63 | + msg = all_types.TestAllTypes.new_message() |
| 64 | + |
| 65 | + # Initial write |
| 66 | + msg.dataField = b"ABCDE" |
| 67 | + |
| 68 | + # Get view via new API |
| 69 | + view = msg.get_data_as_view("dataField") |
| 70 | + |
| 71 | + # Verify view properties |
| 72 | + assert isinstance(view, memoryview) |
| 73 | + assert view.readonly is False |
| 74 | + assert view.tobytes() == b"ABCDE" |
| 75 | + |
| 76 | + # Verify in-place modification |
| 77 | + view[0] = ord("Z") # Change 'A' to 'Z' |
| 78 | + |
| 79 | + # Verify modification is reflected in standard access |
| 80 | + assert msg.dataField == b"ZBCDE" |
| 81 | + |
| 82 | + |
| 83 | +def test_reader_vs_builder_view(all_types): |
| 84 | + """ |
| 85 | + Verify that Builder views are writable, but Reader views are read-only. |
| 86 | + """ |
| 87 | + # 1. Builder phase |
| 88 | + builder = all_types.TestAllTypes.new_message() |
| 89 | + builder.dataField = b"test_rw" |
| 90 | + |
| 91 | + builder_view = builder.get_data_as_view("dataField") |
| 92 | + assert builder_view.readonly is False |
| 93 | + builder_view[0] = ord("T") # Modification allowed |
| 94 | + |
| 95 | + # 2. Reader phase |
| 96 | + reader = builder.as_reader() |
| 97 | + |
| 98 | + # Standard Get |
| 99 | + assert reader.dataField == b"Test_rw" |
| 100 | + |
| 101 | + # Reader get_data_as_view |
| 102 | + reader_view = reader.get_data_as_view("dataField") |
| 103 | + assert isinstance(reader_view, memoryview) |
| 104 | + assert reader_view.readonly is True |
| 105 | + |
| 106 | + # Attempting to modify Reader view should raise TypeError |
| 107 | + with pytest.raises(TypeError): |
| 108 | + reader_view[0] = ord("X") |
| 109 | + |
| 110 | + |
| 111 | +def test_nested_struct_data(all_types): |
| 112 | + """ |
| 113 | + Verify that get_data_as_view works correctly on nested structs. |
| 114 | + """ |
| 115 | + msg = all_types.TestAllTypes.new_message() |
| 116 | + |
| 117 | + # Initialize nested struct |
| 118 | + inner = msg.init("structField") |
| 119 | + inner.int32Field = 100 |
| 120 | + inner.dataField = b"nested_data" |
| 121 | + |
| 122 | + # 1. Verify standard access |
| 123 | + assert msg.structField.dataField == b"nested_data" |
| 124 | + |
| 125 | + # 2. Verify nested get_data_as_view |
| 126 | + view = msg.structField.get_data_as_view("dataField") |
| 127 | + |
| 128 | + assert isinstance(view, memoryview) |
| 129 | + assert view.tobytes() == b"nested_data" |
| 130 | + |
| 131 | + # Modify nested data |
| 132 | + view[0] = ord("N") |
| 133 | + assert msg.structField.dataField == b"Nested_data" |
| 134 | + |
| 135 | + |
| 136 | +def test_corner_cases_values(all_types): |
| 137 | + """ |
| 138 | + Test edge cases: Empty bytes and binary data with nulls. |
| 139 | + """ |
| 140 | + msg = all_types.TestAllTypes.new_message() |
| 141 | + |
| 142 | + # Case A: Empty Bytes |
| 143 | + msg.dataField = b"" |
| 144 | + assert msg.dataField == b"" |
| 145 | + view = msg.get_data_as_view("dataField") |
| 146 | + assert len(view) == 0 |
| 147 | + |
| 148 | + # Case B: Binary data containing null bytes |
| 149 | + binary_data = b"\x00\xff\x00\x01" |
| 150 | + msg.dataField = binary_data |
| 151 | + assert msg.dataField == binary_data |
| 152 | + assert msg.get_data_as_view("dataField").tobytes() == binary_data |
| 153 | + |
| 154 | + |
| 155 | +def test_error_wrong_type(all_types): |
| 156 | + """ |
| 157 | + Test error handling: Calling get_data_as_view on non-Data fields. |
| 158 | + """ |
| 159 | + msg = all_types.TestAllTypes.new_message() |
| 160 | + msg.int32Field = 123 |
| 161 | + msg.textField = "I am text" |
| 162 | + |
| 163 | + # Attempt on Int field |
| 164 | + with pytest.raises(TypeError) as excinfo: |
| 165 | + msg.get_data_as_view("int32Field") |
| 166 | + assert "not a DATA field" in str(excinfo.value) |
| 167 | + |
| 168 | + # Attempt on Text field |
| 169 | + with pytest.raises(TypeError) as excinfo: |
| 170 | + msg.get_data_as_view("textField") |
| 171 | + assert "not a DATA field" in str(excinfo.value) |
| 172 | + |
| 173 | + |
| 174 | +def test_error_missing_field(all_types): |
| 175 | + """ |
| 176 | + Test error handling: Accessing a non-existent field name. |
| 177 | + """ |
| 178 | + msg = all_types.TestAllTypes.new_message() |
| 179 | + |
| 180 | + # Accessing a missing field should raise AttributeError (standard Python behavior) |
| 181 | + with pytest.raises(AttributeError) as excinfo: |
| 182 | + msg.get_data_as_view("non_existent_field") |
| 183 | + |
| 184 | + # Optional: Verify the error message contains the field name |
| 185 | + assert "non_existent_field" in str(excinfo.value) |
| 186 | + |
| 187 | + |
| 188 | +def test_view_keeps_message_alive(all_types): |
| 189 | + """ |
| 190 | + Verify that a View keeps messages alive. |
| 191 | + """ |
| 192 | + msg = all_types.TestAllTypes.new_message() |
| 193 | + expected_data = b"persistence_check" |
| 194 | + msg.dataField = expected_data |
| 195 | + |
| 196 | + initial_ref_count = sys.getrefcount(msg) |
| 197 | + view = msg.get_data_as_view("dataField") |
| 198 | + new_ref_count = sys.getrefcount(msg) |
| 199 | + |
| 200 | + assert ( |
| 201 | + new_ref_count > initial_ref_count |
| 202 | + ), f"View failed to hold reference to Message! (Old: {initial_ref_count}, New: {new_ref_count})" |
| 203 | + print( |
| 204 | + f"\n[Ref Check] Success: Ref count increased from {initial_ref_count} to {new_ref_count}" |
| 205 | + ) |
| 206 | + |
| 207 | + del msg |
| 208 | + gc.collect() |
| 209 | + |
| 210 | + assert view.tobytes() == expected_data |
0 commit comments