unit tests
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
|
||||
|
||||
public class AddManyTests : TestMongoRepositoryContext
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public async Task AddManyAsync_EnsureTokenPassed()
|
||||
{
|
||||
// Arrange
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
var token = new CancellationToken();
|
||||
var documents = new List<TestDocument>
|
||||
{
|
||||
new(), new(), new()
|
||||
};
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync(documents, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddManyAsync<TestDocument, Guid>(documents, token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
|
||||
|
||||
public class AddOneTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public async Task AddOneAsync_EnsureTokenPassed()
|
||||
{
|
||||
// Arrange
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
var token = new CancellationToken();
|
||||
var document = new TestDocument();
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocument>(document, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOneAsync<TestDocument, Guid>(document, token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System.Threading;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class BaseIndexTests : TestMongoRepositoryContext
|
||||
{
|
||||
protected Mock<IAsyncCursor<BsonDocument>> SetupIndex<TDocument>(BsonDocument index, Mock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var asyncCursor = new Mock<IAsyncCursor<BsonDocument>>();
|
||||
asyncCursor
|
||||
.SetupSequence(x => x.MoveNextAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true)
|
||||
.ReturnsAsync(false);
|
||||
|
||||
asyncCursor
|
||||
.SetupGet(x => x.Current)
|
||||
.Returns(new[] {index});
|
||||
|
||||
var indexManager = new Mock<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(x => x.ListAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(asyncCursor.Object);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return asyncCursor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
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<IMongoDbIndexHandler>();
|
||||
var token = new CancellationToken();
|
||||
|
||||
// Act
|
||||
Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
await Sut.CreateAscendingIndexAsync<TestDocument>(fieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(x => x.CreateAscendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, null, token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateTextIndexTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> _fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Creates_Index()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(_fieldExpression, null, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Options()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var options = new IndexCreationOptions { Name = "theIndexName" };
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
_fieldExpression, options, null));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_PartitionKey()
|
||||
{
|
||||
// Arrange
|
||||
const string partitionKey = "thePartitionKey";
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
_fieldExpression, null, partitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Creates_Index_With_CancellationToken()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var token = new CancellationToken();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(_fieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Options_With_CancellationToken()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var token = new CancellationToken();
|
||||
var options = new IndexCreationOptions { Name = "theIndexName" };
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, token, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
_fieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_PartitionKey_With_CancellationToken()
|
||||
{
|
||||
// Arrange
|
||||
const string partitionKey = "thePartitionKey";
|
||||
var token = new CancellationToken();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocument>(_fieldExpression, token, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
_fieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Creates_Index_Custom_Key()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
Expression<Func<TestDocumentWithKey, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey, int>(fieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(x => x.CreateTextIndexAsync<TestDocumentWithKey, int>(fieldExpression, null, null, default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_CancellationToken_Custom_Key()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var token = new CancellationToken();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey, int>(t => t.SomeContent2, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey, int>(
|
||||
t => t.SomeContent2, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Options_Custom_Key()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var options = new IndexCreationOptions { Name = "theIndexName" };
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey, int>(t => t.SomeContent2, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(x => x.CreateTextIndexAsync<TestDocumentWithKey, int>(t => t.SomeContent2, options, null, default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_PartitionKey_Custom_Key()
|
||||
{
|
||||
// Arrange
|
||||
const string partitionKey = "thePartitionKey";
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var options = new IndexCreationOptions { Name = "theIndexName" };
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey, int>(t => t.SomeContent2, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey, int>(
|
||||
t => t.SomeContent2, options, partitionKey, default));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_PartitionKey_And_CancellationToken_Custom_Key()
|
||||
{
|
||||
// Arrange
|
||||
const string partitionKey = "thePartitionKey";
|
||||
const string indexName = "theIndexName";
|
||||
var token = new CancellationToken();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey, int>(t => t.SomeContent2, token, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler
|
||||
.Verify(x => x.CreateTextIndexAsync<TestDocumentWithKey, int>(
|
||||
t => t.SomeContent2, options, partitionKey, token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDB.Bson;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class GetIndexNamesTests : BaseIndexTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task Ensure_Returns_IndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
const string indexName = "theIndexName";
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Provided_CancellationToken()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "theIndexName";
|
||||
var token = new CancellationToken();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null, token))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>(token);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Handles_PartitionKey()
|
||||
{
|
||||
// Arrange
|
||||
const string partitionKey = "thePartitionKey";
|
||||
const string indexName = "theIndexName";
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>(partitionKey);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key()
|
||||
{
|
||||
// Arrange
|
||||
const string partitionKey = "thePartitionKey";
|
||||
const string indexName = "theIndexName";
|
||||
var token = new CancellationToken();
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey, token))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>(token, partitionKey);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Returns_IndexNames_Custom_Primary_Key()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "theIndexName";
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey, int>(null))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey, int>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Provided_CancellationToken_Custom_Primary_Key()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "theIndexName";
|
||||
var token = new CancellationToken();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey, int>(null, token))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey, int>(token);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Handles_PartitionKey_Custom_Primary_Key()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "theIndexName";
|
||||
const string partitionKey = "thePartitionKey";
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey, int>(partitionKey))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey, int>(partitionKey);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Ensure_Passes_Provided_CancellationToken_And_Handles_Partition_Key_Custom_Primary_Key()
|
||||
{
|
||||
// Arrange
|
||||
const string indexName = "theIndexName";
|
||||
const string partitionKey = "thePartitionKey";
|
||||
var token = new CancellationToken();
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey, int>(partitionKey, token))
|
||||
.ReturnsAsync(new List<string>{indexName});
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey, int>(token, partitionKey);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests;
|
||||
|
||||
public class AnyTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public async Task AnyAsync_EnsureTokenPassed()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken();
|
||||
|
||||
Reader = new Mock<IMongoDbReader>();
|
||||
Reader
|
||||
.Setup(x => x.AnyAsync<TestDocument, Guid>(It.IsAny<ExpressionFilterDefinition<TestDocument>>(), null, null, token))
|
||||
.ReturnsAsync(true);
|
||||
|
||||
// Act
|
||||
await Sut.AnyAsync<TestDocument>(t => string.IsNullOrWhiteSpace(t.SomeContent2), token);
|
||||
|
||||
// Assert
|
||||
Reader
|
||||
.Verify(x => x.AnyAsync<TestDocument, Guid>(
|
||||
t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.MainTests;
|
||||
|
||||
public class CountTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public async Task CountAsync_EnsureTokenPassed()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken();
|
||||
|
||||
Reader = new Mock<IMongoDbReader>();
|
||||
Reader
|
||||
.Setup(x => x.CountAsync<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), null, token))
|
||||
.ReturnsAsync(10);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument>(t => string.IsNullOrWhiteSpace(t.SomeContent2), token);
|
||||
|
||||
// Assert
|
||||
Assert.Equal(10, result);
|
||||
Reader.Verify(x => x.CountAsync<TestDocument, Guid>(
|
||||
t => string.IsNullOrWhiteSpace(t.SomeContent2), null, token));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user