tokens for updater and unit tests

This commit is contained in:
Sean Garrett
2023-07-05 20:20:08 +01:00
parent dc7a4dc67b
commit be58460bf1
9 changed files with 2978 additions and 98 deletions
+4
View File
@@ -30,4 +30,8 @@
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="KeyTypedRepositoryTests\UpdateTests\" />
</ItemGroup>
</Project>
@@ -0,0 +1,582 @@
using System;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Update;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
public class UpdateManyAsyncTests : GenericTestContext<MongoDbUpdater>
{
[Fact]
public async Task WithFilterAndUpdateDefinition_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filter,
updateDefinition);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterAndUpdateDefinitionAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
var token = new CancellationToken(true);
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filter,
updateDefinition,
cancellationToken: token);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterAndUpdateDefinitionAndPartitionKey_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count, partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filter,
updateDefinition,
partitionKey);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count, partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
var token = new CancellationToken(true);
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filter,
updateDefinition,
partitionKey,
cancellationToken: token);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterAndFieldExpressionAndValue_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filter,
fieldExpression,
value);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndFieldExpressionAndValue_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var count = Fixture.Create<long>();
var collection = SetupCollection(count, partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var count = Fixture.Create<long>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndUpdateDefinition_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filterExpression,
updateDefinition);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filterExpression,
updateDefinition,
cancellationToken: token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var count = Fixture.Create<long>();
var collection = SetupCollection(count, partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filterExpression,
updateDefinition,
partitionKey);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var count = Fixture.Create<long>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
filterExpression,
updateDefinition,
partitionKey,
token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
{
var replacedId = Fixture.Create<Guid>();
var replaceResult = new ReplaceOneResult.Acknowledged(count, count, BsonValue.Create(replacedId));
var updateResult = new UpdateResult.Acknowledged(count, count, BsonValue.Create(replacedId));
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(
x => x.ReplaceOneAsync(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(replaceResult);
collection
.Setup(
x => x.UpdateManyAsync(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(updateResult);
collection
.Setup(
x => x.ReplaceOneAsync(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(replaceResult);
collection
.Setup(
x => x.UpdateManyAsync(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(updateResult);
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
.Returns(collection.Object);
return collection;
}
}
@@ -0,0 +1,581 @@
using System;
using System.Linq.Expressions;
using System.Threading;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Update;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
public class UpdateManyTests : GenericTestContext<MongoDbUpdater>
{
[Fact]
public void WithFilterAndUpdateDefinition_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filter,
updateDefinition);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterAndUpdateDefinitionAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
var token = new CancellationToken(true);
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filter,
updateDefinition,
cancellationToken: token);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterAndUpdateDefinitionAndPartitionKey_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count, partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filter,
updateDefinition,
partitionKey);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count, partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
var token = new CancellationToken(true);
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filter,
updateDefinition,
partitionKey,
token);
// Assert
result.Should().Be(count);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterAndFieldExpressionAndValue_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filter,
fieldExpression,
value);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndFieldExpressionAndValue_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var collection = SetupCollection(count);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var count = Fixture.Create<long>();
var collection = SetupCollection(count, partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var count = Fixture.Create<long>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateMany<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndUpdateDefinition_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filterExpression,
updateDefinition);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_UpdatesMany()
{
// Arrange
var count = Fixture.Create<long>();
var value = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var collection = SetupCollection(count);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filterExpression,
updateDefinition,
cancellationToken: token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var count = Fixture.Create<long>();
var collection = SetupCollection(count, partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filterExpression,
updateDefinition,
partitionKey);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
{
// Arrange
var value = Fixture.Create<string>();
var count = Fixture.Create<long>();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(count, partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
// Act
var result = Sut.UpdateMany<TestDocument, Guid>(
filterExpression,
updateDefinition,
partitionKey,
token);
// Assert
result.Should().Be(count);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
{
var replacedId = Fixture.Create<Guid>();
var replaceResult = new ReplaceOneResult.Acknowledged(count, count, BsonValue.Create(replacedId));
var updateResult = new UpdateResult.Acknowledged(count, count, BsonValue.Create(replacedId));
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(
x => x.ReplaceOneAsync(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(replaceResult);
collection
.Setup(
x => x.UpdateMany(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.Returns(updateResult);
collection
.Setup(
x => x.ReplaceOneAsync(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(replaceResult);
collection
.Setup(
x => x.UpdateMany(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.Returns(updateResult);
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
.Returns(collection.Object);
return collection;
}
}
@@ -0,0 +1,806 @@
using System;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Update;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
public class UpdateOneAsyncTests : GenericTestContext<MongoDbUpdater>
{
[Fact]
public async Task WithDocument_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithDocumentAndCancellationToken_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
var collection = SetupCollection();
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
token),
Times.Once());
}
[Fact]
public async Task WithDocumentAndUpdateDefinition_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document, updateDefinition);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
var token = new CancellationToken(true);
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document, updateDefinition, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
token),
Times.Once());
}
[Fact]
public async Task WithDocumentAndFieldExpressionAndValue_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(document, fieldExpression, value);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(document, fieldExpression, value, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndDocument_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndDocumentAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
var collection = SetupCollection();
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
token),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndDocumentAndUpdateDefinition_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document, updateDefinition);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
var token = new CancellationToken(true);
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document, updateDefinition, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
token),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndDocumentAndFieldExpressionAndValue_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(session.Object, document, fieldExpression, value);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(session.Object, document, fieldExpression, value, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
session.Object,
filter,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
session.Object,
filter,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
session.Object,
filter,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
session.Object,
filterExpression,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
session.Object,
filterExpression,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
session.Object,
filterExpression,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOneAsync(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
private Mock<IMongoCollection<TestDocument>> SetupCollection(string partitionKey = null)
{
var replacedId = Fixture.Create<Guid>();
var count = Fixture.Create<long>();
var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, BsonValue.Create(replacedId));
var updateResult = new UpdateResult.Acknowledged(count, 1, BsonValue.Create(replacedId));
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(
x => x.ReplaceOneAsync(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(replaceResult);
collection
.Setup(
x => x.UpdateOneAsync(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(updateResult);
collection
.Setup(
x => x.ReplaceOneAsync(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(replaceResult);
collection
.Setup(
x => x.UpdateOneAsync(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(updateResult);
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
.Returns(collection.Object);
return collection;
}
}
@@ -0,0 +1,805 @@
using System;
using System.Linq.Expressions;
using System.Threading;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Update;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
public class UpdateOneTests : GenericTestContext<MongoDbUpdater>
{
[Fact]
public void WithDocument_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(document);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithDocumentAndCancellationToken_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
var collection = SetupCollection();
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(document, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
token),
Times.Once());
}
[Fact]
public void WithDocumentAndUpdateDefinition_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(document, updateDefinition);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
var token = new CancellationToken(true);
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(document, updateDefinition, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
token),
Times.Once());
}
[Fact]
public void WithDocumentAndFieldExpressionAndValue_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(document, fieldExpression, value);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(document, fieldExpression, value, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
filter,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
filterExpression,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithClientSessionAndDocument_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithClientSessionAndDocumentAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
var collection = SetupCollection();
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
ReplaceOptions expectedOptions = null;
collection
.Verify(
x => x.ReplaceOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
document,
expectedOptions,
token),
Times.Once());
}
[Fact]
public void WithClientSessionAndDocumentAndUpdateDefinition_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document, updateDefinition);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithClientSessionAndDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var collection = SetupCollection();
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
var token = new CancellationToken(true);
// Act
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document, updateDefinition, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
updateDefinition,
null,
token),
Times.Once());
}
[Fact]
public void WithClientSessionAndDocumentAndFieldExpressionAndValue_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(session.Object, document, fieldExpression, value);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithClientSessionAndDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var document = Fixture.Create<TestDocument>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(session.Object, document, fieldExpression, value, token);
// Assert
result.Should().BeTrue();
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
session.Object,
filter,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithClientSessionAndFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
session.Object,
filter,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
session.Object,
filter,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var collection = SetupCollection();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
session.Object,
filterExpression,
fieldExpression,
value,
cancellationToken: token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
[Fact]
public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
session.Object,
filterExpression,
fieldExpression,
value,
partitionKey);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
CancellationToken.None),
Times.Once());
}
[Fact]
public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
{
// Arrange
var session = MockOf<IClientSessionHandle>();
var value = Fixture.Create<string>();
var partitionKey = Fixture.Create<string>();
var collection = SetupCollection(partitionKey);
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
// Act
var result = Sut.UpdateOne<TestDocument, Guid, string>(
session.Object,
filterExpression,
fieldExpression,
value,
partitionKey,
token);
// Assert
result.Should().BeTrue();
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
collection
.Verify(
x => x.UpdateOne(
session.Object,
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
null,
token),
Times.Once());
}
private Mock<IMongoCollection<TestDocument>> SetupCollection(string partitionKey = null)
{
var replacedId = Fixture.Create<Guid>();
var count = Fixture.Create<long>();
var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, BsonValue.Create(replacedId));
var updateResult = new UpdateResult.Acknowledged(count, 1, BsonValue.Create(replacedId));
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(
x => x.ReplaceOne(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.Returns(replaceResult);
collection
.Setup(
x => x.UpdateOne(
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.Returns(updateResult);
collection
.Setup(
x => x.ReplaceOne(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<TestDocument>(),
It.IsAny<ReplaceOptions>(),
It.IsAny<CancellationToken>()))
.Returns(replaceResult);
collection
.Setup(
x => x.UpdateOne(
It.IsAny<IClientSessionHandle>(),
It.IsAny<FilterDefinition<TestDocument>>(),
It.IsAny<UpdateDefinition<TestDocument>>(),
It.IsAny<UpdateOptions>(),
It.IsAny<CancellationToken>()))
.Returns(updateResult);
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
.Returns(collection.Object);
return collection;
}
}
@@ -0,0 +1,21 @@
using System;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
namespace CoreUnitTests.Infrastructure;
public static class UpdateDefinitionExtensions
{
public static bool EquivalentTo<TDocument>(this UpdateDefinition<TDocument> update, UpdateDefinition<TDocument> expected)
{
var renderedUpdate = update.Render(
BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(),
BsonSerializer.SerializerRegistry);
var renderedExpected = expected.Render(
BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(),
BsonSerializer.SerializerRegistry);
return renderedUpdate.Equals(renderedExpected);
}
}
@@ -19,7 +19,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
/// <param name="cancellationToken">An optional cancellation token.</param>
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -30,7 +31,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documentToModify">The document you want to modify.</param>
/// <param name="update">The update definition for the document.</param>
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
/// <param name="cancellationToken">An optional cancellation token.</param>
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -43,7 +45,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="documentToModify">The document you want to modify.</param>
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
/// <param name="cancellationToken">An optional cancellation token.</param>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -57,7 +60,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">An optional cancellation token.</param>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -71,7 +75,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The partition key for the document.</param>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">An optional cancellation token.</param>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -84,7 +89,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -98,7 +103,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="update">The update definition.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -114,7 +119,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="value">The value of the field.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -131,7 +136,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="partitionKey">The optional partition key.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -148,7 +153,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="partitionKey">The optional partition key.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -158,7 +163,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
/// <param name="cancellationToken">The optional cancellation token.</param>
bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -169,7 +175,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documentToModify">The document you want to modify.</param>
/// <param name="update">The update definition for the document.</param>
bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
/// <param name="cancellationToken">The optional cancellation token.</param>
bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -182,7 +189,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="documentToModify">The document you want to modify.</param>
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
/// <param name="cancellationToken">The optional cancellation token.</param>
bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -196,7 +204,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param>
bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -210,7 +219,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The partition key for the document.</param>
bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -223,7 +233,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -237,7 +247,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="update">The update definition.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -253,7 +263,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="value">The value of the field.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -270,7 +280,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="partitionKey">The optional partition key.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -287,7 +297,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="partitionKey">The optional partition key.</param>
/// <param name="cancellationToken">The optional cancellation token.</param>
/// <returns></returns>
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -301,7 +311,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The partition key for the document.</param>
Task<long> UpdateManyAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
Task<long> UpdateManyAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -315,7 +326,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param>
Task<long> UpdateManyAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
Task<long> UpdateManyAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -325,9 +337,10 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">The document filter.</param>
/// <param name="update">the update definiton</param>
/// <param name="update">the update definition</param>
/// <param name="partitionKey">The value of the partition key.</param>
Task<long> UpdateManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
Task<long> UpdateManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -339,7 +352,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="filter">The document filter.</param>
/// <param name="updateDefinition">The update definition.</param>
/// <param name="partitionKey">The value of the partition key.</param>
Task<long> UpdateManyAsync<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
Task<long> UpdateManyAsync<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -353,7 +367,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The partition key for the document.</param>
long UpdateMany<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
long UpdateMany<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -367,7 +382,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="field">The field selector.</param>
/// <param name="value">The new value of the property field.</param>
/// <param name="partitionKey">The value of the partition key.</param>
long UpdateMany<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
long UpdateMany<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
@@ -379,7 +395,8 @@ namespace MongoDbGenericRepository.DataAccess.Update
/// <param name="filter">The document filter.</param>
/// <param name="updateDefinition">The update definition.</param>
/// <param name="partitionKey">The value of the partition key.</param>
long UpdateMany<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
/// <param name="cancellationToken">The optional cancellation token.</param>
long UpdateMany<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
}
@@ -10,7 +10,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
public partial class MongoDbUpdater
{
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -22,7 +22,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default(CancellationToken))
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -32,7 +32,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -42,7 +42,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default(CancellationToken))
public virtual bool UpdateOne<TDocument, TKey>(IClientSessionHandle session, TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -52,7 +52,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -65,7 +65,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default(CancellationToken))
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -75,7 +75,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -85,7 +85,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual Task<bool> UpdateOneAsync<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -93,7 +93,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -103,7 +103,7 @@ namespace MongoDbGenericRepository.DataAccess.Update
}
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default(CancellationToken))
public virtual bool UpdateOne<TDocument, TKey, TField>(IClientSessionHandle session, Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
@@ -1,182 +1,246 @@
using MongoDB.Driver;
using System;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDbGenericRepository.DataAccess.Base;
using MongoDbGenericRepository.Models;
using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
namespace MongoDbGenericRepository.DataAccess.Update
{
/// <summary>
/// The MongoDb updater.
/// The MongoDb updater.
/// </summary>
public partial class MongoDbUpdater : DataAccessBase, IMongoDbUpdater
{
/// <summary>
/// Constructor
/// Constructor
/// </summary>
/// <param name="mongoDbContext"></param>
public MongoDbUpdater(IMongoDbContext mongoDbContext) : base(mongoDbContext)
{
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
var updateRes = await HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOneAsync(filter, modifiedDocument);
var updateRes = await HandlePartitioned<TDocument, TKey>(modifiedDocument)
.ReplaceOneAsync(filter, modifiedDocument, cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", modifiedDocument.Id);
var updateRes = HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOne(filter, modifiedDocument);
var updateRes = HandlePartitioned<TDocument, TKey>(modifiedDocument).ReplaceOne(filter, modifiedDocument, cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey>(
TDocument documentToModify,
UpdateDefinition<TDocument> update,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, update);
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, update, cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, update);
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, update, cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(
TDocument documentToModify,
Expression<Func<TDocument, TField>> field,
TField value,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
var updateRes = await HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOneAsync(
filter,
Builders<TDocument>.Update.Set(field, value),
cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var filter = Builders<TDocument>.Filter.Eq("Id", documentToModify.Id);
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
var updateRes = HandlePartitioned<TDocument, TKey>(documentToModify).UpdateOne(filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(
FilterDefinition<TDocument> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = await collection.UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value));
var updateRes = await collection.UpdateOneAsync(filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<bool> UpdateOneAsync<TDocument, TKey, TField>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await UpdateOneAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
return await UpdateOneAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey, cancellationToken);
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey, TField>(
FilterDefinition<TDocument> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = collection.UpdateOne(filter, Builders<TDocument>.Update.Set(field, value));
var updateRes = collection.UpdateOne(filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken);
return updateRes.ModifiedCount == 1;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual bool UpdateOne<TDocument, TKey, TField>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return UpdateOne<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
return UpdateOne<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey, cancellationToken);
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<long> UpdateManyAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<long> UpdateManyAsync<TDocument, TKey, TField>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await UpdateManyAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
return await UpdateManyAsync<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey, cancellationToken);
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<long> UpdateManyAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<long> UpdateManyAsync<TDocument, TKey, TField>(
FilterDefinition<TDocument> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = await collection.UpdateManyAsync(filter, Builders<TDocument>.Update.Set(field, value));
var updateRes = await collection.UpdateManyAsync(filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken);
return updateRes.ModifiedCount;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<long> UpdateManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, UpdateDefinition<TDocument> update, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<long> UpdateManyAsync<TDocument, TKey>(
Expression<Func<TDocument, bool>> filter,
UpdateDefinition<TDocument> update,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return await UpdateManyAsync<TDocument, TKey>(Builders<TDocument>.Filter.Where(filter), update, partitionKey);
return await UpdateManyAsync<TDocument, TKey>(Builders<TDocument>.Filter.Where(filter), update, partitionKey, cancellationToken);
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual async Task<long> UpdateManyAsync<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual async Task<long> UpdateManyAsync<TDocument, TKey>(
FilterDefinition<TDocument> filter,
UpdateDefinition<TDocument> updateDefinition,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = await collection.UpdateManyAsync(filter, updateDefinition);
var updateRes = await collection.UpdateManyAsync(filter, updateDefinition, cancellationToken: cancellationToken);
return updateRes.ModifiedCount;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual long UpdateMany<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual long UpdateMany<TDocument, TKey, TField>(
Expression<Func<TDocument, bool>> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return UpdateMany<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey);
return UpdateMany<TDocument, TKey, TField>(Builders<TDocument>.Filter.Where(filter), field, value, partitionKey, cancellationToken);
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual long UpdateMany<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual long UpdateMany<TDocument, TKey, TField>(
FilterDefinition<TDocument> filter,
Expression<Func<TDocument, TField>> field,
TField value,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = collection.UpdateMany(filter, Builders<TDocument>.Update.Set(field, value));
var updateRes = collection.UpdateMany(filter, Builders<TDocument>.Update.Set(field, value), cancellationToken: cancellationToken);
return updateRes.ModifiedCount;
}
/// <inheritdoc cref="IMongoDbUpdater"/>
public virtual long UpdateMany<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> updateDefinition, string partitionKey = null)
/// <inheritdoc cref="IMongoDbUpdater" />
public virtual long UpdateMany<TDocument, TKey>(
FilterDefinition<TDocument> filter,
UpdateDefinition<TDocument> updateDefinition,
string partitionKey = null,
CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection<TDocument, TKey>() : GetCollection<TDocument, TKey>(partitionKey);
var updateRes = collection.UpdateMany(filter, updateDefinition);
var updateRes = collection.UpdateMany(filter, updateDefinition, cancellationToken: cancellationToken);
return updateRes.ModifiedCount;
}
}