Skip to content

Commit 2ba9a99

Browse files
timmartin19timmartin19
authored andcommitted
Added documentation. Changed .transaction() to .iff(), removed
TransactionException and replaced with LWTException
1 parent e7182df commit 2ba9a99

7 files changed

Lines changed: 20 additions & 22 deletions

File tree

cqlengine/connection.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections import namedtuple
66
from cassandra.cluster import Cluster, NoHostAvailable
77
from cassandra.query import SimpleStatement, Statement
8-
from cqlengine.exceptions import TransactionException
8+
from cqlengine.exceptions import LWTException
99
import six
1010

1111
try:
@@ -112,12 +112,6 @@ def execute(query, params=None, consistency_level=None):
112112
params = params or {}
113113
result = session.execute(query, params)
114114

115-
if result and result[0].get('[applied]', True) is False:
116-
result[0].pop('[applied]')
117-
expected = ', '.join('{0}={1}'.format(t.field, t.value) for t in statement.transactions)
118-
actual = ', '.join('{0}={1}'.format(f, v) for f, v in result[0].items())
119-
message = 'Transaction statement failed: Expected: {0} Actual: {1}'.format(expected, actual)
120-
raise TransactionException(message)
121115
return result
122116

123117
def get_session():

cqlengine/exceptions.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
class CQLEngineException(Exception): pass
33
class ModelException(CQLEngineException): pass
44
class ValidationError(CQLEngineException): pass
5-
class TransactionException(CQLEngineException): pass
65

76
class UndefinedKeyspaceException(CQLEngineException): pass
87
class LWTException(CQLEngineException): pass

cqlengine/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class MultipleObjectsReturned(_MultipleObjectsReturned): pass
266266
objects = QuerySetDescriptor()
267267
ttl = TTLDescriptor()
268268
consistency = ConsistencyDescriptor()
269-
transaction = TransactionDescriptor()
269+
iff = TransactionDescriptor()
270270

271271
# custom timestamps, see USING TIMESTAMP X
272272
timestamp = TimestampDescriptor()

cqlengine/query.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from cqlengine.columns import Counter, List, Set
66

77
from cqlengine.connection import execute
8-
from cqlengine.exceptions import CQLEngineException, ValidationError, TransactionException, LWTException
8+
from cqlengine.exceptions import CQLEngineException, ValidationError, LWTException
99
from cqlengine.functions import Token, BaseQueryFunction, QueryValue, UnicodeMixin
1010

1111
#CQL 3 reference:
@@ -849,14 +849,8 @@ def _execute(self, q):
849849
return self._batch.add_query(q)
850850
else:
851851
tmp = execute(q, consistency_level=self._consistency)
852-
if self._if_not_exists:
852+
if self._if_not_exists or self._transaction:
853853
check_applied(tmp)
854-
if self._transaction and tmp[0].get('[applied]', True) is False:
855-
tmp[0].pop('[applied]')
856-
expected = ', '.join('{0}={1}'.format(t.field, t.value) for t in q.transactions)
857-
actual = ', '.join('{0}={1}'.format(f, v) for f, v in tmp[0].items())
858-
message = 'Transaction statement failed: Expected: {0} Actual: {1}'.format(expected, actual)
859-
raise TransactionException(message)
860854
return tmp
861855

862856
def batch(self, batch_obj):
@@ -966,7 +960,7 @@ def save(self):
966960
if self.instance._has_counter or self.instance._can_update():
967961
return self.update()
968962
else:
969-
insert = InsertStatement(self.column_family_name, ttl=self._ttl, timestamp=self._timestamp, if_not_exists=self._if_not_exists, transactions=self._transaction)
963+
insert = InsertStatement(self.column_family_name, ttl=self._ttl, timestamp=self._timestamp, if_not_exists=self._if_not_exists)
970964
for name, col in self.instance._columns.items():
971965
val = getattr(self.instance, name, None)
972966
if col._val_is_null(val):

cqlengine/tests/base.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
import sys
44
import six
55
from cqlengine.connection import get_session
6+
from cqlengine import connection
67

7-
CASSANDRA_VERSION = int(os.environ['CASSANDRA_VERSION'])
8+
CASSANDRA_VERSION = 20 #int(os.environ['CASSANDRA_VERSION'])
9+
10+
connection.setup(['192.168.56.103'], 'cqlengine_test')
811

912
class BaseCassEngTestCase(TestCase):
1013

cqlengine/tests/test_transaction.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from cqlengine.management import sync_table, drop_table
33
from cqlengine.tests.base import BaseCassEngTestCase
44
from cqlengine.models import Model
5-
from cqlengine.exceptions import TransactionException
5+
from cqlengine.exceptions import LWTException
66
from uuid import uuid4
77
from cqlengine import columns
88
import mock
@@ -41,7 +41,7 @@ def test_update_failure(self):
4141
t = TestTransactionModel.create(text='blah blah')
4242
t.text = 'new blah'
4343
t = t.iff(text='something wrong')
44-
self.assertRaises(TransactionException, t.save)
44+
self.assertRaises(LWTException, t.save)
4545

4646
def test_blind_update(self):
4747
t = TestTransactionModel.create(text='blah blah')
@@ -59,4 +59,4 @@ def test_blind_update_fail(self):
5959
t.text = 'something else'
6060
uid = t.id
6161
qs = TestTransactionModel.objects(id=uid).iff(text='Not dis!')
62-
self.assertRaises(TransactionException, qs.update, text='this will never work')
62+
self.assertRaises(LWTException, qs.update, text='this will never work')

docs/topics/models.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ Model Methods
207207
208208
This method is supported on Cassandra 2.0 or later.
209209

210+
.. method:: iff(**values)
211+
212+
Checks to ensure that the values specified are correct on the Cassandra cluster.
213+
Simply specify the column(s) and the expected value(s). As with if_not_exists,
214+
this incurs a performance cost.
215+
216+
If the insertion isn't applied, a LWTException is raised
217+
210218
.. method:: update(**values)
211219

212220
Performs an update on the model instance. You can pass in values to set on the model

0 commit comments

Comments
 (0)