unit tests

This commit is contained in:
Sean Garrett
2023-06-15 21:56:47 +01:00
parent 0cf6a7e5d3
commit 54e756c695
18 changed files with 817 additions and 8 deletions
@@ -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));
}
}
+6 -1
View File
@@ -2,13 +2,14 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<Nullable>warnings</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
@@ -20,4 +21,8 @@
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
</ItemGroup>
</Project>
@@ -0,0 +1,13 @@
namespace CoreUnitTests.Infrastructure.Model;
public class Child
{
public Child(string type, string value)
{
Type = type;
Value = value;
}
public string Type { get; set; }
public string Value { get; set; }
}
@@ -0,0 +1,13 @@
using System;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
namespace CoreUnitTests.Infrastructure.Model;
public class Nested
{
public DateTime SomeDate { get; set; }
[BsonRepresentation(BsonType.Decimal128)]
public decimal SomeAmount { get; set; }
}
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using MongoDbGenericRepository.Models;
namespace CoreUnitTests.Infrastructure.Model;
public class TestDocument : Document
{
public TestDocument()
{
Version = 2;
Nested = new Nested
{
SomeDate = DateTime.UtcNow
};
Children = new List<Child>();
}
public int SomeValue { get; set; }
public string SomeContent { get; set; }
public string SomeContent2 { get; set; }
public string SomeContent3 { get; set; }
public int GroupingKey { get; set; }
public Nested Nested { get; set; }
public List<Child> Children { get; set; }
}
@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using MongoDbGenericRepository.Models;
namespace CoreUnitTests.Infrastructure.Model;
public class TestDocumentWithKey : IDocument<int>
{
public int Id { get; set; }
public int Version { get; set; }
public TestDocumentWithKey()
{
Version = 2;
Nested = new Nested
{
SomeDate = DateTime.UtcNow
};
Children = new List<Child>();
}
public int SomeValue { get; set; }
public string SomeContent { get; set; }
public string SomeContent2 { get; set; }
public string SomeContent3 { get; set; }
public int GroupingKey { get; set; }
public Nested Nested { get; set; }
public List<Child> Children { get; set; }
}
@@ -1,6 +0,0 @@
namespace CoreUnitTests.Infrastructure;
public class TestDocument
{
}
@@ -1,6 +1,30 @@
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Create;
using MongoDbGenericRepository.DataAccess.Index;
using MongoDbGenericRepository.DataAccess.Read;
namespace CoreUnitTests.Infrastructure;
public class TestKeyedMongoRepository : KeyedMongoRepository<TestDocument, TestDocumentKey>
public class TestKeyedMongoRepository : BaseMongoRepository<int>
{
public TestKeyedMongoRepository(IMongoDatabase mongoDatabase)
: base(mongoDatabase)
{
}
public void SetIndexHandler(IMongoDbIndexHandler indexHandler)
{
MongoDbIndexHandler = indexHandler;
}
public void SetDbCreator(IMongoDbCreator creator)
{
MongoDbCreator = creator;
}
public void SetReader(IMongoDbReader reader)
{
MongoDbReader = reader;
}
}
@@ -0,0 +1,53 @@
using MongoDB.Driver;
using MongoDbGenericRepository.DataAccess.Create;
using MongoDbGenericRepository.DataAccess.Index;
using MongoDbGenericRepository.DataAccess.Read;
using Moq;
namespace CoreUnitTests.Infrastructure;
public class TestKeyedMongoRepositoryContext
{
private readonly Mock<IMongoDatabase> _mongoDatabase;
private TestKeyedMongoRepository _sut;
protected TestKeyedMongoRepositoryContext()
{
_mongoDatabase = new Mock<IMongoDatabase>();
}
protected TestKeyedMongoRepository Sut
{
get
{
if (_sut != null)
{
return _sut;
}
_sut = new TestKeyedMongoRepository(_mongoDatabase.Object);
if (IndexHandler != null)
{
_sut.SetIndexHandler(IndexHandler.Object);
}
if (Creator != null)
{
_sut.SetDbCreator(Creator.Object);
}
if (Reader != null)
{
_sut.SetReader(Reader.Object);
}
return _sut;
}
}
protected Mock<IMongoDbIndexHandler> IndexHandler { get; set; }
protected Mock<IMongoDbCreator> Creator { get; set; }
protected Mock<IMongoDbReader> Reader { get; set; }
}
@@ -0,0 +1,29 @@
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Create;
using MongoDbGenericRepository.DataAccess.Index;
using MongoDbGenericRepository.DataAccess.Read;
namespace CoreUnitTests.Infrastructure;
public class TestMongoRepository : BaseMongoRepository
{
public TestMongoRepository(IMongoDatabase mongoDatabase) : base(mongoDatabase)
{
}
public void SetIndexHandler(IMongoDbIndexHandler indexHandler)
{
MongoDbIndexHandler = indexHandler;
}
public void SetDbCreator(IMongoDbCreator creator)
{
MongoDbCreator = creator;
}
public void SetReader(IMongoDbReader reader)
{
MongoDbReader = reader;
}
}
@@ -0,0 +1,51 @@
using MongoDB.Driver;
using MongoDbGenericRepository.DataAccess.Create;
using MongoDbGenericRepository.DataAccess.Index;
using MongoDbGenericRepository.DataAccess.Read;
using Moq;
namespace CoreUnitTests.Infrastructure;
public class TestMongoRepositoryContext
{
private readonly Mock<IMongoDatabase> _mongoDatabase;
private TestMongoRepository _sut;
protected TestMongoRepositoryContext()
{
_mongoDatabase = new Mock<IMongoDatabase>();
}
protected TestMongoRepository Sut
{
get
{
if (_sut == null)
{
_sut = new TestMongoRepository(_mongoDatabase.Object);
if (IndexHandler != null)
{
_sut.SetIndexHandler(IndexHandler.Object);
}
if (Creator != null)
{
_sut.SetDbCreator(Creator.Object);
}
if (Reader != null)
{
_sut.SetReader(Reader.Object);
}
}
return _sut;
}
}
protected Mock<IMongoDbIndexHandler> IndexHandler { get; set; }
protected Mock<IMongoDbCreator> Creator { get; set; }
protected Mock<IMongoDbReader> Reader { get; set; }
}