From b09b359867f61408305dd7a00af483b1df48f680 Mon Sep 17 00:00:00 2001 From: Sean Garrett Date: Sun, 2 Jul 2023 17:54:30 +0100 Subject: [PATCH] rest of the unit tests for readonly repo --- .../Infrastructure/Model/TestDocument.cs | 2 + .../GetOneAsyncTests.cs | 8 +- .../ProjectManyAsyncTests.cs | 114 ++++++++++ .../ProjectManyTests.cs | 113 ++++++++++ .../ProjectOneAsyncTests.cs | 112 ++++++++++ .../ProjectOneTests.cs | 111 ++++++++++ .../SumByAsyncTests.cs | 198 ++++++++++++++++++ 7 files changed, 654 insertions(+), 4 deletions(-) create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs create mode 100644 CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs diff --git a/CoreUnitTests/Infrastructure/Model/TestDocument.cs b/CoreUnitTests/Infrastructure/Model/TestDocument.cs index 5f92e89..2985c16 100644 --- a/CoreUnitTests/Infrastructure/Model/TestDocument.cs +++ b/CoreUnitTests/Infrastructure/Model/TestDocument.cs @@ -15,6 +15,8 @@ public class TestDocument : Document public int SomeValue { get; set; } + public decimal SomeDecimalValue { get; set; } + public string SomeContent { get; set; } public string SomeContent2 { get; set; } diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs index d817465..9ae88f4 100644 --- a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/GetOneAsyncTests.cs @@ -17,7 +17,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext private readonly Expression> filter = document => document.SomeContent == "SomeContent"; [Fact] - public async Task WithId_GetsOne() + public async Task WithFilter_GetsOne() { // Arrange var document = Fixture.Create(); @@ -36,7 +36,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext } [Fact] - public async Task WithIdAndCancellationToken_GetsOne() + public async Task WithFilterAndCancellationToken_GetsOne() { // Arrange var document = Fixture.Create(); @@ -56,7 +56,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext } [Fact] - public async Task WithIdAndPartitionKey_GetsOne() + public async Task WithFilterAndPartitionKey_GetsOne() { // Arrange var document = Fixture.Create(); @@ -76,7 +76,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext } [Fact] - public async Task WithIdAndPartitionKeyAndCancellationToken_GetsOne() + public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne() { // Arrange var document = Fixture.Create(); diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs new file mode 100644 index 0000000..53535b4 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyAsyncTests.cs @@ -0,0 +1,114 @@ +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; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class ProjectManyAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync(filter, projection, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, partitionKey); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var projections = Fixture.CreateMany().ToList(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(projections); + + // Act + var result = await Sut.ProjectManyAsync(filter, projection, partitionKey, token); + + // Assert + result.Should().OnlyContain(x => projections.Contains(x)); + Reader.Verify( + x => x.ProjectManyAsync(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(List projections) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectManyAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(projections); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs new file mode 100644 index 0000000..47b9a09 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectManyTests.cs @@ -0,0 +1,113 @@ +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.KeyedReadOnlyMongoRepositoryTests; + +public class ProjectManyTests : TestKeyedReadOnlyMongoRepositoryContext +{ + 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); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs new file mode 100644 index 0000000..e027aa3 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneAsyncTests.cs @@ -0,0 +1,112 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class ProjectOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> projection = document => new TestProjection {NestedData = document.Nested.SomeDate}; + + [Fact] + public async Task WithFilterAndProjection_Projects() + { + // Arrange + var expected = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync(filter, projection, null, token), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = await Sut.ProjectOneAsync(filter, projection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOneAsync(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOneAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(result); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs new file mode 100644 index 0000000..280fcd0 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs @@ -0,0 +1,111 @@ +using System; +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 ProjectOneTests : TestKeyedReadOnlyMongoRepositoryContext +{ + 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 expected = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne( + filter, + projection, + null, + CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne(filter, projection, null, token), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKey_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne(filter, projection, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReader(expected); + + // Act + var result = Sut.ProjectOne(filter, projection, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.ProjectOne(filter, projection, partitionKey, token), + Times.Once); + } + + private void SetupReader(TestProjection result) + { + Reader = new Mock(); + Reader + .Setup( + x => x.ProjectOne( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .Returns(result); + } +} diff --git a/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs new file mode 100644 index 0000000..9652042 --- /dev/null +++ b/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/SumByAsyncTests.cs @@ -0,0 +1,198 @@ +using System; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDbGenericRepository.DataAccess.Read; +using Moq; +using Xunit; + +namespace CoreUnitTests.KeyedReadOnlyMongoRepositoryTests; + +public class SumByAsyncTests : TestKeyedReadOnlyMongoRepositoryContext +{ + private readonly Expression> filter = document => document.SomeContent == "SomeContent"; + private readonly Expression> intSelector = document => document.SomeValue; + private readonly Expression> decimalSelector = document => document.SomeDecimalValue; + + [Fact] + public async Task Int_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Int_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderInt(expected); + + // Act + var result = await Sut.SumByAsync(filter, intSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, intSelector, partitionKey, token), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelector_Sums() + { + // Arrange + var expected = Fixture.Create(); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, null, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, null, token), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndPartitionKey_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, partitionKey); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, partitionKey, CancellationToken.None), + Times.Once); + } + + [Fact] + public async Task Decimal_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums() + { + // Arrange + var expected = Fixture.Create(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + + SetupReaderDecimal(expected); + + // Act + var result = await Sut.SumByAsync(filter, decimalSelector, partitionKey, token); + + // Assert + result.Should().Be(expected); + Reader.Verify( + x => x.SumByAsync(filter, decimalSelector, partitionKey, token), + Times.Once); + } + + private void SetupReaderInt(int expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } + + private void SetupReaderDecimal(decimal expected) + { + Reader = new Mock(); + Reader + .Setup( + x => x.SumByAsync( + It.IsAny>>(), + It.IsAny>>(), + It.IsAny(), + It.IsAny())) + .ReturnsAsync(expected); + } +}