using System; using System.Threading; using System.Threading.Tasks; using AutoFixture; using CoreUnitTests.Infrastructure.Model; using MongoDB.Driver; using MongoDbGenericRepository; using Moq; using Xunit; namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; public class DropIndexAsyncTests : BaseIndexTests { [Fact] public async Task WhenIndexName_ThenDropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act await Sut.DropIndexAsync(expectedIndexName); // Assert indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once); } [Fact] public async Task WhenIndexNameAndPartitionKey_ThenDropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var partitionKey = Fixture.Create(); var context = SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act await Sut.DropIndexAsync(expectedIndexName, partitionKey); // Assert indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once); context.Verify(x => x.GetCollection(partitionKey), Times.Once); } [Fact] public async Task WhenIndexNameAndCancellationToken_ThenDropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var token = new CancellationToken(true); SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act await Sut.DropIndexAsync(expectedIndexName, cancellationToken: token); // Assert indexManger.Verify(x => x.DropOneAsync(expectedIndexName, token), Times.Once); } [Fact] public async Task WhenIndexNameAndPartitionKeyAndCancellationToken_ThenDropsIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var token = new CancellationToken(true); var partitionKey = Fixture.Create(); var context = SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act await Sut.DropIndexAsync(expectedIndexName, partitionKey, token); // Assert indexManger.Verify(x => x.DropOneAsync(expectedIndexName, 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; } }