unit tests for repo update methods
This commit is contained in:
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -30,8 +30,4 @@
|
||||
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="KeyTypedRepositoryTests\UpdateTests\" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -5,6 +5,7 @@ using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
@@ -23,4 +24,6 @@ public class TestKeyedMongoRepository<TKey> : BaseMongoRepository<TKey>
|
||||
public void SetReader(IMongoDbReader reader) => MongoDbReader = reader;
|
||||
|
||||
public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser;
|
||||
|
||||
public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
@@ -54,6 +55,11 @@ public class TestKeyedMongoRepositoryContext<TKey>
|
||||
sut.SetEraser(Eraser.Object);
|
||||
}
|
||||
|
||||
if (Updater != null)
|
||||
{
|
||||
sut.SetUpdater(Updater.Object);
|
||||
}
|
||||
|
||||
return sut;
|
||||
}
|
||||
}
|
||||
@@ -65,4 +71,7 @@ public class TestKeyedMongoRepositoryContext<TKey>
|
||||
protected Mock<IMongoDbReader> Reader { get; set; }
|
||||
|
||||
protected Mock<IMongoDbEraser> Eraser { get; set; }
|
||||
|
||||
protected Mock<IMongoDbUpdater> Updater { get; set; }
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
@@ -14,23 +15,13 @@ public class TestMongoRepository : BaseMongoRepository
|
||||
{
|
||||
}
|
||||
|
||||
public void SetIndexHandler(IMongoDbIndexHandler indexHandler)
|
||||
{
|
||||
MongoDbIndexHandler = indexHandler;
|
||||
}
|
||||
public void SetIndexHandler(IMongoDbIndexHandler indexHandler) => MongoDbIndexHandler = indexHandler;
|
||||
|
||||
public void SetDbCreator(IMongoDbCreator creator)
|
||||
{
|
||||
MongoDbCreator = creator;
|
||||
}
|
||||
public void SetDbCreator(IMongoDbCreator creator) => MongoDbCreator = creator;
|
||||
|
||||
public void SetReader(IMongoDbReader reader)
|
||||
{
|
||||
MongoDbReader = reader;
|
||||
}
|
||||
public void SetReader(IMongoDbReader reader) => MongoDbReader = reader;
|
||||
|
||||
public void SetEraser(IMongoDbEraser eraser)
|
||||
{
|
||||
MongoDbEraser = eraser;
|
||||
}
|
||||
}
|
||||
public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser;
|
||||
|
||||
public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
@@ -48,6 +49,11 @@ public class TestMongoRepositoryContext
|
||||
{
|
||||
_sut.SetEraser(Eraser.Object);
|
||||
}
|
||||
|
||||
if (Updater != null)
|
||||
{
|
||||
_sut.SetUpdater(Updater.Object);
|
||||
}
|
||||
}
|
||||
|
||||
return _sut;
|
||||
@@ -61,4 +67,6 @@ public class TestMongoRepositoryContext
|
||||
protected Mock<IMongoDbReader> Reader { get; set; }
|
||||
|
||||
protected Mock<IMongoDbEraser> Eraser { get; set; }
|
||||
}
|
||||
|
||||
protected Mock<IMongoDbUpdater> Updater { get; set; }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,533 @@
|
||||
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.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests;
|
||||
|
||||
public class UpdateManyAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
private readonly UpdateDefinition<TestDocumentWithKey<int>> updateDefinition = Builders<TestDocumentWithKey<int>>.Update.Set(x => x.SomeContent, "Updated");
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, string>> fieldExpression = x => x.SomeContent;
|
||||
private readonly FilterDefinition<TestDocumentWithKey<int>> filterDefinition = Builders<TestDocumentWithKey<int>>.Filter.Eq(x => x.Id, 1);
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, bool>> filterExpression = x => x.SomeContent == "SomeContent";
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateManyAsync<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,532 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests;
|
||||
|
||||
public class UpdateManyTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, string>> fieldExpression = x => x.SomeContent;
|
||||
private readonly FilterDefinition<TestDocumentWithKey<int>> filterDefinition = Builders<TestDocumentWithKey<int>>.Filter.Eq(x => x.Id, 1);
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, bool>> filterExpression = x => x.SomeContent == "SomeContent";
|
||||
private readonly UpdateDefinition<TestDocumentWithKey<int>> updateDefinition = Builders<TestDocumentWithKey<int>>.Update.Set(x => x.SomeContent, "Updated");
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, updateDefinition);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, updateDefinition);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateMany<TestDocumentWithKey<int>, int>(
|
||||
filterDefinition,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().Be(count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,432 @@
|
||||
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.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests;
|
||||
|
||||
public class UpdateOneAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
private readonly UpdateDefinition<TestDocumentWithKey<int>> updateDefinition = Builders<TestDocumentWithKey<int>>.Update.Set(x => x.SomeContent, "Updated");
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, string>> fieldExpression = x => x.SomeContent;
|
||||
private readonly FilterDefinition<TestDocumentWithKey<int>> filterDefinition = Builders<TestDocumentWithKey<int>>.Filter.Eq(x => x.Id, 1);
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, bool>> filterExpression = x => x.SomeContent == "SomeContent";
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocument_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(document);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(document, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndUpdateDefinition_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(document, updateDefinition);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(
|
||||
document,
|
||||
updateDefinition,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(document, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int>(
|
||||
document,
|
||||
updateDefinition,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var value = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(document, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
document,
|
||||
fieldExpression,
|
||||
value,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(document, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
document,
|
||||
fieldExpression,
|
||||
value,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOneAsync<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,432 @@
|
||||
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.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.UpdateTests;
|
||||
|
||||
public class UpdateOneTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
private readonly UpdateDefinition<TestDocumentWithKey<int>> updateDefinition = Builders<TestDocumentWithKey<int>>.Update.Set(x => x.SomeContent, "Updated");
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, string>> fieldExpression = x => x.SomeContent;
|
||||
private readonly FilterDefinition<TestDocumentWithKey<int>> filterDefinition = Builders<TestDocumentWithKey<int>>.Filter.Eq(x => x.Id, 1);
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, bool>> filterExpression = x => x.SomeContent == "SomeContent";
|
||||
|
||||
[Fact]
|
||||
public void WithDocument_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(x => x.UpdateOne<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(document);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(x => x.UpdateOne<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(x => x.UpdateOne<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(document, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(x => x.UpdateOne<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndUpdateDefinition_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(document, updateDefinition);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int>(
|
||||
document,
|
||||
updateDefinition,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(document, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int>(
|
||||
document,
|
||||
updateDefinition,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var value = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(document, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
document,
|
||||
fieldExpression,
|
||||
value,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(document, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
document,
|
||||
fieldExpression,
|
||||
value,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterDefinition, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<FilterDefinition<TestDocumentWithKey<int>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterDefinition,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterExpression, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterExpression, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Updater = new Mock<IMongoDbUpdater>();
|
||||
Updater
|
||||
.Setup(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, string>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Updater.Verify(
|
||||
x => x.UpdateOne<TestDocumentWithKey<int>, int, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
@@ -504,5 +504,22 @@ namespace MongoDbGenericRepository.DataAccess.Update
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// For the entities selected by the filter, apply the update definition.
|
||||
/// </summary>
|
||||
/// <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="updateDefinition">The update definition.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
/// <param name="cancellationToken">The optional cancellation token.</param>
|
||||
long UpdateMany<TDocument, TKey>(
|
||||
Expression<Func<TDocument, bool>> filter,
|
||||
UpdateDefinition<TDocument> updateDefinition,
|
||||
string partitionKey = null,
|
||||
CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
}
|
||||
}
|
||||
@@ -253,5 +253,19 @@ namespace MongoDbGenericRepository.DataAccess.Update
|
||||
var updateRes = collection.UpdateMany(filter, updateDefinition, cancellationToken: cancellationToken);
|
||||
return updateRes.ModifiedCount;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="IMongoDbUpdater" />
|
||||
public virtual long UpdateMany<TDocument, TKey>(
|
||||
Expression<Func<TDocument, bool>> 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, cancellationToken: cancellationToken);
|
||||
return updateRes.ModifiedCount;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user