Files
mongodb-generic-repository/CoreUnitTests/KeyedReadOnlyMongoRepositoryTests/ProjectOneTests.cs
T
2023-07-04 22:38:01 +01:00

112 lines
3.5 KiB
C#

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<int>
{
private readonly Expression<Func<TestDocumentWithKey<int>, bool>> filter = document => document.SomeContent == "SomeContent";
private readonly Expression<Func<TestDocumentWithKey<int>, 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<TestDocumentWithKey<int>, TestProjection, int>(
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<TestDocumentWithKey<int>, TestProjection, int>(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<TestDocumentWithKey<int>, TestProjection, int>(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<TestDocumentWithKey<int>, TestProjection, int>(filter, projection, partitionKey, token),
Times.Once);
}
private void SetupReader(TestProjection result)
{
Reader = new Mock<IMongoDbReader>();
Reader
.Setup(
x => x.ProjectOne<TestDocumentWithKey<int>, TestProjection, int>(
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
It.IsAny<Expression<Func<TestDocumentWithKey<int>, TestProjection>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.Returns(result);
}
}