diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs new file mode 100644 index 0000000..e3040e8 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateAscendingIndexAsyncTests.cs @@ -0,0 +1,180 @@ +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 CreateAscendingIndexAsyncTests : BaseIndexTests +{ + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [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.CreateAscendingIndexAsync(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 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.CreateAscendingIndexAsync(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 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.CreateAscendingIndexAsync(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 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.CreateAscendingIndexAsync(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 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.CreateAscendingIndexAsync(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; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs new file mode 100644 index 0000000..5f1ed30 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateCombinedTextIndexAsyncTests.cs @@ -0,0 +1,190 @@ +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; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs new file mode 100644 index 0000000..d0bb876 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateDescendingIndexAsyncTests.cs @@ -0,0 +1,180 @@ +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 WhenFieldExpression_ThenCreatesIndex() + { + // 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 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.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 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.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 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.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 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.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; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs new file mode 100644 index 0000000..f1172c7 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/CreateHashedIndexAsyncTests.cs @@ -0,0 +1,185 @@ +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; +using Xunit.Abstractions; + +namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests; + +public class CreateHashedIndexAsyncTests : BaseIndexTests +{ + private readonly ITestOutputHelper testOutputHelper; + private readonly Expression> fieldExpression = t => t.SomeContent2; + + public CreateHashedIndexAsyncTests(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.CreateHashedIndexAsync(fieldExpression); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}", 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.CreateHashedIndexAsync(fieldExpression, options); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") && + 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.CreateHashedIndexAsync(fieldExpression, partitionKey: partitionKey); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ), + 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.CreateHashedIndexAsync(fieldExpression, cancellationToken: token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ), + 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.CreateHashedIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + result.Should().Be(expectedIndexName); + indexManger.Verify( + x => x.CreateOneAsync( + It.Is>( + t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") && + 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; + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs new file mode 100644 index 0000000..6485411 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbIndexHandlerTests/DropIndexAsyncTests.cs @@ -0,0 +1,119 @@ +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; + } +} diff --git a/CoreUnitTests/Infrastructure/IndexExtensions.cs b/CoreUnitTests/Infrastructure/IndexExtensions.cs index 7daa789..7ff4fff 100644 --- a/CoreUnitTests/Infrastructure/IndexExtensions.cs +++ b/CoreUnitTests/Infrastructure/IndexExtensions.cs @@ -1,16 +1,30 @@ -using MongoDB.Bson; +using System; using MongoDB.Bson.Serialization; using MongoDB.Driver; using MongoDbGenericRepository.Models; +using Xunit.Abstractions; namespace CoreUnitTests.Infrastructure; public static class IndexExtensions { + public static bool EqualToJson(this IndexKeysDefinition keys, string json, ITestOutputHelper output) + { + var indexModelRendered = RenderIndexModelKeys(keys); + var result = indexModelRendered.Equals(json, StringComparison.Ordinal); + if (!result && output != null) + { + output.WriteLine($"Expected: {json}"); + output.WriteLine($"Actual: {indexModelRendered}"); + } + + return result; + } + public static bool EqualToJson(this IndexKeysDefinition keys, string json) { var indexModelRendered = RenderIndexModelKeys(keys); - return indexModelRendered.Equals(json, System.StringComparison.Ordinal); + return indexModelRendered.Equals(json, StringComparison.Ordinal); } public static bool EqualTo(this IndexCreationOptions x, CreateIndexOptions y) => @@ -28,7 +42,7 @@ public static class IndexExtensions x.Background == y.Background && x.Version == y.Version; - public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) => + public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) => x.Unique == y.Unique && x.TextIndexVersion == y.TextIndexVersion && x.SphereIndexVersion == y.SphereIndexVersion && @@ -52,6 +66,4 @@ public static class IndexExtensions var result = indexModelRendered.ToString(); return result.Replace(" ", ""); } - - }