diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueAsyncTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueAsyncTests .cs new file mode 100644 index 0000000..807a1b3 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueAsyncTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMaxValueAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMaxValueAsync(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupAsyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(new List {result}); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueTests .cs new file mode 100644 index 0000000..414d605 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMaxValueTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMaxValueTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMaxValue(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMaxValue(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMaxValue(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupSyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(new List {result}); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueAsyncTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueAsyncTests .cs new file mode 100644 index 0000000..cf81ed1 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueAsyncTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMinValueAsyncTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public async Task WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupAsyncGet(value, collection, partitionKey); + + // Act + var result = await Sut.GetMinValueAsync(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNextAsync(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupAsyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupAsyncCursor(new List {result}); + + SetupFindAsync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueTests .cs b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueTests .cs new file mode 100644 index 0000000..39a12b6 --- /dev/null +++ b/CoreUnitTests/DataAccessTests/MongoDbReaderTests/GetMinValueTests .cs @@ -0,0 +1,114 @@ +using System; +using System.Collections.Generic; +using System.Linq.Expressions; +using System.Threading; +using System.Threading.Tasks; +using AutoFixture; +using CoreUnitTests.Infrastructure.Model; +using FluentAssertions; +using MongoDB.Driver; +using MongoDbGenericRepository; +using Moq; +using Xunit; + +namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests; + +public class GetMinValueTests : BaseReaderTests +{ + private readonly Expression> filter = x => x.SomeContent == "SomeContent"; + private readonly Expression> selector = x => x.SomeValue; + + [Fact] + public void WithFilterAndSelector_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMinValue(filter, selector); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection); + + // Act + var result = Sut.GetMinValue(filter, selector, cancellationToken: token); + + // Assert + context.Verify(x => x.GetCollection(null), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once); + result.Should().Be(value); + } + + [Fact] + public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument() + { + // Arrange + var value = Fixture.Create(); + var collection = MockOf>(); + var partitionKey = Fixture.Create(); + var token = new CancellationToken(true); + var (context, cursor) = SetupSyncGet(value, collection, partitionKey); + + // Act + var result = Sut.GetMinValue(filter, selector, partitionKey, token); + + // Assert + context.Verify(x => x.GetCollection(partitionKey), Times.Once); + cursor.Verify(x => x.Current, Times.Once); + cursor.Verify(x => x.MoveNext(token), Times.Once); + result.Should().Be(value); + } + + private (Mock, Mock>) SetupSyncGet( + TValue result, + Mock> collection, + string partitionKey = null) + { + var asyncCursor = SetupSyncCursor(new List {result}); + + SetupFindSync(collection, asyncCursor); + + var context = MockOf(); + + context + .Setup(x => x.GetCollection(partitionKey)) + .Returns(collection.Object); + + return (context, asyncCursor); + } +} diff --git a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs index 1875161..3e55c59 100644 --- a/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs +++ b/MongoDbGenericRepository/DataAccess/Base/DataAccessBase.cs @@ -168,7 +168,6 @@ namespace MongoDbGenericRepository.DataAccess.Base .Limit(1); } - #endregion } }