Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raise exception when if_not_exists is not capable. #322

Merged
merged 2 commits into from Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cqlengine/exceptions.py
Expand Up @@ -5,3 +5,4 @@ class ValidationError(CQLEngineException): pass

class UndefinedKeyspaceException(CQLEngineException): pass
class LWTException(CQLEngineException): pass
class IfNotExistsWithCounterColumn(CQLEngineException): pass
4 changes: 3 additions & 1 deletion cqlengine/query.py
Expand Up @@ -7,7 +7,7 @@

from .connection import execute, NOT_SET

from cqlengine.exceptions import CQLEngineException, ValidationError, LWTException
from cqlengine.exceptions import CQLEngineException, ValidationError, LWTException, IfNotExistsWithCounterColumn
from cqlengine.functions import Token, BaseQueryFunction, QueryValue, UnicodeMixin

#CQL 3 reference:
Expand Down Expand Up @@ -784,6 +784,8 @@ def timestamp(self, timestamp):
return clone

def if_not_exists(self):
if self.model._has_counter:
raise IfNotExistsWithCounterColumn('if_not_exists cannot be used with tables containing columns')
clone = copy.deepcopy(self)
clone._if_not_exists = True
return clone
Expand Down
42 changes: 35 additions & 7 deletions cqlengine/tests/test_ifnotexists.py
Expand Up @@ -3,7 +3,7 @@
from cqlengine.tests.base import BaseCassEngTestCase
from cqlengine.tests.base import PROTOCOL_VERSION
from cqlengine.models import Model
from cqlengine.exceptions import LWTException
from cqlengine.exceptions import LWTException, IfNotExistsWithCounterColumn
from cqlengine import columns, BatchQuery
from uuid import uuid4
import mock
Expand All @@ -16,6 +16,12 @@ class TestIfNotExistsModel(Model):
text = columns.Text(required=False)


class TestIfNotExistsWithCounterModel(Model):

id = columns.UUID(primary_key=True, default=lambda:uuid4())
likes = columns.Counter()


class BaseIfNotExistsTest(BaseCassEngTestCase):

@classmethod
Expand All @@ -31,10 +37,23 @@ def setUpClass(cls):

@classmethod
def tearDownClass(cls):
super(BaseCassEngTestCase, cls).tearDownClass()
super(BaseIfNotExistsTest, cls).tearDownClass()
drop_table(TestIfNotExistsModel)


class BaseIfNotExistsWithCounterTest(BaseCassEngTestCase):

@classmethod
def setUpClass(cls):
super(BaseIfNotExistsWithCounterTest, cls).setUpClass()
sync_table(TestIfNotExistsWithCounterModel)

@classmethod
def tearDownClass(cls):
super(BaseIfNotExistsWithCounterTest, cls).tearDownClass()
drop_table(TestIfNotExistsWithCounterModel)


class IfNotExistsInsertTests(BaseIfNotExistsTest):

@skipUnless(PROTOCOL_VERSION >= 2, "only runs against the cql3 protocol v2.0")
Expand All @@ -44,10 +63,8 @@ def test_insert_if_not_exists_success(self):
id = uuid4()

TestIfNotExistsModel.create(id=id, count=8, text='123456789')
self.assertRaises(
LWTException,
TestIfNotExistsModel.if_not_exists().create, id=id, count=9, text='111111111111'
)
with self.assertRaises(LWTException):
TestIfNotExistsModel.if_not_exists().create(id=id, count=9, text='111111111111')

q = TestIfNotExistsModel.objects(id=id)
self.assertEqual(len(q), 1)
Expand Down Expand Up @@ -82,7 +99,8 @@ def test_batch_insert_if_not_exists_success(self):

b = BatchQuery()
TestIfNotExistsModel.batch(b).if_not_exists().create(id=id, count=9, text='111111111111')
self.assertRaises(LWTException, b.execute)
with self.assertRaises(LWTException):
b.execute()

q = TestIfNotExistsModel.objects(id=id)
self.assertEqual(len(q), 1)
Expand Down Expand Up @@ -170,3 +188,13 @@ def test_if_not_exists_is_not_include_with_query_on_update(self):
self.assertNotIn("IF NOT EXIST", query)


class IfNotExistWithCounterTest(BaseIfNotExistsWithCounterTest):

def test_instance_raise_exception(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you change this to use the context manager instead? There's a few examples in cqlengine/tests/test_timestamp.py

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I just realize that cqlengine didn't support python 2.6.

""" make sure exception is raised when calling
if_not_exists on table with counter column
"""
id = uuid4()
with self.assertRaises(IfNotExistsWithCounterColumn):
TestIfNotExistsWithCounterModel.if_not_exists()