From f04c831ec0b69f4eb44bf6f08b1592dcc4c86b1b Mon Sep 17 00:00:00 2001 From: Blake Eggleston Date: Tue, 12 Mar 2013 10:11:55 -0700 Subject: [PATCH] fixing bug caused by batch objects on querysets being copied --- cqlengine/query.py | 6 ++++++ cqlengine/tests/test_batch_query.py | 16 ++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/cqlengine/query.py b/cqlengine/query.py index 36909f1e..c148f8e4 100644 --- a/cqlengine/query.py +++ b/cqlengine/query.py @@ -227,6 +227,12 @@ class QuerySet(object): for k,v in self.__dict__.items(): if k in ['_con', '_cur', '_result_cache', '_result_idx']: clone.__dict__[k] = None + elif k == '_batch': + # we need to keep the same batch instance across + # all queryset clones, otherwise the batched queries + # fly off into other batch instances which are never + # executed, thx @dokai + clone.__dict__[k] = self._batch else: clone.__dict__[k] = copy.deepcopy(v, memo) diff --git a/cqlengine/tests/test_batch_query.py b/cqlengine/tests/test_batch_query.py index c6d509eb..fb4aab4a 100644 --- a/cqlengine/tests/test_batch_query.py +++ b/cqlengine/tests/test_batch_query.py @@ -31,8 +31,6 @@ class BatchQueryTests(BaseCassEngTestCase): for obj in TestMultiKeyModel.filter(partition=self.pkey): obj.delete() - - def test_insert_success_case(self): b = BatchQuery() @@ -90,4 +88,18 @@ class BatchQueryTests(BaseCassEngTestCase): for i in range(5): TestMultiKeyModel.get(partition=self.pkey, cluster=i) + def test_bulk_delete_success_case(self): + + for i in range(1): + for j in range(5): + TestMultiKeyModel.create(partition=i, cluster=j, count=i*j, text='{}:{}'.format(i,j)) + + with BatchQuery() as b: + TestMultiKeyModel.objects.batch(b).filter(partition=0).delete() + assert TestMultiKeyModel.filter(partition=0).count() == 5 + + assert TestMultiKeyModel.filter(partition=0).count() == 0 + #cleanup + for m in TestMultiKeyModel.all(): + m.delete()