diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyAsyncTests.cs new file mode 100644 index 0000000..a1e38a3 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyAsyncTests.cs @@ -0,0 +1,282 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class AnyAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public async Task WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.AnyAsync(condition, partitionKey: partitionKey, cancellationToken:token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyTests.cs new file mode 100644 index 0000000..a58acaa --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/AnyTests.cs @@ -0,0 +1,281 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class AnyTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().BeTrue(); + } + + [Fact] + public void WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Any(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().BeTrue(); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountAsyncTests.cs new file mode 100644 index 0000000..22e22ac --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountAsyncTests.cs @@ -0,0 +1,282 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class CountAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public async Task WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(It.Is>(y => y.Expression == filter), null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public async Task WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocumentsAsync( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(documents.Count); + + // Act + var result = await Sut.CountAsync(condition, partitionKey: partitionKey, cancellationToken:token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocumentsAsync(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountTests.cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountTests.cs new file mode 100644 index 0000000..bdcdba6 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/CountTests.cs @@ -0,0 +1,281 @@ +using System; +using System.Linq; +using System.Linq.Expressions; +using System.Threading; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class CountTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + + [Fact] + public void WithFilter_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithFilterAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithFilterAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(filter, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(It.Is>(y => y.Expression == filter), It.IsAny(), token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithCondition_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithConditionAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(null)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithConditionAndPartitionKey_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition, partitionKey: partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, CancellationToken.None), + Times.Once); + result.Should().Be(documents.Count); + } + + [Fact] + public void WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult() + { + // Arrange + var collection = MockOf>(); + var documents = Fixture.CreateMany().ToList(); + var condition = Builders.Filter.Eq("Id", documents[0].Id); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + collection + .Setup( + x => x.CountDocuments( + It.IsAny>(), + It.IsAny(), + It.IsAny())) + .Returns(documents.Count); + + // Act + var result = Sut.Count(condition, partitionKey: partitionKey, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + collection.Verify( + x => x.CountDocuments(condition, null, token), + Times.Once); + result.Should().Be(documents.Count); + } +}