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; using MongoDbGenericRepository.Models; using Moq; using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; public class CreateDescendingIndexAsyncTests : BaseIndexTests { private readonly Expression> fieldExpression = t => t.SomeContent2; [Fact] public async Task WithFieldExpression_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act var result = await Sut.CreateDescendingIndexAsync(fieldExpression); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":-1}")), null, CancellationToken.None), Times.Once); } [Fact] public async Task WithFieldExpressionAndOptions_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var options = Fixture.Create(); SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act var result = await Sut.CreateDescendingIndexAsync(fieldExpression, options); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") && t.Options.EqualTo(options)), null, CancellationToken.None), Times.Once); } [Fact] public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var partitionKey = Fixture.Create(); var context = SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act var result = await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey: partitionKey); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ), null, CancellationToken.None), Times.Once); context.Verify(x => x.GetCollection(partitionKey), Times.Once); } [Fact] public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var token = new CancellationToken(true); SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act var result = await Sut.CreateDescendingIndexAsync(fieldExpression, cancellationToken: token); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ), null, token), Times.Once); } [Fact] public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var token = new CancellationToken(true); var partitionKey = Fixture.Create(); var options = Fixture.Create(); var context = SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act var result = await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey, token); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") && t.Options.EqualTo(options)), null, token), Times.Once); context.Verify(x => x.GetCollection(partitionKey), Times.Once); } private Mock SetupContext(IMock> collection) { var context = MockOf(); context .Setup(x => x.GetCollection(It.IsAny())) .Returns(collection.Object); return context; } private Mock> SetupIndexManager(Mock> collection, string indexName) { var indexManager = MockOf>(); indexManager .Setup( x => x.CreateOneAsync( It.IsAny>(), It.IsAny(), It.IsAny())) .ReturnsAsync(indexName); collection .SetupGet(x => x.Indexes) .Returns(indexManager.Object); return indexManager; } }