Skip to content

Commit b0d624d

Browse files
committed
BUG23582703: Fix issue while using array_insert in Collection
This patch fixes `array_insert` in `Collection` that was not returning a `DocumentPathItem` object for arrays, thus raising an error. A new test was added for regression.
1 parent 83b6c5a commit b0d624d

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/mysqlx/expr.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,14 @@ def docpath_array_loc(self):
443443
self.consume_token(TokenType.LSQBRACKET)
444444
if self.cur_token_type_is(TokenType.MUL):
445445
self.consume_token(TokenType.RSQBRACKET)
446-
return "[*]"
446+
return DocumentPathItem(type=DocumentPathItem.ARRAY_INDEX_ASTERISK)
447447
elif self.cur_token_type_is(TokenType.LNUM):
448448
v = int(self.consume_token(TokenType.LNUM))
449449
if v < 0:
450450
raise IndexError("Array index cannot be negative at {0}"
451451
"".format(self.pos))
452452
self.consume_token(TokenType.RSQBRACKET)
453-
return "[" + str(v) + "]"
453+
return DocumentPathItem(type=DocumentPathItem.ARRAY_INDEX, index=v)
454454
else:
455455
raise ValueError("Exception token type MUL or LNUM in JSON "
456456
"path array index at token pos {0}"

tests/test_mysqlx_crud.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,19 @@ def test_parameter_binding(self):
333333
self.assertEqual(1, len(docs))
334334
self.assertEqual("Wilma", docs[0]["name"])
335335

336+
def test_array_insert(self):
337+
collection_name = "collection_test"
338+
collection = self.schema.create_collection(collection_name)
339+
collection.add(
340+
{"_id": 1, "name": "Fred", "cards": []},
341+
{"_id": 2, "name": "Barney", "cards": [1, 2, 4]},
342+
{"_id": 3, "name": "Wilma", "cards": []},
343+
{"_id": 4, "name": "Betty", "cards": []},
344+
).execute()
345+
collection.modify("$._id == 2").array_insert("$.cards[2]", 3).execute()
346+
docs = collection.find("$._id == 2").execute().fetch_all()
347+
self.assertEqual([1, 2, 3, 4], docs[0]["cards"])
348+
336349
def test_array_append(self):
337350
collection_name = "collection_test"
338351
collection = self.schema.create_collection(collection_name)
@@ -346,6 +359,7 @@ def test_array_append(self):
346359
docs = collection.find("$._id == 2").execute().fetch_all()
347360
self.assertEqual([1, [2, 3], 4], docs[0]["cards"])
348361

362+
349363
@unittest.skipIf(tests.MYSQL_VERSION < (5, 7, 12), "XPlugin not compatible")
350364
class MySQLxTableTests(tests.MySQLxTests):
351365

0 commit comments

Comments
 (0)