MongodbReader cancellation tokens and some unit tests

This commit is contained in:
Sean Garrett
2023-06-25 15:03:52 +01:00
parent 11ce19cb5c
commit dee1376a41
22 changed files with 1758 additions and 614 deletions
@@ -2,7 +2,6 @@ using System.Collections.Generic;
using System.Threading;
using CoreUnitTests.Infrastructure;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDbGenericRepository.DataAccess.Index;
using Moq;
@@ -41,5 +40,4 @@ public class BaseIndexTests : GenericTestContext<MongoDbIndexHandler>
return (asyncCursor, indexManager);
}
}
@@ -19,7 +19,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
public async Task WithFieldExpression_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -42,7 +42,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptions_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -68,7 +68,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex()
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -95,7 +95,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -121,7 +121,7 @@ public class CreateAscendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -29,7 +29,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests
=> this.testOutputHelper = testOutputHelper;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
public async Task WithFieldExpression_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -52,7 +52,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptions_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -78,7 +78,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex()
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -105,7 +105,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -131,7 +131,7 @@ public class CreateCombinedTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -19,7 +19,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
public async Task WithFieldExpression_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -42,7 +42,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptions_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -68,7 +68,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex()
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -95,7 +95,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -121,7 +121,7 @@ public class CreateDescendingIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -24,7 +24,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests
=> this.testOutputHelper = testOutputHelper;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
public async Task WithFieldExpression_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -47,7 +47,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptions_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -73,7 +73,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex()
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -100,7 +100,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -126,7 +126,7 @@ public class CreateHashedIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -19,7 +19,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
[Fact]
public async Task WhenFieldExpression_ThenCreatesIndex()
public async Task WithFieldExpression_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -42,7 +42,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptions_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptions_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -68,7 +68,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndPartitionKey_ThenCreatesIndex()
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -95,7 +95,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -121,7 +121,7 @@ public class CreateTextIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_ThenCreatesIndex()
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -13,7 +13,7 @@ namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
public class DropIndexAsyncTests : BaseIndexTests
{
[Fact]
public async Task WhenIndexName_ThenDropsIndex()
public async Task WithIndexName_DropsIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -30,7 +30,7 @@ public class DropIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenIndexNameAndPartitionKey_ThenDropsIndex()
public async Task WithIndexNameAndPartitionKey_DropsIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -50,7 +50,7 @@ public class DropIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenIndexNameAndCancellationToken_ThenDropsIndex()
public async Task WithIndexNameAndCancellationToken_DropsIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -69,7 +69,7 @@ public class DropIndexAsyncTests : BaseIndexTests
}
[Fact]
public async Task WhenIndexNameAndPartitionKeyAndCancellationToken_ThenDropsIndex()
public async Task WithIndexNameAndPartitionKeyAndCancellationToken_DropsIndex()
{
// Arrange
var expectedIndexName = Fixture.Create<string>();
@@ -0,0 +1,69 @@
using System.Collections.Generic;
using System.Threading;
using CoreUnitTests.Infrastructure;
using MongoDB.Driver;
using MongoDbGenericRepository.DataAccess.Read;
using Moq;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class BaseReaderTests : GenericTestContext<MongoDbReader>
{
protected Mock<IAsyncCursor<TDocument>> SetupSyncCursor<TDocument>(List<TDocument> documents)
{
var asyncCursor = MockOf<IAsyncCursor<TDocument>>();
var moveNextSequence = asyncCursor
.SetupSequence(x => x.MoveNext(It.IsAny<CancellationToken>()));
var currentSequence = asyncCursor
.SetupSequence(x => x.Current);
foreach (var projection in documents)
{
moveNextSequence.Returns(true);
currentSequence.Returns(new[] {projection});
}
moveNextSequence.Returns(false);
return asyncCursor;
}
protected Mock<IAsyncCursor<TDocument>> SetupAsyncCursor<TDocument>(List<TDocument> documents)
{
var asyncCursor = MockOf<IAsyncCursor<TDocument>>();
var moveNextSequence = asyncCursor
.SetupSequence(x => x.MoveNextAsync(It.IsAny<CancellationToken>()));
var currentSequence = asyncCursor
.SetupSequence(x => x.Current);
foreach (var projection in documents)
{
moveNextSequence.ReturnsAsync(true);
currentSequence.Returns(new[] {projection});
}
moveNextSequence.ReturnsAsync(false);
return asyncCursor;
}
protected static void SetupFindAsync<TDocument, TProjection>(Mock<IMongoCollection<TDocument>> collection, Mock<IAsyncCursor<TProjection>> asyncCursor) =>
collection
.Setup(
x => x.FindAsync(
It.IsAny<FilterDefinition<TDocument>>(),
It.IsAny<FindOptions<TDocument, TProjection>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(asyncCursor.Object);
protected static void SetupFindSync<TDocument, TProjection>(Mock<IMongoCollection<TDocument>> collection, Mock<IAsyncCursor<TProjection>> asyncCursor) =>
collection
.Setup(
x => x.FindSync(
It.IsAny<FilterDefinition<TDocument>>(),
It.IsAny<FindOptions<TDocument, TProjection>>(),
It.IsAny<CancellationToken>()))
.Returns(asyncCursor.Object);
}
@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class GetByIdAsyncTests : BaseReaderTests
{
[Fact]
public async Task WithId_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithIdAndCancellationToken_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithIdAndPartitionKey_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithIdAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
List<TDocument> documents,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupAsyncCursor(documents);
SetupFindAsync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,114 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class GetByIdTests : BaseReaderTests
{
[Fact]
public void WithId_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithIdAndCancellationToken_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithIdAndPartitionKey_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithIdAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
List<TDocument> documents,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupSyncCursor(documents);
SetupFindSync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,283 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class GetOneAsyncTests : BaseReaderTests
{
[Fact]
public async Task WithFilter_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithFilterAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithFilterAndPartitionKey_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithCondition_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithConditionAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithConditionAndPartitionKey_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, partitionKey: partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithConditionAndFindOptions_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var options = Fixture
.Build<FindOptions>()
.Without(x => x.Comment)
.Without(x => x.Hint)
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, options);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var options = Fixture
.Build<FindOptions>()
.Without(x => x.Comment)
.Without(x => x.Hint)
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncGet(documents, collection);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, options, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public async Task WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var options = Fixture
.Build<FindOptions>()
.Without(x => x.Comment)
.Without(x => x.Hint)
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(true);
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, options, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
List<TDocument> documents,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupAsyncCursor(documents);
SetupFindAsync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,282 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class GetOneTests : BaseReaderTests
{
[Fact]
public void WithFilter_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupSyncGet(documents, collection);
// Act
var result = Sut.GetOne<TestDocument, Guid>(filter);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithFilterAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupSyncGet(documents, collection);
// Act
var result = Sut.GetOne<TestDocument, Guid>(filter, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithFilterAndPartitionKey_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
// Act
var result = Sut.GetOne<TestDocument, Guid>(filter, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
// Act
var result = Sut.GetOne<TestDocument, Guid>(filter, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithCondition_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var (context, cursor) = SetupSyncGet(documents, collection);
// Act
var result = Sut.GetOne<TestDocument, Guid>(condition);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithConditionAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(true);
var (context, cursor) = SetupSyncGet(documents, collection);
// Act
var result = Sut.GetOne<TestDocument, Guid>(condition, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithConditionAndPartitionKey_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
// Act
var result = Sut.GetOne<TestDocument, Guid>(condition, partitionKey: partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
// Act
var result = Sut.GetOne<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithConditionAndFindOptions_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var options = Fixture
.Build<FindOptions>()
.Without(x => x.Comment)
.Without(x => x.Hint)
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var (context, cursor) = SetupSyncGet(documents, collection);
// Act
var result = Sut.GetOne<TestDocument, Guid>(condition, options);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var options = Fixture
.Build<FindOptions>()
.Without(x => x.Comment)
.Without(x => x.Hint)
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(true);
var (context, cursor) = SetupSyncGet(documents, collection);
// Act
var result = Sut.GetOne<TestDocument, Guid>(condition, options, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
[Fact]
public void WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var options = Fixture
.Build<FindOptions>()
.Without(x => x.Comment)
.Without(x => x.Hint)
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(true);
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
// Act
var result = Sut.GetOne<TestDocument, Guid>(condition, options, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(documents[0]);
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupSyncGet<TDocument>(
List<TDocument> documents,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupSyncCursor(documents);
SetupFindSync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,123 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class ProjectManyAsyncTests : BaseReaderTests
{
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
private readonly Expression<Func<TestDocument, TestProjection>>
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
[Fact]
public async Task WithFilterAndProjection_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var (context, cursor) = SetupAsyncProjection(projections, collection);
// Act
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.AtLeast(1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
[Fact]
public async Task WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var token = new CancellationToken(false);
var (context, cursor) = SetupAsyncProjection(projections, collection);
// Act
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.AtLeast(1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
// Act
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.AtLeast(1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(false);
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
// Act
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.AtLeast(1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupAsyncProjection<TDocument, TProjection>(
List<TProjection> projections,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupAsyncCursor(projections);
collection
.Setup(
x => x.FindAsync(
It.IsAny<FilterDefinition<TDocument>>(),
It.IsAny<FindOptions<TDocument, TProjection>>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(asyncCursor.Object);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,117 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class ProjectManyTests : BaseReaderTests
{
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
private readonly Expression<Func<TestDocument, TestProjection>>
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
[Fact]
public void WithFilterAndProjection_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var (context, cursor) = SetupAsyncProjection(projections, collection);
// Act
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.AtLeast(1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
[Fact]
public void WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var token = new CancellationToken(false);
var (context, cursor) = SetupAsyncProjection(projections, collection);
// Act
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.AtLeast(1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
[Fact]
public void WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
// Act
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.AtLeast(1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
[Fact]
public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(false);
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
// Act
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.AtLeast(projections.Count));
result.Should().NotBeNull();
result.Should().OnlyContain(x => projections.Contains(x));
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupAsyncProjection<TDocument, TProjection>(
List<TProjection> projections,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupSyncCursor(projections);
SetupFindSync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class ProjectOneAsyncTests : BaseReaderTests
{
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
private readonly Expression<Func<TestDocument, TestProjection>>
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
[Fact]
public async Task WithFilterAndProjection_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var (context, cursor) = SetupAsyncProjection(projections, collection);
// Act
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
[Fact]
public async Task WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncProjection(projections, collection);
// Act
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
// Act
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
// Act
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupAsyncProjection<TDocument, TProjection>(
List<TProjection> projections,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupAsyncCursor(projections);
SetupFindAsync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,121 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class ProjectOneTests : BaseReaderTests
{
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
private readonly Expression<Func<TestDocument, TestProjection>>
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
[Fact]
public void WithFilterAndProjection_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var (context, cursor) = SetupProjection(projections, collection);
// Act
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
[Fact]
public void WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var token = new CancellationToken(true);
var (context, cursor) = SetupProjection(projections, collection);
// Act
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
[Fact]
public void WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupProjection(projections, collection, partitionKey);
// Act
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
[Fact]
public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var (context, cursor) = SetupProjection(projections, collection, partitionKey);
// Act
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Once);
cursor.Verify(x => x.MoveNext(token), Times.Once);
result.Should().NotBeNull();
result.Should().Be(projections[0]);
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupProjection<TDocument, TProjection>(
List<TProjection> projections,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupSyncCursor(projections);
SetupFindSync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();
context
.Setup(x => x.GetCollection<TDocument>(partitionKey))
.Returns(collection.Object);
return (context, asyncCursor);
}
}
@@ -0,0 +1,10 @@
using System;
namespace CoreUnitTests.Infrastructure.Model;
public class TestProjection
{
public Guid TestDocumentId { get; set; }
public DateTime NestedData { get; set; }
}