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> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2); private readonly Expression> projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate}; [Fact] public async Task WithFilterAndProjection_Projects() { // Arrange var collection = MockOf>(); var projections = Fixture.CreateMany().ToList(); var (context, cursor) = SetupAsyncProjection(projections, collection); // Act var result = await Sut.ProjectOneAsync(filter, projectionExpression); // Assert context.Verify(x => x.GetCollection(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>(); var projections = Fixture.CreateMany().ToList(); var token = new CancellationToken(true); var (context, cursor) = SetupAsyncProjection(projections, collection); // Act var result = await Sut.ProjectOneAsync(filter, projectionExpression, cancellationToken: token); // Assert context.Verify(x => x.GetCollection(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>(); var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); // Act var result = await Sut.ProjectOneAsync(filter, projectionExpression, partitionKey); // Assert context.Verify(x => x.GetCollection(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>(); var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey); // Act var result = await Sut.ProjectOneAsync(filter, projectionExpression, partitionKey, token); // Assert context.Verify(x => x.GetCollection(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, Mock>) SetupAsyncProjection( List projections, Mock> collection, string partitionKey = null) { var asyncCursor = SetupAsyncCursor(projections); SetupFindAsync(collection, asyncCursor); var context = MockOf(); context .Setup(x => x.GetCollection(partitionKey)) .Returns(collection.Object); return (context, asyncCursor); } }