Skip to content

Commit

Permalink
Fix counts for upsert operation
Browse files Browse the repository at this point in the history
  • Loading branch information
dparamoshkin authored and asereda-gs committed Oct 24, 2022
1 parent 729f330 commit b511f4e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Expand Up @@ -237,7 +237,7 @@ private <T> Publisher<WriteResult> update(StandardOperations.Update operation) {
Publisher<BulkWriteResult> publisher = ((MongoCollection<Object>) collection).bulkWrite(docs);
return Flowable.fromPublisher(publisher).map(x -> WriteResult.empty()
.withUpdatedCount(x.getModifiedCount())
.withInsertedCount(x.getInsertedCount())
.withInsertedCount(operation.upsert() ? x.getUpserts().size() : x.getInsertedCount())
.withDeletedCount(x.getDeletedCount()));
}

Expand Down
Expand Up @@ -88,4 +88,24 @@ void update() {
check(result2.insertedCount().getAsLong()).is(0L);
check(result2.deletedCount().getAsLong()).is(0L);
}

@Test
void upsert() {
ImmutablePerson person1 = generator.next();
WriteResult result = repository.upsert(person1);
check(result.insertedCount().getAsLong()).is(1L);
check(result.deletedCount().getAsLong()).is(0L);
check(result.updatedCount().getAsLong()).is(0L);

result = repository.upsert(person1.withFullName("name1"));
check(result.insertedCount().getAsLong()).is(0L);
check(result.deletedCount().getAsLong()).is(0L);
check(result.updatedCount().getAsLong()).is(1L);

WriteResult result2 = repository.upsertAll(ImmutableList.of(person1.withFullName("name2"), generator.next()));
check(result2.insertedCount().getAsLong()).is(1L);
check(result2.deletedCount().getAsLong()).is(0L);
check(result2.updatedCount().getAsLong()).is(1L);
}

}

0 comments on commit b511f4e

Please sign in to comment.