-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Remove assertion for verbose result #1835
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vbabanin
wants to merge
5
commits into
mongodb:main
Choose a base branch
from
vbabanin:assertion-removal
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
54930e0
Remove assertion.
vbabanin 11ff53a
Add TODOs.
vbabanin 307f498
Add tests,
vbabanin ab5c67f
Merge branch 'main' into assertion-removal
vbabanin 46b3513
Update driver-core/src/test/unit/com/mongodb/internal/operation/Clien…
vbabanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
217 changes: 217 additions & 0 deletions
217
driver-core/src/test/unit/com/mongodb/internal/operation/ClientBulkWriteOperationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| /* | ||
| * Copyright 2008-present MongoDB, 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. | ||
| */ | ||
|
|
||
| package com.mongodb.internal.operation; | ||
|
|
||
| import com.mongodb.ClusterFixture; | ||
| import com.mongodb.MongoNamespace; | ||
| import com.mongodb.ServerAddress; | ||
| import com.mongodb.WriteConcern; | ||
| import com.mongodb.client.model.Filters; | ||
| import com.mongodb.client.model.bulk.ClientBulkWriteOptions; | ||
| import com.mongodb.client.model.bulk.ClientBulkWriteResult; | ||
| import com.mongodb.client.model.bulk.ClientNamespacedReplaceOneModel; | ||
| import com.mongodb.client.model.bulk.ClientNamespacedWriteModel; | ||
| import com.mongodb.connection.ClusterId; | ||
| import com.mongodb.connection.ConnectionDescription; | ||
| import com.mongodb.connection.ServerConnectionState; | ||
| import com.mongodb.connection.ServerDescription; | ||
| import com.mongodb.connection.ServerId; | ||
| import com.mongodb.connection.ServerType; | ||
| import com.mongodb.internal.binding.ConnectionSource; | ||
| import com.mongodb.internal.binding.ReadWriteBinding; | ||
| import com.mongodb.internal.connection.Connection; | ||
| import com.mongodb.internal.connection.DualMessageSequences; | ||
| import com.mongodb.internal.connection.OperationContext; | ||
| import org.bson.BsonBinaryWriter; | ||
| import org.bson.BsonDocument; | ||
| import org.bson.Document; | ||
| import org.bson.codecs.Codec; | ||
| import org.bson.codecs.DecoderContext; | ||
| import org.bson.io.BasicOutputBuffer; | ||
| import org.bson.json.JsonReader; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Answers; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import static com.mongodb.MongoClientSettings.getDefaultCodecRegistry; | ||
| import static com.mongodb.client.model.bulk.ClientReplaceOneOptions.clientReplaceOneOptions; | ||
| import static java.util.Collections.singletonList; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyBoolean; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.isNull; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| class ClientBulkWriteOperationTest { | ||
| private static final MongoNamespace NAMESPACE = new MongoNamespace("testDb.testCol"); | ||
| @Mock(answer = Answers.RETURNS_SMART_NULLS) | ||
| private Connection connection; | ||
| @Mock(answer = Answers.RETURNS_SMART_NULLS) | ||
| private ConnectionSource connectionSource; | ||
| @Mock(answer = Answers.RETURNS_SMART_NULLS) | ||
| private ReadWriteBinding binding; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| when(connection.getDescription()).thenReturn(new ConnectionDescription(new ServerId(new ClusterId("test"), new ServerAddress()))); | ||
| when(connectionSource.getConnection(any(OperationContext.class))).thenReturn(connection); | ||
| when(connectionSource.getServerDescription()).thenReturn(ServerDescription.builder().address(new ServerAddress()) | ||
| .state(ServerConnectionState.CONNECTED) | ||
| .type(ServerType.STANDALONE) | ||
| .build()); | ||
| when(binding.getWriteConnectionSource(any(OperationContext.class))).thenReturn(connectionSource); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * This test exists due to SERVER-113344 bug. | ||
| */ | ||
| //TODO-JAVA-6002 | ||
| @Test | ||
| void shouldIgnoreSuccessfulCursorResultWhenVerboseResultIsFalse() { | ||
| //given | ||
| mockCommandExecutionResult( | ||
| "{'cursor': {" | ||
| + " 'id': NumberLong(0)," | ||
| + " 'firstBatch': [ { 'ok': 1, 'idx': 0, 'n': 1, 'upserted': { '_id': 1 } } ]," | ||
| + " 'ns': 'admin.$cmd.bulkWrite'" | ||
| + "}," | ||
| + " 'nErrors': 0," | ||
| + " 'nInserted': 0," | ||
| + " 'nMatched': 0," | ||
| + " 'nModified': 0," | ||
| + " 'nUpserted': 1," | ||
| + " 'nDeleted': 0," | ||
| + " 'ok': 1" | ||
| + "}" | ||
| ); | ||
| ClientBulkWriteOptions options = ClientBulkWriteOptions.clientBulkWriteOptions() | ||
| .ordered(false).verboseResults(false); | ||
| List<ClientNamespacedReplaceOneModel> clientNamespacedReplaceOneModels = singletonList(ClientNamespacedWriteModel.replaceOne( | ||
| NAMESPACE, | ||
| Filters.empty(), | ||
| new Document(), | ||
| clientReplaceOneOptions().upsert(true) | ||
| )); | ||
| ClientBulkWriteOperation op = new ClientBulkWriteOperation( | ||
| clientNamespacedReplaceOneModels, | ||
| options, | ||
| WriteConcern.ACKNOWLEDGED, | ||
| false, | ||
| getDefaultCodecRegistry()); | ||
| //when | ||
| ClientBulkWriteResult result = op.execute(binding, ClusterFixture.OPERATION_CONTEXT); | ||
|
|
||
| //then | ||
| assertEquals(0, result.getInsertedCount()); | ||
| assertEquals(1, result.getUpsertedCount()); | ||
| assertEquals(0, result.getMatchedCount()); | ||
| assertEquals(0, result.getModifiedCount()); | ||
| assertEquals(0, result.getDeletedCount()); | ||
| assertFalse(result.getVerboseResults().isPresent()); | ||
| } | ||
|
|
||
| /** | ||
| * This test exists due to SERVER-113026 bug. | ||
| */ | ||
| //TODO-JAVA-6005 | ||
| @Test | ||
| void shouldUseDefaultNumberOfModifiedDocumentsWhenMissingInCursor() { | ||
| //given | ||
| mockCommandExecutionResult("{" | ||
| + " cursor: {" | ||
| + " id: NumberLong(0)," | ||
| + " firstBatch: [ {" | ||
| + " 'ok': 1.0," | ||
| + " 'idx': 0," | ||
| + " 'n': 1," | ||
| //nModified field is missing here | ||
| + " 'upserted': {" | ||
| + " '_id': 1" | ||
| + " }" | ||
| + " }]," | ||
| + " ns: 'admin.$cmd.bulkWrite'" | ||
| + " }," | ||
| + " nErrors: 0," | ||
| + " nInserted: 1," | ||
| + " nMatched: 0," | ||
| + " nModified: 0," | ||
| + " nUpserted: 1," | ||
| + " nDeleted: 0," | ||
| + " ok: 1" | ||
| + "}"); | ||
| ClientBulkWriteOptions options = ClientBulkWriteOptions.clientBulkWriteOptions() | ||
| .ordered(false).verboseResults(true); | ||
| List<ClientNamespacedReplaceOneModel> clientNamespacedReplaceOneModels = singletonList(ClientNamespacedWriteModel.replaceOne( | ||
| NAMESPACE, | ||
| Filters.empty(), | ||
| new Document(), | ||
| clientReplaceOneOptions().upsert(true) | ||
| )); | ||
| ClientBulkWriteOperation op = new ClientBulkWriteOperation( | ||
| clientNamespacedReplaceOneModels, | ||
| options, | ||
| WriteConcern.ACKNOWLEDGED, | ||
| false, | ||
| getDefaultCodecRegistry()); | ||
| //when | ||
| ClientBulkWriteResult result = op.execute(binding, ClusterFixture.OPERATION_CONTEXT); | ||
|
|
||
| //then | ||
| assertEquals(1, result.getInsertedCount()); | ||
| assertEquals(1, result.getUpsertedCount()); | ||
| assertEquals(0, result.getMatchedCount()); | ||
| assertEquals(0, result.getModifiedCount()); | ||
| assertEquals(0, result.getDeletedCount()); | ||
| assertTrue(result.getVerboseResults().isPresent()); | ||
| } | ||
|
|
||
| private void mockCommandExecutionResult(final String serverResponse) { | ||
| when(connection.command( | ||
| anyString(), | ||
| any(BsonDocument.class), | ||
| any(), | ||
| isNull(), | ||
| any(), | ||
| any(OperationContext.class), | ||
| anyBoolean(), | ||
| any(DualMessageSequences.class)) | ||
| ).thenAnswer(invocationOnMock -> { | ||
| DualMessageSequences dualMessageSequences = invocationOnMock.getArgument(7); | ||
| dualMessageSequences.encodeDocuments(write -> { | ||
| write.doAndGetBatchCount(new BsonBinaryWriter(new BasicOutputBuffer()), new BsonBinaryWriter(new BasicOutputBuffer())); | ||
| return DualMessageSequences.WritersProviderAndLimitsChecker.WriteResult.OK_LIMIT_NOT_REACHED; | ||
| }); | ||
| return toBsonDocument(serverResponse); | ||
| }); | ||
| } | ||
|
|
||
| private static BsonDocument toBsonDocument(final String serverResponse) { | ||
| Codec<BsonDocument> bsonDocumentCodec = | ||
| CommandResultDocumentCodec.create(getDefaultCodecRegistry().get(BsonDocument.class), CommandBatchCursorHelper.FIRST_BATCH); | ||
| return bsonDocumentCodec.decode(new JsonReader(serverResponse), DecoderContext.builder().build()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
TODOcomments made the method larger and harder to follow. To keep the control flow maintainable, I moved one of the logical branches into a separate method. This keeps the main if/else structure concise and makes each path easier to identify and understand.