-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_verify.py
390 lines (330 loc) · 13.6 KB
/
test_verify.py
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
388
389
from datetime import date, datetime
import unittest
import uuid
from uuid import UUID
from cqlengine import columns
import cqlengine.connection
from cqlengine.connection import get_cluster, setup
from cqlengine.management import create_keyspace, delete_keyspace, sync_table
from cqlengine.models import Model
from cqlengine.query import DoesNotExist
from cqlengine_session import verify
def make_model(table_name, skip={}, different={}, index={'text_index': True}):
def get_col(name, col, args=(), kwargs={}):
if name in skip:
return None
if name in different:
return different[name]
if name in index:
return col(*args, index=True, **kwargs)
else:
return col(*args, **kwargs)
class TestTable(Model):
__table_name__ = table_name
partition = columns.Text(primary_key=True)
uuida = columns.UUID(primary_key=True, default=uuid.uuid4)
uuidb = get_col('uuidb', columns.UUID, kwargs={'primary_key':True, 'default':uuid.uuid4})
uuidc = columns.UUID(primary_key=True, default=uuid.uuid4)
uuidd = get_col('uuidd', columns.UUID, kwargs={'primary_key':True, 'partition_key':True, 'default':uuid.uuid4})
uuide = get_col('uuide', columns.UUID, kwargs={'primary_key':True, 'partition_key':True, 'default':uuid.uuid4})
uuidf = get_col('uuidf', columns.UUID, kwargs={'primary_key':True, 'partition_key':True, 'default':uuid.uuid4})
title = get_col('title', columns.Text)
text_index = get_col('text_index', columns.Text)
done = columns.Boolean()
pub_date = columns.DateTime()
# One column type of each type that has its own validation method.
bytes = columns.Bytes()
ascii = columns.Ascii()
text = columns.Text()
integer = columns.Integer()
bigint = columns.BigInt()
varint = columns.VarInt()
uuid2 = columns.UUID()
float = columns.Float()
decimal = columns.Decimal()
date = columns.Date()
datetime = columns.DateTime()
timeuuid = columns.TimeUUID()
boolean = columns.Boolean()
setcol = columns.Set(columns.Integer)
listcol = columns.List(columns.Integer)
mapcol = columns.Map(columns.Text, columns.Integer)
return TestTable
def make_counter_model(table_name, skip={}, different={}, index={'text_index': True}):
def get_col(name, col, args=(), kwargs={}):
if name in skip:
return None
if name in different:
return different[name]
if name in index:
return col(*args, index=True, **kwargs)
else:
return col(*args, **kwargs)
class TestCountTable(Model):
__table_name__ = table_name
uuid = columns.UUID(primary_key=True, default=uuid.uuid4)
count1 = get_col('count1', columns.Counter)
count2 = get_col('count2', columns.Counter)
count3 = get_col('count3', columns.Counter)
return TestCountTable
class VerifyTest(unittest.TestCase):
def setUp(self):
keyspace = 'testkeyspace{}'.format(str(uuid.uuid1()).replace('-', ''))
self.keyspace = keyspace
# Configure cqlengine's global connection pool.
setup(['localhost'], default_keyspace=keyspace)
create_keyspace(keyspace)
def tearDown(self):
delete_keyspace(self.keyspace)
get_cluster().shutdown()
def test_has_extra_field(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', skip=set(['title']))
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.missing
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
assert len(result.extra) == 1
assert 'title' in result.extra
def test_has_missing_field(self):
Foo = make_model(table_name='foo_bar', skip=set(['title']))
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar')
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.is_missing
assert not result.extra
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
assert len(result.missing) == 1
assert 'title' in result.missing
def test_has_extra_primary_key_field(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', skip=set(['uuidb']))
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.missing
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
assert len(result.extra) == 1
assert 'uuidb' in result.extra
def test_has_missing_primary_key_field(self):
Foo = make_model(table_name='foo_bar', skip=set(['uuidb']))
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar')
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.is_missing
assert not result.extra
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
assert len(result.missing) == 1
assert 'uuidb' in result.missing
def test_has_different_primary_key(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', different={'uuidb': columns.Ascii(primary_key=True, default=uuid.uuid4)})
results = verify(Foo2)
assert len(results) == 1
result = results[0]
assert not result.extra
assert not result.missing
print result.different
assert len(result.different) == 1
assert 'uuidb' in result.different
def test_has_extra_partition_key_field(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', skip=set(['uuide']))
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.missing
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
assert len(result.extra) == 1
assert 'uuide' in result.extra
def test_has_missing_partition_key_field(self):
Foo = make_model(table_name='foo_bar', skip=set(['uuide']))
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar')
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.is_missing
assert not result.extra
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
assert len(result.missing) == 1
assert 'uuide' in result.missing
def test_has_different_partition_key(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', different={'uuide': columns.Ascii(primary_key=True, partition_key=True, default=uuid.uuid4)})
results = verify(Foo2)
assert len(results) == 1
result = results[0]
assert not result.extra
assert not result.missing
assert len(result.different) == 1
assert 'uuide' in result.different
def test_has_extra_single_partition_key_field(self):
Foo = make_model(table_name='foo_bar', skip={'uuidd', 'uuidf'})
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', skip={'uuidd', 'uuide', 'uuidf'})
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
# Note that 'partition' will be 'missing' too because Foo.partition
# gets a default 'partition_key=True' when all the explicit
# partition_keys are skipped. So, the verify will report a partition
# key 'partition'.
# When uuide is not skipped, Foo2.partition is not a partition key.
# When verifying Foo2 against Foo's schema partition will show up
# as 'missing' because it is a missing partition_key (not a missing
# column.)
assert len(result.missing) == 1
assert 'partition' in result.missing
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
assert len(result.extra) == 1
assert 'uuide' in result.extra
def test_has_missing_single_partition_key_field(self):
Foo = make_model(table_name='foo_bar', skip={'uuidd', 'uuide', 'uuidf'})
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', skip={'uuidd', 'uuidf'})
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.is_missing
assert not result.extra
assert not result.different
assert not result.missing_indexes
assert not result.extra_indexes
# Note that 'partition' will be 'missing' too because Foo.partition
# gets a default 'partition_key=True' when all the explicit
# partition_keys are skipped. So, the verify will report a partition
# key 'partition'.
# When uuide is not skipped, Foo2.partition is not a partition key.
# When verifying Foo2 against Foo's schema partition will show up
# as 'missing' because it is a missing partition_key (not a missing
# column.)
assert len(result.missing) == 2
assert 'uuide' in result.missing
assert 'partition' in result.missing
def test_has_different_single_partition_key(self):
Foo = make_model(table_name='foo_bar', skip={'uuidd', 'uuidf'})
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar',
skip={'uuidd', 'uuidf'},
different={
'uuide': columns.Ascii(primary_key=True,
partition_key=True,
default=uuid.uuid4)})
results = verify(Foo2)
assert len(results) == 1
result = results[0]
assert not result.extra
assert not result.missing
assert len(result.different) == 1
assert 'uuide' in result.different
def test_has_ok_index(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
results = verify(Foo)
print [result.report() for result in results]
assert len(results) == 0
def test_has_extra_index(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', index={})
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.missing
assert not result.extra
assert not result.different
assert not result.missing_indexes
assert len(result.extra_indexes) == 1
assert 'text_index' in result.extra_indexes
def test_has_missing_index(self):
Foo = make_model(table_name='foo_bar', index={})
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar')
results = verify(Foo2)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert not result.is_missing
assert not result.missing
assert not result.extra
assert not result.different
assert not result.extra_indexes
assert len(result.missing_indexes) == 1
assert 'text_index' in result.missing_indexes
def test_has_different(self):
Foo = make_model(table_name='foo_bar')
sync_table(Foo)
Foo2 = make_model(table_name='foo_bar', different={'title': columns.Ascii()})
results = verify(Foo2)
assert len(results) == 1
result = results[0]
assert not result.extra
assert not result.missing
assert len(result.different) == 1
assert 'title' in result.different
def test_has_two(self):
Foo = make_model(table_name='foo_bar')
Bar = make_model(table_name='Bar')
sync_table(Foo)
sync_table(Bar)
results = verify(Foo, Bar)
assert not results
def test_has_extra_cf(self):
Foo = make_model(table_name='foo_bar')
Bar = make_model(table_name='baz_qux')
sync_table(Foo)
sync_table(Bar)
results = verify(Foo)
[result.report() for result in results]
assert len(results) == 1
result = results[0]
assert result.model == u'baz_qux'
assert result.is_extra
def test_has_missing_cf(self):
Foo = make_model(table_name='foo_bar')
Bar = make_model(table_name='baz_qux')
sync_table(Foo)
results = verify(Foo, Bar)
[result.report() for result in results]
assert len(results) == 1
bar_result = results[0]
assert bar_result.is_missing
def test_counter_verify(self):
Foo = make_counter_model(table_name='foo_bar')
sync_table(Foo)
results = verify(Foo)
assert len(results) == 0