Reader GetAll, GetByMin and getByMax test

This commit is contained in:
Sean Garrett
2023-06-25 21:13:17 +01:00
parent 0699130733
commit f7ba046f20
6 changed files with 142 additions and 139 deletions
@@ -3,7 +3,6 @@ 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;
@@ -14,73 +13,73 @@ using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
public class GetAllAsyncTests : BaseReaderTests
public class GetAllTests : BaseReaderTests
{
[Fact]
public async Task WithFilter_GetsMatchingDocuments()
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) = SetupAsyncGet(documents, collection);
var (context, cursor) = SetupGet(documents, collection);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter);
var result = Sut.GetAll<TestDocument, Guid>(filter);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithFilterAndCancellationToken_GetsMatchingDocuments()
public void WithFilterAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(false);
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupAsyncGet(documents, collection);
var (context, cursor) = SetupGet(documents, collection);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter, cancellationToken: token);
var result = Sut.GetAll<TestDocument, Guid>(filter, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithFilterAndPartitionKey_GetsMatchingDocuments()
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) = SetupAsyncGet(documents, collection, partitionKey);
var (context, cursor) = SetupGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter, partitionKey);
var result = Sut.GetAll<TestDocument, Guid>(filter, partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
public void WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
@@ -88,83 +87,83 @@ public class GetAllAsyncTests : BaseReaderTests
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(false);
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
var (context, cursor) = SetupGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter, partitionKey, token);
var result = Sut.GetAll<TestDocument, Guid>(filter, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithCondition_GetsMatchingDocuments()
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) = SetupAsyncGet(documents, collection);
var (context, cursor) = SetupGet(documents, collection);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition);
var result = Sut.GetAll<TestDocument, Guid>(condition);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithConditionAndCancellationToken_GetsMatchingDocuments()
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(false);
var (context, cursor) = SetupAsyncGet(documents, collection);
var (context, cursor) = SetupGet(documents, collection);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, cancellationToken: token);
var result = Sut.GetAll<TestDocument, Guid>(condition, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithConditionAndPartitionKey_GetsMatchingDocuments()
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) = SetupAsyncGet(documents, collection, partitionKey);
var (context, cursor) = SetupGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, partitionKey: partitionKey);
var result = Sut.GetAll<TestDocument, Guid>(condition, partitionKey: partitionKey);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
public void WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
@@ -172,21 +171,21 @@ public class GetAllAsyncTests : BaseReaderTests
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(false);
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
var (context, cursor) = SetupGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
var result = Sut.GetAll<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithConditionAndFindOptions_GetsMatchingDocuments()
public void WithConditionAndFindOptions_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
@@ -197,21 +196,21 @@ public class GetAllAsyncTests : BaseReaderTests
.Without(x => x.Hint)
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var (context, cursor) = SetupAsyncGet(documents, collection);
var (context, cursor) = SetupGet(documents, collection);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, options);
var result = Sut.GetAll<TestDocument, Guid>(condition, options);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
public void WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
@@ -223,21 +222,21 @@ public class GetAllAsyncTests : BaseReaderTests
.Create();
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(false);
var (context, cursor) = SetupAsyncGet(documents, collection);
var (context, cursor) = SetupGet(documents, collection);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, options, cancellationToken: token);
var result = Sut.GetAll<TestDocument, Guid>(condition, options, cancellationToken: token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
[Fact]
public async Task WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
public void WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
{
// Arrange
var collection = MockOf<IMongoCollection<TestDocument>>();
@@ -250,27 +249,27 @@ public class GetAllAsyncTests : BaseReaderTests
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
var token = new CancellationToken(false);
var partitionKey = Fixture.Create<string>();
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
var (context, cursor) = SetupGet(documents, collection, partitionKey);
// Act
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, options, partitionKey, token);
var result = Sut.GetAll<TestDocument, Guid>(condition, options, partitionKey, token);
// Assert
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
result.Should().NotBeNull();
result.Should().OnlyContain(x => documents.Contains(x));
}
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupGet<TDocument>(
List<TDocument> documents,
Mock<IMongoCollection<TDocument>> collection,
string partitionKey = null)
{
var asyncCursor = SetupAsyncCursor(documents);
var asyncCursor = SetupSyncCursor(documents);
SetupFindAsync(collection, asyncCursor);
SetupFindSync(collection, asyncCursor);
var context = MockOf<IMongoDbContext>();