|
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 |
25 | 28 | ) |
| 29 | +from cassandra.protocol_features import ProtocolFeatures |
26 | 30 | from cassandra.query import BatchType |
27 | 31 | from cassandra.marshal import uint32_unpack |
28 | 32 | from cassandra.cluster import ContinuousPagingOptions |
@@ -68,6 +72,87 @@ def test_execute_message(self): |
68 | 72 | (b'\x00\x04',), |
69 | 73 | (b'\x00\x00\x00\x01',), (b'\x00\x00',)]) |
70 | 74 |
|
| 75 | + def test_execute_message_skip_meta_flag(self): |
| 76 | + """skip_meta=True must set _SKIP_METADATA_FLAG (0x02) in the flags byte.""" |
| 77 | + message = ExecuteMessage('1', [], 4, skip_meta=True) |
| 78 | + mock_io = Mock() |
| 79 | + |
| 80 | + message.send_body(mock_io, 4) |
| 81 | + # flags byte should be VALUES_FLAG | SKIP_METADATA_FLAG = 0x01 | 0x02 = 0x03 |
| 82 | + self._check_calls(mock_io, [(b'\x00\x01',), (b'1',), (b'\x00\x04',), (b'\x03',), (b'\x00\x00',)]) |
| 83 | + |
| 84 | + def test_execute_message_scylla_metadata_id_v4(self): |
| 85 | + """result_metadata_id should be written on protocol v4 when set (Scylla extension).""" |
| 86 | + message = ExecuteMessage('1', [], 4) |
| 87 | + message.result_metadata_id = b'foo' |
| 88 | + mock_io = Mock() |
| 89 | + |
| 90 | + message.send_body(mock_io, 4) |
| 91 | + # metadata_id written before query params (same position as v5) |
| 92 | + self._check_calls(mock_io, [(b'\x00\x01',), (b'1',), |
| 93 | + (b'\x00\x03',), (b'foo',), |
| 94 | + (b'\x00\x04',), (b'\x01',), (b'\x00\x00',)]) |
| 95 | + |
| 96 | + def test_recv_results_prepared_scylla_extension_reads_metadata_id(self): |
| 97 | + """ |
| 98 | + When use_metadata_id is True (Scylla extension), result_metadata_id must be |
| 99 | + read from the PREPARE response even for protocol v4. |
| 100 | + """ |
| 101 | + # Build a minimal valid PREPARE response binary (no bind/result columns): |
| 102 | + # query_id: short(2) + b'ab' |
| 103 | + # result_metadata_id: short(3) + b'xyz' <-- only present when extension active |
| 104 | + # prepared flags: int(1) = global_tables_spec |
| 105 | + # colcount: int(0) |
| 106 | + # num_pk_indexes: int(0) |
| 107 | + # ksname: short(2) + b'ks' |
| 108 | + # cfname: short(2) + b'tb' |
| 109 | + # result flags: int(4) = no_metadata |
| 110 | + # result colcount: int(0) |
| 111 | + buf = io.BytesIO( |
| 112 | + struct.pack('>H', 2) + b'ab' # query_id |
| 113 | + + struct.pack('>H', 3) + b'xyz' # result_metadata_id |
| 114 | + + struct.pack('>i', 1) # prepared flags: global_tables_spec |
| 115 | + + struct.pack('>i', 0) # colcount = 0 |
| 116 | + + struct.pack('>i', 0) # num_pk_indexes = 0 |
| 117 | + + struct.pack('>H', 2) + b'ks' # ksname |
| 118 | + + struct.pack('>H', 2) + b'tb' # cfname |
| 119 | + + struct.pack('>i', 4) # result flags: no_metadata |
| 120 | + + struct.pack('>i', 0) # result colcount = 0 |
| 121 | + ) |
| 122 | + |
| 123 | + features_with_extension = ProtocolFeatures(use_metadata_id=True) |
| 124 | + msg = ResultMessage(kind=4) # RESULT_KIND_PREPARED = 4 |
| 125 | + msg.recv_results_prepared(buf, protocol_version=4, |
| 126 | + protocol_features=features_with_extension, |
| 127 | + user_type_map={}) |
| 128 | + assert msg.query_id == b'ab' |
| 129 | + assert msg.result_metadata_id == b'xyz' |
| 130 | + |
| 131 | + def test_recv_results_prepared_no_extension_skips_metadata_id(self): |
| 132 | + """ |
| 133 | + Without use_metadata_id, result_metadata_id must NOT be read on protocol v4. |
| 134 | + The buffer must NOT contain a metadata_id field. |
| 135 | + """ |
| 136 | + buf = io.BytesIO( |
| 137 | + struct.pack('>H', 2) + b'ab' # query_id |
| 138 | + # no result_metadata_id |
| 139 | + + struct.pack('>i', 1) # prepared flags: global_tables_spec |
| 140 | + + struct.pack('>i', 0) # colcount = 0 |
| 141 | + + struct.pack('>i', 0) # num_pk_indexes = 0 |
| 142 | + + struct.pack('>H', 2) + b'ks' # ksname |
| 143 | + + struct.pack('>H', 2) + b'tb' # cfname |
| 144 | + + struct.pack('>i', 4) # result flags: no_metadata |
| 145 | + + struct.pack('>i', 0) # result colcount = 0 |
| 146 | + ) |
| 147 | + |
| 148 | + features_without_extension = ProtocolFeatures(use_metadata_id=False) |
| 149 | + msg = ResultMessage(kind=4) |
| 150 | + msg.recv_results_prepared(buf, protocol_version=4, |
| 151 | + protocol_features=features_without_extension, |
| 152 | + user_type_map={}) |
| 153 | + assert msg.query_id == b'ab' |
| 154 | + assert msg.result_metadata_id is None |
| 155 | + |
71 | 156 | def test_query_message(self): |
72 | 157 | """ |
73 | 158 | Test to check the appropriate calls are made |
|
0 commit comments