forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest_protocol.py
More file actions
208 lines (173 loc) · 7.07 KB
/
test_protocol.py
File metadata and controls
208 lines (173 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import io
import unittest
from unittest.mock import Mock
from cassandra import DriverException, ProtocolVersion, UnsupportedOperation, type_codes
from cassandra.protocol import (
PrepareMessage, QueryMessage, ExecuteMessage, ResultMessage, UnsupportedOperation,
_PAGING_OPTIONS_FLAG, _WITH_SERIAL_CONSISTENCY_FLAG,
_PAGE_SIZE_FLAG, _WITH_PAGING_STATE_FLAG,
BatchMessage, RESULT_KIND_ROWS, write_int, write_short, write_string
)
from cassandra.query import BatchType
from cassandra.marshal import uint32_unpack
from cassandra.cluster import ContinuousPagingOptions
import pytest
class MessageTest(unittest.TestCase):
def test_result_message_wraps_inline_decode_errors(self):
body = io.BytesIO()
write_int(body, RESULT_KIND_ROWS)
write_int(body, 0)
write_int(body, 1)
write_string(body, "ks")
write_string(body, "tbl")
write_string(body, "v")
write_short(body, type_codes.DateType)
write_int(body, 1)
write_int(body, 1)
body.write(b"\x00")
with pytest.raises(DriverException, match='Failed decoding result column "v"'):
ResultMessage.recv_body(io.BytesIO(body.getvalue()), ProtocolVersion.V4, 0, {}, None, None)
def test_prepare_message(self):
"""
Test to check the appropriate calls are made
@since 3.9
@jira_ticket PYTHON-713
@expected_result the values are correctly written
@test_category connection
"""
message = PrepareMessage("a")
io = Mock()
message.send_body(io, 4)
self._check_calls(io, [(b'\x00\x00\x00\x01',), (b'a',)])
io.reset_mock()
message.send_body(io, 5)
self._check_calls(io, [(b'\x00\x00\x00\x01',), (b'a',), (b'\x00\x00\x00\x00',)])
def test_execute_message(self):
message = ExecuteMessage('1', [], 4)
io = Mock()
message.send_body(io, 4)
self._check_calls(io, [(b'\x00\x01',), (b'1',), (b'\x00\x04',), (b'\x01',), (b'\x00\x00',)])
io.reset_mock()
message.result_metadata_id = 'foo'
message.send_body(io, 5)
self._check_calls(io, [(b'\x00\x01',), (b'1',),
(b'\x00\x03',), (b'foo',),
(b'\x00\x04',),
(b'\x00\x00\x00\x01',), (b'\x00\x00',)])
def test_query_message(self):
"""
Test to check the appropriate calls are made
@since 3.9
@jira_ticket PYTHON-713
@expected_result the values are correctly written
@test_category connection
"""
message = QueryMessage("a", 3)
io = Mock()
message.send_body(io, 4)
self._check_calls(io, [(b'\x00\x00\x00\x01',), (b'a',), (b'\x00\x03',), (b'\x00',)])
io.reset_mock()
message.send_body(io, 5)
self._check_calls(io, [(b'\x00\x00\x00\x01',), (b'a',), (b'\x00\x03',), (b'\x00\x00\x00\x00',)])
def _check_calls(self, io, expected):
assert tuple(c[1] for c in io.write.mock_calls) == tuple(expected)
def test_prepare_flag(self):
"""
Test to check the prepare flag is properly set, This should only happen for V5 at the moment.
@since 3.9
@jira_ticket PYTHON-694, PYTHON-713
@expected_result the values are correctly written
@test_category connection
"""
message = PrepareMessage("a")
io = Mock()
for version in ProtocolVersion.SUPPORTED_VERSIONS:
message.send_body(io, version)
if ProtocolVersion.uses_prepare_flags(version):
assert len(io.write.mock_calls) == 3
else:
assert len(io.write.mock_calls) == 2
io.reset_mock()
def test_prepare_flag_with_keyspace(self):
message = PrepareMessage("a", keyspace='ks')
io = Mock()
for version in ProtocolVersion.SUPPORTED_VERSIONS:
if ProtocolVersion.uses_keyspace_flag(version):
message.send_body(io, version)
self._check_calls(io, [
(b'\x00\x00\x00\x01',),
(b'a',),
(b'\x00\x00\x00\x01',),
(b'\x00\x02',),
(b'ks',),
])
else:
with pytest.raises(UnsupportedOperation):
message.send_body(io, version)
io.reset_mock()
def test_keyspace_flag_raises_before_v5(self):
keyspace_message = QueryMessage('a', consistency_level=3, keyspace='ks')
io = Mock(name='io')
with pytest.raises(UnsupportedOperation, match='Keyspaces.*set'):
keyspace_message.send_body(io, protocol_version=4)
io.assert_not_called()
def test_keyspace_written_with_length(self):
io = Mock(name='io')
base_expected = [
(b'\x00\x00\x00\x01',),
(b'a',),
(b'\x00\x03',),
(b'\x00\x00\x00\x80',), # options w/ keyspace flag
]
QueryMessage('a', consistency_level=3, keyspace='ks').send_body(
io, protocol_version=5
)
self._check_calls(io, base_expected + [
(b'\x00\x02',), # length of keyspace string
(b'ks',),
])
io.reset_mock()
QueryMessage('a', consistency_level=3, keyspace='keyspace').send_body(
io, protocol_version=5
)
self._check_calls(io, base_expected + [
(b'\x00\x08',), # length of keyspace string
(b'keyspace',),
])
def test_batch_message_with_keyspace(self):
self.maxDiff = None
io = Mock(name='io')
batch = BatchMessage(
batch_type=BatchType.LOGGED,
queries=((False, 'stmt a', ('param a',)),
(False, 'stmt b', ('param b',)),
(False, 'stmt c', ('param c',))
),
consistency_level=3,
keyspace='ks'
)
batch.send_body(io, protocol_version=5)
self._check_calls(io,
((b'\x00',), (b'\x00\x03',), (b'\x00',),
(b'\x00\x00\x00\x06',), (b'stmt a',),
(b'\x00\x01',), (b'\x00\x00\x00\x07',), ('param a',),
(b'\x00',), (b'\x00\x00\x00\x06',), (b'stmt b',),
(b'\x00\x01',), (b'\x00\x00\x00\x07',), ('param b',),
(b'\x00',), (b'\x00\x00\x00\x06',), (b'stmt c',),
(b'\x00\x01',), (b'\x00\x00\x00\x07',), ('param c',),
(b'\x00\x03',),
(b'\x00\x00\x00\x80',), (b'\x00\x02',), (b'ks',))
)