rest of the unit tests for readonly repo

This commit is contained in:
Sean Garrett
2023-07-02 17:54:30 +01:00
parent f9ea630611
commit b09b359867
7 changed files with 654 additions and 4 deletions
@@ -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; }
@@ -17,7 +17,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext<Guid>
private readonly Expression<Func<TestDocument, bool>> filter = document => document.SomeContent == "SomeContent";
[Fact]
public async Task WithId_GetsOne()
public async Task WithFilter_GetsOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
@@ -36,7 +36,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext<Guid>
}
[Fact]
public async Task WithIdAndCancellationToken_GetsOne()
public async Task WithFilterAndCancellationToken_GetsOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
@@ -56,7 +56,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext<Guid>
}
[Fact]
public async Task WithIdAndPartitionKey_GetsOne()
public async Task WithFilterAndPartitionKey_GetsOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
@@ -76,7 +76,7 @@ public class GetOneAsyncTests : TestKeyedReadOnlyMongoRepositoryContext<Guid>
}
[Fact]
public async Task WithIdAndPartitionKeyAndCancellationToken_GetsOne()
public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
@@ -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<Guid>
{
private readonly Expression<Func<TestDocument, bool>> filter = document => document.SomeContent == "SomeContent";
private readonly Expression<Func<TestDocument, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate};
[Fact]
public async Task WithFilterAndProjection_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().ToList();
SetupReader(projections);
// Act
var result = await Sut.ProjectManyAsync(filter, projection);
// Assert
result.Should().OnlyContain(x => projections.Contains(x));
Reader.Verify(
x => x.ProjectManyAsync<TestDocument, TestProjection, Guid>(
filter,
projection,
null,
CancellationToken.None),
Times.Once);
}
[Fact]
public async Task WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().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<TestDocument, TestProjection, Guid>(filter, projection, null, token),
Times.Once);
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
SetupReader(projections);
// Act
var result = await Sut.ProjectManyAsync(filter, projection, partitionKey);
// Assert
result.Should().OnlyContain(x => projections.Contains(x));
Reader.Verify(
x => x.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, CancellationToken.None),
Times.Once);
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
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<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, token),
Times.Once);
}
private void SetupReader(List<TestProjection> projections)
{
Reader = new Mock<IMongoDbReader>();
Reader
.Setup(
x => x.ProjectManyAsync<TestDocument, TestProjection, Guid>(
It.IsAny<Expression<Func<TestDocument, bool>>>(),
It.IsAny<Expression<Func<TestDocument, TestProjection>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(projections);
}
}
@@ -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<Guid>
{
private readonly Expression<Func<TestDocument, bool>> filter = document => document.SomeContent == "SomeContent";
private readonly Expression<Func<TestDocument, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate};
[Fact]
public void WithFilterAndProjection_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().ToList();
SetupReader(projections);
// Act
var result = Sut.ProjectMany(filter, projection);
// Assert
result.Should().OnlyContain(x => projections.Contains(x));
Reader.Verify(
x => x.ProjectMany<TestDocument, TestProjection, Guid>(
filter,
projection,
null,
CancellationToken.None),
Times.Once);
}
[Fact]
public void WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().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<TestDocument, TestProjection, Guid>(filter, projection, null, token),
Times.Once);
}
[Fact]
public void WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
SetupReader(projections);
// Act
var result = Sut.ProjectMany(filter, projection, partitionKey);
// Assert
result.Should().OnlyContain(x => projections.Contains(x));
Reader.Verify(
x => x.ProjectMany<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, CancellationToken.None),
Times.Once);
}
[Fact]
public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var projections = Fixture.CreateMany<TestProjection>().ToList();
var partitionKey = Fixture.Create<string>();
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<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, token),
Times.Once);
}
private void SetupReader(List<TestProjection> projections)
{
Reader = new Mock<IMongoDbReader>();
Reader
.Setup(
x => x.ProjectMany<TestDocument, TestProjection, Guid>(
It.IsAny<Expression<Func<TestDocument, bool>>>(),
It.IsAny<Expression<Func<TestDocument, TestProjection>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.Returns(projections);
}
}
@@ -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<Guid>
{
private readonly Expression<Func<TestDocument, bool>> filter = document => document.SomeContent == "SomeContent";
private readonly Expression<Func<TestDocument, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate};
[Fact]
public async Task WithFilterAndProjection_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
SetupReader(expected);
// Act
var result = await Sut.ProjectOneAsync(filter, projection);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.ProjectOneAsync<TestDocument, TestProjection, Guid>(
filter,
projection,
null,
CancellationToken.None),
Times.Once);
}
[Fact]
public async Task WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
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<TestDocument, TestProjection, Guid>(filter, projection, null, token),
Times.Once);
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
var partitionKey = Fixture.Create<string>();
SetupReader(expected);
// Act
var result = await Sut.ProjectOneAsync(filter, projection, partitionKey);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, CancellationToken.None),
Times.Once);
}
[Fact]
public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
var partitionKey = Fixture.Create<string>();
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<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, token),
Times.Once);
}
private void SetupReader(TestProjection result)
{
Reader = new Mock<IMongoDbReader>();
Reader
.Setup(
x => x.ProjectOneAsync<TestDocument, TestProjection, Guid>(
It.IsAny<Expression<Func<TestDocument, bool>>>(),
It.IsAny<Expression<Func<TestDocument, TestProjection>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(result);
}
}
@@ -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<Guid>
{
private readonly Expression<Func<TestDocument, bool>> filter = document => document.SomeContent == "SomeContent";
private readonly Expression<Func<TestDocument, TestProjection>> projection = document => new TestProjection {NestedData = document.Nested.SomeDate};
[Fact]
public void WithFilterAndProjection_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
SetupReader(expected);
// Act
var result = Sut.ProjectOne(filter, projection);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.ProjectOne<TestDocument, TestProjection, Guid>(
filter,
projection,
null,
CancellationToken.None),
Times.Once);
}
[Fact]
public void WithFilterAndProjectionAndCancellationToken_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
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<TestDocument, TestProjection, Guid>(filter, projection, null, token),
Times.Once);
}
[Fact]
public void WithFilterAndProjectionAndPartitionKey_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
var partitionKey = Fixture.Create<string>();
SetupReader(expected);
// Act
var result = Sut.ProjectOne(filter, projection, partitionKey);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.ProjectOne<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, CancellationToken.None),
Times.Once);
}
[Fact]
public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
{
// Arrange
var expected = Fixture.Create<TestProjection>();
var partitionKey = Fixture.Create<string>();
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<TestDocument, TestProjection, Guid>(filter, projection, partitionKey, token),
Times.Once);
}
private void SetupReader(TestProjection result)
{
Reader = new Mock<IMongoDbReader>();
Reader
.Setup(
x => x.ProjectOne<TestDocument, TestProjection, Guid>(
It.IsAny<Expression<Func<TestDocument, bool>>>(),
It.IsAny<Expression<Func<TestDocument, TestProjection>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.Returns(result);
}
}
@@ -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<Guid>
{
private readonly Expression<Func<TestDocument, bool>> filter = document => document.SomeContent == "SomeContent";
private readonly Expression<Func<TestDocument, int>> intSelector = document => document.SomeValue;
private readonly Expression<Func<TestDocument, decimal>> decimalSelector = document => document.SomeDecimalValue;
[Fact]
public async Task Int_WithFilterAndSelector_Sums()
{
// Arrange
var expected = Fixture.Create<int>();
SetupReaderInt(expected);
// Act
var result = await Sut.SumByAsync(filter, intSelector);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.SumByAsync<TestDocument, Guid>(filter, intSelector, null, CancellationToken.None),
Times.Once);
}
[Fact]
public async Task Int_WithFilterAndSelectorAndCancellationToken_Sums()
{
// Arrange
var expected = Fixture.Create<int>();
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<TestDocument, Guid>(filter, intSelector, null, token),
Times.Once);
}
[Fact]
public async Task Int_WithFilterAndSelectorAndPartitionKey_Sums()
{
// Arrange
var expected = Fixture.Create<int>();
var partitionKey = Fixture.Create<string>();
SetupReaderInt(expected);
// Act
var result = await Sut.SumByAsync(filter, intSelector, partitionKey);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.SumByAsync<TestDocument, Guid>(filter, intSelector, partitionKey, CancellationToken.None),
Times.Once);
}
[Fact]
public async Task Int_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums()
{
// Arrange
var expected = Fixture.Create<int>();
var partitionKey = Fixture.Create<string>();
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<TestDocument, Guid>(filter, intSelector, partitionKey, token),
Times.Once);
}
[Fact]
public async Task Decimal_WithFilterAndSelector_Sums()
{
// Arrange
var expected = Fixture.Create<decimal>();
SetupReaderDecimal(expected);
// Act
var result = await Sut.SumByAsync(filter, decimalSelector);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.SumByAsync<TestDocument, Guid>(filter, decimalSelector, null, CancellationToken.None),
Times.Once);
}
[Fact]
public async Task Decimal_WithFilterAndSelectorAndCancellationToken_Sums()
{
// Arrange
var expected = Fixture.Create<decimal>();
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<TestDocument, Guid>(filter, decimalSelector, null, token),
Times.Once);
}
[Fact]
public async Task Decimal_WithFilterAndSelectorAndPartitionKey_Sums()
{
// Arrange
var expected = Fixture.Create<decimal>();
var partitionKey = Fixture.Create<string>();
SetupReaderDecimal(expected);
// Act
var result = await Sut.SumByAsync(filter, decimalSelector, partitionKey);
// Assert
result.Should().Be(expected);
Reader.Verify(
x => x.SumByAsync<TestDocument, Guid>(filter, decimalSelector, partitionKey, CancellationToken.None),
Times.Once);
}
[Fact]
public async Task Decimal_WithFilterAndSelectorAndPartitionKeyAndCancellationToken_Sums()
{
// Arrange
var expected = Fixture.Create<decimal>();
var partitionKey = Fixture.Create<string>();
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<TestDocument, Guid>(filter, decimalSelector, partitionKey, token),
Times.Once);
}
private void SetupReaderInt(int expected)
{
Reader = new Mock<IMongoDbReader>();
Reader
.Setup(
x => x.SumByAsync<TestDocument, Guid>(
It.IsAny<Expression<Func<TestDocument, bool>>>(),
It.IsAny<Expression<Func<TestDocument,int>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(expected);
}
private void SetupReaderDecimal(decimal expected)
{
Reader = new Mock<IMongoDbReader>();
Reader
.Setup(
x => x.SumByAsync<TestDocument, Guid>(
It.IsAny<Expression<Func<TestDocument, bool>>>(),
It.IsAny<Expression<Func<TestDocument, decimal>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(expected);
}
}