using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Threading; using AutoFixture; using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure.Model; using FluentAssertions; using MongoDbGenericRepository.DataAccess.Read; using Moq; using Xunit; namespace CoreUnitTests.ReadOnlyMongoRepositoryTests; public class ProjectManyTests : TestReadOnlyMongoRepositoryContext { private readonly Expression> filter = document => document.SomeContent == "SomeContent"; private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; [Fact] public void WithFilterAndProjection_Projects() { // Arrange var projections = Fixture.CreateMany().ToList(); SetupReader(projections); // Act var result = Sut.ProjectMany(filter, projection); // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( x => x.ProjectMany( filter, projection, null, CancellationToken.None), Times.Once); } [Fact] public void WithFilterAndProjectionAndCancellationToken_Projects() { // Arrange var projections = Fixture.CreateMany().ToList(); var token = new CancellationToken(true); SetupReader(projections); // Act var result = Sut.ProjectMany(filter, projection, token); // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( x => x.ProjectMany(filter, projection, null, token), Times.Once); } [Fact] public void WithFilterAndProjectionAndPartitionKey_Projects() { // Arrange var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); SetupReader(projections); // Act var result = Sut.ProjectMany(filter, projection, partitionKey); // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( x => x.ProjectMany(filter, projection, partitionKey, CancellationToken.None), Times.Once); } [Fact] public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() { // Arrange var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); SetupReader(projections); // Act var result = Sut.ProjectMany(filter, projection, partitionKey, token); // Assert result.Should().OnlyContain(x => projections.Contains(x)); Reader.Verify( x => x.ProjectMany(filter, projection, partitionKey, token), Times.Once); } private void SetupReader(List projections) { Reader = new Mock(); Reader .Setup( x => x.ProjectMany( It.IsAny>>(), It.IsAny>>(), It.IsAny(), It.IsAny())) .Returns(projections); } #region Keyed private readonly Expression, bool>> keyedFilter = document => document.SomeContent == "SomeContent"; private readonly Expression, TestProjection>> keyedProjection = document => new TestProjection {NestedData = document.Nested.SomeDate}; [Fact] public void Keyed_WithFilterAndProjection_Projects() { // Arrange var keyedProjections = Fixture.CreateMany().ToList(); SetupKeyedReader(keyedProjections); // Act var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection); // Assert result.Should().OnlyContain(x => keyedProjections.Contains(x)); Reader.Verify( x => x.ProjectMany, TestProjection, int>( keyedFilter, keyedProjection, null, CancellationToken.None), Times.Once); } [Fact] public void Keyed_WithFilterAndProjectionAndCancellationToken_Projects() { // Arrange var keyedProjections = Fixture.CreateMany().ToList(); var token = new CancellationToken(true); SetupKeyedReader(keyedProjections); // Act var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, token); // Assert result.Should().OnlyContain(x => keyedProjections.Contains(x)); Reader.Verify( x => x.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, null, token), Times.Once); } [Fact] public void Keyed_WithFilterAndProjectionAndPartitionKey_Projects() { // Arrange var keyedProjections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); SetupKeyedReader(keyedProjections); // Act var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey); // Assert result.Should().OnlyContain(x => keyedProjections.Contains(x)); Reader.Verify( x => x.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, CancellationToken.None), Times.Once); } [Fact] public void Keyed_WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() { // Arrange var keyedProjections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); SetupKeyedReader(keyedProjections); // Act var result = Sut.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token); // Assert result.Should().OnlyContain(x => keyedProjections.Contains(x)); Reader.Verify( x => x.ProjectMany, TestProjection, int>(keyedFilter, keyedProjection, partitionKey, token), Times.Once); } private void SetupKeyedReader(List keyedProjections) { Reader = new Mock(); Reader .Setup( x => x.ProjectMany, TestProjection, int>( It.IsAny, bool>>>(), It.IsAny, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(keyedProjections); } #endregion }