10000 Add a test and docs · datastax/python-driver@d3769c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d3769c3

Browse files
committed
Add a test and docs
1 parent ed76b47 commit d3769c3

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Features
66
--------
77
* Ensure the driver can connect when invalid peer hosts are in system.peers (PYTHON-1260)
88
* Implement protocol v5 checksumming (PYTHON-1258)
9+
* Fix the default cqlengine connection mechanism to work with Astra (PYTHON-1265)
910

1011
Bug Fixes
1112
---------

cassandra/cqlengine/connection.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ def setup(self):
9999
return
100100

101101
if 'cloud' in self.cluster_options:
102+
if self.hosts:
103+
log.warning("Ignoring hosts %s because a cloud config was provided.", self.hosts)
102104
self.cluster = Cluster(**self.cluster_options)
103105
else:
104106
self.cluster = Cluster(self.hosts, **self.cluster_options)
@@ -305,6 +307,8 @@ def set_session(s):
305307
log.debug("cqlengine default connection initialized with %s", s)
306308

307309

310+
# TODO next major: if a cloud config is specified in kwargs, hosts will be ignored.
311+
# This function should be refactored to reflect this change. PYTHON-1265
308312
def setup(
309313
hosts,
310314
default_keyspace,

docs/cloud.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,36 @@ Limitations
5454
Event loops
5555
^^^^^^^^^^^
5656
Evenlet isn't yet supported for python 3.7+ due to an `issue in Eventlet <https://github.com/eventlet/eventlet/issues/526>`_.
57+
58+
59+
CqlEngine
60+
=========
61+
62+
When using the object mapper, you can configure cqlengine with :func:`~.cqlengine.connection.set_session`:
63+
64+
.. code:: python
65+
66+
from cassandra.cqlengine import connection
67+
...
68+
69+
c = Cluster(cloud={'secure_connect_bundle':'/path/to/secure-connect-test.zip'},
70+
auth_provider=PlainTextAuthProvider('user', 'pass'))
71+
s = c.connect('myastrakeyspace')
72+
connection.set_session(s)
73+
...
74+
75+
If you are using some third-party libraries (flask, django, etc.), you might not be able to change the
76+
configuration mechanism. For this reason, the `hosts` argument of the default
77+
:func:`~.cqlengine.connection.setup` function will be ignored if a `cloud` config is provided:
78+
79+
.. code:: python
80+
81+
from cassandra.cqlengine import connection
82+
...
83+
84+
connection.setup(
85+
None, # or anything else
86+
"myastrakeyspace", cloud={
87+
'secure_connect_bundle':'/path/to/secure-connect-test.zip'
88+
},
89+
auth_provider=PlainTextAuthProvider('user', 'pass'))

tests/integration/cloud/test_cloud.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
# limitations under the License
1414
from cassandra.datastax.cloud import parse_metadata_info
1515
from cassandra.query import SimpleStatement
16+
from cassandra.cqlengine import connection
17+
from cassandra.cqlengine.management import sync_table, create_keyspace_simple
18+
from cassandra.cqlengine.models import Model
19+
from cassandra.cqlengine import columns
1620

1721
try:
1822
import unittest2 as unittest
@@ -30,7 +34,7 @@
3034

3135
from mock import patch
3236

33-
from tests.integration import requirescloudproxy, TestCluster
37+
from tests.integration import requirescloudproxy
3438
from tests.util import wait_until_not_raised
3539
from tests.integration.cloud import CloudProxyCluster, CLOUD_PROXY_SERVER
3640

@@ -143,7 +147,7 @@ def test_resolve_and_reconnect_on_node_down(self):
143147
wait_until_not_raised(
144148
lambda: self.assertEqual(len(self.hosts_up()), 3),
145149
0.02, 250)
146-
mocked_resolve.assert_called_once()
150+
mocked_resolve.assert_called()
147151

148152
def test_metadata_unreachable(self):
149153
with self.assertRaises(DriverException) as cm:
@@ -234,3 +238,14 @@ def test_consistency_guardrails(self):
234238
self.session.execute(statement)
235239
except InvalidRequest:
236240
self.fail("InvalidRequest was incorrectly raised for write query at LOCAL QUORUM!")
241+
242+
def test_cqlengine_can_connect(self):
243+
class TestModel(Model):
244+
id = columns.Integer(primary_key=True)
245+
val = columns.Text()
246+
247+
connection.setup(None, "test", cloud={'secure_connect_bundle': self.creds})
248+
create_keyspace_simple('test', 1)
249+
sync_table(TestModel)
250+
TestModel.objects.create(id=42, value='test')
251+
self.assertEqual(len(TestModel.objects.all()), 1)

0 commit comments

Comments
 (0)
0