forked from apache/cassandra-python-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_named.py
More file actions
387 lines (323 loc) · 15.4 KB
/
Copy pathtest_named.py
File metadata and controls
387 lines (323 loc) · 15.4 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# 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.
try:
import unittest2 as unittest
except ImportError:
import unittest # noqa
from cassandra import ConsistencyLevel
from cassandra.cqlengine import operators
from cassandra.cqlengine.named import NamedKeyspace
from cassandra.cqlengine.operators import EqualsOperator, GreaterThanOrEqualOperator
from cassandra.cqlengine.query import ResultObject
from cassandra.concurrent import execute_concurrent_with_args
from cassandra.cqlengine import models
from tests.integration.cqlengine import setup_connection, execute_count
from tests.integration.cqlengine.base import BaseCassEngTestCase
from tests.integration.cqlengine.query.test_queryset import BaseQuerySetUsage
from tests.integration import BasicSharedKeyspaceUnitTestCase, greaterthanorequalcass30
class TestQuerySetOperation(BaseCassEngTestCase):
@classmethod
def setUpClass(cls):
super(TestQuerySetOperation, cls).setUpClass()
cls.keyspace = NamedKeyspace('cqlengine_test')
cls.table = cls.keyspace.table('test_model')
def test_query_filter_parsing(self):
"""
Tests the queryset filter method parses it's kwargs properly
"""
query1 = self.table.objects(test_id=5)
assert len(query1._where) == 1
op = query1._where[0]
assert isinstance(op.operator, operators.EqualsOperator)
assert op.value == 5
query2 = query1.filter(expected_result__gte=1)
assert len(query2._where) == 2
op = query2._where[1]
assert isinstance(op.operator, operators.GreaterThanOrEqualOperator)
assert op.value == 1
def test_query_expression_parsing(self):
""" Tests that query experessions are evaluated properly """
query1 = self.table.filter(self.table.column('test_id') == 5)
assert len(query1._where) == 1
op = query1._where[0]
assert isinstance(op.operator, operators.EqualsOperator)
assert op.value == 5
query2 = query1.filter(self.table.column('expected_result') >= 1)
assert len(query2._where) == 2
op = query2._where[1]
assert isinstance(op.operator, operators.GreaterThanOrEqualOperator)
assert op.value == 1
def test_filter_method_where_clause_generation(self):
"""
Tests the where clause creation
"""
query1 = self.table.objects(test_id=5)
self.assertEqual(len(query1._where), 1)
where = query1._where[0]
self.assertEqual(where.field, 'test_id')
self.assertEqual(where.value, 5)
query2 = query1.filter(expected_result__gte=1)
self.assertEqual(len(query2._where), 2)
where = query2._where[0]
self.assertEqual(where.field, 'test_id')
self.assertIsInstance(where.operator, EqualsOperator)
self.assertEqual(where.value, 5)
where = query2._where[1]
self.assertEqual(where.field, 'expected_result')
self.assertIsInstance(where.operator, GreaterThanOrEqualOperator)
self.assertEqual(where.value, 1)
def test_query_expression_where_clause_generation(self):
"""
Tests the where clause creation
"""
query1 = self.table.objects(self.table.column('test_id') == 5)
self.assertEqual(len(query1._where), 1)
where = query1._where[0]
self.assertEqual(where.field, 'test_id')
self.assertEqual(where.value, 5)
query2 = query1.filter(self.table.column('expected_result') >= 1)
self.assertEqual(len(query2._where), 2)
where = query2._where[0]
self.assertEqual(where.field, 'test_id')
self.assertIsInstance(where.operator, EqualsOperator)
self.assertEqual(where.value, 5)
where = query2._where[1]
self.assertEqual(where.field, 'expected_result')
self.assertIsInstance(where.operator, GreaterThanOrEqualOperator)
self.assertEqual(where.value, 1)
class TestQuerySetCountSelectionAndIteration(BaseQuerySetUsage):
@classmethod
def setUpClass(cls):
super(TestQuerySetCountSelectionAndIteration, cls).setUpClass()
from tests.integration.cqlengine.query.test_queryset import TestModel
ks, tn = TestModel.column_family_name().split('.')
cls.keyspace = NamedKeyspace(ks)
cls.table = cls.keyspace.table(tn)
@execute_count(2)
def test_count(self):
""" Tests that adding filtering statements affects the count query as expected """
assert self.table.objects.count() == 12
q = self.table.objects(test_id=0)
assert q.count() == 4
@execute_count(2)
def test_query_expression_count(self):
""" Tests that adding query statements affects the count query as expected """
assert self.table.objects.count() == 12
q = self.table.objects(self.table.column('test_id') == 0)
assert q.count() == 4
@execute_count(3)
def test_iteration(self):
""" Tests that iterating over a query set pulls back all of the expected results """
q = self.table.objects(test_id=0)
# tuple of expected attempt_id, expected_result values
compare_set = set([(0, 5), (1, 10), (2, 15), (3, 20)])
for t in q:
val = t.attempt_id, t.expected_result
assert val in compare_set
compare_set.remove(val)
assert len(compare_set) == 0
# test with regular filtering
q = self.table.objects(attempt_id=3).allow_filtering()
assert len(q) == 3
# tuple of expected test_id, expected_result values
compare_set = set([(0, 20), (1, 20), (2, 75)])
for t in q:
val = t.test_id, t.expected_result
assert val in compare_set
compare_set.remove(val)
assert len(compare_set) == 0
# test with query method
q = self.table.objects(self.table.column('attempt_id') == 3).allow_filtering()
assert len(q) == 3
# tuple of expected test_id, expected_result values
compare_set = set([(0, 20), (1, 20), (2, 75)])
for t in q:
val = t.test_id, t.expected_result
assert val in compare_set
compare_set.remove(val)
assert len(compare_set) == 0
@execute_count(2)
def test_multiple_iterations_work_properly(self):
""" Tests that iterating over a query set more than once works """
# test with both the filtering method and the query method
for q in (self.table.objects(test_id=0), self.table.objects(self.table.column('test_id') == 0)):
# tuple of expected attempt_id, expected_result values
compare_set = set([(0, 5), (1, 10), (2, 15), (3, 20)])
for t in q:
val = t.attempt_id, t.expected_result
assert val in compare_set
compare_set.remove(val)
assert len(compare_set) == 0
# try it again
compare_set = set([(0, 5), (1, 10), (2, 15), (3, 20)])
for t in q:
val = t.attempt_id, t.expected_result
assert val in compare_set
compare_set.remove(val)
assert len(compare_set) == 0
@execute_count(2)
def test_multiple_iterators_are_isolated(self):
"""
tests that the use of one iterator does not affect the behavior of another
"""
for q in (self.table.objects(test_id=0), self.table.objects(self.table.column('test_id') == 0)):
q = q.order_by('attempt_id')
expected_order = [0, 1, 2, 3]
iter1 = iter(q)
iter2 = iter(q)
for attempt_id in expected_order:
assert next(iter1).attempt_id == attempt_id
assert next(iter2).attempt_id == attempt_id
@execute_count(3)
def test_get_success_case(self):
"""
Tests that the .get() method works on new and existing querysets
"""
m = self.table.objects.get(test_id=0, attempt_id=0)
assert isinstance(m, ResultObject)
assert m.test_id == 0
assert m.attempt_id == 0
q = self.table.objects(test_id=0, attempt_id=0)
m = q.get()
assert isinstance(m, ResultObject)
assert m.test_id == 0
assert m.attempt_id == 0
q = self.table.objects(test_id=0)
m = q.get(attempt_id=0)
assert isinstance(m, ResultObject)
assert m.test_id == 0
assert m.attempt_id == 0
@execute_count(3)
def test_query_expression_get_success_case(self):
"""
Tests that the .get() method works on new and existing querysets
"""
m = self.table.get(self.table.column('test_id') == 0, self.table.column('attempt_id') == 0)
assert isinstance(m, ResultObject)
assert m.test_id == 0
assert m.attempt_id == 0
q = self.table.objects(self.table.column('test_id') == 0, self.table.column('attempt_id') == 0)
m = q.get()
assert isinstance(m, ResultObject)
assert m.test_id == 0
assert m.attempt_id == 0
q = self.table.objects(self.table.column('test_id') == 0)
m = q.get(self.table.column('attempt_id') == 0)
assert isinstance(m, ResultObject)
assert m.test_id == 0
assert m.attempt_id == 0
@execute_count(1)
def test_get_doesnotexist_exception(self):
"""
Tests that get calls that don't return a result raises a DoesNotExist error
"""
with self.assertRaises(self.table.DoesNotExist):
self.table.objects.get(test_id=100)
@execute_count(1)
def test_get_multipleobjects_exception(self):
"""
Tests that get calls that return multiple results raise a MultipleObjectsReturned error
"""
with self.assertRaises(self.table.MultipleObjectsReturned):
self.table.objects.get(test_id=1)
class TestNamedWithMV(BasicSharedKeyspaceUnitTestCase):
@classmethod
def setUpClass(cls):
super(TestNamedWithMV, cls).setUpClass()
cls.default_keyspace = models.DEFAULT_KEYSPACE
models.DEFAULT_KEYSPACE = cls.ks_name
@classmethod
def tearDownClass(cls):
models.DEFAULT_KEYSPACE = cls.default_keyspace
super(TestNamedWithMV, cls).tearDownClass()
@greaterthanorequalcass30
@execute_count(5)
def test_named_table_with_mv(self):
"""
Test NamedTable access to materialized views
Creates some materialized views using Traditional CQL. Then ensures we can access those materialized view using
the NamedKeyspace, and NamedTable interfaces. Tests basic filtering as well.
@since 3.0.0
@jira_ticket PYTHON-406
@expected_result Named Tables should have access to materialized views
@test_category materialized_view
"""
ks = models.DEFAULT_KEYSPACE
self.session.execute("DROP MATERIALIZED VIEW IF EXISTS {0}.alltimehigh".format(ks))
self.session.execute("DROP MATERIALIZED VIEW IF EXISTS {0}.monthlyhigh".format(ks))
self.session.execute("DROP TABLE IF EXISTS {0}.scores".format(ks))
create_table = """CREATE TABLE {0}.scores(
user TEXT,
game TEXT,
year INT,
month INT,
day INT,
score INT,
PRIMARY KEY (user, game, year, month, day)
)""".format(ks)
self.session.execute(create_table)
create_mv = """CREATE MATERIALIZED VIEW {0}.monthlyhigh AS
SELECT game, year, month, score, user, day FROM {0}.scores
WHERE game IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL AND day IS NOT NULL
PRIMARY KEY ((game, year, month), score, user, day)
WITH CLUSTERING ORDER BY (score DESC, user ASC, day ASC)""".format(ks)
self.session.execute(create_mv)
create_mv_alltime = """CREATE MATERIALIZED VIEW {0}.alltimehigh AS
SELECT * FROM {0}.scores
WHERE game IS NOT NULL AND score IS NOT NULL AND user IS NOT NULL AND year IS NOT NULL AND month IS NOT NULL AND day IS NOT NULL
PRIMARY KEY (game, score, user, year, month, day)
WITH CLUSTERING ORDER BY (score DESC, user DESC, year DESC, month DESC, day DESC)""".format(ks)
self.session.execute(create_mv_alltime)
# Populate the base table with data
prepared_insert = self.session.prepare("""INSERT INTO {0}.scores (user, game, year, month, day, score) VALUES (?, ?, ? ,? ,?, ?)""".format(ks))
parameters = (('pcmanus', 'Coup', 2015, 5, 1, 4000),
('jbellis', 'Coup', 2015, 5, 3, 1750),
('yukim', 'Coup', 2015, 5, 3, 2250),
('tjake', 'Coup', 2015, 5, 3, 500),
('iamaleksey', 'Coup', 2015, 6, 1, 2500),
('tjake', 'Coup', 2015, 6, 2, 1000),
('pcmanus', 'Coup', 2015, 6, 2, 2000),
('jmckenzie', 'Coup', 2015, 6, 9, 2700),
('jbellis', 'Coup', 2015, 6, 20, 3500),
('jbellis', 'Checkers', 2015, 6, 20, 1200),
('jbellis', 'Chess', 2015, 6, 21, 3500),
('pcmanus', 'Chess', 2015, 1, 25, 3200))
prepared_insert.consistency_level = ConsistencyLevel.ALL
execute_concurrent_with_args(self.session, prepared_insert, parameters)
# Attempt to query the data using Named Table interface
# Also test filtering on mv's
key_space = NamedKeyspace(ks)
mv_monthly = key_space.table("monthlyhigh")
mv_all_time = key_space.table("alltimehigh")
self.assertTrue(self.check_table_size("scores", key_space, len(parameters)))
self.assertTrue(self.check_table_size("monthlyhigh", key_space, len(parameters)))
self.assertTrue(self.check_table_size("alltimehigh", key_space, len(parameters)))
filtered_mv_monthly_objects = mv_monthly.objects.filter(game='Chess', year=2015, month=6)
self.assertEqual(len(filtered_mv_monthly_objects), 1)
self.assertEqual(filtered_mv_monthly_objects[0]['score'], 3500)
self.assertEqual(filtered_mv_monthly_objects[0]['user'], 'jbellis')
filtered_mv_alltime_objects = mv_all_time.objects.filter(game='Chess')
self.assertEqual(len(filtered_mv_alltime_objects), 2)
self.assertEqual(filtered_mv_alltime_objects[0]['score'], 3500)
def check_table_size(self, table_name, key_space, expected_size):
table = key_space.table(table_name)
attempts = 0
while attempts < 10:
attempts += 1
table_size = len(table.objects.all())
if(table_size is not expected_size):
print("Table {0} size was {1} and was expected to be {2}".format(table_name, table_size, expected_size))
else:
return True
return False