From 1122018f4546c4a4de1b5f90593e2b0538eec6a6 Mon Sep 17 00:00:00 2001 From: Mission Liao Date: Thu, 15 Jan 2015 10:50:54 +0800 Subject: [PATCH] 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. --- cqlengine/exceptions.py | 1 + cqlengine/query.py | 4 +++- cqlengine/tests/test_ifnotexists.py | 34 +++++++++++++++++++++++++++-- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cqlengine/exceptions.py b/cqlengine/exceptions.py index f40f8af7..19b5c1fe 100644 --- a/cqlengine/exceptions.py +++ b/cqlengine/exceptions.py @@ -5,3 +5,4 @@ class ValidationError(CQLEngineException): pass class UndefinedKeyspaceException(CQLEngineException): pass class LWTException(CQLEngineException): pass +class IfNotExistsWithCounterColumn(CQLEngineException): pass diff --git a/cqlengine/query.py b/cqlengine/query.py index 5f4c2cb5..b220ace1 100644 --- a/cqlengine/query.py +++ b/cqlengine/query.py @@ -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 diff --git a/cqlengine/tests/test_ifnotexists.py b/cqlengine/tests/test_ifnotexists.py index aa5ae596..6b65dd25 100644 --- a/cqlengine/tests/test_ifnotexists.py +++ b/cqlengine/tests/test_ifnotexists.py @@ -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 + )