Added tests for real.

This commit is contained in:
timmartin19
2014-10-16 13:32:02 -04:00
parent de18d56296
commit 281fb84a15
4 changed files with 286 additions and 3 deletions

3
.gitignore vendored
View File

@@ -45,6 +45,3 @@ docs/_build
#iPython
*.ipynb
cqlengine/tests/*
cqlengine/tests*

View 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

View File

@@ -0,0 +1,178 @@
from unittest import skipUnless
from cqlengine.management import sync_table, drop_table, create_keyspace, delete_keyspace
from cqlengine.tests.base import BaseCassEngTestCase
from cqlengine.models import Model
from cqlengine.exceptions import LWTException
from cqlengine import columns, BatchQuery
from uuid import uuid4
import mock
from cqlengine.connection import get_cluster
cluster = get_cluster()
class TestIfNotExistsModel(Model):
__keyspace__ = 'cqlengine_test_lwt'
id = columns.UUID(primary_key=True, default=lambda:uuid4())
count = columns.Integer()
text = columns.Text(required=False)
class BaseIfNotExistsTest(BaseCassEngTestCase):
@classmethod
def setUpClass(cls):
super(BaseIfNotExistsTest, cls).setUpClass()
"""
when receiving an insert statement with 'if not exist', cassandra would
perform a read with QUORUM level. Unittest would be failed if replica_factor
is 3 and one node only. Therefore I have create a new keyspace with
replica_factor:1.
"""
create_keyspace(TestIfNotExistsModel.__keyspace__, replication_factor=1)
sync_table(TestIfNotExistsModel)
@classmethod
def tearDownClass(cls):
super(BaseCassEngTestCase, cls).tearDownClass()
drop_table(TestIfNotExistsModel)
delete_keyspace(TestIfNotExistsModel.__keyspace__)
class IfNotExistsInsertTests(BaseIfNotExistsTest):
@skipUnless(cluster.protocol_version >= 2, "only runs against the cql3 protocol v2.0")
def test_insert_if_not_exists_success(self):
""" tests that insertion with if_not_exists work as expected """
id = uuid4()
TestIfNotExistsModel.create(id=id, count=8, text='123456789')
self.assertRaises(
LWTException,
TestIfNotExistsModel.if_not_exists().create, id=id, count=9, text='111111111111'
)
q = TestIfNotExistsModel.objects(id=id)
self.assertEqual(len(q), 1)
tm = q.first()
self.assertEquals(tm.count, 8)
self.assertEquals(tm.text, '123456789')
def test_insert_if_not_exists_failure(self):
""" tests that insertion with if_not_exists failure """
id = uuid4()
TestIfNotExistsModel.create(id=id, count=8, text='123456789')
TestIfNotExistsModel.create(id=id, count=9, text='111111111111')
q = TestIfNotExistsModel.objects(id=id)
self.assertEquals(len(q), 1)
tm = q.first()
self.assertEquals(tm.count, 9)
self.assertEquals(tm.text, '111111111111')
@skipUnless(cluster.protocol_version >= 2, "only runs against the cql3 protocol v2.0")
def test_batch_insert_if_not_exists_success(self):
""" tests that batch insertion with if_not_exists work as expected """
id = uuid4()
with BatchQuery() as b:
TestIfNotExistsModel.batch(b).if_not_exists().create(id=id, count=8, text='123456789')
b = BatchQuery()
TestIfNotExistsModel.batch(b).if_not_exists().create(id=id, count=9, text='111111111111')
self.assertRaises(LWTException, b.execute)
q = TestIfNotExistsModel.objects(id=id)
self.assertEqual(len(q), 1)
tm = q.first()
self.assertEquals(tm.count, 8)
self.assertEquals(tm.text, '123456789')
def test_batch_insert_if_not_exists_failure(self):
""" tests that batch insertion with if_not_exists failure """
id = uuid4()
with BatchQuery() as b:
TestIfNotExistsModel.batch(b).create(id=id, count=8, text='123456789')
with BatchQuery() as b:
TestIfNotExistsModel.batch(b).create(id=id, count=9, text='111111111111')
q = TestIfNotExistsModel.objects(id=id)
self.assertEquals(len(q), 1)
tm = q.first()
self.assertEquals(tm.count, 9)
self.assertEquals(tm.text, '111111111111')
class IfNotExistsModelTest(BaseIfNotExistsTest):
def test_if_not_exists_included_on_create(self):
""" tests that if_not_exists on models works as expected """
with mock.patch.object(self.session, 'execute') as m:
TestIfNotExistsModel.if_not_exists().create(count=8)
query = m.call_args[0][0].query_string
self.assertIn("IF NOT EXISTS", query)
def test_if_not_exists_included_on_save(self):
""" tests if we correctly put 'IF NOT EXISTS' for insert statement """
with mock.patch.object(self.session, 'execute') as m:
tm = TestIfNotExistsModel(count=8)
tm.if_not_exists(True).save()
query = m.call_args[0][0].query_string
self.assertIn("IF NOT EXISTS", query)
def test_queryset_is_returned_on_class(self):
""" ensure we get a queryset description back """
qs = TestIfNotExistsModel.if_not_exists()
self.assertTrue(isinstance(qs, TestIfNotExistsModel.__queryset__), type(qs))
def test_batch_if_not_exists(self):
""" ensure 'IF NOT EXISTS' exists in statement when in batch """
with mock.patch.object(self.session, 'execute') as m:
with BatchQuery() as b:
TestIfNotExistsModel.batch(b).if_not_exists().create(count=8)
self.assertIn("IF NOT EXISTS", m.call_args[0][0].query_string)
class IfNotExistsInstanceTest(BaseIfNotExistsTest):
def test_instance_is_returned(self):
"""
ensures that we properly handle the instance.if_not_exists(True).save()
scenario
"""
o = TestIfNotExistsModel.create(text="whatever")
o.text = "new stuff"
o = o.if_not_exists(True)
self.assertEqual(True, o._if_not_exists)
def test_if_not_exists_is_not_include_with_query_on_update(self):
"""
make sure we don't put 'IF NOT EXIST' in update statements
"""
o = TestIfNotExistsModel.create(text="whatever")
o.text = "new stuff"
o = o.if_not_exists(True)
with mock.patch.object(self.session, 'execute') as m:
o.save()
query = m.call_args[0][0].query_string
self.assertNotIn("IF NOT EXIST", query)

View 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')