Converted .transaction() to .iff()
This commit is contained in:
@@ -84,7 +84,7 @@ class TransactionDescriptor(object):
|
||||
if len(prepared_transaction) > 0:
|
||||
transactions = prepared_transaction[0]
|
||||
else:
|
||||
transactions = instance.objects.transaction(**unprepared_transactions)._transaction
|
||||
transactions = instance.objects.iff(**unprepared_transactions)._transaction
|
||||
instance._transaction = transactions
|
||||
return instance
|
||||
|
||||
@@ -92,7 +92,7 @@ class TransactionDescriptor(object):
|
||||
qs = model.__queryset__(model)
|
||||
|
||||
def transaction_setter(**unprepared_transactions):
|
||||
transactions = model.objects.transaction(**unprepared_transactions)._transaction
|
||||
transactions = model.objects.iff(**unprepared_transactions)._transaction
|
||||
qs._transaction = transactions
|
||||
return qs
|
||||
return transaction_setter
|
||||
|
||||
@@ -25,7 +25,7 @@ import six
|
||||
def check_applied(result):
|
||||
"""
|
||||
check if result contains some column '[applied]' with false value,
|
||||
if that value is false, it means our light-weight transaction didn't
|
||||
if that value is false, it means our light-weight iff didn't
|
||||
applied to database.
|
||||
"""
|
||||
if result and '[applied]' in result[0] and result[0]['[applied]'] == False:
|
||||
@@ -92,7 +92,7 @@ class BatchQuery(object):
|
||||
:param batch_type: (optional) One of batch type values available through BatchType enum
|
||||
:type batch_type: str or None
|
||||
:param timestamp: (optional) A datetime or timedelta object with desired timestamp to be applied
|
||||
to the batch transaction.
|
||||
to the batch iff.
|
||||
:type timestamp: datetime or timedelta or None
|
||||
:param consistency: (optional) One of consistency values ("ANY", "ONE", "QUORUM" etc)
|
||||
:type consistency: str or None
|
||||
@@ -409,10 +409,10 @@ class AbstractQuerySet(object):
|
||||
else:
|
||||
raise QueryException("Can't parse '{}'".format(arg))
|
||||
|
||||
def transaction(self, *args, **kwargs):
|
||||
def iff(self, *args, **kwargs):
|
||||
"""Adds IF statements to queryset"""
|
||||
if len([x for x in kwargs.values() if x is None]):
|
||||
raise CQLEngineException("None values on transaction are not allowed")
|
||||
raise CQLEngineException("None values on iff are not allowed")
|
||||
|
||||
clone = copy.deepcopy(self)
|
||||
for operator in args:
|
||||
@@ -425,9 +425,7 @@ class AbstractQuerySet(object):
|
||||
try:
|
||||
column = self.model._get_column(col_name)
|
||||
except KeyError:
|
||||
if col_name == 'not_exists':
|
||||
exists = True
|
||||
elif col_name == 'pk__token':
|
||||
if col_name == 'pk__token':
|
||||
if not isinstance(val, Token):
|
||||
raise QueryException("Virtual column 'pk__token' may only be compared to Token() values")
|
||||
column = columns._PartitionKeysToken(self.model)
|
||||
|
||||
@@ -140,7 +140,7 @@ class AssignmentClause(BaseClause):
|
||||
|
||||
|
||||
class TransactionClause(BaseClause):
|
||||
""" A single variable transaction statement """
|
||||
""" A single variable iff statement """
|
||||
|
||||
def __unicode__(self):
|
||||
return u'"{}" = %({})s'.format(self.field, self.context_id)
|
||||
@@ -694,7 +694,7 @@ class UpdateStatement(AssignmentStatement):
|
||||
ttl=ttl,
|
||||
timestamp=timestamp)
|
||||
|
||||
# Add transaction statements
|
||||
# Add iff statements
|
||||
self.transactions = []
|
||||
for transaction in transactions or []:
|
||||
self.add_transaction_clause(transaction)
|
||||
@@ -726,9 +726,9 @@ class UpdateStatement(AssignmentStatement):
|
||||
|
||||
def add_transaction_clause(self, clause):
|
||||
"""
|
||||
Adds a transaction clause to this statement
|
||||
Adds a iff clause to this statement
|
||||
|
||||
:param clause: The clause that will be added to the transaction statement
|
||||
:param clause: The clause that will be added to the iff statement
|
||||
:type clause: TransactionClause
|
||||
"""
|
||||
if not isinstance(clause, TransactionClause):
|
||||
|
||||
@@ -3,28 +3,12 @@ from unittest import TestCase
|
||||
from cqlengine.statements import TransactionClause
|
||||
import six
|
||||
|
||||
|
||||
class TestTransactionClause(TestCase):
|
||||
|
||||
def test_not_exists_clause(self):
|
||||
tc = TransactionClause('not_exists', True)
|
||||
|
||||
self.assertEqual('NOT EXISTS', six.text_type(tc))
|
||||
self.assertEqual('NOT EXISTS', str(tc))
|
||||
|
||||
def test_normal_transaction(self):
|
||||
tc = TransactionClause('some_value', 23)
|
||||
tc.set_context_id(3)
|
||||
|
||||
self.assertEqual('"some_value" = %(3)s', six.text_type(tc))
|
||||
self.assertEqual('"some_value" = %(3)s', str(tc))
|
||||
|
||||
def test_equality(self):
|
||||
tc1 = TransactionClause('some_value', 5)
|
||||
tc2 = TransactionClause('some_value', 5)
|
||||
|
||||
assert tc1 == tc2
|
||||
|
||||
tc3 = TransactionClause('not_exists', True)
|
||||
tc4 = TransactionClause('not_exists', True)
|
||||
|
||||
assert tc3 == tc4
|
||||
self.assertEqual('"some_value" = %(3)s', str(tc))
|
||||
@@ -28,22 +28,11 @@ class TestTransaction(BaseCassEngTestCase):
|
||||
super(TestTransaction, cls).tearDownClass()
|
||||
drop_table(TestTransactionModel)
|
||||
|
||||
def test_create_uses_transaction(self):
|
||||
qs = TestTransactionModel.transaction(not_exists=True)
|
||||
with mock.patch.object(self.session, 'execute') as m:
|
||||
qs.create(text='blah blah', count=2)
|
||||
args = m.call_args
|
||||
self.assertIn('IF NOT EXISTS', args[0][0].query_string)
|
||||
|
||||
def test_queryset_returned_on_create(self):
|
||||
qs = TestTransactionModel.transaction(not_exists=True)
|
||||
self.assertTrue(isinstance(qs, TestTransactionModel.__queryset__), type(qs))
|
||||
|
||||
def test_update_using_transaction(self):
|
||||
t = TestTransactionModel.create(text='blah blah')
|
||||
t.text = 'new blah'
|
||||
with mock.patch.object(self.session, 'execute') as m:
|
||||
t.transaction(text='blah blah').save()
|
||||
t.iff(text='blah blah').save()
|
||||
|
||||
args = m.call_args
|
||||
self.assertIn('IF "text" = %(0)s', args[0][0].query_string)
|
||||
@@ -51,21 +40,16 @@ class TestTransaction(BaseCassEngTestCase):
|
||||
def test_update_failure(self):
|
||||
t = TestTransactionModel.create(text='blah blah')
|
||||
t.text = 'new blah'
|
||||
t = t.transaction(text='something wrong')
|
||||
t = t.iff(text='something wrong')
|
||||
self.assertRaises(TransactionException, t.save)
|
||||
|
||||
def test_creation_failure(self):
|
||||
t = TestTransactionModel.create(text='blah blah')
|
||||
t_clone = TestTransactionModel.transaction(not_exists=True)
|
||||
self.assertRaises(TransactionException, t_clone.create, id=t.id, count=t.count, text=t.text)
|
||||
|
||||
def test_blind_update(self):
|
||||
t = TestTransactionModel.create(text='blah blah')
|
||||
t.text = 'something else'
|
||||
uid = t.id
|
||||
|
||||
with mock.patch.object(self.session, 'execute') as m:
|
||||
TestTransactionModel.objects(id=uid).transaction(text='blah blah').update(text='oh hey der')
|
||||
TestTransactionModel.objects(id=uid).iff(text='blah blah').update(text='oh hey der')
|
||||
|
||||
args = m.call_args
|
||||
self.assertIn('IF "text" = %(1)s', args[0][0].query_string)
|
||||
@@ -74,5 +58,5 @@ class TestTransaction(BaseCassEngTestCase):
|
||||
t = TestTransactionModel.create(text='blah blah')
|
||||
t.text = 'something else'
|
||||
uid = t.id
|
||||
qs = TestTransactionModel.objects(id=uid).transaction(text='Not dis!')
|
||||
qs = TestTransactionModel.objects(id=uid).iff(text='Not dis!')
|
||||
self.assertRaises(TransactionException, qs.update, text='this will never work')
|
||||
Reference in New Issue
Block a user