using System; using System.Collections.Generic; 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; using Xunit.Abstractions; namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; public class CreateCombinedTextIndexAsyncTests : BaseIndexTests { private readonly ITestOutputHelper testOutputHelper; private readonly List>> fieldExpressions = new() { t => t.SomeContent2, t => t.GroupingKey }; public CreateCombinedTextIndexAsyncTests(ITestOutputHelper testOutputHelper) => this.testOutputHelper = testOutputHelper; [Fact] public async Task WhenFieldExpression_ThenCreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act var result = await Sut.CreateCombinedTextIndexAsync(fieldExpressions); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}", testOutputHelper)), null, CancellationToken.None), Times.Once); } [Fact] public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex() { // Arrange var expectedIndexName = Fixture.Create(); var collection = MockOf>(); var options = Fixture.Create(); SetupContext(collection); var indexManger = SetupIndexManager(collection, expectedIndexName); // Act var result = await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") && t.Options.EqualTo(options)), null, CancellationToken.None), Times.Once); } [Fact] public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex() { // 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.CreateCombinedTextIndexAsync(fieldExpressions, partitionKey: partitionKey); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ), null, CancellationToken.None), Times.Once); context.Verify(x => x.GetCollection(partitionKey), Times.Once); } [Fact] public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex() { // 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.CreateCombinedTextIndexAsync(fieldExpressions, cancellationToken: token); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ), null, token), Times.Once); } [Fact] public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex() { // 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.CreateCombinedTextIndexAsync(fieldExpressions, options, partitionKey, token); // Assert result.Should().Be(expectedIndexName); indexManger.Verify( x => x.CreateOneAsync( It.Is>( t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") && 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; } }