Skip to content

Commit ea59f32

Browse files
committed
cqlengine: fix map remove feature for non-nullable values
following code: ``` .update( bin_map__remove={123, 456} ) ``` Would fail if map is defined with non-nullable value type: ``` columns.Map(columns.BigInt, columns.Bytes, required=False, default={}) ```
1 parent c5bed09 commit ea59f32

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

cassandra/cqlengine/statements.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,9 @@ def update_context_id(self, i):
821821
self.context_counter += conditional.get_context_size()
822822

823823
def add_update(self, column, value, operation=None, previous=None):
824-
value = column.to_database(value)
824+
# For remove all values are None, no need to convert them
825+
if operation != 'remove':
826+
value = column.to_database(value)
825827
col_type = type(column)
826828
container_update_type = ContainerUpdateClause.type_map.get(col_type)
827829
if container_update_type:

tests/integration/cqlengine/base.py

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ class TestQueryUpdateModel(Model):
3030
text_set = columns.Set(columns.Text, required=False)
3131
text_list = columns.List(columns.Text, required=False)
3232
text_map = columns.Map(columns.Text, columns.Text, required=False)
33+
bin_map = columns.Map(columns.BigInt, columns.Bytes, required=False, default={})
34+
3335

3436
class BaseCassEngTestCase(unittest.TestCase):
3537

tests/integration/cqlengine/operators/test_where_operators.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def test_is_not_null_to_cql(self):
8080
self.assertEqual(
8181
str(TestQueryUpdateModel.filter(IsNotNull("text"), partition=uuid4())),
8282
('SELECT "cluster", "count", "text", "text_set", '
83-
'"text_list", "text_map" FROM cqlengine_test.test_query_update_model '
83+
'"text_list", "text_map", "bin_map" FROM cqlengine_test.test_query_update_model '
8484
'WHERE "text" IS NOT NULL AND "partition" = %(0)s LIMIT 10000')
8585
)
8686

tests/integration/cqlengine/query/test_updates.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,30 @@ def test_map_update_remove(self):
246246
TestQueryUpdateModel.objects.create(
247247
partition=partition,
248248
cluster=cluster,
249-
text_map={"foo": '1', "bar": '2'}
249+
text_map={"foo": '1', "bar": '2'},
250+
bin_map={123: b'1', 789: b'2'}
250251
)
251252
TestQueryUpdateModel.objects(partition=partition, cluster=cluster).update(
252253
text_map__remove={"bar"},
253-
text_map__update={"foz": '4', "foo": '2'}
254+
text_map__update={"foz": '4', "foo": '2'},
255+
bin_map__remove={789},
256+
bin_map__update={456: b'4', 123: b'2'}
254257
)
255258
obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
256259
self.assertEqual(obj.text_map, {"foo": '2', "foz": '4'})
260+
self.assertEqual(obj.bin_map, {123: b'2', 456: b'4'})
257261

258262
TestQueryUpdateModel.objects(partition=partition, cluster=cluster).update(
259-
text_map__remove={"foo", "foz"}
263+
text_map__remove={"foo", "foz"},
264+
bin_map__remove={123, 456}
265+
)
266+
rec = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
267+
self.assertEqual(
268+
rec.text_map,
269+
{}
260270
)
261271
self.assertEqual(
262-
TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster).text_map,
272+
rec.bin_map,
263273
{}
264274
)
265275

tests/integration/cqlengine/statements/test_base_statement.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def _verify_statement(self, original):
6565

6666
for assignment in original.assignments:
6767
self.assertEqual(response[assignment.field], assignment.value)
68-
self.assertEqual(len(response), 7)
68+
self.assertEqual(len(response), 8)
6969

7070
def test_insert_statement_execute(self):
7171
"""
@@ -92,6 +92,7 @@ def test_insert_statement_execute(self):
9292
st.add_assignment(Column(db_field='text_set'), set(("foo_update", "bar_update")))
9393
st.add_assignment(Column(db_field='text_list'), ["foo_update", "bar_update"])
9494
st.add_assignment(Column(db_field='text_map'), {"foo": '3', "bar": '4'})
95+
st.add_assignment(Column(db_field='bin_map'), {123: b'3', 456: b'4'})
9596

9697
execute(st)
9798
self._verify_statement(st)
@@ -150,6 +151,7 @@ def _insert_statement(self, partition, cluster):
150151
st.add_assignment(Column(db_field='text_set'), set(("foo", "bar")))
151152
st.add_assignment(Column(db_field='text_list'), ["foo", "bar"])
152153
st.add_assignment(Column(db_field='text_map'), {"foo": '1', "bar": '2'})
154+
st.add_assignment(Column(db_field='bin_map'), {123: b'1', 456: b'2'})
153155

154156
execute(st)
155157
self._verify_statement(st)

0 commit comments

Comments
 (0)