File: test_updates.py

package info (click to toggle)
python-cassandra-driver 3.29.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,144 kB
  • sloc: python: 51,532; ansic: 768; makefile: 136; sh: 13
file content (347 lines) | stat: -rw-r--r-- 13,803 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# Copyright DataStax, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from uuid import uuid4
from cassandra.cqlengine import ValidationError

from cassandra.cqlengine.models import Model
from cassandra.cqlengine.management import sync_table, drop_table
from cassandra.cqlengine import columns

from tests.integration.cqlengine import is_prepend_reversed
from tests.integration.cqlengine.base import BaseCassEngTestCase, TestQueryUpdateModel
from tests.integration.cqlengine import execute_count
from tests.integration import greaterthancass20


class QueryUpdateTests(BaseCassEngTestCase):

    @classmethod
    def setUpClass(cls):
        super(QueryUpdateTests, cls).setUpClass()
        sync_table(TestQueryUpdateModel)

    @classmethod
    def tearDownClass(cls):
        super(QueryUpdateTests, cls).tearDownClass()
        drop_table(TestQueryUpdateModel)

    @execute_count(8)
    def test_update_values(self):
        """ tests calling udpate on a queryset """
        partition = uuid4()
        for i in range(5):
            TestQueryUpdateModel.create(partition=partition, cluster=i, count=i, text=str(i))

        # sanity check
        for i, row in enumerate(TestQueryUpdateModel.objects(partition=partition)):
            self.assertEqual(row.cluster, i)
            self.assertEqual(row.count, i)
            self.assertEqual(row.text, str(i))

        # perform update
        TestQueryUpdateModel.objects(partition=partition, cluster=3).update(count=6)

        for i, row in enumerate(TestQueryUpdateModel.objects(partition=partition)):
            self.assertEqual(row.cluster, i)
            self.assertEqual(row.count, 6 if i == 3 else i)
            self.assertEqual(row.text, str(i))

    @execute_count(6)
    def test_update_values_validation(self):
        """ tests calling udpate on models with values passed in """
        partition = uuid4()
        for i in range(5):
            TestQueryUpdateModel.create(partition=partition, cluster=i, count=i, text=str(i))

        # sanity check
        for i, row in enumerate(TestQueryUpdateModel.objects(partition=partition)):
            self.assertEqual(row.cluster, i)
            self.assertEqual(row.count, i)
            self.assertEqual(row.text, str(i))

        # perform update
        with self.assertRaises(ValidationError):
            TestQueryUpdateModel.objects(partition=partition, cluster=3).update(count='asdf')

    def test_invalid_update_kwarg(self):
        """ tests that passing in a kwarg to the update method that isn't a column will fail """
        with self.assertRaises(ValidationError):
            TestQueryUpdateModel.objects(partition=uuid4(), cluster=3).update(bacon=5000)

    def test_primary_key_update_failure(self):
        """ tests that attempting to update the value of a primary key will fail """
        with self.assertRaises(ValidationError):
            TestQueryUpdateModel.objects(partition=uuid4(), cluster=3).update(cluster=5000)

    @execute_count(8)
    def test_null_update_deletes_column(self):
        """ setting a field to null in the update should issue a delete statement """
        partition = uuid4()
        for i in range(5):
            TestQueryUpdateModel.create(partition=partition, cluster=i, count=i, text=str(i))

        # sanity check
        for i, row in enumerate(TestQueryUpdateModel.objects(partition=partition)):
            self.assertEqual(row.cluster, i)
            self.assertEqual(row.count, i)
            self.assertEqual(row.text, str(i))

        # perform update
        TestQueryUpdateModel.objects(partition=partition, cluster=3).update(text=None)

        for i, row in enumerate(TestQueryUpdateModel.objects(partition=partition)):
            self.assertEqual(row.cluster, i)
            self.assertEqual(row.count, i)
            self.assertEqual(row.text, None if i == 3 else str(i))

    @execute_count(9)
    def test_mixed_value_and_null_update(self):
        """ tests that updating a columns value, and removing another works properly """
        partition = uuid4()
        for i in range(5):
            TestQueryUpdateModel.create(partition=partition, cluster=i, count=i, text=str(i))

        # sanity check
        for i, row in enumerate(TestQueryUpdateModel.objects(partition=partition)):
            self.assertEqual(row.cluster, i)
            self.assertEqual(row.count, i)
            self.assertEqual(row.text, str(i))

        # perform update
        TestQueryUpdateModel.objects(partition=partition, cluster=3).update(count=6, text=None)

        for i, row in enumerate(TestQueryUpdateModel.objects(partition=partition)):
            self.assertEqual(row.cluster, i)
            self.assertEqual(row.count, 6 if i == 3 else i)
            self.assertEqual(row.text, None if i == 3 else str(i))

    @execute_count(3)
    def test_set_add_updates(self):
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
                partition=partition, cluster=cluster, text_set=set(("foo",)))
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(text_set__add=set(('bar',)))
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_set, set(("foo", "bar")))

    @execute_count(2)
    def test_set_add_updates_new_record(self):
        """ If the key doesn't exist yet, an update creates the record
        """
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(text_set__add=set(('bar',)))
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_set, set(("bar",)))

    @execute_count(3)
    def test_set_remove_updates(self):
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
                partition=partition, cluster=cluster, text_set=set(("foo", "baz")))
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(
                text_set__remove=set(('foo',)))
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_set, set(("baz",)))

    @execute_count(3)
    def test_set_remove_new_record(self):
        """ Removing something not in the set should silently do nothing
        """
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
                partition=partition, cluster=cluster, text_set=set(("foo",)))
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(
                text_set__remove=set(('afsd',)))
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_set, set(("foo",)))

    @execute_count(3)
    def test_list_append_updates(self):
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
                partition=partition, cluster=cluster, text_list=["foo"])
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(
                text_list__append=['bar'])
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_list, ["foo", "bar"])

    @execute_count(3)
    def test_list_prepend_updates(self):
        """ Prepend two things since order is reversed by default by CQL """
        partition = uuid4()
        cluster = 1
        original = ["foo"]
        TestQueryUpdateModel.objects.create(
                partition=partition, cluster=cluster, text_list=original)
        prepended = ['bar', 'baz']
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(
                text_list__prepend=prepended)
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        expected = (prepended[::-1] if is_prepend_reversed() else prepended) + original
        self.assertEqual(obj.text_list, expected)

    @execute_count(3)
    def test_map_update_updates(self):
        """ Merge a dictionary into existing value """
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
                partition=partition, cluster=cluster,
                text_map={"foo": '1', "bar": '2'})
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(
                text_map__update={"bar": '3', "baz": '4'})
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_map, {"foo": '1', "bar": '3', "baz": '4'})

    @execute_count(3)
    def test_map_update_none_deletes_key(self):
        """ The CQL behavior is if you set a key in a map to null it deletes
        that key from the map.  Test that this works with __update.
        """
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
                partition=partition, cluster=cluster,
                text_map={"foo": '1', "bar": '2'})
        TestQueryUpdateModel.objects(
                partition=partition, cluster=cluster).update(
                text_map__update={"bar": None})
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_map, {"foo": '1'})

    @greaterthancass20
    @execute_count(5)
    def test_map_update_remove(self):
        """
        Test that map item removal with update(<columnname>__remove=...) works

        @jira_ticket PYTHON-688
        """
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
            partition=partition,
            cluster=cluster,
            text_map={"foo": '1', "bar": '2'}
        )
        TestQueryUpdateModel.objects(partition=partition, cluster=cluster).update(
            text_map__remove={"bar"},
            text_map__update={"foz": '4', "foo": '2'}
        )
        obj = TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster)
        self.assertEqual(obj.text_map, {"foo": '2', "foz": '4'})

        TestQueryUpdateModel.objects(partition=partition, cluster=cluster).update(
            text_map__remove={"foo", "foz"}
        )
        self.assertEqual(
            TestQueryUpdateModel.objects.get(partition=partition, cluster=cluster).text_map,
            {}
        )

    def test_map_remove_rejects_non_sets(self):
        """
        Map item removal requires a set to match the CQL API

        @jira_ticket PYTHON-688
        """
        partition = uuid4()
        cluster = 1
        TestQueryUpdateModel.objects.create(
            partition=partition,
            cluster=cluster,
            text_map={"foo": '1', "bar": '2'}
        )
        with self.assertRaises(ValidationError):
            TestQueryUpdateModel.objects(partition=partition, cluster=cluster).update(
                text_map__remove=["bar"]
            )

    @execute_count(3)
    def test_an_extra_delete_is_not_sent(self):
        """
        Test to ensure that an extra DELETE is not sent if an object is read
        from the DB with a None value

        @since 3.9
        @jira_ticket PYTHON-719
        @expected_result only three queries are executed, the first one for
        inserting the object, the second one for reading it, and the third
        one for updating it

        @test_category object_mapper
        """
        partition = uuid4()
        cluster = 1

        TestQueryUpdateModel.objects.create(
            partition=partition, cluster=cluster)

        obj = TestQueryUpdateModel.objects(
            partition=partition, cluster=cluster).first()

        self.assertFalse({k: v for (k, v) in obj._values.items() if v.deleted})

        obj.text = 'foo'
        obj.save()
        #execute_count will check the execution count and
        #assert no more calls than necessary where made

class StaticDeleteModel(Model):
    example_id = columns.Integer(partition_key=True, primary_key=True, default=uuid4)
    example_static1 = columns.Integer(static=True)
    example_static2 = columns.Integer(static=True)
    example_clust = columns.Integer(primary_key=True)


class StaticDeleteTests(BaseCassEngTestCase):

    @classmethod
    def setUpClass(cls):
        super(StaticDeleteTests, cls).setUpClass()
        sync_table(StaticDeleteModel)

    @classmethod
    def tearDownClass(cls):
        super(StaticDeleteTests, cls).tearDownClass()
        drop_table(StaticDeleteModel)

    def test_static_deletion(self):
        """
        Test to ensure that cluster keys are not included when removing only static columns

        @since 3.6
        @jira_ticket PYTHON-608
        @expected_result Server should not throw an exception, and the static column should be deleted

        @test_category object_mapper
        """
        StaticDeleteModel.create(example_id=5, example_clust=5, example_static2=1)
        sdm = StaticDeleteModel.filter(example_id=5).first()
        self.assertEqual(1, sdm.example_static2)
        sdm.update(example_static2=None)
        self.assertIsNone(sdm.example_static2)