File: test_qa_mysqlx_crud_collection_add.py

package info (click to toggle)
mysql-connector-python 9.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 27,768 kB
  • sloc: python: 83,598; sql: 47,030; ansic: 3,494; cpp: 860; sh: 394; makefile: 208; javascript: 2
file content (511 lines) | stat: -rw-r--r-- 21,758 bytes parent folder | download
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# Copyright (c) 2021, 2024, Oracle and/or its affiliates.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License, version 2.0, as
# published by the Free Software Foundation.
#
# This program is designed to work with certain software (including
# but not limited to OpenSSL) that is licensed under separate terms,
# as designated in a particular file or component or in included license
# documentation. The authors of MySQL hereby grant you an
# additional permission to link the program and your derivative works
# with the separately licensed software that they have either included with
# the program or referenced in the documentation.
#
# Without limiting anything contained in the foregoing, this file,
# which is part of MySQL Connector/Python, is also subject to the
# Universal FOSS Exception, version 1.0, a copy of which can be found at
# http://oss.oracle.com/licenses/universal-foss-exception.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License, version 2.0, for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

import os
import unittest

import tests

import mysqlx


def is_unique_id(lst):
    """To test if the generated IDs are unique."""
    if len(lst) == len(set(lst)):
        return True
    return False


def getmac(interface):
    """To get the MAC address."""
    try:
        mac = open(f"/sys/class/net/{interface}/address").readline()
    except:
        mac = "00:00:00:00:00:00"
    return mac[0:17]


def create_json_without_id():
    """Create big JSON doc without _id."""
    data = []
    for i in range(-50, 60):
        data1 = {"name": "Sam", "age": i}
        data.append(data1)
    return data


def create_json_with_id():
    """Create big JSON doc with _id."""
    data = []
    for idx in range(-50, 60):
        data1 = {"_id": idx, "name": "Ram", "age": idx}
        data.append(data1)
    return data


@unittest.skipIf(tests.MYSQL_VERSION < (8, 0, 25), "XPlugin not compatible")
class CollectionAddTests(tests.MySQLxTests):
    """Tests for collection.add()."""

    def _drop_collection_if_exists(self, name):
        collection = self.schema.get_collection(name)
        if collection.exists_in_database():
            self.schema.drop_collection(name)

    @tests.foreach_session()
    def test_collection_add_test1(self):
        """Test collection."""
        self._drop_collection_if_exists("mycoll1")
        collection = self.schema.create_collection("mycoll1")
        result = collection.add({"name": "a"}, {"name": "b"}).execute()
        self.assertEqual(collection.count(), 2)
        self.assertEqual(len(result.get_generated_ids()), 2)
        self.assertEqual(len(result.get_generated_ids()[0]), 28)
        self.assertRegex(result.get_generated_ids()[0], r"[a-f0-9]{28}")
        self.schema.drop_collection("mycoll1")

    @tests.foreach_session()
    def test_collection_add_test2(self):
        """Test the collection.add with empty document."""
        self._drop_collection_if_exists("mycoll2")
        collection = self.schema.create_collection("mycoll2")
        collection.add().execute()
        self.schema.drop_collection("mycoll2")

    @tests.foreach_session()
    def test_collection_test1(self):
        """Test the get_name()."""
        self._drop_collection_if_exists("mycoll7")
        collection = self.schema.create_collection("mycoll7")
        self.assertEqual(collection.get_name(), "mycoll7")
        self.schema.drop_collection("mycoll7")

    @tests.foreach_session()
    def test_collection_test2(self):
        """Test the get_schema()."""
        self._drop_collection_if_exists("mycoll8")
        config = tests.get_mysqlx_config()
        collection = self.schema.create_collection("mycoll8")
        self.assertEqual(collection.get_schema().name, config["schema"])
        self.schema.drop_collection("mycoll8")

    @tests.foreach_session()
    def test_collection_test8(self):
        """Test the with big name."""
        self._drop_collection_if_exists(
            "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm7"
        )
        collection = self.schema.create_collection(
            "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm7",
        )
        self.assertEqual(
            collection.get_name(),
            "mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm7",
        )
        self.schema.drop_collection("mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm7")

    @tests.foreach_session()
    def test_collection_test4(self):
        """Test the exists_in_database()."""
        self._drop_collection_if_exists("mycoll13")
        collection = self.schema.create_collection("mycoll13")
        self.assertTrue(collection.exists_in_database())
        self.schema.drop_collection("mycoll13")

    @tests.foreach_session()
    def test_collection_test5(self):
        """Test the get_generated_ids."""
        self._drop_collection_if_exists("mycoll14")
        collection = self.schema.create_collection("mycoll14")
        result = collection.add(
            {"_id": "la", "a": 1, "b": 2, "c": 100},
            {"_id": 2, "a": 2, "b": 1, "c": 200},
            {"a": 3, "b": 2, "c": 300},
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 1)
        self.schema.drop_collection("mycoll14")

    @tests.foreach_session()
    def test_collection_test6(self):
        """Testing the get_generated_ids."""
        self._drop_collection_if_exists("mycoll15")
        collection = self.schema.create_collection("mycoll15")
        result = collection.add(
            {"_id": 1, "a": 1, "b": 2, "c": 100},
            {"_id": 2, "a": 2, "b": 1, "c": 200},
            {"_id": 3, "a": 3, "b": 2, "c": 300},
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        self.schema.drop_collection("mycoll15")

    @tests.foreach_session()
    def test_collection_test7(self):
        """Test the get_generated_ids with multiple add() methods."""
        self._drop_collection_if_exists("mycoll16")
        collection = self.schema.create_collection("mycoll16")
        result = (
            collection.add({"a": 1, "b": 2, "c": 100})
            .add({"_id": 2, "a": 2, "b": 1, "c": 200})
            .add({"a": 3, "b": 2, "c": 300})
            .add({})
            .execute()
        )
        self.assertEqual(len(result.get_generated_ids()), 3)
        self.assertTrue(
            result.get_generated_ids()[0]
            < result.get_generated_ids()[1]
            < result.get_generated_ids()[2]
        )
        self.schema.drop_collection("mycoll16")

    @tests.foreach_session()
    def test_collection_add_test3(self):
        """MCPY-384 add an empty array to a collection which doesnt exist."""
        self._drop_collection_if_exists("mycoll17")
        collection = self.schema.create_collection("mycoll17")
        collection.add().execute()
        self.schema.drop_collection("mycoll17")

    @tests.foreach_session()
    def test_collection_add_test4(self):
        """Test the parameter list."""
        self._drop_collection_if_exists("mycoll18")
        collection = self.schema.create_collection("mycoll18")
        collection.add({"name": "a"}, {"name": "b"}, {"name": "c"}).execute()
        self.assertEqual(collection.count(), 3)
        self.schema.drop_collection("mycoll18")

    @tests.foreach_session()
    def test_collection_add_id_test1(self):
        """Test if _id is preserved when JSON doc contains _id field and not
        overridden by generated ID."""
        self._drop_collection_if_exists("mycoll22")
        collection = self.schema.create_collection("mycoll22")
        result = collection.add({"_id": 1, "name": "a"}).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        self.schema.drop_collection("mycoll22")

    @tests.foreach_session()
    def test_collection_add_id_test2(self):
        """Test if ID is generated for multiple JSON docs when _id field is
        not provided."""
        self._drop_collection_if_exists("mycoll23")
        collection = self.schema.create_collection("mycoll23")
        result = (
            collection.add({"name": "a", "a": 1, "b": 2, "c": 100})
            .add({"name": "b", "a": 2, "b": 3, "c": 200})
            .add({"name": "c", "a": 3, "b": 4, "c": 300})
            .add({"name": "d", "a": 4, "b": 5, "c": 400})
            .add({"name": "e", "a": 5, "b": 6, "c": 500})
            .execute()
        )
        self.assertEqual(len(result.get_generated_ids()), collection.count())
        self.assertTrue(is_unique_id(result.get_generated_ids()))
        self.schema.drop_collection("mycoll23")

    @tests.foreach_session()
    def test_collection_add_id_test3(self):
        """Test if ID is generated for multiple JSON docs when _id field is
        not provided."""
        self._drop_collection_if_exists("mycoll24")
        collection = self.schema.create_collection("mycoll24")
        result = collection.add(
            {"name": "a", "a": 1, "b": 2, "c": 100},
            {"name": "b", "a": 2, "b": 3, "c": 200},
            {"name": "c", "a": 3, "b": 4, "c": 300},
            {"name": "d", "a": 4, "b": 5, "c": 400},
            {"name": "e", "a": 5, "b": 6, "c": 500},
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), collection.count())
        self.assertTrue(is_unique_id(result.get_generated_ids()))
        self.schema.drop_collection("mycoll24")

    @tests.foreach_session()
    def test_collection_add_id_test4(self):
        """Test ID generation when few docs contain _id while others doesn't
        contain."""
        self._drop_collection_if_exists("mycoll25")
        collection = self.schema.create_collection("mycoll25")
        result = collection.add(
            {"name": "a", "a": 1, "b": 2, "c": 100},
            {"name": "b", "a": 2, "b": 3, "c": 200},
            {"_id": 1, "name": "c", "a": 3, "b": 4, "c": 300},
            {"_id": 2, "name": "d", "a": 4, "b": 5, "c": 400},
            {"name": "e", "a": 5, "b": 6, "c": 500},
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 3)
        self.assertEqual(collection.count(), 5)
        self.assertTrue(is_unique_id(result.get_generated_ids()))
        self.schema.drop_collection("mycoll25")

    @tests.foreach_session()
    def test_collection_add_id_test5(self):
        """Test ID is not generated when multiple docs are added with
        _id field."""
        self._drop_collection_if_exists("mycoll26")
        collection = self.schema.create_collection("mycoll26")
        result = collection.add(
            {"_id": 1, "name": "a", "a": 1, "b": 2, "c": 100},
            {"_id": 2, "name": "b", "a": 2, "b": 3, "c": 200},
            {"_id": 3, "name": "c", "a": 3, "b": 4, "c": 300},
            {"_id": 4, "name": "d", "a": 4, "b": 5, "c": 400},
            {"_id": 5, "name": "e", "a": 5, "b": 6, "c": 500},
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        self.assertEqual(collection.count(), 5)
        res = result.get_generated_ids()
        self.assertEqual(
            res,
            [],
            "IDs are generated while they are not expected ",
        )
        self.schema.drop_collection("mycoll26")

    @tests.foreach_session()
    def test_collection_add_id_test6(self):
        """Testing IDs are not duplicate when data added without _id filed
        from multiple sessions to the same collection."""
        config = tests.get_mysqlx_config()
        session1 = mysqlx.get_session(config)
        schema1 = session1.get_schema(config["schema"])
        self._drop_collection_if_exists("mycoll27")
        collection = self.schema.create_collection("mycoll27")
        collection1 = schema1.get_collection("mycoll27")
        result = collection.add(
            {"name": "a", "a": 1, "b": 2, "c": 100},
            {"name": "b", "a": 2, "b": 3, "c": 200},
            {"name": "c", "a": 3, "b": 4, "c": 300},
            {"name": "d", "a": 4, "b": 5, "c": 400},
            {"name": "e", "a": 5, "b": 6, "c": 500},
        ).execute()
        result1 = collection1.add(
            {"name": "a", "a": 1, "b": 2, "c": 100},
            {"name": "b", "a": 2, "b": 3, "c": 200},
            {"name": "c", "a": 3, "b": 4, "c": 300},
            {"name": "d", "a": 4, "b": 5, "c": 400},
            {"name": "e", "a": 5, "b": 6, "c": 500},
        ).execute()
        overall_ids = result.get_generated_ids() + result1.get_generated_ids()
        self.assertEqual(len(overall_ids), collection.count())
        self.assertTrue(is_unique_id(overall_ids))
        self.schema.drop_collection("mycoll27")

    @tests.foreach_session()
    def test_collection_add_id_test7(self):
        """Test ID is generated when different values are provided to _id
        field."""
        self._drop_collection_if_exists("mycoll28")
        collection = self.schema.create_collection("mycoll28")
        result = collection.add(
            {"_id": -11, "name": "a", "a": 1, "b": 2, "c": 100}
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        res = collection.find("$.name == 'a'").execute()
        mydoc = res.fetch_all()[0]
        self.assertEqual(mydoc["_id"], -11)
        self.schema.drop_collection("mycoll28")

    @tests.foreach_session()
    def test_collection_add_id_test8(self):
        """Test when _id is given value 0."""
        self._drop_collection_if_exists("mycoll29")
        collection = self.schema.create_collection("mycoll29")
        result = collection.add(
            {"_id": 0, "name": "a", "a": 1, "b": 2, "c": 100}
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        res = collection.find("$.name == 'a'").execute().fetch_all()[0]
        self.assertEqual(res["_id"], 0)
        self.schema.drop_collection("mycoll29")

    @unittest.skipUnless(tests.ARCH_64BIT, "Test available only for 64 bit platforms")
    @unittest.skipIf(os.name == "nt", "Test not available for Windows")
    @tests.foreach_session()
    def test_collection_add_id_test9(self):
        """Test when _id is given big positive number."""
        self._drop_collection_if_exists("mycoll30")
        collection = self.schema.create_collection("mycoll30")
        result = collection.add(
            {"_id": 876848644738567, "name": "a", "a": 1, "b": 2, "c": 100}
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        res = collection.find("$.name == 'a'").execute().fetch_all()[0]
        self.assertEqual(res["_id"], 876848644738567)
        self.schema.drop_collection("mycoll30")

    @tests.foreach_session()
    def test_collection_add_id_test10(self):
        """Test when _id is given string."""
        self._drop_collection_if_exists("mycoll31")
        collection = self.schema.create_collection("mycoll31")
        result = collection.add(
            {"_id": "It is my ID", "name": "a", "a": 1, "b": 2, "c": 100}
        ).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        res = collection.find("$.name == 'a'").execute().fetch_all()[0]
        self.assertEqual(res["_id"], "It is my ID")
        self.schema.drop_collection("mycoll31")

    @tests.foreach_session()
    def test_collection_add_id_test11(self):
        """Test if ID is generated for multiple JSON docs when _id field is
        not provided."""
        self._drop_collection_if_exists("mycoll32")
        collection = self.schema.create_collection("mycoll32")
        result = (
            collection.add({"_id": 101, "name": "a", "a": 1, "b": 2, "c": 100})
            .add(create_json_without_id())
            .add({"name": "c", "a": 3, "b": 4, "c": 300})
            .add({"name": "d", "a": 4, "b": 5, "c": 400})
            .add({"name": "e", "a": 5, "b": 6, "c": 500})
            .execute()
        )
        self.assertEqual(len(result.get_generated_ids()), 113)
        self.assertEqual(collection.count(), 114)
        self.assertTrue(is_unique_id(result.get_generated_ids()))
        for idx in range(0, 112):
            self.assertTrue(
                result.get_generated_ids()[idx] < result.get_generated_ids()[idx + 1]
            )
            self.assertRegex(result.get_generated_ids()[idx], r"[a-f0-9]{28}")
            idx = idx + 1
        self.schema.drop_collection("mycoll32")

    @tests.foreach_session()
    def test_collection_add_id_test12(self):
        """Test to ensure that the generated ID depends on client's MAC."""
        self._drop_collection_if_exists("mycoll33")
        collection = self.schema.create_collection("mycoll33")
        result = (
            collection.add({"name": "a", "a": 1, "b": 2, "c": 100})
            .add({"name": "b", "a": 2, "b": 3, "c": 200})
            .add({"name": "c", "a": 3, "b": 4, "c": 300})
            .add({"name": "d", "a": 4, "b": 5, "c": 400})
            .add({"name": "e", "a": 5, "b": 6, "c": 500})
            .execute()
        )
        self.assertEqual(len(result.get_generated_ids()), collection.count())
        self.assertEqual(len(result.get_generated_ids()), 5)
        self.assertTrue(is_unique_id(result.get_generated_ids()))
        self.schema.drop_collection("mycoll33")

    @tests.foreach_session()
    def test_collection_add_id_test13(self):
        """Test if ID is generated with big JSON docs without _id field."""
        self._drop_collection_if_exists("mycoll34")
        collection = self.schema.create_collection("mycoll34")
        result = collection.add(create_json_without_id()).execute()
        self.assertEqual(len(result.get_generated_ids()), collection.count())
        self.assertTrue(is_unique_id(result.get_generated_ids()))
        self.schema.drop_collection("mycoll34")

    @tests.foreach_session()
    def test_collection_add_id_test14(self):
        """Test ID is not generated by adding big JSON docs with _id field."""
        self._drop_collection_if_exists("mycoll35")
        collection = self.schema.create_collection("mycoll35")
        result = collection.add(create_json_with_id()).execute()
        self.assertEqual(len(result.get_generated_ids()), 0)
        self.assertEqual(collection.count(), 110)
        self.schema.drop_collection("mycoll35")

    @tests.foreach_session()
    def test_collection_add_id_test15(self):
        """Test adding duplicate values to _id. User should get error."""
        self._drop_collection_if_exists("mycoll36")
        collection = self.schema.create_collection("mycoll36")
        self.assertRaises(
            mysqlx.errors.OperationalError,
            collection.add({"_id": "abcde1234", "name": "myname1", "age": 28})
            .add({"_id": "abcde1234", "name": "myname2", "age": 30})
            .execute,
        )
        self.schema.drop_collection("mycoll36")

    @tests.foreach_session()
    def test_collection_add_id_test16(self):
        """Test inserting invalid values to _id."""
        self._drop_collection_if_exists("mycoll37")
        collection = self.schema.create_collection("mycoll37")
        res = collection.add({"_id": "", "name": "myname1", "age": 28}).execute()
        res1 = collection.add({"_id": None, "name": "myname2", "age": 30}).execute()
        result = collection.find().fields("_id", "name", "age").sort("age").execute()
        self.assertEqual(len(res.get_generated_ids()), 0)
        self.assertEqual(collection.count(), 2)
        row = result.fetch_all()
        self.assertEqual(row[0]["_id"], "")
        self.assertEqual(row[1]["_id"], None)
        self.schema.drop_collection("mycoll37")

    @tests.foreach_session()
    def test_dbdoc_test1(self):
        self._drop_collection_if_exists("mycoll1")
        collection = self.schema.create_collection("mycoll1")
        collection.add({"_id": 1, "name": "a"}, {"_id": 2, "name": "b"}).execute()
        self.assertEqual(collection.count(), 2)
        self.schema.drop_collection("mycoll1")

    @tests.foreach_session()
    def test_dbdoc_test2(self):
        """Test the json array."""
        data = [{"_id": 1, "a": "b"}, {"_id": 2, "a": "c"}]
        self._drop_collection_if_exists("mycoll2")
        collection = self.schema.create_collection("mycoll2")
        collection.add(*data).execute()
        result = collection.find().execute()
        self.schema.drop_collection("mycoll2")

    @tests.foreach_session()
    def test_dbdoc_test3(self):
        """Test the json doc."""
        data = {
            "_id": 1,
            "name": "abc",
        }
        self._drop_collection_if_exists("mycoll3")
        collection = self.schema.create_collection("mycoll3")
        collection.add(data).execute()
        result = collection.find().execute()
        row = result.fetch_all()
        self.assertEqual(row[0]["_id"], 1)
        self.schema.drop_collection("mycoll3")

    @tests.foreach_session()
    def test_dbdoc_test4(self):
        """Test the json doc array."""
        mydoc = [None] * 5
        for i in range(0, 5):
            mydoc[i] = {}
            mydoc[i]["_id"] = i
            mydoc[i]["name"] = "abc"
            mydoc[i]["age"] = i * 10
        self._drop_collection_if_exists("mycoll4")
        collection = self.schema.create_collection("mycoll4")
        collection.add(*mydoc).execute()
        self.assertEqual(collection.count(), 5)
        self.schema.drop_collection("mycoll4")