From b5e985688158c7f4e5fbbee0dc9d0311b3552337 Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 29 Mar 2020 11:21:19 +0200 Subject: [PATCH 1/3] Failed to handle positional update for entire element --- .../fakemongo/impl/UpdateEngineTest.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java b/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java index 747e73fd..bb3db48e 100644 --- a/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java +++ b/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java @@ -416,7 +416,7 @@ public void testPositionalOperator() { public void testPositionalOperatorWithElemMatch() { String random1 = UUID.randomUUID().toString(); String random2 = UUID.randomUUID().toString(); - + DBObject object = (DBObject) FongoJSON.parse("{ \"name\" : \"Tenant 1\", \"appAllocations\" : [ { \"appId\" : \"" + random1 + "\" , \"maxUser\" : 4} , { \"appId\"" + ": \"" + random2 + "\" , \"maxUser\" : 42}]}"); @@ -431,12 +431,32 @@ public void testPositionalOperatorWithElemMatch() { assertEquals(expected.toString(), updateEngine.doUpdate(object, update, query, false).toString()); } - + + @Test + public void testPositionalOperatorWithElemMatch2() { + String random1 = UUID.randomUUID().toString(); + String random2 = UUID.randomUUID().toString(); + + DBObject object = (DBObject) FongoJSON.parse("{ \"name\" : \"Tenant 1\", \"appAllocations\" : [ { \"appId\" : \"" + + random1 + "\" , \"maxUser\" : 4} , { \"appId\"" + + ": \"" + random2 + "\" , \"maxUser\" : 42}]}"); + DBObject query = (DBObject) FongoJSON.parse("{ \"appAllocations\" : { \"$elemMatch\" : { \"appId\" : \"" + random1 + "\"}}}"); + DBObject update = (DBObject) FongoJSON.parse("{ \"$set\" : { \"appAllocations.$\" : { \"appId\" : \"" + random1 + "\", \"maxUser\" : 9 }}}"); + + DBObject expected = (DBObject) FongoJSON.parse("{ \"name\" : \"Tenant 1\", \"appAllocations\" : [ { \"appId\" : \"" + + random1 + "\" , \"maxUser\" : 9} , { \"appId\"" + + ": \"" + random2 + "\" , \"maxUser\" : 42}]}"); + UpdateEngine updateEngine = new UpdateEngine(); + + assertEquals(expected.toString(), + updateEngine.doUpdate(object, update, query, false).toString()); + } + @Test public void testAddOrReplaceElementMustWorkWithDollarOperator() { String random1 = UUID.randomUUID().toString(); String random2 = UUID.randomUUID().toString(); - + DBObject object = (DBObject) FongoJSON.parse("{ \"_id\" : \"1234\" , \"var1\" : \"val1\" , \"parentObject\" : { \"var2\" : \"val21\" , \"subObject\" : [ { \"_id\" : \"" + random1 + "\" , \"var3\" : \"val31\"}, { \"_id\" : \"" + random2 + "\" , \"var3\" : \"val32\"}]}}"); DBObject update = (DBObject) FongoJSON.parse("{ \"$set\" : { \"parentObject.subObject.$\" : { \"_id\" : \"" + random1 + "\" , \"var3\" : \"val33\"}}}"); DBObject query = (DBObject) FongoJSON.parse("{ \"_id\" : \"1234\" , \"parentObject.subObject._id\" : \"" + random1 + "\"}"); From 5c2ec0454f30d0c9e11bfb2c6df73d1d91328527 Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 29 Mar 2020 13:31:25 +0200 Subject: [PATCH 2/3] Fix nested when postPath is empty --- src/main/java/com/github/fakemongo/impl/UpdateEngine.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/fakemongo/impl/UpdateEngine.java b/src/main/java/com/github/fakemongo/impl/UpdateEngine.java index d9f4e66b..a5b0ce42 100644 --- a/src/main/java/com/github/fakemongo/impl/UpdateEngine.java +++ b/src/main/java/com/github/fakemongo/impl/UpdateEngine.java @@ -136,7 +136,11 @@ public void handlePositionalUpdate(final String updateKey, Object object, List v } else { //this is kind of a waste DBObject o = ExpressionParser.isDbObject(listItem) ? ExpressionParser.toDbObject(listItem) : new BasicDBObject(prePath, listItem); - if (filter.apply(o)) { + BasicDBList listWithSingleItem = new BasicDBList(); + listWithSingleItem.add(listItem); + if (filter.apply(o) || + //Case of a nested $elemMatch + filter.apply(new BasicDBObject(prePath, listWithSingleItem))) { BasicDBList newList = new BasicDBList(); newList.addAll(valueList); //do not put any data on ownerObj, because the prePath can be composed of different parts From 6ba3d7c61cd3497e1a1ff659982458114f7772f7 Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 29 Mar 2020 13:40:09 +0200 Subject: [PATCH 3/3] Rename tests --- src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java b/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java index bb3db48e..90e68f73 100644 --- a/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java +++ b/src/test/java/com/github/fakemongo/impl/UpdateEngineTest.java @@ -413,7 +413,7 @@ public void testPositionalOperator() { } @Test - public void testPositionalOperatorWithElemMatch() { + public void testPositionalOperatorWithElemMatchWithPostPath() { String random1 = UUID.randomUUID().toString(); String random2 = UUID.randomUUID().toString(); @@ -433,7 +433,7 @@ public void testPositionalOperatorWithElemMatch() { } @Test - public void testPositionalOperatorWithElemMatch2() { + public void testPositionalOperatorWithElemMatchWithoutPostPath() { String random1 = UUID.randomUUID().toString(); String random2 = UUID.randomUUID().toString();