using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using AutoFixture; using CoreUnitTests.Infrastructure.Model; using MongoDbGenericRepository.DataAccess.Index; using Moq; using Xunit; namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; public class GetIndexNamesAsyncTests : BaseIndexTests { [Fact] public async Task WithNoParameters_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync(null, CancellationToken.None), Times.Once()); } [Fact] public async Task WithCancellationToken_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); var token = new CancellationToken(true); IndexHandler .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(token); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync(null, token), Times.Once()); } [Fact] public async Task WithPartitionKey_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); var partitionKey = Fixture.Create(); IndexHandler .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(partitionKey); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync(partitionKey, CancellationToken.None), Times.Once()); } [Fact] public async Task WithPartitionKeyAndCancellationToken_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); IndexHandler .Setup(x => x.GetIndexesNamesAsync(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync(partitionKey, token); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync(partitionKey, token), Times.Once()); } [Fact] public async Task Keyed_WithNoParameters_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); IndexHandler .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync, int>(); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(null, CancellationToken.None), Times.Once()); } [Fact] public async Task Keyed_WithCancellationToken_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); var token = new CancellationToken(true); IndexHandler .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync, int>(token); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(null, token), Times.Once()); } [Fact] public async Task Keyed_WithPartitionKey_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); var partitionKey = Fixture.Create(); IndexHandler .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync, int>(partitionKey); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(partitionKey, CancellationToken.None), Times.Once()); } [Fact] public async Task Keyed_WithPartitionKeyAndCancellationToken_ReturnsIndexNames() { // Arrange IndexHandler = new Mock(); var indexName = Fixture.Create(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); IndexHandler .Setup(x => x.GetIndexesNamesAsync, int>(It.IsAny(), It.IsAny())) .ReturnsAsync(new List { indexName }); // Act var result = await Sut.GetIndexesNamesAsync, int>(partitionKey, token); // Assert Assert.NotNull(result); Assert.Contains(result, x => x == indexName); IndexHandler.Verify(x => x.GetIndexesNamesAsync, int>(partitionKey, token), Times.Once()); } }