|
45 | 45 | import java.util.concurrent.ThreadFactory; |
46 | 46 | import java.util.concurrent.TimeUnit; |
47 | 47 | import java.util.regex.Pattern; |
| 48 | +import java.util.stream.Stream; |
48 | 49 |
|
49 | 50 | import org.assertj.core.api.ThrowableAssert.ThrowingCallable; |
50 | 51 | import org.bson.BsonInt32; |
|
65 | 66 | import org.junit.jupiter.api.Test; |
66 | 67 | import org.junit.jupiter.api.TestInfo; |
67 | 68 | import org.junit.jupiter.params.ParameterizedTest; |
| 69 | +import org.junit.jupiter.params.provider.Arguments; |
68 | 70 | import org.junit.jupiter.params.provider.EnumSource; |
| 71 | +import org.junit.jupiter.params.provider.MethodSource; |
69 | 72 | import org.junit.jupiter.params.provider.ValueSource; |
70 | 73 | import org.mockito.AdditionalAnswers; |
71 | 74 | import org.mockito.Mockito; |
@@ -1436,6 +1439,38 @@ public void testFindOneAndUpdateWithArrayFilters() { |
1436 | 1439 | .isEqualTo(json("_id: 3, a: {b: [1, 20, 3]}")); |
1437 | 1440 | } |
1438 | 1441 |
|
| 1442 | + private static Stream<Arguments> findAndUpdate_upsert_idInQueryArguments() { |
| 1443 | + return Stream.of( |
| 1444 | + Arguments.of("_id: 'some value'", false, "_id: 'some value', value: 100"), |
| 1445 | + Arguments.of("'$and': [{_id: 'some value'}]", false, "_id: 'some value', value: 100"), |
| 1446 | + Arguments.of("'$and': [{_id: 'some value'}, {other: 123}]", false, "_id: 'some value', other: 123, value: 100"), |
| 1447 | + Arguments.of("'$or': [{_id: 'some value'}]", false, "_id: 'some value', value: 100"), |
| 1448 | + Arguments.of("'$or': [{_id: 'some value'}, {other: 123}]", true, "value: 100"), |
| 1449 | + Arguments.of("'$or': [{_id: 'some value', other: 123}]", false, "_id: 'some value', other: 123, value: 100"), |
| 1450 | + Arguments.of("'$or': [{_id: 'some value'}, {$and: [{other: 123}, {more: 'abc'}]}]", true, "value: 100"), |
| 1451 | + Arguments.of("'$and': [{other: 123}, {$and: [{_id: 'some value'}, {more: 'abc'}]}]", false, "_id: 'some value', other: 123, more: 'abc', value: 100") |
| 1452 | + ); |
| 1453 | + } |
| 1454 | + |
| 1455 | + // https://github.com/bwaldvogel/mongo-java-server/issues/208 |
| 1456 | + @ParameterizedTest |
| 1457 | + @MethodSource("findAndUpdate_upsert_idInQueryArguments") |
| 1458 | + void testFindAndUpdate_upsert_idInQuery(String query, boolean randomObjectIdExpected, String expectedDocument) throws Exception { |
| 1459 | + collection.findOneAndUpdate( |
| 1460 | + json(query), |
| 1461 | + json("{'$set': {value: 100}}"), |
| 1462 | + new FindOneAndUpdateOptions().upsert(true)); |
| 1463 | + |
| 1464 | + if (randomObjectIdExpected) { |
| 1465 | + Document document = collection.find().first(); |
| 1466 | + assertThat(document.remove("_id")).isInstanceOf(ObjectId.class); |
| 1467 | + assertThat(document).isEqualTo(json(expectedDocument)); |
| 1468 | + } else { |
| 1469 | + assertThat(collection.find()) |
| 1470 | + .containsExactly(json(expectedDocument)); |
| 1471 | + } |
| 1472 | + } |
| 1473 | + |
1439 | 1474 | // https://github.com/bwaldvogel/mongo-java-server/issues/60 |
1440 | 1475 | @Test |
1441 | 1476 | public void testUpdateManyWithArrayFilters() { |
|
0 commit comments