added tests for index handler

This commit is contained in:
Sean Garrett
2023-06-22 22:06:27 +01:00
parent 25766febd4
commit b7b23e7b92
6 changed files with 871 additions and 5 deletions
@@ -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<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":1}")),
null,
CancellationToken.None),
Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var options = Fixture.Create<IndexCreationOptions>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, options);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
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<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var partitionKey = Fixture.Create<string>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":1}") ),
null,
CancellationToken.None),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":1}") ),
null,
token),
Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
var partitionKey = Fixture.Create<string>();
var options = Fixture.Create<IndexCreationOptions>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":1}") &&
t.Options.EqualTo(options)),
null,
token),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
{
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
.Returns(collection.Object);
return context;
}
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
{
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
indexManager
.Setup(
x => x.CreateOneAsync(
It.IsAny<CreateIndexModel<TDocument>>(),
It.IsAny<CreateOneIndexOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(indexName);
collection
.SetupGet(x => x.Indexes)
.Returns(indexManager.Object);
return indexManager;
}
}
@@ -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<Expression<Func<TestDocument, object>>> 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<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(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<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var options = Fixture.Create<IndexCreationOptions>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, options);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
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<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var partitionKey = Fixture.Create<string>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, partitionKey: partitionKey);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ),
null,
CancellationToken.None),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, cancellationToken: token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ),
null,
token),
Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
var partitionKey = Fixture.Create<string>();
var options = Fixture.Create<IndexCreationOptions>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, options, partitionKey, token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") &&
t.Options.EqualTo(options)),
null,
token),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
{
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
.Returns(collection.Object);
return context;
}
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
{
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
indexManager
.Setup(
x => x.CreateOneAsync(
It.IsAny<CreateIndexModel<TDocument>>(),
It.IsAny<CreateOneIndexOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(indexName);
collection
.SetupGet(x => x.Indexes)
.Returns(indexManager.Object);
return indexManager;
}
}
@@ -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<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":-1}")),
null,
CancellationToken.None),
Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var options = Fixture.Create<IndexCreationOptions>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, options);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
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<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var partitionKey = Fixture.Create<string>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ),
null,
CancellationToken.None),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ),
null,
token),
Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
var partitionKey = Fixture.Create<string>();
var options = Fixture.Create<IndexCreationOptions>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") &&
t.Options.EqualTo(options)),
null,
token),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
{
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
.Returns(collection.Object);
return context;
}
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
{
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
indexManager
.Setup(
x => x.CreateOneAsync(
It.IsAny<CreateIndexModel<TDocument>>(),
It.IsAny<CreateOneIndexOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(indexName);
collection
.SetupGet(x => x.Indexes)
.Returns(indexManager.Object);
return indexManager;
}
}
@@ -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<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
public CreateHashedIndexAsyncTests(ITestOutputHelper testOutputHelper)
=> this.testOutputHelper = testOutputHelper;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}", testOutputHelper)),
null,
CancellationToken.None),
Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var options = Fixture.Create<IndexCreationOptions>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, options);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
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<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var partitionKey = Fixture.Create<string>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ),
null,
CancellationToken.None),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ),
null,
token),
Times.Once);
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
var partitionKey = Fixture.Create<string>();
var options = Fixture.Create<IndexCreationOptions>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
// Assert
result.Should().Be(expectedIndexName);
indexManger.Verify(
x => x.CreateOneAsync(
It.Is<CreateIndexModel<TestDocument>>(
t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") &&
t.Options.EqualTo(options)),
null,
token),
Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
{
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
.Returns(collection.Object);
return context;
}
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
{
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
indexManager
.Setup(
x => x.CreateOneAsync(
It.IsAny<CreateIndexModel<TDocument>>(),
It.IsAny<CreateOneIndexOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(indexName);
collection
.SetupGet(x => x.Indexes)
.Returns(indexManager.Object);
return indexManager;
}
}
@@ -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<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName);
// Assert
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once);
}
[Fact]
public async Task WhenIndexNameAndPartitionKey_ThenDropsIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var partitionKey = Fixture.Create<string>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName, partitionKey);
// Assert
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
[Fact]
public async Task WhenIndexNameAndCancellationToken_ThenDropsIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName, cancellationToken: token);
// Assert
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, token), Times.Once);
}
[Fact]
public async Task WhenIndexNameAndPartitionKeyAndCancellationToken_ThenDropsIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
var token = new CancellationToken(true);
var partitionKey = Fixture.Create<string>();
var context = SetupContext(collection);
var indexManger = SetupIndexManager(collection, expectedIndexName);
// Act
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName, partitionKey, token);
// Assert
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, token), Times.Once);
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
{
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
.Returns(collection.Object);
return context;
}
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
{
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
indexManager
.Setup(
x => x.CreateOneAsync(
It.IsAny<CreateIndexModel<TDocument>>(),
It.IsAny<CreateOneIndexOptions>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(indexName);
collection
.SetupGet(x => x.Indexes)
.Returns(indexManager.Object);
return indexManager;
}
}
@@ -1,16 +1,30 @@
using MongoDB.Bson; using System;
using MongoDB.Bson.Serialization; using MongoDB.Bson.Serialization;
using MongoDB.Driver; using MongoDB.Driver;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit.Abstractions;
namespace CoreUnitTests.Infrastructure; namespace CoreUnitTests.Infrastructure;
public static class IndexExtensions public static class IndexExtensions
{ {
public static bool EqualToJson<TDocument>(this IndexKeysDefinition<TDocument> 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<TDocument>(this IndexKeysDefinition<TDocument> keys, string json) public static bool EqualToJson<TDocument>(this IndexKeysDefinition<TDocument> keys, string json)
{ {
var indexModelRendered = RenderIndexModelKeys(keys); 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) => public static bool EqualTo(this IndexCreationOptions x, CreateIndexOptions y) =>
@@ -52,6 +66,4 @@ public static class IndexExtensions
var result = indexModelRendered.ToString(); var result = indexModelRendered.ToString();
return result.Replace(" ", ""); return result.Replace(" ", "");
} }
} }