Skip to content

Commit dd4df43

Browse files
committed
cqle: make default CL based on core default
+ test updates PYTHON-416
1 parent 6255891 commit dd4df43

4 files changed

Lines changed: 19 additions & 29 deletions

File tree

cassandra/cqlengine/connection.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import logging
1717
import six
1818

19-
from cassandra import ConsistencyLevel
2019
from cassandra.cluster import Cluster, _NOT_SET, NoHostAvailable, UserTypeDoesNotExist
2120
from cassandra.query import SimpleStatement, Statement, dict_factory
2221

@@ -33,7 +32,6 @@
3332
cluster = None
3433
session = None
3534
lazy_connect_args = None
36-
default_consistency_level = ConsistencyLevel.LOCAL_QUORUM
3735

3836

3937
# Because type models may be registered before a connection is present,
@@ -95,7 +93,7 @@ def set_session(s):
9593
def setup(
9694
hosts,
9795
default_keyspace,
98-
consistency=ConsistencyLevel.ONE,
96+
consistency=None,
9997
lazy_connect=False,
10098
retry_connect=False,
10199
**kwargs):
@@ -104,20 +102,19 @@ def setup(
104102
105103
:param list hosts: list of hosts, (``contact_points`` for :class:`cassandra.cluster.Cluster`)
106104
:param str default_keyspace: The default keyspace to use
107-
:param int consistency: The global default :class:`~.ConsistencyLevel`
105+
:param int consistency: The global default :class:`~.ConsistencyLevel` - default is the same as :attr:`.Session.default_consistency_level`
108106
:param bool lazy_connect: True if should not connect until first use
109107
:param bool retry_connect: True if we should retry to connect even if there was a connection failure initially
110108
:param \*\*kwargs: Pass-through keyword arguments for :class:`cassandra.cluster.Cluster`
111109
"""
112-
global cluster, session, default_consistency_level, lazy_connect_args
110+
global cluster, session, lazy_connect_args
113111

114112
if 'username' in kwargs or 'password' in kwargs:
115113
raise CQLEngineException("Username & Password are now handled by using the native driver's auth_provider")
116114

117115
from cassandra.cqlengine import models
118116
models.DEFAULT_KEYSPACE = default_keyspace
119117

120-
default_consistency_level = consistency
121118
if lazy_connect:
122119
kwargs['default_keyspace'] = default_keyspace
123120
kwargs['consistency'] = consistency
@@ -139,6 +136,8 @@ def setup(
139136
kwargs['retry_connect'] = retry_connect
140137
lazy_connect_args = (hosts, kwargs)
141138
raise
139+
if consistency is not None:
140+
session.default_consistency_level = consistency
142141
session.row_factory = dict_factory
143142

144143
_register_known_types(cluster)
@@ -151,9 +150,6 @@ def execute(query, params=None, consistency_level=None, timeout=NOT_SET):
151150
if not session:
152151
raise CQLEngineException("It is required to setup() cqlengine before executing queries")
153152

154-
if consistency_level is None:
155-
consistency_level = default_consistency_level
156-
157153
if isinstance(query, Statement):
158154
pass
159155

tests/integration/cqlengine/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import os
1616
import warnings
17+
from cassandra import ConsistencyLevel
1718

1819
from cassandra.cqlengine import connection
1920
from cassandra.cqlengine.management import create_keyspace_simple, CQLENG_ALLOW_SCHEMA_MANAGEMENT
@@ -29,6 +30,7 @@ def setup_package():
2930

3031
keyspace = 'cqlengine_test'
3132
connection.setup(['127.0.0.1'],
33+
consistency=ConsistencyLevel.ONE,
3234
protocol_version=PROTOCOL_VERSION,
3335
default_keyspace=keyspace)
3436

tests/integration/cqlengine/test_consistency.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import mock
1616
from uuid import uuid4
1717

18-
from cassandra import ConsistencyLevel as CL
18+
from cassandra import ConsistencyLevel as CL, ConsistencyLevel
19+
from cassandra.cluster import Session
1920
from cassandra.cqlengine import columns
21+
from cassandra.cqlengine import connection
2022
from cassandra.cqlengine.management import sync_table, drop_table
2123
from cassandra.cqlengine.models import Model
2224
from cassandra.cqlengine.query import BatchQuery
@@ -110,3 +112,12 @@ def test_delete(self):
110112

111113
args = m.call_args
112114
self.assertEqual(CL.ALL, args[0][0].consistency_level)
115+
116+
def test_default_consistency(self):
117+
# verify global assumed default
118+
self.assertEqual(Session.default_consistency_level, ConsistencyLevel.LOCAL_QUORUM)
119+
120+
# verify that this session default is set according to connection.setup
121+
# assumes tests/cqlengine/__init__ setup uses CL.ONE
122+
session = connection.get_session()
123+
self.assertEqual(session.default_consistency_level, ConsistencyLevel.ONE)

tests/unit/test_connection.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from cassandra.protocol import (write_stringmultimap, write_int, write_string,
3030
SupportedMessage, ProtocolHandler)
3131

32-
import cassandra.cqlengine.connection
3332

3433
class ConnectionTest(unittest.TestCase):
3534

@@ -414,21 +413,3 @@ def send_msg(msg, req_id, msg_callback):
414413
self.assertIsInstance(exc, Exception)
415414
self.assertEqual(exc.args, Exception('Connection heartbeat failure').args)
416415
holder.return_connection.assert_has_calls([call(connection)] * get_holders.call_count)
417-
418-
419-
class ConnectionDefaultTest(unittest.TestCase):
420-
"""
421-
Test to ensure object mapper and base driver default cl's are the same.
422-
423-
424-
@since 3.0.0
425-
@jira_ticket PYTHON-416
426-
@expected_result cl's matchy between object mapper and base driver.
427-
428-
@test_category consistency
429-
"""
430-
def test_default_cl(self, *args):
431-
base_driver_cl = Session.default_consistency_level
432-
cqlengine_cl = cassandra.cqlengine.connection.default_consistency_level
433-
self.assertEqual(base_driver_cl, cqlengine_cl)
434-

0 commit comments

Comments
 (0)