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.Read; using Moq; using Xunit; namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; public class AnyAsyncTests : TestReadOnlyMongoRepositoryContext { private readonly Expression> expression = document => document.SomeContent == "SomeContent"; private readonly Expression, bool>> keyedExpression = document => document.SomeContent == "SomeContent"; private readonly FilterDefinition> keyedFilter = Builders>.Filter.Eq(x => x.Id, 1); [Fact] public async Task WithExpression_GetsResult() { // Arrange SetupReader(); // Act var result = await Sut.AnyAsync(expression); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync(expression, null, CancellationToken.None), Times.Once); } [Fact] public async Task WithExpressionAndCancellationToken_GetsResult() { // Arrange var token = new CancellationToken(true); SetupReader(); // Act var result = await Sut.AnyAsync(expression, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync(expression, null, token), Times.Once); } [Fact] public async Task WithExpressionAndPartitionKey_GetsResult() { // Arrange var partitionKey = Fixture.Create(); SetupReader(); // Act var result = await Sut.AnyAsync(expression, partitionKey); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync(expression, partitionKey, CancellationToken.None), Times.Once); } [Fact] public async Task WithExpressionAndPartitionKeyAndCancellationToken_GetsResult() { // Arrange var partitionKey = Fixture.Create(); var token = new CancellationToken(true); SetupReader(); // Act var result = await Sut.AnyAsync(expression, partitionKey, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync(expression, partitionKey, token), Times.Once); } #region keyed [Fact] public async Task Keyed_WithExpression_GetsResult() { // Arrange SetupKeyedReaderWithExpression(); // Act var result = await Sut.AnyAsync, int>(keyedExpression); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedExpression, null, CancellationToken.None), Times.Once); } [Fact] public async Task Keyed_WithExpressionAndCancellationToken_GetsResult() { // Arrange var token = new CancellationToken(true); SetupKeyedReaderWithExpression(); // Act var result = await Sut.AnyAsync, int>(keyedExpression, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedExpression, null, token), Times.Once); } [Fact] public async Task Keyed_WithExpressionAndPartitionKey_GetsResult() { // Arrange var partitionKey = Fixture.Create(); SetupKeyedReaderWithExpression(); // Act var result = await Sut.AnyAsync, int>(keyedExpression, partitionKey); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedExpression, partitionKey, CancellationToken.None), Times.Once); } [Fact] public async Task Keyed_WithExpressionAndPartitionKeyAndCancellationToken_GetsResult() { // Arrange var partitionKey = Fixture.Create(); var token = new CancellationToken(true); SetupKeyedReaderWithExpression(); // Act var result = await Sut.AnyAsync, int>(keyedExpression, partitionKey, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedExpression, partitionKey, token), Times.Once); } [Fact] public async Task Keyed_WithFilter_GetsResult() { // Arrange SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, null, null, CancellationToken.None), Times.Once); } [Fact] public async Task Keyed_WithFilterAndOptions_GetsResult() { // Arrange var options = new CountOptions(); SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter, options); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, options, null, CancellationToken.None), Times.Once); } [Fact] public async Task Keyed_WithFilterAndCancellationToken_GetsResult() { // Arrange var token = new CancellationToken(true); SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, null, null, token), Times.Once); } [Fact] public async Task Keyed_WithFilterAndOptionsAndCancellationToken_GetsResult() { // Arrange var token = new CancellationToken(true); var options = new CountOptions(); SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter, options, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, options, null, token), Times.Once); } [Fact] public async Task Keyed_WithFilterAndPartitionKey_GetsResult() { // Arrange var partitionKey = Fixture.Create(); SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter, partitionKey); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, null, partitionKey, CancellationToken.None), Times.Once); } [Fact] public async Task Keyed_WithFilterAndCountOptionsAndPartitionKey_GetsResult() { // Arrange var partitionKey = Fixture.Create(); var options = new CountOptions(); SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter, options, partitionKey); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, options, partitionKey, CancellationToken.None), Times.Once); } [Fact] public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_GetsResult() { // Arrange var partitionKey = Fixture.Create(); var token = new CancellationToken(true); SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter, partitionKey, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, null, partitionKey, token), Times.Once); } [Fact] public async Task Keyed_WithFilterAndCountOptionsAndPartitionKeyAndCancellationToken_GetsResult() { // Arrange var partitionKey = Fixture.Create(); var token = new CancellationToken(true); var options = new CountOptions(); SetupKeyedReaderWithFilter(); // Act var result = await Sut.AnyAsync, int>(keyedFilter, options, partitionKey, token); // Assert result.Should().BeTrue(); Reader.Verify( x => x.AnyAsync, int>(keyedFilter, options, partitionKey, token), Times.Once); } private void SetupKeyedReaderWithExpression() { Reader = new Mock(); Reader .Setup( x => x.AnyAsync, int>( It.IsAny, bool>>>(), It.IsAny(), It.IsAny())) .ReturnsAsync(true); } private void SetupKeyedReaderWithFilter() { Reader = new Mock(); Reader .Setup( x => x.AnyAsync, int>( It.IsAny>>(), It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(true); } #endregion private void SetupReader() { Reader = new Mock(); Reader .Setup( x => x.AnyAsync( It.IsAny>>(), It.IsAny(), It.IsAny())) .ReturnsAsync(true); } }