diff --git a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs index 8577eff..001521c 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs @@ -947,8 +947,6 @@ namespace CoreIntegrationTests.Infrastructure #region Index Management - - //Instantiate a Singleton of the Semaphore with a value of 1. This means that only 1 thread can be granted access at a time. static SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1); [Fact] @@ -1004,6 +1002,57 @@ namespace CoreIntegrationTests.Infrastructure } } + [Fact] + public async Task CreateAscendingIndexAsync() + { + // Arrange + const string expectedIndexName = "SomeContent_1"; + + // Act + var result = await SUT.CreateAscendingIndexAsync(x => x.SomeContent, null, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + + [Fact] + public async Task CreateDescendingIndexAsync() + { + // Arrange + const string expectedIndexName = "SomeContent_-1"; + + // Act + var result = await SUT.CreateDescendingIndexAsync(x => x.SomeContent, null, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + + [Fact] + public async Task CreateHashedIndexAsync() + { + // Arrange + const string expectedIndexName = "SomeContent_hashed"; + + // Act + var result = await SUT.CreateHashedIndexAsync(x => x.SomeContent, null, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + #endregion Index Management #region Test Utils diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs index 056163f..dc3e54a 100644 --- a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs +++ b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; using Xunit; @@ -937,6 +938,116 @@ namespace CoreIntegrationTests.Infrastructure #endregion Max / Min Queries + #region Index Management + + static SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1); + + [Fact] + public async Task CreateTextIndexNoOptionAsync() + { + // Arrange + const string expectedIndexName = "SomeContent_text"; + + // Act + await textIndexSemaphore.WaitAsync(); + try + { + var result = await SUT.CreateTextIndexAsync(x => x.SomeContent, null, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + finally + { + textIndexSemaphore.Release(); + } + } + + [Fact] + public async Task CreateTextIndexWithOptionAsync() + { + // Arrange + string expectedIndexName = $"{Guid.NewGuid()}"; + var option = new IndexCreationOptions + { + Name = expectedIndexName + }; + await textIndexSemaphore.WaitAsync(); + try + { + // Act + var result = await SUT.CreateTextIndexAsync(x => x.Version, option, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + finally + { + textIndexSemaphore.Release(); + } + } + + [Fact] + public async Task CreateAscendingIndexAsync() + { + // Arrange + const string expectedIndexName = "SomeContent_1"; + + // Act + var result = await SUT.CreateAscendingIndexAsync(x => x.SomeContent, null, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + + [Fact] + public async Task CreateDescendingIndexAsync() + { + // Arrange + const string expectedIndexName = "SomeContent_-1"; + + // Act + var result = await SUT.CreateDescendingIndexAsync(x => x.SomeContent, null, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + + [Fact] + public async Task CreateHashedIndexAsync() + { + // Arrange + const string expectedIndexName = "SomeContent_hashed"; + + // Act + var result = await SUT.CreateHashedIndexAsync(x => x.SomeContent, null, PartitionKey); + + // Assert + var listOfIndexNames = await SUT.GetIndexesNamesAsync(PartitionKey); + Assert.Contains(expectedIndexName, listOfIndexNames); + + // Cleanup + await SUT.DropIndexAsync(expectedIndexName, PartitionKey); + } + + #endregion Index Management + #region Test Utils [MethodImpl(MethodImplOptions.NoInlining)] private string GetCurrentMethod() diff --git a/MongoDbGenericRepository/BaseMongoDbRepository.IndexManagement.cs b/MongoDbGenericRepository/BaseMongoDbRepository.IndexManagement.cs index b24394f..0f4df54 100644 --- a/MongoDbGenericRepository/BaseMongoDbRepository.IndexManagement.cs +++ b/MongoDbGenericRepository/BaseMongoDbRepository.IndexManagement.cs @@ -19,9 +19,9 @@ namespace MongoDbGenericRepository /// 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 field we want to index. + /// Options for creating an index. + /// An optional partition key. /// The result of the create index operation. Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument; @@ -33,91 +33,120 @@ namespace MongoDbGenericRepository /// /// 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 field we want to index. + /// Options for creating an index. + /// An optional partition key. /// The result of the create index operation. Task CreateTextIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// - /// Creates an index on the given field in ascending order + /// 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 field we want to index. + /// 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) where TDocument : IDocument; /// - /// Creates an index on the given field in ascending order + /// 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 field we want to index. + /// 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) where TDocument : IDocument where TKey : IEquatable; /// - /// Creates an index on the given field in descending order + /// 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 field we want to index. + /// Options for creating an index. + /// An optional partition key. /// A string containing the result of the operation. Task CreateDescendingIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument; /// - /// Creates a hashed index on the given field. + /// 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 type of the primary key for a Document. + /// The field we want to index. + /// 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) + where TDocument : IDocument + where TKey : IEquatable; + + /// + /// Creates a hashed index on the given field. + /// 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. /// A string containing the result of the operation. Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument; /// /// Creates a hashed index on the given field. + /// 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 field we want to index. + /// Options for creating an index. + /// An optional partition key. /// The result of the create index operation. Task CreateHashedIndexAsync(Expression> field, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument where TKey : IEquatable; /// - /// Creates a combined text index + /// Creates a combined text index. + /// IndexCreationOptions can be supplied to further specify + /// how the creation should be done. /// /// The type representing a Document. - /// The fields we want to index - /// Options for creating an index.. - /// An optional partition key + /// The fields we want to index. + /// Options for creating an index. + /// An optional partition key. /// The result of the create index operation. Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument; /// - /// Creates a combined text index + /// Creates a combined text index. + /// 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 fields we want to index - /// Options for creating an index.. - /// An optional partition key + /// The fields we want to index. + /// Options for creating an index. + /// An optional partition key. /// The result of the create index operation. Task CreateCombinedTextIndexAsync(IEnumerable>> fields, IndexCreationOptions indexCreationOptions = null, string partitionKey = null) where TDocument : IDocument