Compare commits
65 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e90f9b600e | |||
| 9c48653dc6 | |||
| b083988509 | |||
| 7bd356c7da | |||
| f59cccb3d9 | |||
| 710b7b992f | |||
| 9812bded04 | |||
| 3b6b08a9ad | |||
| 5ff1c21780 | |||
| 8f2f92f9d3 | |||
| 091ee9c204 | |||
| 7bbbd7bd71 | |||
| cb55d3bffa | |||
| 67784e64ee | |||
| 3a7b24262e | |||
| 26b71ff76e | |||
| c70727212e | |||
| be58460bf1 | |||
| dc7a4dc67b | |||
| b09b359867 | |||
| f9ea630611 | |||
| 40e287418b | |||
| 240b61d1d2 | |||
| f7ba046f20 | |||
| 0699130733 | |||
| 03b8b273f8 | |||
| dee1376a41 | |||
| 11ce19cb5c | |||
| 387bf6beec | |||
| 807a4b17ce | |||
| 251ffdacfa | |||
| aaf80a7ae2 | |||
| b7b23e7b92 | |||
| 25766febd4 | |||
| ed2eddd33e | |||
| 39e93dc968 | |||
| cae2b01dd9 | |||
| 86d9ea6990 | |||
| 1b8f795e00 | |||
| b285c7d919 | |||
| 23780697fb | |||
| f09cfc53c2 | |||
| c6545b9448 | |||
| 54e756c695 | |||
| 0cf6a7e5d3 | |||
| e23f659ffe | |||
| 02b9385fd8 | |||
| 14e261261c | |||
| d166feb5d6 | |||
| 885507274d | |||
| 52390fad25 | |||
| f888d5996b | |||
| f74254b411 | |||
| c14ceffe4f | |||
| dc196b24f7 | |||
| fe02d871f3 | |||
| ff94611ee9 | |||
| 71b64c84a0 | |||
| 5f6e015bf2 | |||
| 6d8e358ccc | |||
| 76a1aab671 | |||
| eebdc89575 | |||
| ee4950c5d4 | |||
| 658c753989 | |||
| 0e8d629a8f |
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -1,27 +1,23 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.0</TargetFramework>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.2" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.1.39" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.9.3" />
|
||||
<PackageReference Include="MongoDbGenericRepository" Version="1.4.5" />
|
||||
<PackageReference Include="xunit" Version="2.4.1" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.4.1">
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
|
||||
<PackageReference Include="xunit" Version="2.5.0" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.5.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Reference Include="System.Configuration">
|
||||
<HintPath>..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll</HintPath>
|
||||
</Reference>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -30,4 +26,8 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -62,18 +62,18 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
}
|
||||
|
||||
#region IDisposable Support
|
||||
private bool disposedValue = false; // Pour détecter les appels redondants
|
||||
private bool _disposedValue; // Pour détecter les appels redondants
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposedValue)
|
||||
if (!_disposedValue)
|
||||
{
|
||||
if (disposing)
|
||||
{
|
||||
Cleanup();
|
||||
}
|
||||
|
||||
disposedValue = true;
|
||||
_disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,8 +5,8 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
internal static class MongoDbConfig
|
||||
{
|
||||
private static bool _initialized = false;
|
||||
private static object _initializationLock = new object();
|
||||
private static bool _initialized;
|
||||
private static object _initializationLock = new();
|
||||
private static object _initializationTarget;
|
||||
|
||||
public static void EnsureConfigured()
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
@@ -8,12 +6,12 @@ using System.Linq.Expressions;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
public abstract partial class MongoDbDocumentTestBase<T> :
|
||||
IClassFixture<MongoDbTestFixture<T, Guid>>
|
||||
public abstract partial class MongoDbDocumentTestBase<T>
|
||||
where T : TestDoc, new()
|
||||
{
|
||||
|
||||
@@ -74,7 +72,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
// Act
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
@@ -87,7 +85,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync<T>(document);
|
||||
await SUT.AddOneAsync(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
@@ -100,7 +98,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(2);
|
||||
// Act
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
@@ -123,7 +121,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey;
|
||||
((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey;
|
||||
// Act
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<T>(e => e.Id.Equals(documents[0].Id) || e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
long secondPartitionCount = SUT.Count<T>(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey);
|
||||
@@ -141,7 +139,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(2);
|
||||
// Act
|
||||
await SUT.AddManyAsync<T>(documents);
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
@@ -164,7 +162,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
((IPartitionedDocument)documents[2]).PartitionKey = secondPartitionKey;
|
||||
((IPartitionedDocument)documents[3]).PartitionKey = secondPartitionKey;
|
||||
// Act
|
||||
await SUT.AddManyAsync<T>(documents);
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<T>(e => e.Id.Equals(documents[0].Id) || e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
long secondPartitionCount = SUT.Count<T>(e => e.Id.Equals(documents[2].Id) || e.Id.Equals(documents[3].Id), secondPartitionKey);
|
||||
@@ -185,7 +183,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
await SUT.AddOneAsync(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<T>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
@@ -197,7 +195,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
@@ -209,7 +207,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
@@ -221,7 +219,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
@@ -233,7 +231,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
var count = cursor.CountDocuments();
|
||||
@@ -246,7 +244,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
@@ -258,7 +256,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
|
||||
// Assert
|
||||
@@ -270,7 +268,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
@@ -282,7 +280,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
|
||||
// Assert
|
||||
@@ -296,7 +294,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
@@ -310,7 +308,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
@@ -324,7 +322,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
@@ -338,7 +336,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.Count<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
@@ -354,9 +352,9 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T>(document);
|
||||
var result = SUT.DeleteOne(document);
|
||||
// Assert
|
||||
Assert.True(1 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
@@ -367,7 +365,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
@@ -380,9 +378,9 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T>(document);
|
||||
var result = await SUT.DeleteOneAsync(document);
|
||||
// Assert
|
||||
Assert.True(1 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
@@ -393,7 +391,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
@@ -408,7 +406,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
@@ -432,9 +430,9 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
((IPartitionedDocument)documents[4]).PartitionKey = secondKey;
|
||||
}
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T>(documents);
|
||||
var result = await SUT.DeleteManyAsync(documents);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
@@ -451,7 +449,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
@@ -475,9 +473,9 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
((IPartitionedDocument)documents[4]).PartitionKey = secondKey;
|
||||
}
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T>(documents);
|
||||
var result = SUT.DeleteMany(documents);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
@@ -500,7 +498,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<T, MyTestProjection>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
@@ -526,7 +524,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T>(document);
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<T, MyTestProjection>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
@@ -556,7 +554,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<T, MyTestProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
@@ -586,7 +584,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<T, MyTestProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
@@ -619,7 +617,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -642,7 +640,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -665,7 +663,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -688,7 +686,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -711,7 +709,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -734,7 +732,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMin = documents.OrderBy(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -757,7 +755,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMax = documents.OrderByDescending(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -780,7 +778,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeDate = e.Nested.SomeDate.AddDays(i++);
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedMin = documents.OrderByDescending(e => e.Nested.SomeDate).First();
|
||||
|
||||
// Act
|
||||
@@ -910,7 +908,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Act
|
||||
Expression <Func<T, object>> ex = x => x.SomeContent2;
|
||||
Expression <Func<T, object>> ex2 = x => x.SomeContent3;
|
||||
var result = await SUT.CreateCombinedTextIndexAsync<T>(new[] { ex, ex2 }, null, PartitionKey);
|
||||
var result = await SUT.CreateCombinedTextIndexAsync(new[] { ex, ex2 }, null, PartitionKey);
|
||||
|
||||
// Assert
|
||||
var listOfIndexNames = await SUT.GetIndexesNamesAsync<T>(PartitionKey);
|
||||
@@ -937,7 +935,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeAmount = 5m;
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
|
||||
|
||||
// Act
|
||||
@@ -960,7 +958,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
e.Nested.SomeAmount = 5m;
|
||||
e.SomeContent = criteria;
|
||||
});
|
||||
SUT.AddMany<T>(documents);
|
||||
SUT.AddMany(documents);
|
||||
var expectedSum = documents.Sum(e => e.Nested.SomeAmount);
|
||||
|
||||
// Act
|
||||
|
||||
@@ -204,6 +204,30 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOneByFilterDefinitionAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<T, TKey>(Builders<T>.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOneByFilterDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<T, TKey>(Builders<T>.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOneAsync()
|
||||
@@ -241,6 +265,30 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Assert
|
||||
Assert.True(1 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncByDefinitionReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T, TKey>(Builders<T>.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncByDefinitionReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T, TKey>(Builders<T>.Filter.Eq(x => x.Id, document.Init<TKey>()), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
@@ -265,6 +313,30 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyByDefinitionReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T, TKey>(Builders<T>.Filter.Eq(x => x.Id, document.Id), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyByDefinitionReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T, TKey>(Builders<T>.Filter.Eq(x => x.Id, document.Init<TKey>()), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyReturnsTrue()
|
||||
@@ -289,6 +361,34 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllByDefinitionAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<T, TKey>(Builders<T>.Filter.Eq(x => x.SomeContent, content), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAllByDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<T, TKey>(Builders<T>.Filter.Eq(x => x.SomeContent, content), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync()
|
||||
@@ -317,6 +417,34 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountByDefinitionAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<T, TKey>(Builders<T>.Filter.Eq(x => x.SomeContent, content), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void CountByDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.Count<T, TKey>(Builders<T>.Filter.Eq(x => x.SomeContent, content), null, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountAsync()
|
||||
@@ -796,7 +924,7 @@ namespace CoreIntegrationTests.Infrastructure
|
||||
|
||||
#region Index Management
|
||||
|
||||
static SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1);
|
||||
static readonly SemaphoreSlim textIndexSemaphore = new SemaphoreSlim(1, 1);
|
||||
|
||||
[Fact]
|
||||
public async Task CreateTextIndexNoOptionAsync()
|
||||
|
||||
@@ -0,0 +1,218 @@
|
||||
# All Files
|
||||
[*]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
|
||||
#########################################
|
||||
# File Extension Settings
|
||||
##########################################
|
||||
|
||||
# .NET Style Rules
|
||||
# https://docs.microsoft.com/dotnet/fundamentals/code-analysis/style-rules/language-rules#net-style-rules
|
||||
[*.cs]
|
||||
# "this." and "Me." qualifiers
|
||||
dotnet_style_qualification_for_field = false:warning
|
||||
dotnet_style_qualification_for_property = false:warning
|
||||
dotnet_style_qualification_for_method = false:warning
|
||||
dotnet_style_qualification_for_event = false:warning
|
||||
# Language keywords instead of framework type names for type references
|
||||
dotnet_style_predefined_type_for_locals_parameters_members = true:warning
|
||||
dotnet_style_predefined_type_for_member_access = true:warning
|
||||
# Modifier preferences
|
||||
dotnet_style_require_accessibility_modifiers = always:warning
|
||||
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:warning
|
||||
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:warning
|
||||
dotnet_style_readonly_field = true:warning
|
||||
# Parentheses preferences
|
||||
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:warning
|
||||
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:warning
|
||||
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:warning
|
||||
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:warning
|
||||
# Expression-level preferences
|
||||
dotnet_style_object_initializer = true:warning
|
||||
dotnet_style_collection_initializer = true:warning
|
||||
dotnet_style_explicit_tuple_names = true:warning
|
||||
dotnet_style_prefer_inferred_tuple_names = true:warning
|
||||
dotnet_style_prefer_inferred_anonymous_type_member_names = true:warning
|
||||
dotnet_style_prefer_auto_properties = true:warning
|
||||
dotnet_style_prefer_conditional_expression_over_assignment = false:suggestion
|
||||
dotnet_diagnostic.IDE0045.severity = suggestion
|
||||
dotnet_style_prefer_conditional_expression_over_return = false:suggestion
|
||||
dotnet_diagnostic.IDE0046.severity = suggestion
|
||||
dotnet_style_prefer_compound_assignment = true:warning
|
||||
dotnet_style_prefer_simplified_interpolation = true:warning
|
||||
dotnet_style_prefer_simplified_boolean_expressions = true:warning
|
||||
# Null-checking preferences
|
||||
dotnet_style_coalesce_expression = true:warning
|
||||
dotnet_style_null_propagation = true:warning
|
||||
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
|
||||
# File header preferences
|
||||
# file_header_template = <copyright file="{fileName}" company="PROJECT-AUTHOR">\n© PROJECT-AUTHOR\n</copyright>
|
||||
# If you use StyleCop, you'll need to disable SA1636: File header copyright text should match.
|
||||
# dotnet_diagnostic.SA1636.severity = none
|
||||
# Undocumented
|
||||
dotnet_style_operator_placement_when_wrapping = end_of_line:warning
|
||||
csharp_style_prefer_null_check_over_type_check = true:warning
|
||||
dotnet_analyzer_diagnostic.severity = warning
|
||||
dotnet_code_quality_unused_parameters = all:warning
|
||||
dotnet_remove_unnecessary_suppression_exclusions = none:warning
|
||||
dotnet_sort_system_directives_first = true
|
||||
dotnet_separate_import_directive_groups = false
|
||||
dotnet_style_namespace_match_folder = true:suggestion
|
||||
dotnet_diagnostic.IDE0130.severity = suggestion
|
||||
dotnet_naming_style.camel_case_style.capitalization = camel_case
|
||||
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
|
||||
dotnet_naming_style.first_upper_style.capitalization = first_word_upper
|
||||
dotnet_naming_style.prefix_interface_with_i_style.capitalization = pascal_case
|
||||
dotnet_naming_style.prefix_interface_with_i_style.required_prefix = I
|
||||
dotnet_naming_style.prefix_type_parameters_with_t_style.capitalization = pascal_case
|
||||
dotnet_naming_style.prefix_type_parameters_with_t_style.required_prefix = T
|
||||
dotnet_naming_style.disallowed_style.capitalization = pascal_case
|
||||
dotnet_naming_style.disallowed_style.required_prefix = ____RULE_VIOLATION____
|
||||
dotnet_naming_style.disallowed_style.required_suffix = ____RULE_VIOLATION____
|
||||
dotnet_naming_style.internal_error_style.capitalization = pascal_case
|
||||
dotnet_naming_style.internal_error_style.required_prefix = ____INTERNAL_ERROR____
|
||||
dotnet_naming_style.internal_error_style.required_suffix = ____INTERNAL_ERROR____
|
||||
dotnet_naming_symbols.public_protected_constant_fields_group.applicable_accessibilities = public, protected, protected_internal
|
||||
dotnet_naming_symbols.public_protected_constant_fields_group.required_modifiers = const
|
||||
dotnet_naming_symbols.public_protected_constant_fields_group.applicable_kinds = field
|
||||
dotnet_naming_rule.public_protected_constant_fields_must_be_pascal_case_rule.symbols = public_protected_constant_fields_group
|
||||
dotnet_naming_rule.public_protected_constant_fields_must_be_pascal_case_rule.style = pascal_case_style
|
||||
dotnet_naming_rule.public_protected_constant_fields_must_be_pascal_case_rule.severity = warning
|
||||
dotnet_naming_symbols.public_protected_static_readonly_fields_group.applicable_accessibilities = public, protected, protected_internal
|
||||
dotnet_naming_symbols.public_protected_static_readonly_fields_group.required_modifiers = static, readonly
|
||||
dotnet_naming_symbols.public_protected_static_readonly_fields_group.applicable_kinds = field
|
||||
dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.symbols = public_protected_static_readonly_fields_group
|
||||
dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.style = pascal_case_style
|
||||
dotnet_naming_rule.public_protected_static_readonly_fields_must_be_pascal_case_rule.severity = warning
|
||||
dotnet_naming_symbols.other_public_protected_fields_group.applicable_accessibilities = public, protected, protected_internal
|
||||
dotnet_naming_symbols.other_public_protected_fields_group.applicable_kinds = field
|
||||
dotnet_naming_rule.other_public_protected_fields_disallowed_rule.symbols = other_public_protected_fields_group
|
||||
dotnet_naming_rule.other_public_protected_fields_disallowed_rule.style = disallowed_style
|
||||
dotnet_naming_rule.other_public_protected_fields_disallowed_rule.severity = error
|
||||
dotnet_naming_symbols.stylecop_constant_fields_group.applicable_accessibilities = public, internal, protected_internal, protected, private_protected, private
|
||||
dotnet_naming_symbols.stylecop_constant_fields_group.required_modifiers = const
|
||||
dotnet_naming_symbols.stylecop_constant_fields_group.applicable_kinds = field
|
||||
dotnet_naming_rule.stylecop_constant_fields_must_be_pascal_case_rule.symbols = stylecop_constant_fields_group
|
||||
dotnet_naming_rule.stylecop_constant_fields_must_be_pascal_case_rule.style = pascal_case_style
|
||||
dotnet_naming_rule.stylecop_constant_fields_must_be_pascal_case_rule.severity = warning
|
||||
dotnet_naming_symbols.stylecop_static_readonly_fields_group.applicable_accessibilities = public, internal, protected_internal, protected, private_protected, private
|
||||
dotnet_naming_symbols.stylecop_static_readonly_fields_group.required_modifiers = static, readonly
|
||||
dotnet_naming_symbols.stylecop_static_readonly_fields_group.applicable_kinds = field
|
||||
dotnet_naming_rule.stylecop_static_readonly_fields_must_be_pascal_case_rule.symbols = stylecop_static_readonly_fields_group
|
||||
dotnet_naming_rule.stylecop_static_readonly_fields_must_be_pascal_case_rule.style = pascal_case_style
|
||||
dotnet_naming_rule.stylecop_static_readonly_fields_must_be_pascal_case_rule.severity = warning
|
||||
dotnet_naming_symbols.stylecop_fields_must_be_private_group.applicable_accessibilities = public, internal, protected_internal, protected, private_protected
|
||||
dotnet_naming_symbols.stylecop_fields_must_be_private_group.applicable_kinds = field
|
||||
dotnet_naming_rule.stylecop_instance_fields_must_be_private_rule.symbols = stylecop_fields_must_be_private_group
|
||||
dotnet_naming_rule.stylecop_instance_fields_must_be_private_rule.style = disallowed_style
|
||||
dotnet_naming_rule.stylecop_instance_fields_must_be_private_rule.severity = error
|
||||
dotnet_naming_symbols.stylecop_private_fields_group.applicable_accessibilities = private
|
||||
dotnet_naming_symbols.stylecop_private_fields_group.applicable_kinds = field
|
||||
dotnet_naming_rule.stylecop_private_fields_must_be_camel_case_rule.symbols = stylecop_private_fields_group
|
||||
dotnet_naming_rule.stylecop_private_fields_must_be_camel_case_rule.style = camel_case_style
|
||||
dotnet_naming_rule.stylecop_private_fields_must_be_camel_case_rule.severity = warning
|
||||
dotnet_naming_symbols.stylecop_local_fields_group.applicable_accessibilities = local
|
||||
dotnet_naming_symbols.stylecop_local_fields_group.applicable_kinds = local
|
||||
dotnet_naming_rule.stylecop_local_fields_must_be_camel_case_rule.symbols = stylecop_local_fields_group
|
||||
dotnet_naming_rule.stylecop_local_fields_must_be_camel_case_rule.style = camel_case_style
|
||||
dotnet_naming_rule.stylecop_local_fields_must_be_camel_case_rule.severity = silent
|
||||
dotnet_naming_symbols.sanity_check_uncovered_field_case_group.applicable_accessibilities = *
|
||||
dotnet_naming_symbols.sanity_check_uncovered_field_case_group.applicable_kinds = field
|
||||
dotnet_naming_rule.sanity_check_uncovered_field_case_rule.symbols = sanity_check_uncovered_field_case_group
|
||||
dotnet_naming_rule.sanity_check_uncovered_field_case_rule.style = internal_error_style
|
||||
dotnet_naming_rule.sanity_check_uncovered_field_case_rule.severity = error
|
||||
dotnet_naming_symbols.element_group.applicable_kinds = namespace, class, enum, struct, delegate, event, method, property
|
||||
dotnet_naming_rule.element_rule.symbols = element_group
|
||||
dotnet_naming_rule.element_rule.style = pascal_case_style
|
||||
dotnet_naming_rule.element_rule.severity = warning
|
||||
dotnet_naming_symbols.interface_group.applicable_kinds = interface
|
||||
dotnet_naming_rule.interface_rule.symbols = interface_group
|
||||
dotnet_naming_rule.interface_rule.style = prefix_interface_with_i_style
|
||||
dotnet_naming_rule.interface_rule.severity = warning
|
||||
dotnet_naming_symbols.type_parameter_group.applicable_kinds = type_parameter
|
||||
dotnet_naming_rule.type_parameter_rule.symbols = type_parameter_group
|
||||
dotnet_naming_rule.type_parameter_rule.style = prefix_type_parameters_with_t_style
|
||||
dotnet_naming_rule.type_parameter_rule.severity = warning
|
||||
dotnet_naming_symbols.parameters_group.applicable_kinds = parameter
|
||||
dotnet_naming_rule.parameters_rule.symbols = parameters_group
|
||||
dotnet_naming_rule.parameters_rule.style = camel_case_style
|
||||
dotnet_naming_rule.parameters_rule.severity = warning
|
||||
csharp_style_var_for_built_in_types = true:warning
|
||||
csharp_style_var_when_type_is_apparent = true:warning
|
||||
csharp_style_var_elsewhere = true:warning
|
||||
csharp_style_expression_bodied_methods = true:warning
|
||||
csharp_style_expression_bodied_constructors = true:warning
|
||||
csharp_style_expression_bodied_operators = true:warning
|
||||
csharp_style_expression_bodied_properties = true:warning
|
||||
csharp_style_expression_bodied_indexers = true:warning
|
||||
csharp_style_expression_bodied_accessors = true:warning
|
||||
csharp_style_expression_bodied_lambdas = true:warning
|
||||
csharp_style_expression_bodied_local_functions = true:warning
|
||||
csharp_style_pattern_matching_over_is_with_cast_check = true:warning
|
||||
csharp_style_pattern_matching_over_as_with_null_check = true:warning
|
||||
csharp_style_prefer_switch_expression = true:warning
|
||||
csharp_style_prefer_pattern_matching = true:warning
|
||||
csharp_style_prefer_not_pattern = true:warning
|
||||
csharp_style_inlined_variable_declaration = true:warning
|
||||
csharp_prefer_simple_default_expression = true:warning
|
||||
csharp_style_pattern_local_over_anonymous_function = true:warning
|
||||
csharp_style_deconstructed_variable_declaration = true:warning
|
||||
csharp_style_prefer_index_operator = true:warning
|
||||
csharp_style_prefer_range_operator = true:warning
|
||||
csharp_style_implicit_object_creation_when_type_is_apparent = true:warning
|
||||
csharp_style_throw_expression = true:warning
|
||||
csharp_style_conditional_delegate_call = true:warning
|
||||
csharp_prefer_braces = true:warning
|
||||
csharp_prefer_simple_using_statement = true:suggestion
|
||||
dotnet_diagnostic.IDE0063.severity = suggestion
|
||||
csharp_using_directive_placement = outside_namespace
|
||||
csharp_prefer_static_local_function = true:warning
|
||||
csharp_style_unused_value_expression_statement_preference = discard_variable:suggestion
|
||||
dotnet_diagnostic.IDE0058.severity = suggestion
|
||||
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
|
||||
dotnet_diagnostic.IDE0059.severity = suggestion
|
||||
csharp_new_line_before_open_brace = all
|
||||
csharp_new_line_before_else = true
|
||||
csharp_new_line_before_catch = true
|
||||
csharp_new_line_before_finally = true
|
||||
csharp_new_line_before_members_in_object_initializers = true
|
||||
csharp_new_line_before_members_in_anonymous_types = true
|
||||
csharp_new_line_between_query_expression_clauses = true
|
||||
csharp_indent_case_contents = true
|
||||
csharp_indent_switch_labels = true
|
||||
csharp_indent_labels = no_change
|
||||
csharp_indent_block_contents = true
|
||||
csharp_indent_braces = false
|
||||
csharp_indent_case_contents_when_block = false
|
||||
csharp_space_after_cast = false
|
||||
csharp_space_after_keywords_in_control_flow_statements = true
|
||||
csharp_space_between_parentheses = false
|
||||
csharp_space_before_colon_in_inheritance_clause = true
|
||||
csharp_space_after_colon_in_inheritance_clause = true
|
||||
csharp_space_around_binary_operators = before_and_after
|
||||
csharp_space_between_method_declaration_parameter_list_parentheses = false
|
||||
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
|
||||
csharp_space_between_method_declaration_name_and_open_parenthesis = false
|
||||
csharp_space_between_method_call_parameter_list_parentheses = false
|
||||
csharp_space_between_method_call_empty_parameter_list_parentheses = false
|
||||
csharp_space_between_method_call_name_and_opening_parenthesis = false
|
||||
csharp_space_after_comma = true
|
||||
csharp_space_before_comma = false
|
||||
csharp_space_after_dot = false
|
||||
csharp_space_before_dot = false
|
||||
csharp_space_after_semicolon_in_for_statement = true
|
||||
csharp_space_before_semicolon_in_for_statement = false
|
||||
csharp_space_around_declaration_statements = false
|
||||
csharp_space_before_open_square_brackets = false
|
||||
csharp_space_between_empty_square_brackets = false
|
||||
csharp_space_between_square_brackets = false
|
||||
csharp_preserve_single_line_statements = false
|
||||
csharp_preserve_single_line_blocks = true
|
||||
csharp_style_namespace_declarations = file_scoped:warning
|
||||
dotnet_diagnostic.CA1707.severity = none
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
|
||||
|
||||
public class AddManyAsyncTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync(documents);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddManyAsync<TestDocument, Guid>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync(documents, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddManyAsync<TestDocument, Guid>(documents, token), Times.Once);
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocumentWithKey<int>, int>(documents);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocumentWithKey<int>, int>(documents, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
|
||||
|
||||
public class AddManyTests : TestMongoRepositoryContext
|
||||
{
|
||||
|
||||
[Fact]
|
||||
public void WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddMany(documents);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddMany<TestDocument, Guid>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddMany(documents, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddMany<TestDocument, Guid>(documents, token), Times.Once);
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocumentWithKey<int>, int>(documents);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocumentWithKey<int>, int>(documents, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
|
||||
|
||||
public class AddOneAsyncTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync(document);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOneAsync<TestDocument, Guid>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync(document, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOneAsync<TestDocument, Guid>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocumentWithKey<int>, int>(document);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocumentWithKey<int>, int>(document, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
|
||||
|
||||
public class AddOneTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddOne(document);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOne<TestDocument, Guid>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddOne(document, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOne<TestDocument, Guid>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocumentWithKey<int>, int>(document);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocumentWithKey<int>, int>(document, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,320 @@
|
||||
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.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests;
|
||||
|
||||
public class DeleteManyAsyncTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocuments_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocument, Guid>(It.IsAny<IEnumerable<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(documents);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocument, Guid>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentsAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
var cancellationToken = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocument, Guid>(It.IsAny<IEnumerable<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(documents, cancellationToken);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocument, Guid>(documents, cancellationToken), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilter_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocument, Guid>(
|
||||
It.IsAny<Expression<Func<TestDocument, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocument, Guid>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocument, Guid>(
|
||||
It.IsAny<Expression<Func<TestDocument, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocument, Guid>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocument, Guid>(
|
||||
It.IsAny<Expression<Func<TestDocument, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocument, Guid>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocument, Guid>(
|
||||
It.IsAny<Expression<Func<TestDocument, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocument, Guid>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocuments_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocumentWithKey<int>, int>(documents);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocumentsAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
var cancellationToken = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocumentWithKey<int>, int>(documents, cancellationToken);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(documents, cancellationToken), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilter_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilterAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilterAndPartitionKey_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
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.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests;
|
||||
|
||||
public class DeleteManyTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocuments_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<IEnumerable<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(documents);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentsAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<IEnumerable<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(documents, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilter_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithDocuments_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(documents);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithDocumentsAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilter_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), null, CancellationToken.None))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilterAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilterAndPartitionKey_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,296 @@
|
||||
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.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests;
|
||||
|
||||
public class DeleteOneAsyncTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocument, Guid>(It.IsAny<TestDocument>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(document);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocument, Guid>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocument, Guid>(It.IsAny<TestDocument>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocument, Guid>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilter_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocument, Guid>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocument, Guid>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocument, Guid>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocument, Guid>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocument_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocumentWithKey<int>, int>(document);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithDocumentAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocumentWithKey<int>, int>(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilter_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilterAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilterAndPartitionKey_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,317 @@
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.DeleteTests;
|
||||
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using FluentAssertions;
|
||||
using Infrastructure;
|
||||
using Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
public class DeleteOneTests : TestMongoRepositoryContext
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocument, Guid>(It.IsAny<TestDocument>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(document);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocument, Guid>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocument, Guid>(It.IsAny<TestDocument>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocument, Guid>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilter_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocument, Guid>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteOne<TestDocument, Guid>(
|
||||
It.IsAny<Expression<Func<TestDocument, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocument, Guid>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteOne<TestDocument, Guid>(
|
||||
It.IsAny<Expression<Func<TestDocument, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocument, Guid>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteOne<TestDocument, Guid>(
|
||||
It.IsAny<Expression<Func<TestDocument, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocument, Guid>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithKeyedDocument_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocumentWithKey<int>, int>(document);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithKeyedDocumentAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocumentWithKey<int>, int>(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilter_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocumentWithKey<int>, int>(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilterAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocumentWithKey<int>, int>(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilterAndPartitionKey_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocumentWithKey<int>, int>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocumentWithKey<int>, int>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class BaseIndexTests : TestMongoRepositoryContext
|
||||
{
|
||||
protected Mock<IAsyncCursor<BsonDocument>> SetupIndex<TDocument>(BsonDocument index, Mock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var asyncCursor = new Mock<IAsyncCursor<BsonDocument>>();
|
||||
asyncCursor
|
||||
.SetupSequence(x => x.MoveNextAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(true)
|
||||
.ReturnsAsync(false);
|
||||
|
||||
asyncCursor
|
||||
.SetupGet(x => x.Current)
|
||||
.Returns(new[] { index });
|
||||
|
||||
var indexManager = new Mock<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(x => x.ListAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(asyncCursor.Object);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return asyncCursor;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateAscendingIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, object>> keyedFieldExpression = t => t.SomeContent2;
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
+295
@@ -0,0 +1,295 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using CancellationToken = System.Threading.CancellationToken;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateCombinedTextIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly List<Expression<Func<TestDocument, object>>> fieldExpressions = new() {t => t.SomeContent2, t => t.SomeContent3};
|
||||
private readonly List<Expression<Func<TestDocumentWithKey<int>, object>>> keyedFieldExpressions = new() {t => t.SomeContent2, t => t.SomeContent3};
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpressions, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpressions, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpressions, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpressions, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpressions, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(fieldExpressions, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpressions, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateDescendingIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, object>> keyedFieldExpression = t => t.SomeContent2;
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateDescendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using CancellationToken = System.Threading.CancellationToken;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateHashedIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, object>> keyedFieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateHashedIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,294 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using CancellationToken = System.Threading.CancellationToken;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateTextIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, object>> keyedFieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocument, Guid>(
|
||||
fieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithKeyedFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, token));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class DropIndexAsyncTests: BaseIndexTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WitIndexName_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument>(indexName);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocument, Guid>(indexName, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WitIndexNameAndCancellationToken_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument>(indexName, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocument, Guid>(indexName, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WitIndexNameAndPartitionKey_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument>(indexName, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocument, Guid>(indexName, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WitIndexNameAndPartitionKeyAndCancellationToken_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument>(indexName, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocument, Guid>(indexName, partitionKey, token));
|
||||
}
|
||||
|
||||
#region Keyed
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithIndexName_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithIndexNameAndCancellationToken_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithIndexNameAndPartitionKey_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithIndexNameAndPartitionKeyAndCancellationToken_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.DropIndexAsync<TestDocumentWithKey<int>, int>(indexName, partitionKey, token));
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.BaseMongoRepositoryTests.IndexTests;
|
||||
|
||||
public class GetIndexNamesAsyncTests : BaseIndexTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithNoParameters_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null, CancellationToken.None), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithCancellationToken_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>(token);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocument, Guid>(null, token), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionKey_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>(partitionKey);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey, CancellationToken.None), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionKeyAndCancellationToken_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocument, Guid>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument>(partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey, token), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithNoParameters_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>();
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(null, CancellationToken.None), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithCancellationToken_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(token);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(null, token), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithPartitionKey_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(partitionKey);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(partitionKey, CancellationToken.None), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Keyed_WithPartitionKeyAndCancellationToken_ReturnsIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
IndexHandler
|
||||
.Setup(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new List<string> { indexName });
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(partitionKey, token);
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Contains(result, x => x == indexName);
|
||||
IndexHandler.Verify(x => x.GetIndexesNamesAsync<TestDocumentWithKey<int>, int>(partitionKey, token), Times.Once());
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,37 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<Nullable>warnings</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<EnableNETAnalyzers>true</EnableNETAnalyzers>
|
||||
<AnalysisLevel>latest</AnalysisLevel>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture" Version="4.18.0" />
|
||||
<PackageReference Include="AutoFixture.AutoMoq" Version="4.18.0" />
|
||||
<PackageReference Include="FluentAssertions" Version="6.11.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
|
||||
<PackageReference Include="Moq" Version="4.18.4" />
|
||||
<PackageReference Include="xunit" Version="2.5.0" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.5.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="coverlet.collector" Version="3.1.0">
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddManyAsyncTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocuments_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentsHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture
|
||||
.Build<TestDocumentWithKey<string>>()
|
||||
.Without(x => x.Id)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocumentWithKey<string>, string>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<TestDocumentWithKey<string>>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
documents.Should().AllSatisfy(d => d.Id.Should().NotBeNull());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocument_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<PartitionedTestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentsAndCancellationToken_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<PartitionedTestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddManyTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocuments_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentsHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture
|
||||
.Build<TestDocumentWithKey<string>>()
|
||||
.Without(x => x.Id)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocumentWithKey<string>, string>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<TestDocumentWithKey<string>>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
documents.Should().AllSatisfy(d => d.Id.Should().NotBeNull());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocument_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<PartitionedTestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentsAndCancellationToken_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<PartitionedTestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddOneAsyncTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocumentWithKey<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocumentWithKey<string>, string>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<TestDocumentWithKey<string>>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
document.Id.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<PartitionedTestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<PartitionedTestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddOneTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocumentWithKey<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocumentWithKey<string>, string>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<TestDocumentWithKey<string>>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
document.Id.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<PartitionedTestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<PartitionedTestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
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 MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
|
||||
|
||||
public class DeleteManyAsyncTests : GenericTestContext<MongoDbEraser>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithEmptyDocuments_DeletesNothing()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<TestDocument>(0);
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(0);
|
||||
|
||||
var idsToDelete = documents.Select(e => e.Id).ToArray();
|
||||
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentsAndCancellationToken_DeletesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var idsToDelete = documents.Select(e => e.Id).ToArray();
|
||||
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionDocumentsAndCancellationToken_DeletesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture
|
||||
.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<PartitionedTestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<PartitionedTestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var idsToDelete = documents.Select(e => e.Id).ToArray();
|
||||
Expression<Func<PartitionedTestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteManyAsync(
|
||||
It.Is<FilterDefinition<PartitionedTestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Once);
|
||||
|
||||
dbContext.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var id = Fixture.Create<Guid>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = d => d.Id == id;
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
collection.Verify(
|
||||
x => x.DeleteManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
token),
|
||||
Times.Once);
|
||||
|
||||
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
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 MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
|
||||
|
||||
public class DeleteManyTests : GenericTestContext<MongoDbEraser>
|
||||
{
|
||||
[Fact]
|
||||
public void WithEmptyDocuments_DeletesNothing()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<TestDocument>(0);
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(0);
|
||||
|
||||
var idsToDelete = documents.Select(e => e.Id).ToArray();
|
||||
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Never);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentsAndCancellationToken_DeletesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteMany(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var idsToDelete = documents.Select(e => e.Id).ToArray();
|
||||
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionDocumentsAndCancellationToken_DeletesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture
|
||||
.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteMany(It.IsAny<FilterDefinition<PartitionedTestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<PartitionedTestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var idsToDelete = documents.Select(e => e.Id).ToArray();
|
||||
Expression<Func<PartitionedTestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteMany(
|
||||
It.Is<FilterDefinition<PartitionedTestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Once);
|
||||
|
||||
dbContext.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var id = Fixture.Create<Guid>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteMany(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = d => d.Id == id;
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
collection.Verify(
|
||||
x => x.DeleteMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
token),
|
||||
Times.Once);
|
||||
|
||||
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
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 MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
|
||||
|
||||
public class DeleteOneAsyncTests : GenericTestContext<MongoDbEraser>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_DeletesOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteOneAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteOneAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = d => d.Id == document.Id;
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
collection.Verify(
|
||||
x => x.DeleteOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
|
||||
|
||||
public class DeleteOneTests : GenericTestContext<MongoDbEraser>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_DeletesOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteOne(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection.Verify(
|
||||
x => x.DeleteOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(x => x.DeleteOne(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(new DeleteResult.Acknowledged(count));
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filter = d => d.Id == document.Id;
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
collection.Verify(
|
||||
x => x.DeleteOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class BaseIndexTests : GenericTestContext<MongoDbIndexHandler>
|
||||
{
|
||||
protected (Mock<IAsyncCursor<BsonDocument>>, Mock<IMongoIndexManager<TDocument>>) SetupIndexes<TDocument>(
|
||||
List<BsonDocument> indexes,
|
||||
Mock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var asyncCursor = MockOf<IAsyncCursor<BsonDocument>>();
|
||||
var moveNextSequence = asyncCursor
|
||||
.SetupSequence(x => x.MoveNextAsync(It.IsAny<CancellationToken>()));
|
||||
|
||||
var currentSequence = asyncCursor
|
||||
.SetupSequence(x => x.Current);
|
||||
|
||||
foreach (var bsonDocument in indexes)
|
||||
{
|
||||
moveNextSequence.ReturnsAsync(true);
|
||||
currentSequence.Returns(new[] {bsonDocument});
|
||||
}
|
||||
|
||||
moveNextSequence.ReturnsAsync(false);
|
||||
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(x => x.ListAsync(It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(asyncCursor.Object);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return (asyncCursor, indexManager);
|
||||
}
|
||||
}
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
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 MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class CreateAscendingIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":1}")),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
SetupContext(collection);
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":1}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":1}") ),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":1}") ),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateAscendingIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":1}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
|
||||
{
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(
|
||||
x => x.CreateOneAsync(
|
||||
It.IsAny<CreateIndexModel<TDocument>>(),
|
||||
It.IsAny<CreateOneIndexOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(indexName);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return indexManager;
|
||||
}
|
||||
}
|
||||
+190
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class CreateCombinedTextIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly ITestOutputHelper testOutputHelper;
|
||||
private readonly List<Expression<Func<TestDocument, object>>> fieldExpressions = new()
|
||||
{
|
||||
t => t.SomeContent2,
|
||||
t => t.GroupingKey
|
||||
};
|
||||
|
||||
public CreateCombinedTextIndexAsyncTests(ITestOutputHelper testOutputHelper)
|
||||
=> this.testOutputHelper = testOutputHelper;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}", testOutputHelper)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
SetupContext(collection);
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, options);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") ),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateCombinedTextIndexAsync<TestDocument, Guid>(fieldExpressions, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\",\"GroupingKey\":\"text\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
|
||||
{
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(
|
||||
x => x.CreateOneAsync(
|
||||
It.IsAny<CreateIndexModel<TDocument>>(),
|
||||
It.IsAny<CreateOneIndexOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(indexName);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return indexManager;
|
||||
}
|
||||
}
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
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 MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class CreateDescendingIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":-1}")),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
SetupContext(collection);
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") ),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateDescendingIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":-1}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
|
||||
{
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(
|
||||
x => x.CreateOneAsync(
|
||||
It.IsAny<CreateIndexModel<TDocument>>(),
|
||||
It.IsAny<CreateOneIndexOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(indexName);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return indexManager;
|
||||
}
|
||||
}
|
||||
+185
@@ -0,0 +1,185 @@
|
||||
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 MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class CreateHashedIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly ITestOutputHelper testOutputHelper;
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
public CreateHashedIndexAsyncTests(ITestOutputHelper testOutputHelper)
|
||||
=> this.testOutputHelper = testOutputHelper;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}", testOutputHelper)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
SetupContext(collection);
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") ),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateHashedIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"hashed\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
|
||||
{
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(
|
||||
x => x.CreateOneAsync(
|
||||
It.IsAny<CreateIndexModel<TDocument>>(),
|
||||
It.IsAny<CreateOneIndexOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(indexName);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return indexManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,180 @@
|
||||
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 MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class CreateTextIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, object>> fieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}")),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
SetupContext(collection);
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, options);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") ),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") ),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var options = Fixture.Create<IndexCreationOptions>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CreateTextIndexAsync<TestDocument, Guid>(fieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(expectedIndexName);
|
||||
indexManger.Verify(
|
||||
x => x.CreateOneAsync(
|
||||
It.Is<CreateIndexModel<TestDocument>>(
|
||||
t => t.Keys.EqualToJson("{\"SomeContent2\":\"text\"}") &&
|
||||
t.Options.EqualTo(options)),
|
||||
null,
|
||||
token),
|
||||
Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
private Mock<IMongoDbContext> SetupContext<TDocument>(Mock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
|
||||
{
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(
|
||||
x => x.CreateOneAsync(
|
||||
It.IsAny<CreateIndexModel<TDocument>>(),
|
||||
It.IsAny<CreateOneIndexOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(indexName);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return indexManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class DropIndexAsyncTests : BaseIndexTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithIndexName_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName);
|
||||
|
||||
// Assert
|
||||
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithIndexNameAndPartitionKey_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName, partitionKey);
|
||||
|
||||
// Assert
|
||||
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, CancellationToken.None), Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithIndexNameAndCancellationToken_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithIndexNameAndPartitionKeyAndCancellationToken_DropsIndex()
|
||||
{
|
||||
// Arrange
|
||||
var expectedIndexName = Fixture.Create<string>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = SetupContext(collection);
|
||||
|
||||
var indexManger = SetupIndexManager(collection, expectedIndexName);
|
||||
|
||||
// Act
|
||||
await Sut.DropIndexAsync<TestDocument, Guid>(expectedIndexName, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
indexManger.Verify(x => x.DropOneAsync(expectedIndexName, token), Times.Once);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
}
|
||||
|
||||
private Mock<IMongoDbContext> SetupContext<TDocument>(IMock<IMongoCollection<TDocument>> collection)
|
||||
{
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
private Mock<IMongoIndexManager<TDocument>> SetupIndexManager<TDocument>(Mock<IMongoCollection<TDocument>> collection, string indexName)
|
||||
{
|
||||
var indexManager = MockOf<IMongoIndexManager<TDocument>>();
|
||||
indexManager
|
||||
.Setup(
|
||||
x => x.CreateOneAsync(
|
||||
It.IsAny<CreateIndexModel<TDocument>>(),
|
||||
It.IsAny<CreateOneIndexOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(indexName);
|
||||
|
||||
collection
|
||||
.SetupGet(x => x.Indexes)
|
||||
.Returns(indexManager.Object);
|
||||
|
||||
return indexManager;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbIndexHandlerTests;
|
||||
|
||||
public class GetIndexNamesAsyncTests : BaseIndexTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithNoParameters_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null));
|
||||
manager.Verify(x => x.ListAsync(CancellationToken.None));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionKey_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey));
|
||||
manager.Verify(x => x.ListAsync(CancellationToken.None));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithCancellationToken_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken();
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>(cancellationToken:token);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null));
|
||||
manager.Verify(x => x.ListAsync(token));
|
||||
cursor.Verify(x => x.MoveNextAsync(token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionKeyCancellationToken_ReturnsAllIndexNames()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken();
|
||||
var indexNames = Fixture.CreateMany<string>().ToList();
|
||||
var indexes = indexNames.Select(x => new BsonDocument {{"name", x}}).ToList();
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
var (cursor, manager) = SetupIndexes(indexes, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetIndexesNamesAsync<TestDocument, Guid>(partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Should().BeEquivalentTo(indexNames);
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey));
|
||||
manager.Verify(x => x.ListAsync(token));
|
||||
cursor.Verify(x => x.MoveNextAsync(token));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
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 AnyAsyncTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilter_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithCondition_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.AnyAsync<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken:token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class AnyTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
|
||||
[Fact]
|
||||
public void WithFilter_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithCondition_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Any<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().BeTrue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class BaseReaderTests : GenericTestContext<MongoDbReader>
|
||||
{
|
||||
protected Mock<IAsyncCursor<TDocument>> SetupSyncCursor<TDocument>(List<TDocument> documents)
|
||||
{
|
||||
var asyncCursor = MockOf<IAsyncCursor<TDocument>>();
|
||||
|
||||
var moveNextSequence = asyncCursor
|
||||
.SetupSequence(x => x.MoveNext(It.IsAny<CancellationToken>()));
|
||||
|
||||
var currentSequence = asyncCursor
|
||||
.SetupSequence(x => x.Current);
|
||||
|
||||
foreach (var projection in documents)
|
||||
{
|
||||
moveNextSequence.Returns(true);
|
||||
currentSequence.Returns(new[] {projection});
|
||||
}
|
||||
|
||||
moveNextSequence.Returns(false);
|
||||
return asyncCursor;
|
||||
}
|
||||
|
||||
protected Mock<IAsyncCursor<TDocument>> SetupAsyncCursor<TDocument>(List<TDocument> documents)
|
||||
{
|
||||
var asyncCursor = MockOf<IAsyncCursor<TDocument>>();
|
||||
|
||||
var moveNextSequence = asyncCursor
|
||||
.SetupSequence(x => x.MoveNextAsync(It.IsAny<CancellationToken>()));
|
||||
|
||||
var currentSequence = asyncCursor
|
||||
.SetupSequence(x => x.Current);
|
||||
|
||||
foreach (var projection in documents)
|
||||
{
|
||||
moveNextSequence.ReturnsAsync(true);
|
||||
currentSequence.Returns(new[] {projection});
|
||||
}
|
||||
|
||||
moveNextSequence.ReturnsAsync(false);
|
||||
return asyncCursor;
|
||||
}
|
||||
|
||||
protected static void SetupFindAsync<TDocument, TProjection>(Mock<IMongoCollection<TDocument>> collection, Mock<IAsyncCursor<TProjection>> asyncCursor) =>
|
||||
collection
|
||||
.Setup(
|
||||
x => x.FindAsync(
|
||||
It.IsAny<FilterDefinition<TDocument>>(),
|
||||
It.IsAny<FindOptions<TDocument, TProjection>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(asyncCursor.Object);
|
||||
|
||||
protected static void SetupFindSync<TDocument, TProjection>(Mock<IMongoCollection<TDocument>> collection, Mock<IAsyncCursor<TProjection>> asyncCursor) =>
|
||||
collection
|
||||
.Setup(
|
||||
x => x.FindSync(
|
||||
It.IsAny<FilterDefinition<TDocument>>(),
|
||||
It.IsAny<FindOptions<TDocument, TProjection>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(asyncCursor.Object);
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
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 CountAsyncTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilter_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), null, token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithCondition_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocumentsAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.CountAsync<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken:token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocumentsAsync(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,281 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class CountTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
|
||||
[Fact]
|
||||
public void WithFilter_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), It.IsAny<CountOptions>(), CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), It.IsAny<CountOptions>(), token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), It.IsAny<CountOptions>(), CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(It.Is<ExpressionFilterDefinition<TestDocument>>(y => y.Expression == filter), It.IsAny<CountOptions>(), token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithCondition_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(null))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKey_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, CancellationToken.None),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKeyAndCancellationToken_ReturnsResult()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.CountDocuments(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<CountOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(documents.Count);
|
||||
|
||||
// Act
|
||||
var result = Sut.Count<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
collection.Verify(
|
||||
x => x.CountDocuments(condition, null, token),
|
||||
Times.Once);
|
||||
result.Should().Be(documents.Count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetAllAsyncTests : BaseReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithFilter_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(false);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(false);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithCondition_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndFindOptions_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, options);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, options, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(false);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetAllAsync<TestDocument, Guid>(condition, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(documents);
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetAllTests : BaseReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public void WithFilter_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupGet(documents, collection);
|
||||
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(false);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(false);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithCondition_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndFindOptions_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(condition, options);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(condition, options, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(false);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetAll<TestDocument, Guid>(condition, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Exactly(documents.Count));
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Exactly(documents.Count + 1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => documents.Contains(x));
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(documents);
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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 GetByIdAsyncTests : BaseReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithId_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithIdAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithIdAndPartitionKey_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithIdAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByIdAsync<TestDocument, Guid>(documents[0].Id, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(documents);
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetByIdTests : BaseReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public void WithId_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithIdAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithIdAndPartitionKey_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithIdAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetById<TestDocument, Guid>(documents[0].Id, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(documents);
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetByMaxAsyncTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, object>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMaxAsync<TestDocument, Guid>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMaxAsync<TestDocument, Guid>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMaxAsync<TestDocument, Guid>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMaxAsync<TestDocument, Guid>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(documents);
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetByMaxTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, object>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMax<TestDocument, Guid>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMax<TestDocument, Guid>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMax<TestDocument, Guid>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMax<TestDocument, Guid>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupSyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(documents);
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetByMinAsyncTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, object>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMinAsync<TestDocument, Guid>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMinAsync<TestDocument, Guid>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMinAsync<TestDocument, Guid>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetByMinAsync<TestDocument, Guid>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(documents);
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetByMinTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, object>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMin<TestDocument, Guid>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelectorAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMin<TestDocument, Guid>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelectorAndPartitionKey_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMin<TestDocument, Guid>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelectorAndPartitionKeyAndCancellationToken_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetByMin<TestDocument, Guid>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupSyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(documents);
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -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<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, int>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var (context, cursor) = SetupAsyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMaxValueAsync<TestDocument, Guid, int>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMaxValueAsync<TestDocument, Guid, int>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMaxValueAsync<TestDocument, Guid, int>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMaxValueAsync<TestDocument, Guid, int>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().Be(value);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TValue>>) SetupAsyncGet<TDocument, TValue>(
|
||||
TValue result,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(new List<TValue> {result});
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -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<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, int>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var (context, cursor) = SetupSyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMaxValue<TestDocument, Guid, int>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMaxValue<TestDocument, Guid, int>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupSyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMaxValue<TestDocument, Guid, int>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMaxValue<TestDocument, Guid, int>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().Be(value);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TValue>>) SetupSyncGet<TDocument, TValue>(
|
||||
TValue result,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(new List<TValue> {result});
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -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<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, int>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var (context, cursor) = SetupAsyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMinValueAsync<TestDocument, Guid, int>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMinValueAsync<TestDocument, Guid, int>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMinValueAsync<TestDocument, Guid, int>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetMinValueAsync<TestDocument, Guid, int>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().Be(value);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TValue>>) SetupAsyncGet<TDocument, TValue>(
|
||||
TValue result,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(new List<TValue> {result});
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -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<Func<TestDocument, bool>> filter = x => x.SomeContent == "SomeContent";
|
||||
private readonly Expression<Func<TestDocument, int>> selector = x => x.SomeValue;
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndSelector_GetsMatchingDocument()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var (context, cursor) = SetupSyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMinValue<TestDocument, Guid, int>(filter, selector);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(value, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMinValue<TestDocument, Guid, int>(filter, selector, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupSyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMinValue<TestDocument, Guid, int>(filter, selector, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(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<int>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(value, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetMinValue<TestDocument, Guid, int>(filter, selector, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().Be(value);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TValue>>) SetupSyncGet<TDocument, TValue>(
|
||||
TValue result,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(new List<TValue> {result});
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,283 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetOneAsyncTests : BaseReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithFilter_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithCondition_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndFindOptions_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, options);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, options, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.GetOneAsync<TestDocument, Guid>(condition, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupAsyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(documents);
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,282 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class GetOneTests : BaseReaderTests
|
||||
{
|
||||
[Fact]
|
||||
public void WithFilter_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(filter);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(filter, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filter = x => x.Id == documents[0].Id;
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithCondition_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(condition);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(condition, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKey_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(condition, partitionKey: partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(condition, partitionKey: partitionKey, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndFindOptions_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(condition, options);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndFindOptionsAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupSyncGet(documents, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(condition, options, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithConditionAndFindOptionsAndPartitionKeyAndCancellationToken_GetsMatchingDocuments()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var options = Fixture
|
||||
.Build<FindOptions>()
|
||||
.Without(x => x.Comment)
|
||||
.Without(x => x.Hint)
|
||||
.Create();
|
||||
var condition = Builders<TestDocument>.Filter.Eq("Id", documents[0].Id);
|
||||
var token = new CancellationToken(true);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupSyncGet(documents, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.GetOne<TestDocument, Guid>(condition, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(documents[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TDocument>>) SetupSyncGet<TDocument>(
|
||||
List<TDocument> documents,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(documents);
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class ProjectManyAsyncTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
|
||||
|
||||
private readonly Expression<Func<TestDocument, TestProjection>>
|
||||
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjection_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.AtLeast(1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjectionAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.AtLeast(1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjectionAndPartitionKey_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.AtLeast(1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectManyAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.AtLeast(1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupAsyncProjection<TDocument, TProjection>(
|
||||
List<TProjection> projections,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(projections);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.FindAsync(
|
||||
It.IsAny<FilterDefinition<TDocument>>(),
|
||||
It.IsAny<FindOptions<TDocument, TProjection>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(asyncCursor.Object);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class ProjectManyTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
|
||||
|
||||
private readonly Expression<Func<TestDocument, TestProjection>>
|
||||
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjection_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.AtLeast(1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjectionAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.AtLeast(1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjectionAndPartitionKey_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.AtLeast(1));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(false);
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectMany<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.AtLeast(projections.Count));
|
||||
result.Should().NotBeNull();
|
||||
result.Should().OnlyContain(x => projections.Contains(x));
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupAsyncProjection<TDocument, TProjection>(
|
||||
List<TProjection> projections,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(projections);
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class ProjectOneAsyncTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
|
||||
|
||||
private readonly Expression<Func<TestDocument, TestProjection>>
|
||||
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjection_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjectionAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjectionAndPartitionKey_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupAsyncProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = await Sut.ProjectOneAsync<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNextAsync(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupAsyncProjection<TDocument, TProjection>(
|
||||
List<TProjection> projections,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupAsyncCursor(projections);
|
||||
|
||||
SetupFindAsync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
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.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbReaderTests;
|
||||
|
||||
public class ProjectOneTests : BaseReaderTests
|
||||
{
|
||||
private readonly Expression<Func<TestDocument, bool>> filter = t => string.IsNullOrWhiteSpace(t.SomeContent2);
|
||||
|
||||
private readonly Expression<Func<TestDocument, TestProjection>>
|
||||
projectionExpression = t => new TestProjection {TestDocumentId = t.Id, NestedData = t.Nested.SomeDate};
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjection_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var (context, cursor) = SetupProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjectionAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupProjection(projections, collection);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression, cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(null), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjectionAndPartitionKey_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var (context, cursor) = SetupProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(CancellationToken.None), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndProjectionAndPartitionKeyAndCancellationToken_Projects()
|
||||
{
|
||||
// Arrange
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var projections = Fixture.CreateMany<TestProjection>().ToList();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var (context, cursor) = SetupProjection(projections, collection, partitionKey);
|
||||
|
||||
// Act
|
||||
var result = Sut.ProjectOne<TestDocument, TestProjection, Guid>(filter, projectionExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
context.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
|
||||
cursor.Verify(x => x.Current, Times.Once);
|
||||
cursor.Verify(x => x.MoveNext(token), Times.Once);
|
||||
result.Should().NotBeNull();
|
||||
result.Should().Be(projections[0]);
|
||||
}
|
||||
|
||||
private (Mock<IMongoDbContext>, Mock<IAsyncCursor<TProjection>>) SetupProjection<TDocument, TProjection>(
|
||||
List<TProjection> projections,
|
||||
Mock<IMongoCollection<TDocument>> collection,
|
||||
string partitionKey = null)
|
||||
{
|
||||
var asyncCursor = SetupSyncCursor(projections);
|
||||
|
||||
SetupFindSync(collection, asyncCursor);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
|
||||
return (context, asyncCursor);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,582 @@
|
||||
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 MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
|
||||
|
||||
public class UpdateManyAsyncTests : GenericTestContext<MongoDbUpdater>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithFilterAndUpdateDefinition_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndUpdateDefinitionAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndUpdateDefinitionAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndFieldExpressionAndValue_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValue_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinition_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinitionAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var count = Fixture.Create<long>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateManyAsync<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateManyAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
var replaceResult = new ReplaceOneResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
var updateResult = new UpdateResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(updateResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateManyAsync(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(updateResult);
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,581 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
|
||||
|
||||
public class UpdateManyTests : GenericTestContext<MongoDbUpdater>
|
||||
{
|
||||
[Fact]
|
||||
public void WithFilterAndUpdateDefinition_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndUpdateDefinitionAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndUpdateDefinitionAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filter,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(updateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndFieldExpressionAndValue_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValue_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinition_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinitionAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var value = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var collection = SetupCollection(count);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinitionAndPartitionKey_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var count = Fixture.Create<long>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndUpdateDefinitionAndPartitionKeyAndCancellationToken_UpdatesMany()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(count, partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateMany<TestDocument, Guid>(
|
||||
filterExpression,
|
||||
updateDefinition,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateMany(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
private Mock<IMongoCollection<TestDocument>> SetupCollection(long count, string partitionKey = null)
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
var replaceResult = new ReplaceOneResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
var updateResult = new UpdateResult.Acknowledged(count, count, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateMany(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(updateResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateMany(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(updateResult);
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,806 @@
|
||||
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 MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
|
||||
|
||||
public class UpdateOneAsyncTests : GenericTestContext<MongoDbUpdater>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndUpdateDefinition_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document, updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(document, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndFieldExpressionAndValue_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(document, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(document, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndDocument_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndDocumentAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndDocumentAndUpdateDefinition_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document, updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid>(session.Object, document, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndDocumentAndFieldExpressionAndValue_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(session.Object, document, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(session.Object, document, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = await Sut.UpdateOneAsync<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOneAsync(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
private Mock<IMongoCollection<TestDocument>> SetupCollection(string partitionKey = null)
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
var count = Fixture.Create<long>();
|
||||
var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
var updateResult = new UpdateResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(updateResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOneAsync(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateOneAsync(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(updateResult);
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,805 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbUpdaterTests;
|
||||
|
||||
public class UpdateOneTests : GenericTestContext<MongoDbUpdater>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndUpdateDefinition_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(document, updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(document, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndFieldExpressionAndValue_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(document, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(document, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndDocument_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndDocumentAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var token = new CancellationToken(true);
|
||||
var collection = SetupCollection();
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
ReplaceOptions expectedOptions = null;
|
||||
collection
|
||||
.Verify(
|
||||
x => x.ReplaceOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
document,
|
||||
expectedOptions,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndDocumentAndUpdateDefinition_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document, updateDefinition);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndDocumentAndUpdateDefinitionAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var collection = SetupCollection();
|
||||
var updateDefinition = Builders<TestDocument>.Update.Set(x => x.Id, document.Id);
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid>(session.Object, document, updateDefinition, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
updateDefinition,
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndDocumentAndFieldExpressionAndValue_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(session.Object, document, fieldExpression, value);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndDocumentAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var document = Fixture.Create<TestDocument>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(session.Object, document, fieldExpression, value, token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndFilterAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndFilterAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
var filter = Builders<TestDocument>.Filter.Eq(x => x.SomeContent, "SomeContent");
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filter,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var collection = SetupCollection();
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
cancellationToken: token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKey_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
CancellationToken.None),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithClientSessionAndFilterExpressionAndFieldExpressionAndValueAndPartitionKeyAndCancellationToken_ReplacesOne()
|
||||
{
|
||||
// Arrange
|
||||
var session = MockOf<IClientSessionHandle>();
|
||||
var value = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var collection = SetupCollection(partitionKey);
|
||||
var token = new CancellationToken(true);
|
||||
Expression<Func<TestDocument, bool>> filterExpression = testDocument => testDocument.SomeContent == "SomeContent";
|
||||
Expression<Func<TestDocument, string>> fieldExpression = testDocument => testDocument.SomeContent;
|
||||
|
||||
// Act
|
||||
var result = Sut.UpdateOne<TestDocument, Guid, string>(
|
||||
session.Object,
|
||||
filterExpression,
|
||||
fieldExpression,
|
||||
value,
|
||||
partitionKey,
|
||||
token);
|
||||
|
||||
// Assert
|
||||
result.Should().BeTrue();
|
||||
|
||||
var expectedUpdateDefinition = Builders<TestDocument>.Update.Set(x => x.SomeContent, value);
|
||||
var expectedFilter = Builders<TestDocument>.Filter.Where(filterExpression);
|
||||
collection
|
||||
.Verify(
|
||||
x => x.UpdateOne(
|
||||
session.Object,
|
||||
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
|
||||
It.Is<UpdateDefinition<TestDocument>>(u => u.EquivalentTo(expectedUpdateDefinition)),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
private Mock<IMongoCollection<TestDocument>> SetupCollection(string partitionKey = null)
|
||||
{
|
||||
var replacedId = Fixture.Create<Guid>();
|
||||
var count = Fixture.Create<long>();
|
||||
var replaceResult = new ReplaceOneResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
var updateResult = new UpdateResult.Acknowledged(count, 1, new BsonBinaryData(replacedId, GuidRepresentation.Standard));
|
||||
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOne(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateOne(
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(updateResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.ReplaceOne(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<TestDocument>(),
|
||||
It.IsAny<ReplaceOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(replaceResult);
|
||||
|
||||
collection
|
||||
.Setup(
|
||||
x => x.UpdateOne(
|
||||
It.IsAny<IClientSessionHandle>(),
|
||||
It.IsAny<FilterDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateDefinition<TestDocument>>(),
|
||||
It.IsAny<UpdateOptions>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(updateResult);
|
||||
|
||||
var dbContext = MockOf<IMongoDbContext>();
|
||||
dbContext
|
||||
.Setup(x => x.GetCollection<TestDocument>(partitionKey))
|
||||
.Returns(collection.Object);
|
||||
return collection;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Driver;
|
||||
|
||||
public static class FilterDefinitionExtensions
|
||||
{
|
||||
public static string RenderToJson<TDocument>(this FilterDefinition<TDocument> filter)
|
||||
=> filter.Render(BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(), BsonSerializer.SerializerRegistry).ToJson();
|
||||
|
||||
public static bool EquivalentTo<TDocument>(this FilterDefinition<TDocument> filter, FilterDefinition<TDocument> other)
|
||||
=> filter.RenderToJson() == other.RenderToJson();
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoMoq;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class GenericTestContext<TSut>
|
||||
{
|
||||
public GenericTestContext() => Fixture = new Fixture().Customize(new AutoMoqCustomization());
|
||||
|
||||
protected Mock<T> MockOf<T>()
|
||||
where T : class =>
|
||||
Fixture.Freeze<Mock<T>>();
|
||||
|
||||
protected IFixture Fixture { get; set; }
|
||||
|
||||
protected TSut Sut => Fixture.Create<TSut>();
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public static class IndexExtensions
|
||||
{
|
||||
public static bool EqualToJson<TDocument>(this IndexKeysDefinition<TDocument> keys, string json, ITestOutputHelper output)
|
||||
{
|
||||
var indexModelRendered = RenderIndexModelKeys(keys);
|
||||
var result = indexModelRendered.Equals(json, StringComparison.Ordinal);
|
||||
if (!result && output != null)
|
||||
{
|
||||
output.WriteLine($"Expected: {json}");
|
||||
output.WriteLine($"Actual: {indexModelRendered}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool EqualToJson<TDocument>(this IndexKeysDefinition<TDocument> keys, string json)
|
||||
{
|
||||
var indexModelRendered = RenderIndexModelKeys(keys);
|
||||
return indexModelRendered.Equals(json, StringComparison.Ordinal);
|
||||
}
|
||||
|
||||
public static bool EqualTo(this IndexCreationOptions x, CreateIndexOptions y) =>
|
||||
x.Unique == y.Unique &&
|
||||
x.TextIndexVersion == y.TextIndexVersion &&
|
||||
x.SphereIndexVersion == y.SphereIndexVersion &&
|
||||
x.Sparse == y.Sparse &&
|
||||
x.Name == y.Name &&
|
||||
x.Min == y.Min &&
|
||||
x.Max == y.Max &&
|
||||
x.LanguageOverride == y.LanguageOverride &&
|
||||
x.ExpireAfter == y.ExpireAfter &&
|
||||
x.DefaultLanguage == y.DefaultLanguage &&
|
||||
x.Bits == y.Bits &&
|
||||
x.Background == y.Background &&
|
||||
x.Version == y.Version;
|
||||
|
||||
public static bool EqualTo(this CreateIndexOptions x, IndexCreationOptions y) =>
|
||||
x.Unique == y.Unique &&
|
||||
x.TextIndexVersion == y.TextIndexVersion &&
|
||||
x.SphereIndexVersion == y.SphereIndexVersion &&
|
||||
x.Sparse == y.Sparse &&
|
||||
x.Name == y.Name &&
|
||||
x.Min == y.Min &&
|
||||
x.Max == y.Max &&
|
||||
x.LanguageOverride == y.LanguageOverride &&
|
||||
x.ExpireAfter == y.ExpireAfter &&
|
||||
x.DefaultLanguage == y.DefaultLanguage &&
|
||||
x.Bits == y.Bits &&
|
||||
x.Background == y.Background &&
|
||||
x.Version == y.Version;
|
||||
|
||||
private static string RenderIndexModelKeys<TDocument>(IndexKeysDefinition<TDocument> keys)
|
||||
{
|
||||
var indexModelRendered = keys.Render(
|
||||
BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(),
|
||||
BsonSerializer.SerializerRegistry);
|
||||
|
||||
var result = indexModelRendered.ToString();
|
||||
return result.Replace(" ", "");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
namespace CoreUnitTests.Infrastructure.Model;
|
||||
|
||||
public class Child
|
||||
{
|
||||
public Child(string type, string value)
|
||||
{
|
||||
Type = type;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public string Type { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure.Model;
|
||||
|
||||
public class Nested
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
|
||||
[BsonRepresentation(BsonType.Decimal128)]
|
||||
public decimal SomeAmount { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure.Model;
|
||||
|
||||
public class PartitionedTestDocument : TestDocument, IPartitionedDocument
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public string PartitionKey { get; set; } = "PartitionedTestDocument";
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure.Model;
|
||||
|
||||
public class TestDocument : Document
|
||||
{
|
||||
public TestDocument()
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested {SomeDate = DateTime.UtcNow};
|
||||
Children = new List<Child>();
|
||||
}
|
||||
|
||||
public int SomeValue { get; set; }
|
||||
|
||||
public decimal SomeDecimalValue { get; set; }
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public string SomeContent2 { get; set; }
|
||||
|
||||
public string SomeContent3 { get; set; }
|
||||
|
||||
public int GroupingKey { get; set; }
|
||||
|
||||
public Guid OtherGroupingKey { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure.Model;
|
||||
|
||||
public class TestDocumentWithKey<TKey> : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
public TKey Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
|
||||
public TestDocumentWithKey()
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
Children = new List<Child>();
|
||||
}
|
||||
|
||||
public int SomeValue { get; set; }
|
||||
|
||||
public int SomeDecimalValue { get; set; }
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
public string SomeContent2 { get; set; }
|
||||
public string SomeContent3 { get; set; }
|
||||
|
||||
public int GroupingKey { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure.Model;
|
||||
|
||||
public class TestProjection
|
||||
{
|
||||
public Guid TestDocumentId { get; set; }
|
||||
|
||||
public DateTime NestedData { get; set; }
|
||||
|
||||
public int Count { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestKeyedMongoRepository<TKey> : BaseMongoRepository<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
public TestKeyedMongoRepository(IMongoDatabase mongoDatabase)
|
||||
: base(mongoDatabase)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetIndexHandler(IMongoDbIndexHandler indexHandler) => MongoDbIndexHandler = indexHandler;
|
||||
|
||||
public void SetDbCreator(IMongoDbCreator creator) => MongoDbCreator = creator;
|
||||
|
||||
public void SetReader(IMongoDbReader reader) => MongoDbReader = reader;
|
||||
|
||||
public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser;
|
||||
|
||||
public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater;
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using System;
|
||||
using AutoFixture;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestKeyedMongoRepositoryContext<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
private readonly Mock<IMongoDatabase> mongoDatabase;
|
||||
|
||||
private TestKeyedMongoRepository<TKey> sut;
|
||||
|
||||
protected TestKeyedMongoRepositoryContext()
|
||||
{
|
||||
mongoDatabase = new Mock<IMongoDatabase>();
|
||||
Fixture = new Fixture();
|
||||
}
|
||||
|
||||
protected Fixture Fixture { get; set; }
|
||||
|
||||
protected TestKeyedMongoRepository<TKey> Sut
|
||||
{
|
||||
get
|
||||
{
|
||||
if (sut != null)
|
||||
{
|
||||
return sut;
|
||||
}
|
||||
|
||||
sut = new TestKeyedMongoRepository<TKey>(mongoDatabase.Object);
|
||||
if (IndexHandler != null)
|
||||
{
|
||||
sut.SetIndexHandler(IndexHandler.Object);
|
||||
}
|
||||
|
||||
if (Creator != null)
|
||||
{
|
||||
sut.SetDbCreator(Creator.Object);
|
||||
}
|
||||
|
||||
if (Reader != null)
|
||||
{
|
||||
sut.SetReader(Reader.Object);
|
||||
}
|
||||
|
||||
if (Eraser != null)
|
||||
{
|
||||
sut.SetEraser(Eraser.Object);
|
||||
}
|
||||
|
||||
if (Updater != null)
|
||||
{
|
||||
sut.SetUpdater(Updater.Object);
|
||||
}
|
||||
|
||||
return sut;
|
||||
}
|
||||
}
|
||||
|
||||
protected Mock<IMongoDbIndexHandler> IndexHandler { get; set; }
|
||||
|
||||
protected Mock<IMongoDbCreator> Creator { get; set; }
|
||||
|
||||
protected Mock<IMongoDbReader> Reader { get; set; }
|
||||
|
||||
protected Mock<IMongoDbEraser> Eraser { get; set; }
|
||||
|
||||
protected Mock<IMongoDbUpdater> Updater { get; set; }
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using System;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestKeyedReadOnlyMongoRepository<TKey> : ReadOnlyMongoRepository<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public TestKeyedReadOnlyMongoRepository(string connectionString, string databaseName = null)
|
||||
: base(connectionString, databaseName)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TestKeyedReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
||||
: base(mongoDatabase)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TestKeyedReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
||||
: base(mongoDbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetReader(IMongoDbReader reader) => MongoDbReader = reader;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using AutoFixture;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestKeyedReadOnlyMongoRepositoryContext<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
private readonly Mock<IMongoDatabase> mongoDatabase;
|
||||
|
||||
private TestKeyedReadOnlyMongoRepository<TKey> sut;
|
||||
|
||||
protected TestKeyedReadOnlyMongoRepositoryContext()
|
||||
{
|
||||
mongoDatabase = new Mock<IMongoDatabase>();
|
||||
Fixture = new Fixture();
|
||||
}
|
||||
|
||||
protected Fixture Fixture { get; set; }
|
||||
|
||||
protected TestKeyedReadOnlyMongoRepository<TKey> Sut
|
||||
{
|
||||
get
|
||||
{
|
||||
if (sut != null)
|
||||
{
|
||||
return sut;
|
||||
}
|
||||
|
||||
sut = new TestKeyedReadOnlyMongoRepository<TKey>(mongoDatabase.Object);
|
||||
|
||||
if (Reader != null)
|
||||
{
|
||||
sut.SetReader(Reader.Object);
|
||||
}
|
||||
|
||||
return sut;
|
||||
}
|
||||
}
|
||||
|
||||
protected Mock<IMongoDbReader> Reader { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestMongoRepository : BaseMongoRepository
|
||||
{
|
||||
public TestMongoRepository(IMongoDatabase mongoDatabase)
|
||||
: base(mongoDatabase)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetIndexHandler(IMongoDbIndexHandler indexHandler) => MongoDbIndexHandler = indexHandler;
|
||||
|
||||
public void SetDbCreator(IMongoDbCreator creator) => MongoDbCreator = creator;
|
||||
|
||||
public void SetReader(IMongoDbReader reader) => MongoDbReader = reader;
|
||||
|
||||
public void SetEraser(IMongoDbEraser eraser) => MongoDbEraser = eraser;
|
||||
|
||||
public void SetUpdater(IMongoDbUpdater updater) => MongoDbUpdater = updater;
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
using AutoFixture;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using MongoDbGenericRepository.DataAccess.Update;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestMongoRepositoryContext
|
||||
{
|
||||
private readonly Mock<IMongoDatabase> _mongoDatabase;
|
||||
|
||||
private TestMongoRepository _sut;
|
||||
|
||||
protected TestMongoRepositoryContext()
|
||||
{
|
||||
_mongoDatabase = new Mock<IMongoDatabase>();
|
||||
Fixture = new Fixture();
|
||||
}
|
||||
|
||||
public Fixture Fixture { get; set; }
|
||||
|
||||
protected TestMongoRepository Sut
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_sut == null)
|
||||
{
|
||||
_sut = new TestMongoRepository(_mongoDatabase.Object);
|
||||
if (IndexHandler != null)
|
||||
{
|
||||
_sut.SetIndexHandler(IndexHandler.Object);
|
||||
}
|
||||
|
||||
if (Creator != null)
|
||||
{
|
||||
_sut.SetDbCreator(Creator.Object);
|
||||
}
|
||||
|
||||
if (Reader != null)
|
||||
{
|
||||
_sut.SetReader(Reader.Object);
|
||||
}
|
||||
|
||||
if (Eraser != null)
|
||||
{
|
||||
_sut.SetEraser(Eraser.Object);
|
||||
}
|
||||
|
||||
if (Updater != null)
|
||||
{
|
||||
_sut.SetUpdater(Updater.Object);
|
||||
}
|
||||
}
|
||||
|
||||
return _sut;
|
||||
}
|
||||
}
|
||||
|
||||
protected Mock<IMongoDbIndexHandler> IndexHandler { get; set; }
|
||||
|
||||
protected Mock<IMongoDbCreator> Creator { get; set; }
|
||||
|
||||
protected Mock<IMongoDbReader> Reader { get; set; }
|
||||
|
||||
protected Mock<IMongoDbEraser> Eraser { get; set; }
|
||||
|
||||
protected Mock<IMongoDbUpdater> Updater { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestReadOnlyMongoRepository : ReadOnlyMongoRepository
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public TestReadOnlyMongoRepository(string connectionString, string databaseName = null)
|
||||
: base(connectionString, databaseName)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TestReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
||||
: base(mongoDatabase)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public TestReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
||||
: base(mongoDbContext)
|
||||
{
|
||||
}
|
||||
|
||||
public void SetReader(IMongoDbReader reader) => MongoDbReader = reader;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoMoq;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.DataAccess.Read;
|
||||
using Moq;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public class TestReadOnlyMongoRepositoryContext
|
||||
{
|
||||
private readonly Mock<IMongoDatabase> mongoDatabase;
|
||||
|
||||
private TestReadOnlyMongoRepository sut;
|
||||
|
||||
protected TestReadOnlyMongoRepositoryContext()
|
||||
{
|
||||
mongoDatabase = new Mock<IMongoDatabase>();
|
||||
Fixture = new Fixture().Customize(new AutoMoqCustomization());
|
||||
}
|
||||
|
||||
protected IFixture Fixture { get; set; }
|
||||
|
||||
protected TestReadOnlyMongoRepository Sut
|
||||
{
|
||||
get
|
||||
{
|
||||
if (sut != null)
|
||||
{
|
||||
return sut;
|
||||
}
|
||||
|
||||
sut = Fixture.Create<TestReadOnlyMongoRepository>();
|
||||
|
||||
if (Reader != null)
|
||||
{
|
||||
sut.SetReader(Reader.Object);
|
||||
}
|
||||
|
||||
return sut;
|
||||
}
|
||||
}
|
||||
|
||||
protected Mock<IMongoDbReader> Reader { get; set; }
|
||||
|
||||
protected Mock<T> MockOf<T>()
|
||||
where T : class =>
|
||||
Fixture.Freeze<Mock<T>>();
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace CoreUnitTests.Infrastructure;
|
||||
|
||||
public static class UpdateDefinitionExtensions
|
||||
{
|
||||
public static bool EquivalentTo<TDocument>(this UpdateDefinition<TDocument> update, UpdateDefinition<TDocument> expected)
|
||||
{
|
||||
var renderedUpdate = update.Render(
|
||||
BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(),
|
||||
BsonSerializer.SerializerRegistry);
|
||||
|
||||
var renderedExpected = expected.Render(
|
||||
BsonSerializer.SerializerRegistry.GetSerializer<TDocument>(),
|
||||
BsonSerializer.SerializerRegistry);
|
||||
|
||||
return renderedUpdate.Equals(renderedExpected);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
|
||||
|
||||
public class AddManyAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync(documents);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync(documents, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
|
||||
|
||||
public class AddManyTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddMany(documents);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddMany(documents, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
|
||||
|
||||
public class AddOneAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync(document);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync(document, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
|
||||
|
||||
public class AddOneTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddOne(document);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ShouldAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var token = new CancellationToken(true);
|
||||
Creator = new Mock<IMongoDbCreator>();
|
||||
|
||||
// Act
|
||||
Sut.AddOne(document, token);
|
||||
|
||||
// Assert
|
||||
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
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.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests;
|
||||
|
||||
public class DeleteManyAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocuments_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(documents);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentsAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
var cancellationToken = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(documents, cancellationToken);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(documents, cancellationToken), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilter_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteManyAsync(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteManyAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
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.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests;
|
||||
|
||||
public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocuments_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), CancellationToken.None))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(documents);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentsAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(documents, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilter_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), null, CancellationToken.None))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(
|
||||
x => x.DeleteMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
CancellationToken.None))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = Fixture.Create<string>();
|
||||
var count = Fixture.Create<long>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteMany(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
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.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests;
|
||||
|
||||
public class DeleteOneAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(document);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<TestDocumentWithKey<int>>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilter_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKey_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
|
||||
.ReturnsAsync(count);
|
||||
|
||||
// Act
|
||||
var result = await Sut.DeleteOneAsync(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOneAsync<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDbGenericRepository.DataAccess.Delete;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.DeleteTests;
|
||||
|
||||
public class DeleteOneTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(document);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = Fixture.Create<TestDocumentWithKey<int>>();
|
||||
var count = Fixture.Create<long>();
|
||||
var token = new CancellationToken(true);
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<TestDocumentWithKey<int>>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(document, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(document, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilter_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKey_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter, partitionKey);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var count = Fixture.Create<long>();
|
||||
var content = Fixture.Create<string>();
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
|
||||
|
||||
Eraser = new Mock<IMongoDbEraser>();
|
||||
|
||||
Eraser
|
||||
.Setup(x => x.DeleteOne<TestDocumentWithKey<int>, int>(
|
||||
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
|
||||
It.IsAny<string>(),
|
||||
It.IsAny<CancellationToken>()))
|
||||
.Returns(count);
|
||||
|
||||
// Act
|
||||
var result = Sut.DeleteOne(filter, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
result.Should().Be(count);
|
||||
Eraser.Verify(x => x.DeleteOne<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
using System;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateAscendingIndexAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
private readonly Expression<Func<TestDocumentWithKey<int>, object>> keyedFieldExpression = t => t.SomeContent2;
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpression, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateAscendingIndexAsync(keyedFieldExpression, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateAscendingIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpression, options, partitionKey, token));
|
||||
}
|
||||
}
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq.Expressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using MongoDbGenericRepository.DataAccess.Index;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.KeyTypedRepositoryTests.IndexTests;
|
||||
|
||||
public class CreateCombinedTextIndexAsyncTests : TestKeyedMongoRepositoryContext<int>
|
||||
{
|
||||
private readonly List<Expression<Func<TestDocumentWithKey<int>, object>>> keyedFieldExpressions = new() {t => t.SomeContent2, t => t.SomeContent3};
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpression_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, null, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(keyedFieldExpressions, null, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptions_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, null, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, null, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, null, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, null, partitionKey, token));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKey_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options, partitionKey);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, partitionKey, CancellationToken.None));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithFieldExpressionAndOptionsAndPartitionKeyAndCancellationToken_CreatesIndex()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var token = new CancellationToken(true);
|
||||
var indexName = Fixture.Create<string>();
|
||||
var options = new IndexCreationOptions { Name = indexName };
|
||||
|
||||
IndexHandler = new Mock<IMongoDbIndexHandler>();
|
||||
|
||||
// Act
|
||||
await Sut.CreateCombinedTextIndexAsync(keyedFieldExpressions, options, partitionKey, token);
|
||||
|
||||
// Assert
|
||||
IndexHandler.Verify(
|
||||
x => x.CreateCombinedTextIndexAsync<TestDocumentWithKey<int>, int>(
|
||||
keyedFieldExpressions, options, partitionKey, token));
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user