raise exception when if_not_exists is not capable.

raise exception when calling if_not_exists for table with counter
columns. refer to cqlengine#302 for details.
This commit is contained in:
Mission Liao
2015-01-15 10:50:54 +08:00
parent 4bb8b313c0
commit 1122018f45
3 changed files with 36 additions and 3 deletions

View File

@@ -5,3 +5,4 @@ class ValidationError(CQLEngineException): pass
class UndefinedKeyspaceException(CQLEngineException): pass
class LWTException(CQLEngineException): pass
class IfNotExistsWithCounterColumn(CQLEngineException): pass

View File

@@ -7,7 +7,7 @@ from cqlengine.columns import Counter, List, Set
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:
@@ -784,6 +784,8 @@ class ModelQuerySet(AbstractQuerySet):
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

View File

@@ -3,7 +3,7 @@ from cqlengine.management import sync_table, drop_table, create_keyspace, delete
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
@@ -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
@@ -31,10 +37,23 @@ class BaseIfNotExistsTest(BaseCassEngTestCase):
@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")
@@ -170,3 +189,14 @@ class IfNotExistsInstanceTest(BaseIfNotExistsTest):
self.assertNotIn("IF NOT EXIST", query)
class IfNotExistWithCounterTest(BaseIfNotExistsWithCounterTest):
def test_instance_raise_exception(self):
""" make sure exception is raised when calling
if_not_exists on table with counter column
"""
id = uuid4()
self.assertRaises(
IfNotExistsWithCounterColumn,
TestIfNotExistsWithCounterModel.if_not_exists
)