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
|
# 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.
from unittest import mock
from uuid import uuid4
from cassandra import ConsistencyLevel as CL, ConsistencyLevel
from cassandra.cluster import Session
from cassandra.cqlengine import columns
from cassandra.cqlengine import connection
from cassandra.cqlengine.management import sync_table, drop_table
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.query import BatchQuery
from tests.integration.cqlengine.base import BaseCassEngTestCase
class TestConsistencyModel(Model):
id = columns.UUID(primary_key=True, default=lambda:uuid4())
count = columns.Integer()
text = columns.Text(required=False)
class BaseConsistencyTest(BaseCassEngTestCase):
@classmethod
def setUpClass(cls):
super(BaseConsistencyTest, cls).setUpClass()
sync_table(TestConsistencyModel)
@classmethod
def tearDownClass(cls):
super(BaseConsistencyTest, cls).tearDownClass()
drop_table(TestConsistencyModel)
class TestConsistency(BaseConsistencyTest):
def test_create_uses_consistency(self):
qs = TestConsistencyModel.consistency(CL.ALL)
with mock.patch.object(self.session, 'execute') as m:
qs.create(text="i am not fault tolerant this way")
args = m.call_args
self.assertEqual(CL.ALL, args[0][0].consistency_level)
def test_queryset_is_returned_on_create(self):
qs = TestConsistencyModel.consistency(CL.ALL)
self.assertTrue(isinstance(qs, TestConsistencyModel.__queryset__), type(qs))
def test_update_uses_consistency(self):
t = TestConsistencyModel.create(text="bacon and eggs")
t.text = "ham sandwich"
with mock.patch.object(self.session, 'execute') as m:
t.consistency(CL.ALL).save()
args = m.call_args
self.assertEqual(CL.ALL, args[0][0].consistency_level)
def test_batch_consistency(self):
with mock.patch.object(self.session, 'execute') as m:
with BatchQuery(consistency=CL.ALL) as b:
TestConsistencyModel.batch(b).create(text="monkey")
args = m.call_args
self.assertEqual(CL.ALL, args[0][0].consistency_level)
with mock.patch.object(self.session, 'execute') as m:
with BatchQuery() as b:
TestConsistencyModel.batch(b).create(text="monkey")
args = m.call_args
self.assertNotEqual(CL.ALL, args[0][0].consistency_level)
def test_blind_update(self):
t = TestConsistencyModel.create(text="bacon and eggs")
t.text = "ham sandwich"
uid = t.id
with mock.patch.object(self.session, 'execute') as m:
TestConsistencyModel.objects(id=uid).consistency(CL.ALL).update(text="grilled cheese")
args = m.call_args
self.assertEqual(CL.ALL, args[0][0].consistency_level)
def test_delete(self):
# ensures we always carry consistency through on delete statements
t = TestConsistencyModel.create(text="bacon and eggs")
t.text = "ham and cheese sandwich"
uid = t.id
with mock.patch.object(self.session, 'execute') as m:
t.consistency(CL.ALL).delete()
with mock.patch.object(self.session, 'execute') as m:
TestConsistencyModel.objects(id=uid).consistency(CL.ALL).delete()
args = m.call_args
self.assertEqual(CL.ALL, args[0][0].consistency_level)
def test_default_consistency(self):
# verify global assumed default
self.assertEqual(Session._default_consistency_level, ConsistencyLevel.LOCAL_ONE)
# verify that this session default is set according to connection.setup
# assumes tests/cqlengine/__init__ setup uses CL.ONE
session = connection.get_session()
self.assertEqual(session.default_consistency_level, ConsistencyLevel.ONE)
|