|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +import io |
| 16 | +import struct |
15 | 17 | import unittest |
16 | 18 |
|
17 | 19 | from unittest.mock import Mock |
|
21 | 23 | PrepareMessage, QueryMessage, ExecuteMessage, UnsupportedOperation, |
22 | 24 | _PAGING_OPTIONS_FLAG, _WITH_SERIAL_CONSISTENCY_FLAG, |
23 | 25 | _PAGE_SIZE_FLAG, _WITH_PAGING_STATE_FLAG, |
24 | | - BatchMessage |
| 26 | + _SKIP_METADATA_FLAG, |
| 27 | + BatchMessage, ResultMessage, |
| 28 | + RESULT_KIND_ROWS |
25 | 29 | ) |
| 30 | +from cassandra.protocol_features import ProtocolFeatures |
26 | 31 | from cassandra.query import BatchType |
27 | 32 | from cassandra.marshal import uint32_unpack |
28 | 33 | from cassandra.cluster import ContinuousPagingOptions |
@@ -68,6 +73,162 @@ def test_execute_message(self): |
68 | 73 | (b'\x00\x04',), |
69 | 74 | (b'\x00\x00\x00\x01',), (b'\x00\x00',)]) |
70 | 75 |
|
| 76 | + def test_execute_message_skip_meta_flag(self): |
| 77 | + """skip_meta=True must set _SKIP_METADATA_FLAG (0x02) in the flags byte.""" |
| 78 | + message = ExecuteMessage('1', [], 4, skip_meta=True) |
| 79 | + mock_io = Mock() |
| 80 | + |
| 81 | + message.send_body(mock_io, 4) |
| 82 | + # flags byte should be VALUES_FLAG | SKIP_METADATA_FLAG = 0x01 | 0x02 = 0x03 |
| 83 | + self._check_calls(mock_io, [(b'\x00\x01',), (b'1',), (b'\x00\x04',), (b'\x03',), (b'\x00\x00',)]) |
| 84 | + |
| 85 | + def test_execute_message_scylla_metadata_id_v4(self): |
| 86 | + """result_metadata_id should be written on protocol v4 when use_metadata_id=True (Scylla extension).""" |
| 87 | + message = ExecuteMessage('1', [], 4) |
| 88 | + message.result_metadata_id = b'foo' |
| 89 | + message.use_metadata_id = True |
| 90 | + mock_io = Mock() |
| 91 | + |
| 92 | + message.send_body(mock_io, 4) |
| 93 | + # metadata_id written before query params (same position as v5) |
| 94 | + self._check_calls(mock_io, [(b'\x00\x01',), (b'1',), |
| 95 | + (b'\x00\x03',), (b'foo',), |
| 96 | + (b'\x00\x04',), (b'\x01',), (b'\x00\x00',)]) |
| 97 | + |
| 98 | + def test_execute_message_scylla_metadata_id_none_writes_sentinel(self): |
| 99 | + """ |
| 100 | + When use_metadata_id=True but result_metadata_id is None (e.g. LWT statement or |
| 101 | + mixed cluster), send_body must still write the field as an empty string sentinel |
| 102 | + (\\x00\\x00) so the frame layout matches what the server expects. |
| 103 | + """ |
| 104 | + message = ExecuteMessage('1', [], 4) |
| 105 | + message.use_metadata_id = True |
| 106 | + # result_metadata_id intentionally left as None |
| 107 | + mock_io = Mock() |
| 108 | + |
| 109 | + message.send_body(mock_io, 4) |
| 110 | + # empty sentinel: \x00\x00 (zero-length short) + b'' (zero bytes), then normal query params |
| 111 | + self._check_calls(mock_io, [(b'\x00\x01',), (b'1',), |
| 112 | + (b'\x00\x00',), (b'',), |
| 113 | + (b'\x00\x04',), (b'\x01',), (b'\x00\x00',)]) |
| 114 | + |
| 115 | + def test_execute_message_v5_metadata_id_none_writes_sentinel(self): |
| 116 | + """ |
| 117 | + On protocol v5, result_metadata_id is always written (uses_prepared_metadata). |
| 118 | + When result_metadata_id is None (e.g. LWT statement or mixed cluster where the |
| 119 | + statement was prepared before the extension was active), send_body must write an |
| 120 | + empty sentinel instead of crashing with TypeError. |
| 121 | + """ |
| 122 | + message = ExecuteMessage('1', [], 4) |
| 123 | + # result_metadata_id intentionally left as None; use_metadata_id stays False (v5 native path) |
| 124 | + mock_io = Mock() |
| 125 | + |
| 126 | + message.send_body(mock_io, 5) |
| 127 | + # v5 always writes metadata_id: None → empty sentinel \x00\x00 + b'', then query params |
| 128 | + # v5 uses 4-byte flags: VALUES_FLAG = \x00\x00\x00\x01 |
| 129 | + self._check_calls(mock_io, [(b'\x00\x01',), (b'1',), |
| 130 | + (b'\x00\x00',), (b'',), |
| 131 | + (b'\x00\x04',), |
| 132 | + (b'\x00\x00\x00\x01',), (b'\x00\x00',)]) |
| 133 | + |
| 134 | + def test_recv_results_prepared_scylla_extension_reads_metadata_id(self): |
| 135 | + """ |
| 136 | + When use_metadata_id is True (Scylla extension), result_metadata_id must be |
| 137 | + read from the PREPARE response even for protocol v4. |
| 138 | + """ |
| 139 | + # Build a minimal valid PREPARE response binary (no bind/result columns): |
| 140 | + # query_id: short(2) + b'ab' |
| 141 | + # result_metadata_id: short(3) + b'xyz' <-- only present when extension active |
| 142 | + # prepared flags: int(1) = global_tables_spec |
| 143 | + # colcount: int(0) |
| 144 | + # num_pk_indexes: int(0) |
| 145 | + # ksname: short(2) + b'ks' |
| 146 | + # cfname: short(2) + b'tb' |
| 147 | + # result flags: int(4) = no_metadata |
| 148 | + # result colcount: int(0) |
| 149 | + buf = io.BytesIO( |
| 150 | + struct.pack('>H', 2) + b'ab' # query_id |
| 151 | + + struct.pack('>H', 3) + b'xyz' # result_metadata_id |
| 152 | + + struct.pack('>i', 1) # prepared flags: global_tables_spec |
| 153 | + + struct.pack('>i', 0) # colcount = 0 |
| 154 | + + struct.pack('>i', 0) # num_pk_indexes = 0 |
| 155 | + + struct.pack('>H', 2) + b'ks' # ksname |
| 156 | + + struct.pack('>H', 2) + b'tb' # cfname |
| 157 | + + struct.pack('>i', 4) # result flags: no_metadata |
| 158 | + + struct.pack('>i', 0) # result colcount = 0 |
| 159 | + ) |
| 160 | + |
| 161 | + features_with_extension = ProtocolFeatures(use_metadata_id=True) |
| 162 | + msg = ResultMessage(kind=4) # RESULT_KIND_PREPARED = 4 |
| 163 | + msg.recv_results_prepared(buf, protocol_version=4, |
| 164 | + protocol_features=features_with_extension, |
| 165 | + user_type_map={}) |
| 166 | + assert msg.query_id == b'ab' |
| 167 | + assert msg.result_metadata_id == b'xyz' |
| 168 | + |
| 169 | + def test_recv_results_prepared_no_extension_skips_metadata_id(self): |
| 170 | + """ |
| 171 | + Without use_metadata_id, result_metadata_id must NOT be read on protocol v4. |
| 172 | + The buffer must NOT contain a metadata_id field. |
| 173 | + """ |
| 174 | + buf = io.BytesIO( |
| 175 | + struct.pack('>H', 2) + b'ab' # query_id |
| 176 | + # no result_metadata_id |
| 177 | + + struct.pack('>i', 1) # prepared flags: global_tables_spec |
| 178 | + + struct.pack('>i', 0) # colcount = 0 |
| 179 | + + struct.pack('>i', 0) # num_pk_indexes = 0 |
| 180 | + + struct.pack('>H', 2) + b'ks' # ksname |
| 181 | + + struct.pack('>H', 2) + b'tb' # cfname |
| 182 | + + struct.pack('>i', 4) # result flags: no_metadata |
| 183 | + + struct.pack('>i', 0) # result colcount = 0 |
| 184 | + ) |
| 185 | + |
| 186 | + features_without_extension = ProtocolFeatures(use_metadata_id=False) |
| 187 | + msg = ResultMessage(kind=4) |
| 188 | + msg.recv_results_prepared(buf, protocol_version=4, |
| 189 | + protocol_features=features_without_extension, |
| 190 | + user_type_map={}) |
| 191 | + assert msg.query_id == b'ab' |
| 192 | + assert msg.result_metadata_id is None |
| 193 | + |
| 194 | + def test_recv_results_metadata_changed_flag(self): |
| 195 | + """ |
| 196 | + When _METADATA_ID_FLAG (0x0008) is set in a ROWS result, |
| 197 | + recv_results_metadata must read and store the new result_metadata_id |
| 198 | + sent by the server (METADATA_CHANGED signal), and still populate |
| 199 | + column_metadata normally. |
| 200 | + """ |
| 201 | + # Wire layout for a ROWS result with METADATA_CHANGED: |
| 202 | + # flags: int(0x0008) = _METADATA_ID_FLAG |
| 203 | + # colcount: int(0) |
| 204 | + # result_metadata_id: short(4) + b'new1' |
| 205 | + # (no columns — colcount=0 — to keep the buffer minimal) |
| 206 | + buf = io.BytesIO( |
| 207 | + struct.pack('>i', 0x0008) # flags: METADATA_ID_FLAG |
| 208 | + + struct.pack('>i', 0) # colcount = 0 |
| 209 | + + struct.pack('>H', 4) + b'new1' # result_metadata_id = b'new1' |
| 210 | + ) |
| 211 | + msg = ResultMessage(kind=RESULT_KIND_ROWS) |
| 212 | + msg.recv_results_metadata(buf, user_type_map={}) |
| 213 | + assert msg.result_metadata_id == b'new1' |
| 214 | + assert msg.column_metadata == [] |
| 215 | + |
| 216 | + def test_recv_results_metadata_no_metadata_flag_skips_metadata_id(self): |
| 217 | + """ |
| 218 | + When _NO_METADATA_FLAG (0x0004) is set, recv_results_metadata returns |
| 219 | + early and must NOT read or set result_metadata_id, even if the caller |
| 220 | + mistakenly sets _METADATA_ID_FLAG alongside it. |
| 221 | + """ |
| 222 | + # flags = _NO_METADATA_FLAG (0x0004), colcount = 0 |
| 223 | + buf = io.BytesIO( |
| 224 | + struct.pack('>i', 0x0004) # flags: NO_METADATA |
| 225 | + + struct.pack('>i', 0) # colcount = 0 |
| 226 | + ) |
| 227 | + msg = ResultMessage(kind=RESULT_KIND_ROWS) |
| 228 | + msg.recv_results_metadata(buf, user_type_map={}) |
| 229 | + assert not hasattr(msg, 'result_metadata_id') or msg.result_metadata_id is None |
| 230 | + assert not hasattr(msg, 'column_metadata') or msg.column_metadata is None |
| 231 | + |
71 | 232 | def test_query_message(self): |
72 | 233 | """ |
73 | 234 | Test to check the appropriate calls are made |
|
0 commit comments