forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathtest_resultset.py
More file actions
288 lines (241 loc) · 12.2 KB
/
Copy pathtest_resultset.py
File metadata and controls
288 lines (241 loc) · 12.2 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# 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 unittest
from unittest.mock import Mock, PropertyMock, patch
from cassandra.cluster import ResultSet
from cassandra.query import named_tuple_factory, dict_factory, tuple_factory, SimpleStatement, BatchStatement
from tests.util import assertListEqual
import pytest
class ResultSetTests(unittest.TestCase):
def test_iter_non_paged(self):
expected = list(range(10))
rs = ResultSet(Mock(has_more_pages=False), expected)
itr = iter(rs)
assertListEqual(list(itr), expected)
def test_iter_paged(self):
expected = list(range(10))
response_future = Mock(has_more_pages=True, _continuous_paging_session=None)
response_future.result.side_effect = (ResultSet(Mock(), expected[-5:]), ) # ResultSet is iterable, so it must be protected in order to be returned whole by the Mock
rs = ResultSet(response_future, expected[:5])
itr = iter(rs)
# this is brittle, depends on internal impl details. Would like to find a better way
type(response_future).has_more_pages = PropertyMock(side_effect=(True, True, False)) # after init to avoid side effects being consumed by init
assertListEqual(list(itr), expected)
def test_iter_paged_with_empty_pages(self):
expected = list(range(10))
response_future = Mock(has_more_pages=True, _continuous_paging_session=None)
response_future.result.side_effect = [
ResultSet(Mock(), []),
ResultSet(Mock(), [0, 1, 2, 3, 4]),
ResultSet(Mock(), []),
ResultSet(Mock(), [5, 6, 7, 8, 9]),
]
rs = ResultSet(response_future, [])
itr = iter(rs)
assertListEqual(list(itr), expected)
def test_list_non_paged(self):
# list access on RS for backwards-compatibility
expected = list(range(10))
rs = ResultSet(Mock(has_more_pages=False), expected)
for i in range(10):
assert rs[i] == expected[i]
assert list(rs) == expected
def test_list_paged(self):
# list access on RS for backwards-compatibility
expected = list(range(10))
response_future = Mock(has_more_pages=True, _continuous_paging_session=None)
response_future.result.side_effect = (ResultSet(Mock(), expected[-5:]), ) # ResultSet is iterable, so it must be protected in order to be returned whole by the Mock
rs = ResultSet(response_future, expected[:5])
# this is brittle, depends on internal impl details. Would like to find a better way
type(response_future).has_more_pages = PropertyMock(side_effect=(True, True, True, False)) # First two True are consumed on check entering list mode
assert rs[9] == expected[9]
assert list(rs) == expected
def test_has_more_pages(self):
response_future = Mock()
response_future.has_more_pages.side_effect = PropertyMock(side_effect=(True, False))
rs = ResultSet(response_future, [])
type(response_future).has_more_pages = PropertyMock(side_effect=(True, False)) # after init to avoid side effects being consumed by init
assert rs.has_more_pages
assert not rs.has_more_pages
def test_iterate_then_index(self):
# RuntimeError if indexing with no pages
expected = list(range(10))
rs = ResultSet(Mock(has_more_pages=False), expected)
itr = iter(rs)
# before consuming
with pytest.raises(RuntimeError):
rs[0]
list(itr)
# after consuming
with pytest.raises(RuntimeError):
rs[0]
assert not rs
assert not list(rs)
# RuntimeError if indexing during or after pages
response_future = Mock(has_more_pages=True, _continuous_paging_session=None)
response_future.result.side_effect = (ResultSet(Mock(), expected[-5:]), ) # ResultSet is iterable, so it must be protected in order to be returned whole by the Mock
rs = ResultSet(response_future, expected[:5])
type(response_future).has_more_pages = PropertyMock(side_effect=(True, False))
itr = iter(rs)
# before consuming
with pytest.raises(RuntimeError):
rs[0]
for row in itr:
# while consuming
with pytest.raises(RuntimeError):
rs[0]
# after consuming
with pytest.raises(RuntimeError):
rs[0]
assert not rs
assert not list(rs)
def test_index_list_mode(self):
# no pages
expected = list(range(10))
rs = ResultSet(Mock(has_more_pages=False), expected)
# index access before iteration causes list to be materialized
assert rs[0] == expected[0]
# resusable iteration
assertListEqual(list(rs), expected)
assertListEqual(list(rs), expected)
assert rs
# pages
response_future = Mock(has_more_pages=True, _continuous_paging_session=None)
response_future.result.side_effect = (ResultSet(Mock(), expected[-5:]), ) # ResultSet is iterable, so it must be protected in order to be returned whole by the Mock
rs = ResultSet(response_future, expected[:5])
# this is brittle, depends on internal impl details. Would like to find a better way
type(response_future).has_more_pages = PropertyMock(side_effect=(True, True, True, False)) # First two True are consumed on check entering list mode
# index access before iteration causes list to be materialized
assert rs[0] == expected[0]
assert rs[9] == expected[9]
# resusable iteration
assertListEqual(list(rs), expected)
assertListEqual(list(rs), expected)
assert rs
def test_eq(self):
# no pages
expected = list(range(10))
rs = ResultSet(Mock(has_more_pages=False), expected)
# eq before iteration causes list to be materialized
assert rs == expected
# results can be iterated or indexed once we're materialized
assertListEqual(list(rs), expected)
assert rs[9] == expected[9]
assert rs
# pages
response_future = Mock(has_more_pages=True, _continuous_paging_session=None)
response_future.result.side_effect = (ResultSet(Mock(), expected[-5:]), ) # ResultSet is iterable, so it must be protected in order to be returned whole by the Mock
rs = ResultSet(response_future, expected[:5])
type(response_future).has_more_pages = PropertyMock(side_effect=(True, True, True, False))
# eq before iteration causes list to be materialized
assert rs == expected
# results can be iterated or indexed once we're materialized
assertListEqual(list(rs), expected)
assert rs[9] == expected[9]
assert rs
def test_bool(self):
assert not ResultSet(Mock(has_more_pages=False), [])
assert ResultSet(Mock(has_more_pages=False), [1])
def test_was_applied(self):
# Create a non-LWT query so these assertions exercise the slow (regex) path.
# Without this, Mock().query.is_lwt() returns a truthy Mock, accidentally
# routing all checks through the fast path.
non_lwt_query = Mock(spec=SimpleStatement)
non_lwt_query.is_lwt.return_value = False
non_lwt_query.query_string = "INSERT INTO t (k) VALUES (1)"
# unknown row factory raises
with pytest.raises(RuntimeError):
ResultSet(Mock(query=non_lwt_query), []).was_applied
response_future = Mock(row_factory=named_tuple_factory, query=non_lwt_query)
# no row
with pytest.raises(RuntimeError):
ResultSet(response_future, []).was_applied
# too many rows
with pytest.raises(RuntimeError):
ResultSet(response_future, [tuple(), tuple()]).was_applied
# various internal row factories
for row_factory in (named_tuple_factory, tuple_factory):
for applied in (True, False):
rs = ResultSet(Mock(row_factory=row_factory, query=non_lwt_query), [(applied,)])
assert rs.was_applied == applied
row_factory = dict_factory
for applied in (True, False):
rs = ResultSet(Mock(row_factory=row_factory, query=non_lwt_query), [{'[applied]': applied}])
assert rs.was_applied == applied
def test_was_applied_lwt_fast_path(self):
"""Test that was_applied uses fast path for known LWT statements."""
# BoundStatement-like query with is_lwt() = True (fast path)
lwt_query = Mock()
lwt_query.is_lwt.return_value = True
for row_factory in (named_tuple_factory, tuple_factory):
for applied in (True, False):
rf = Mock(row_factory=row_factory, query=lwt_query)
rs = ResultSet(rf, [(applied,)])
assert rs.was_applied == applied
for applied in (True, False):
rf = Mock(row_factory=dict_factory, query=lwt_query)
rs = ResultSet(rf, [{'[applied]': applied}])
assert rs.was_applied == applied
# Fast path with too many rows should raise
rf = Mock(row_factory=named_tuple_factory, query=lwt_query)
with pytest.raises(RuntimeError, match="exactly one row"):
ResultSet(rf, [tuple(), tuple()]).was_applied
def test_was_applied_non_lwt_fallback(self):
"""Test that was_applied falls back to slow path for non-LWT statements."""
# SimpleStatement-like query with is_lwt() = False (slow path, non-batch)
non_lwt_query = Mock(spec=SimpleStatement)
non_lwt_query.is_lwt.return_value = False
non_lwt_query.query_string = "INSERT INTO t (k) VALUES (1)"
for applied in (True, False):
rf = Mock(row_factory=tuple_factory, query=non_lwt_query)
rs = ResultSet(rf, [(applied,)])
assert rs.was_applied == applied
def test_was_applied_batch_statement(self):
"""Test that was_applied handles BatchStatement correctly (slow path)."""
# BatchStatement with LWT should check column_names
batch_query = Mock(spec=BatchStatement)
batch_query.is_lwt.return_value = True
# Batch with [applied] column -- pass _col_names so ResultSet.__init__
# sets column_names correctly (instead of post-construction override).
rf = Mock(row_factory=tuple_factory, query=batch_query,
_col_names=['[applied]'], _col_types=None)
rs = ResultSet(rf, [(True,)])
assert rs.was_applied == True
# Batch without [applied] column raises
rf = Mock(row_factory=tuple_factory, query=batch_query,
_col_names=['other'], _col_types=None)
rs = ResultSet(rf, [(True,)])
with pytest.raises(RuntimeError, match="No LWT were present"):
rs.was_applied
def test_one(self):
# no pages
first, second = Mock(), Mock()
rs = ResultSet(Mock(has_more_pages=False), [first, second])
assert rs.one() == first
def test_all(self):
first, second = Mock(), Mock()
rs1 = ResultSet(Mock(has_more_pages=False), [first, second])
rs2 = ResultSet(Mock(has_more_pages=False), [first, second])
assert rs1.all() == list(rs2)
@patch('cassandra.cluster.warn')
def test_indexing_deprecation(self, mocked_warn):
# normally we'd use catch_warnings to test this, but that doesn't work
# pre-Py3.0 for some reason
first, second = Mock(), Mock()
rs = ResultSet(Mock(has_more_pages=False), [first, second])
assert rs[0] == first
assert len(mocked_warn.mock_calls) == 1
index_warning_args = tuple(mocked_warn.mock_calls[0])[1]
assert 'indexing support will be removed in 4.0' in str(index_warning_args[0])
assert index_warning_args[1] is DeprecationWarning