diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs new file mode 100644 index 0000000..bc9d818 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs @@ -0,0 +1,1055 @@ +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.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateManyAsyncTests : TestMongoRepositoryContext +{ + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #region Keyed + + private readonly UpdateDefinition> keyedUpdateDefinition = + Builders>.Update.Set(x => x.SomeContent, "Updated"); + + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyTests.cs new file mode 100644 index 0000000..7ca972e --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateManyTests.cs @@ -0,0 +1,1053 @@ +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.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateManyTests : TestMongoRepositoryContext +{ + + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany( + It.IsAny>(), + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #region Keyed + + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition> keyedUpdateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterExpression, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterExpression, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany, int>(keyedFilterDefinition, keyedUpdateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + keyedFilterDefinition, + keyedUpdateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs new file mode 100644 index 0000000..9cc0421 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs @@ -0,0 +1,1318 @@ +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.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateOneAsyncTests : TestMongoRepositoryContext +{ + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document); + + // Assert + Updater.Verify(x => x.UpdateOneAsync(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync(It.IsAny(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #region Keyed + + private readonly UpdateDefinition> keyedUpdateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task Keyed_WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + var expectedFilter = Builders>.Filter.Where(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + It.Is>>(y => y.EquivalentTo(expectedFilter)), + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion + + #region ClientSession + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocument_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + session, + document, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(session, document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + session, + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int>(session, document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + session, + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task Keyed_WithClientSessionHandlerAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny(), + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + session, + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneTests.cs new file mode 100644 index 0000000..7e1fe53 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/UpdateTests/UpdateOneTests.cs @@ -0,0 +1,1315 @@ +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.BaseMongoRepositoryTests.UpdateTests; + +public class UpdateOneTests : TestMongoRepositoryContext +{ + private readonly UpdateDefinition updateDefinition = Builders.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition filterDefinition = Builders.Filter.Eq(x => x.SomeContent, "SomeContent"); + private readonly Expression> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne(It.IsAny(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document); + + // Assert + Updater.Verify(x => x.UpdateOne(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne(It.IsAny(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, token); + + // Assert + Updater.Verify(x => x.UpdateOne(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #region Keyed + + private readonly UpdateDefinition> keyedUpdateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> keyedFieldExpression = x => x.SomeContent; + private readonly FilterDefinition> keyedFilterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> keyedFilterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public void Keyed_WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document, token); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + keyedFilterExpression, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion + + #region client session + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var session = new Mock().Object; + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document); + + // Assert + Updater.Verify(x => x.UpdateOne, int>( + session, + document, + CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + var session = new Mock().Object; + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document, token); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(session, document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document, keyedUpdateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + session, + document, + keyedUpdateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny(), + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int>(session, document, keyedUpdateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + session, + document, + keyedUpdateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, document, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + document, + keyedFieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerAndDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, document, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + document, + keyedFieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterDefinition, keyedFieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + keyedFilterDefinition, + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, token); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void Keyed_WithClientSessionHandlerFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var session = new Mock().Object; + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny(), + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne, int, string>(session, keyedFilterExpression, keyedFieldExpression, value, partitionKey, token); + + // Assert + var expectedFilterDefinition = new ExpressionFilterDefinition>(keyedFilterExpression); + Updater.Verify( + x => x.UpdateOne, int, string>( + session, + It.Is>>(y => y.EquivalentTo(expectedFilterDefinition)), + keyedFieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + #endregion +} diff --git a/CoreUnitTests/CoreUnitTests.csproj b/CoreUnitTests/CoreUnitTests.csproj index 58aa65b..d417073 100644 --- a/CoreUnitTests/CoreUnitTests.csproj +++ b/CoreUnitTests/CoreUnitTests.csproj @@ -30,8 +30,4 @@ - - - - diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs index 69f219d..b6e1377 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepository.cs @@ -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 : BaseMongoRepository public void SetReader(IMongoDbReader reader) => MongoDbReader = reader; public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser; + + public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater; } diff --git a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs index 2b86c1a..91a988d 100644 --- a/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestKeyedMongoRepositoryContext.cs @@ -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 sut.SetEraser(Eraser.Object); } + if (Updater != null) + { + sut.SetUpdater(Updater.Object); + } + return sut; } } @@ -65,4 +71,7 @@ public class TestKeyedMongoRepositoryContext protected Mock Reader { get; set; } protected Mock Eraser { get; set; } + + protected Mock Updater { get; set; } + } diff --git a/CoreUnitTests/Infrastructure/TestMongoRepository.cs b/CoreUnitTests/Infrastructure/TestMongoRepository.cs index b204f07..4252a4f 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepository.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepository.cs @@ -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; - } -} \ No newline at end of file + public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser; + + public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater; +} diff --git a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs index 6dea5f0..c335e13 100644 --- a/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs +++ b/CoreUnitTests/Infrastructure/TestMongoRepositoryContext.cs @@ -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 Reader { get; set; } protected Mock Eraser { get; set; } -} \ No newline at end of file + + protected Mock Updater { get; set; } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs new file mode 100644 index 0000000..f8d663c --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyAsyncTests.cs @@ -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 +{ + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, 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(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, 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(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, 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(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, 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(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public async Task WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateManyAsync, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(count); + + // Act + var result = await Sut.UpdateManyAsync(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateManyAsync, int>( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyTests.cs new file mode 100644 index 0000000..01425e6 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateManyTests.cs @@ -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 +{ + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var count = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny, bool>>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterExpression, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterExpression, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + null, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var token = new CancellationToken(true); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + null, + token), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().Be(count); + } + + [Fact] + public void WithFilterDefinitionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var count = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateMany, int>( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(count); + + // Act + var result = Sut.UpdateMany(filterDefinition, updateDefinition, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateMany, int>( + filterDefinition, + updateDefinition, + partitionKey, + token), + Times.Once); + result.Should().Be(count); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs new file mode 100644 index 0000000..2e49f52 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneAsyncTests.cs @@ -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 +{ + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOneAsync, int>(It.IsAny>(), It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, token); + + // Assert + Updater.Verify(x => x.UpdateOneAsync, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int>( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOneAsync, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(true); + + // Act + var result = await Sut.UpdateOneAsync(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOneAsync, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneTests.cs new file mode 100644 index 0000000..1dd486e --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/UpdateTests/UpdateOneTests.cs @@ -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 +{ + private readonly UpdateDefinition> updateDefinition = Builders>.Update.Set(x => x.SomeContent, "Updated"); + private readonly Expression, string>> fieldExpression = x => x.SomeContent; + private readonly FilterDefinition> filterDefinition = Builders>.Filter.Eq(x => x.Id, 1); + private readonly Expression, bool>> filterExpression = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithDocument_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, CancellationToken.None), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup(x => x.UpdateOne, int>(It.IsAny>(), It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, token); + + // Assert + Updater.Verify(x => x.UpdateOne, int>(document, token), Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinition_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + updateDefinition, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndUpdateDefinitionAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int>( + It.IsAny>(), + It.IsAny>>(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, updateDefinition, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int>( + document, + updateDefinition, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + fieldExpression, + value, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var document = Fixture.Create>(); + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(document, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + document, + fieldExpression, + value, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterDefinitionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterDefinition, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterDefinition, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValue_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + null, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + null, + token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ShouldUpdateOne() + { + // Arrange + var value = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + Updater = new Mock(); + Updater + .Setup( + x => x.UpdateOne, int, string>( + It.IsAny, bool>>>(), + It.IsAny, string>>>(), + It.IsAny(), + It.IsAny(), + It.IsAny())) + .Returns(true); + + // Act + var result = Sut.UpdateOne(filterExpression, fieldExpression, value, partitionKey, token); + + // Assert + Updater.Verify( + x => x.UpdateOne, int, string>( + filterExpression, + fieldExpression, + value, + partitionKey, + token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs index fa15af7..757606d 100644 --- a/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/IMongoDbUpdater.cs @@ -504,5 +504,22 @@ namespace MongoDbGenericRepository.DataAccess.Update CancellationToken cancellationToken = default) where TDocument : IDocument where TKey : IEquatable; + + /// + /// For the entities selected by the filter, apply the update definition. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The document filter. + /// The update definition. + /// The value of the partition key. + /// The optional cancellation token. + long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable; } } \ No newline at end of file diff --git a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs index ebb74ef..7529b16 100644 --- a/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs +++ b/MongoDbGenericRepository/DataAccess/Update/MongoDbUpdater.cs @@ -253,5 +253,19 @@ namespace MongoDbGenericRepository.DataAccess.Update var updateRes = collection.UpdateMany(filter, updateDefinition, cancellationToken: cancellationToken); return updateRes.ModifiedCount; } + + /// + public virtual long UpdateMany( + Expression> filter, + UpdateDefinition updateDefinition, + string partitionKey = null, + CancellationToken cancellationToken = default) + where TDocument : IDocument + where TKey : IEquatable + { + var collection = string.IsNullOrEmpty(partitionKey) ? GetCollection() : GetCollection(partitionKey); + var updateRes = collection.UpdateMany(filter, updateDefinition, cancellationToken: cancellationToken); + return updateRes.ModifiedCount; + } } } \ No newline at end of file