Added tests for real.
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -45,6 +45,3 @@ docs/_build
|
||||
|
||||
#iPython
|
||||
*.ipynb
|
||||
|
||||
cqlengine/tests/*
|
||||
cqlengine/tests*
|
||||
30
cqlengine/tests/statements/test_transaction_statement.py
Normal file
30
cqlengine/tests/statements/test_transaction_statement.py
Normal file
@@ -0,0 +1,30 @@
|
||||
__author__ = 'Tim Martin'
|
||||
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
|
||||
78
cqlengine/tests/test_transaction.py
Normal file
78
cqlengine/tests/test_transaction.py
Normal file
@@ -0,0 +1,78 @@
|
||||
__author__ = 'Tim Martin'
|
||||
from cqlengine.management import sync_table, drop_table
|
||||
from cqlengine.tests.base import BaseCassEngTestCase
|
||||
from cqlengine.models import Model
|
||||
from cqlengine.exceptions import TransactionException
|
||||
from uuid import uuid4
|
||||
from cqlengine import columns
|
||||
import mock
|
||||
from cqlengine import ALL, BatchQuery
|
||||
|
||||
|
||||
class TestTransactionModel(Model):
|
||||
__keyspace__ = 'test'
|
||||
id = columns.UUID(primary_key=True, default=lambda:uuid4())
|
||||
count = columns.Integer()
|
||||
text = columns.Text(required=False)
|
||||
|
||||
|
||||
class TestTransaction(BaseCassEngTestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super(TestTransaction, cls).setUpClass()
|
||||
sync_table(TestTransactionModel)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
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()
|
||||
|
||||
args = m.call_args
|
||||
self.assertIn('IF "text" = %(0)s', args[0][0].query_string)
|
||||
|
||||
def test_update_failure(self):
|
||||
t = TestTransactionModel.create(text='blah blah')
|
||||
t.text = 'new blah'
|
||||
t = t.transaction(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')
|
||||
|
||||
args = m.call_args
|
||||
self.assertIn('IF "text" = %(1)s', args[0][0].query_string)
|
||||
|
||||
def test_blind_update_fail(self):
|
||||
t = TestTransactionModel.create(text='blah blah')
|
||||
t.text = 'something else'
|
||||
uid = t.id
|
||||
qs = TestTransactionModel.objects(id=uid).transaction(text='Not dis!')
|
||||
self.assertRaises(TransactionException, qs.update, text='this will never work')
|
||||
Reference in New Issue
Block a user