Skip to content

Commit 4cd4e98

Browse files
committed
BatchStatement.clear + unit test
1 parent e97e6f3 commit 4cd4e98

2 files changed

Lines changed: 72 additions & 0 deletions

File tree

cassandra/query.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,19 @@ def __init__(self, batch_type=BatchType.LOGGED, retry_policy=None,
699699
Statement.__init__(self, retry_policy=retry_policy, consistency_level=consistency_level,
700700
serial_consistency_level=serial_consistency_level, custom_payload=custom_payload)
701701

702+
def clear(self):
703+
"""
704+
This is a convenience method to clear a batch statement for reuse.
705+
706+
*Note:* it should not be used concurrently with uncompleted execution futures executing the same
707+
``BatchStatement``.
708+
"""
709+
del self._statements_and_parameters[:]
710+
self.keyspace = None
711+
self.routing_key = None
712+
if self.custom_payload:
713+
self.custom_payload.clear()
714+
702715
def add(self, statement, parameters=None):
703716
"""
704717
Adds a :class:`.Statement` and optional sequence of parameters

tests/unit/test_query.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2013-2016 DataStax, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
try:
16+
import unittest2 as unittest
17+
except ImportError:
18+
import unittest # noqa
19+
20+
import six
21+
22+
from cassandra.query import BatchStatement, SimpleStatement
23+
24+
25+
class BatchStatementTest(unittest.TestCase):
26+
# TODO: this suite could be expanded; for now just adding a test covering a PR
27+
28+
def test_clear(self):
29+
keyspace = 'keyspace'
30+
routing_key = 'routing_key'
31+
custom_payload = {'key': six.b('value')}
32+
33+
ss = SimpleStatement('whatever', keyspace=keyspace, routing_key=routing_key, custom_payload=custom_payload)
34+
35+
batch = BatchStatement()
36+
batch.add(ss)
37+
38+
self.assertTrue(batch._statements_and_parameters)
39+
self.assertEqual(batch.keyspace, keyspace)
40+
self.assertEqual(batch.routing_key, routing_key)
41+
self.assertEqual(batch.custom_payload, custom_payload)
42+
43+
batch.clear()
44+
self.assertFalse(batch._statements_and_parameters)
45+
self.assertIsNone(batch.keyspace)
46+
self.assertIsNone(batch.routing_key)
47+
self.assertFalse(batch.custom_payload)
48+
49+
batch.add(ss)
50+
51+
def test_clear_empty(self):
52+
batch = BatchStatement()
53+
batch.clear()
54+
self.assertFalse(batch._statements_and_parameters)
55+
self.assertIsNone(batch.keyspace)
56+
self.assertIsNone(batch.routing_key)
57+
self.assertFalse(batch.custom_payload)
58+
59+
batch.add('something')

0 commit comments

Comments
 (0)