diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs new file mode 100644 index 0000000..15655eb --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs @@ -0,0 +1,294 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateAscendingIndexAsyncTests : BaseIndexTests +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync(fieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync(fieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync( + fieldExpression, options, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync, int>(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs deleted file mode 100644 index 85b5b70..0000000 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateAscendingIndexTests.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System; -using System.Linq.Expressions; -using System.Threading; -using System.Threading.Tasks; -using CoreUnitTests.Infrastructure.Model; -using MongoDbGenericRepository.DataAccess.Index; -using Moq; -using Xunit; - -namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; - -public class CreateAscendingIndexTests : BaseIndexTests -{ - /*[Fact] - public async Task CreateAscendingIndexAsync_EnsureTokenPassed() - { - // Arrange - IndexHandler = new Mock(); - var token = new CancellationToken(true); - - // Act - Expression> fieldExpression = t => t.SomeContent2; - // await Sut.CreateAscendingIndexAsync(fieldExpression, token); - - // Assert - IndexHandler.Verify(x => x.CreateAscendingIndexAsync( - fieldExpression, null, null, token)); - }*/ -} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs new file mode 100644 index 0000000..c56f676 --- /dev/null +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs @@ -0,0 +1,294 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; + +public class CreateDescendingIndexAsyncTests : BaseIndexTests +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + private readonly Expression> fieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync(fieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync(fieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync( + fieldExpression, options, partitionKey, token)); + } + + #region Keyed + + [Fact] + public async Task Keyed_WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync, int>(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } + + #endregion +} diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs similarity index 99% rename from CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs rename to CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs index 485fa54..7e1f651 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/CreateTextIndexAsyncTests.cs @@ -11,7 +11,7 @@ using CancellationToken = System.Threading.CancellationToken; namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; -public class CreateTextIndexTests : BaseIndexTests +public class CreateTextIndexAsyncTests : BaseIndexTests { private readonly Expression> fieldExpression = t => t.SomeContent2; private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; diff --git a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs similarity index 99% rename from CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs rename to CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs index 78b2b77..6c9afb8 100644 --- a/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesTests.cs +++ b/CoreUnitTests/BaseMongoRepositoryTests/IndexTests/GetIndexNamesAsyncTests.cs @@ -10,7 +10,7 @@ using Xunit; namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests; -public class GetIndexNamesTests : BaseIndexTests +public class GetIndexNamesAsyncTests : BaseIndexTests { [Fact] public async Task WithNoParameters_ReturnsIndexNames() diff --git a/CoreUnitTests/Infrastructure/GenericTestContext.cs b/CoreUnitTests/Infrastructure/GenericTestContext.cs index 9b0a708..9c494d6 100644 --- a/CoreUnitTests/Infrastructure/GenericTestContext.cs +++ b/CoreUnitTests/Infrastructure/GenericTestContext.cs @@ -6,18 +6,13 @@ namespace CoreUnitTests.Infrastructure; public class GenericTestContext { - public GenericTestContext() - { - Fixture = new Fixture().Customize(new AutoMoqCustomization()); - } + public GenericTestContext() => Fixture = new Fixture().Customize(new AutoMoqCustomization()); protected Mock MockOf() - where T : class - { - return Fixture.Freeze>(); - } + where T : class => + Fixture.Freeze>(); protected IFixture Fixture { get; set; } - protected TSut Sut { get => Fixture.Create(); } -} \ No newline at end of file + protected TSut Sut => Fixture.Create(); +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs new file mode 100644 index 0000000..3404c48 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateAscendingIndexAsyncTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class CreateAscendingIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateAscendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } +} diff --git a/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs new file mode 100644 index 0000000..c327000 --- /dev/null +++ b/CoreUnitTests/KeyTypedRepositoryTests/IndexTests/CreateDescendingIndexAsyncTests.cs @@ -0,0 +1,154 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests; + +public class CreateDescendingIndexAsyncTests : TestKeyedMongoRepositoryContext +{ + private readonly Expression, object>> keyedFieldExpression = t => t.SomeContent2; + + [Fact] + public async Task WithFieldExpression_CreatesIndex() + { + // Arrange + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndCancellationToken_CreatesIndex() + { + // Arrange + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>(keyedFieldExpression, null, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptions_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex() + { + // Arrange + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, null, token)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, null, partitionKey, token)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options, partitionKey); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, CancellationToken.None)); + } + + [Fact] + public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex() + { + // Arrange + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var indexName = Fixture.Create(); + var options = new IndexCreationOptions { Name = indexName }; + + IndexHandler = new Mock(); + + // Act + await Sut.CreateDescendingIndexAsync(keyedFieldExpression, options, partitionKey, token); + + // Assert + IndexHandler.Verify( + x => x.CreateDescendingIndexAsync, int>( + keyedFieldExpression, options, partitionKey, token)); + } +} diff --git a/MongoDbGenericRepository/BaseMongoRepository.Index.cs b/MongoDbGenericRepository/BaseMongoRepository.Index.cs index c32dde0..c8192fd 100644 --- a/MongoDbGenericRepository/BaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/BaseMongoRepository.Index.cs @@ -1,29 +1,32 @@ -using MongoDbGenericRepository.DataAccess.Index; -using System; +using System; using System.Collections.Generic; -using System.Threading.Tasks; using System.Linq.Expressions; using System.Threading; +using System.Threading.Tasks; +using MongoDbGenericRepository.DataAccess.Index; using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index { private IMongoDbIndexHandler _mongoDbIndexHandler; /// - /// The MongoDb accessor to manage indexes. + /// The MongoDb accessor to manage indexes. /// protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { - if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; } + if (_mongoDbIndexHandler != null) + { + return _mongoDbIndexHandler; + } lock (_initLock) { @@ -32,9 +35,10 @@ namespace MongoDbGenericRepository _mongoDbIndexHandler = new MongoDbIndexHandler(MongoDbContext); } } + return _mongoDbIndexHandler; } - set { _mongoDbIndexHandler = value; } + set => _mongoDbIndexHandler = value; } /// @@ -120,7 +124,10 @@ namespace MongoDbGenericRepository } /// - public async Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + public async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, null, partitionKey, cancellationToken); @@ -134,21 +141,31 @@ namespace MongoDbGenericRepository } /// - public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, null, cancellationToken); } /// - public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + public async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); } /// - public async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + public async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); @@ -179,7 +196,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -187,7 +207,9 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) where TDocument : IDocument where TKey : IEquatable { @@ -195,7 +217,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -203,7 +228,10 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument where TKey : IEquatable { @@ -211,7 +239,11 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { @@ -219,44 +251,291 @@ namespace MongoDbGenericRepository } /// - public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public async Task CreateAscendingIndexAsync(Expression> field) where TDocument : IDocument { - return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); } /// - public virtual async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public async Task CreateAscendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public async Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync(Expression> field) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); } /// - public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) - where TDocument : IDocument - { - return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); - } - - /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable { - return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, cancellationToken); } /// - public async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable + { + return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument { return await CreateHashedIndexAsync(field, indexCreationOptions, partitionKey); } /// - public virtual async Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateHashedIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -264,14 +543,20 @@ namespace MongoDbGenericRepository } /// - public async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument { return await CreateCombinedTextIndexAsync(fields, indexCreationOptions, partitionKey); } /// - public virtual async Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateCombinedTextIndexAsync( + IEnumerable>> fields, + IndexCreationOptions indexCreationOptions = null, + string partitionKey = null) where TDocument : IDocument where TKey : IEquatable { @@ -293,4 +578,4 @@ namespace MongoDbGenericRepository await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs index b3de813..6d44731 100644 --- a/MongoDbGenericRepository/IBaseMongoRepository.Index.cs +++ b/MongoDbGenericRepository/IBaseMongoRepository.Index.cs @@ -172,6 +172,91 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TKey : IEquatable; + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + /// /// Creates an index on the given field in ascending order. /// IndexCreationOptions can be supplied to further specify @@ -183,7 +268,108 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; @@ -198,7 +384,23 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The type of the primary key for a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument where TKey : IEquatable; diff --git a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs index e24022c..7f67270 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/BaseMongoRepository.TKey.Index.cs @@ -1,16 +1,16 @@ -using MongoDbGenericRepository.DataAccess.Index; -using MongoDbGenericRepository.Models; -using System; +using System; using System.Collections.Generic; using System.Linq.Expressions; +using System.Threading; using System.Threading.Tasks; -using CancellationToken = System.Threading.CancellationToken; +using MongoDbGenericRepository.DataAccess.Index; +using MongoDbGenericRepository.Models; namespace MongoDbGenericRepository { /// - /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. - /// Its constructor must be given a connection string and a database name. + /// The base Repository, it is meant to be inherited from by your custom custom MongoRepository implementation. + /// Its constructor must be given a connection string and a database name. /// public abstract partial class BaseMongoRepository : IBaseMongoRepository_Index where TKey : IEquatable @@ -18,13 +18,16 @@ namespace MongoDbGenericRepository private IMongoDbIndexHandler _mongoDbIndexHandler; /// - /// The MongoDb accessor to manage indexes. + /// The MongoDb accessor to manage indexes. /// protected virtual IMongoDbIndexHandler MongoDbIndexHandler { get { - if (_mongoDbIndexHandler != null) { return _mongoDbIndexHandler; } + if (_mongoDbIndexHandler != null) + { + return _mongoDbIndexHandler; + } lock (_initLock) { @@ -36,7 +39,7 @@ namespace MongoDbGenericRepository return _mongoDbIndexHandler; } - set { _mongoDbIndexHandler = value; } + set => _mongoDbIndexHandler = value; } /// @@ -89,14 +92,20 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, null, cancellationToken); } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) where TDocument : IDocument { return await CreateTextIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); @@ -110,31 +119,168 @@ namespace MongoDbGenericRepository } /// - public virtual async Task CreateTextIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await CreateTextIndexAsync(field, null, partitionKey, cancellationToken); } /// - public virtual async Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + public virtual async Task CreateTextIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) where TDocument : IDocument { return await MongoDbIndexHandler.CreateTextIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); } /// - public virtual async Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateAscendingIndexAsync(Expression> field) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, CancellationToken.None); } /// - public virtual async Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + CancellationToken cancellationToken) where TDocument : IDocument { - return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey); + return await CreateAscendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateAscendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.CreateAscendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, null, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, null, partitionKey, cancellationToken); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey) + where TDocument : IDocument + { + return await CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, CancellationToken.None); + } + + /// + public virtual async Task CreateDescendingIndexAsync( + Expression> field, + IndexCreationOptions indexCreationOptions, + string partitionKey, + CancellationToken cancellationToken) + where TDocument : IDocument + { + return await MongoDbIndexHandler.CreateDescendingIndexAsync(field, indexCreationOptions, partitionKey, cancellationToken); } /// @@ -158,4 +304,4 @@ namespace MongoDbGenericRepository await MongoDbIndexHandler.DropIndexAsync(indexName, partitionKey); } } -} +} \ No newline at end of file diff --git a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs index cb97145..7b0678f 100644 --- a/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs +++ b/MongoDbGenericRepository/KeyTypedRepository/IBaseMongoRepository.TKey.Index.cs @@ -149,6 +149,80 @@ namespace MongoDbGenericRepository Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + /// /// Creates an index on the given field in ascending order. /// IndexCreationOptions can be supplied to further specify @@ -159,7 +233,94 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in ascending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateAscendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, CancellationToken cancellationToken) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument; /// @@ -172,7 +333,21 @@ namespace MongoDbGenericRepository /// Options for creating an index. /// An optional partition key. /// The result of the create index operation. - Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey) + where TDocument : IDocument; + + /// + /// Creates an index on the given field in descending order. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. + /// + /// The type representing a Document. + /// The field we want to index. + /// Options for creating an index. + /// An optional partition key. + /// The cancellation token. + /// The result of the create index operation. + Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions, string partitionKey, CancellationToken cancellationToken) where TDocument : IDocument; ///