using System; 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.KeyedReadOnlyMongoRepositoryTests; public class GroupByTests : TestKeyedReadOnlyMongoRepositoryContext { private readonly Expression, int>> grouping = document => document.GroupingKey; private readonly Expression>, TestProjection>> projection = documents => new TestProjection {Count = documents.Count()}; private readonly Expression, bool>> filter = document => document.GroupingKey == 1; [Fact] public void WithGroupingCriteriaAndProjection_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); Reader = new Mock(); Reader.Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(grouping, projection); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(grouping, projection, null, CancellationToken.None), Times.Once); } [Fact] public void WithGroupingCriteriaAndProjectionAndCancellationToken_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); var token = new CancellationToken(true); Reader = new Mock(); Reader .Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(grouping, projection, token); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(grouping, projection, null, token), Times.Once); } [Fact] public void WithGroupingCriteriaAndProjectionAndPartitionKey_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); Reader = new Mock(); Reader .Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(grouping, projection, partitionKey); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(grouping, projection, partitionKey, CancellationToken.None), Times.Once); } [Fact] public void WithGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); Reader = new Mock(); Reader .Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(grouping, projection, partitionKey, token); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(grouping, projection, partitionKey, token), Times.Once); } [Fact] public void WithFilterAndGroupingCriteriaAndProjection_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); Reader = new Mock(); Reader.Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny,bool>>>(), It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(filter, grouping, projection); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, null, CancellationToken.None), Times.Once); } [Fact] public void WithFilterAndGroupingCriteriaAndProjectionAndCancellationToken_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); var token = new CancellationToken(true); Reader = new Mock(); Reader.Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny,bool>>>(), It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(filter, grouping, projection, token); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, null, token), Times.Once); } [Fact] public void WithFilterAndGroupingCriteriaAndProjectionAndPartitionKey_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); Reader = new Mock(); Reader.Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny,bool>>>(), It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(filter, grouping, projection, partitionKey); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, partitionKey, CancellationToken.None), Times.Once); } [Fact] public void WithFilterAndGroupingCriteriaAndProjectionAndPartitionKeyAndCancellationToken_Groups() { // Arrange var projections = Fixture.CreateMany().ToList(); var partitionKey = Fixture.Create(); var token = new CancellationToken(true); Reader = new Mock(); Reader.Setup( x => x.GroupBy, int, TestProjection, int>( It.IsAny,bool>>>(), It.IsAny, int>>>(), It.IsAny>, TestProjection>>>(), It.IsAny(), It.IsAny())) .Returns(projections); // Act var result = Sut.GroupBy(filter, grouping, projection, partitionKey, token); // Assert result.Should().NotBeNull(); result.Should().BeEquivalentTo(projections); Reader.Verify( x => x.GroupBy, int, TestProjection, int>(filter, grouping, projection, partitionKey, token), Times.Once); } }