Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 345fca2e6e | |||
| 57d332ef2a | |||
| 2f6515a809 | |||
| 82dc2f2024 | |||
| bc992e8c61 | |||
| 47e85f1760 | |||
| 6f5eb71dad | |||
| ed0d206a97 | |||
| 40ff874cf6 | |||
| 9fc74fc28e | |||
| 5270271008 | |||
| bf2119432e | |||
| e581586161 | |||
| 1a83abd25f | |||
| 03b0e4265b | |||
| c117bf2a7f | |||
| c50e4d086e | |||
| 3a42a1c574 | |||
| 67e7318021 | |||
| 29012d1e52 | |||
| e15c1e4ad2 | |||
| 3d104764ae | |||
| 3af68c06db | |||
| a3ae98d077 | |||
| aff276f9e3 | |||
| e91c89f054 | |||
| 657beeac99 | |||
| fbb07475a1 | |||
| 30ea910b9c | |||
| aae71cddc3 | |||
| ad2cd66a7d |
@@ -0,0 +1,22 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
public class CoreObjectIdTestDocument : TestDoc<ObjectId>
|
||||
{
|
||||
}
|
||||
|
||||
public class CRUDObjectIdTests : MongoDbTKeyDocumentTestBase<CoreObjectIdTestDocument, ObjectId>
|
||||
{
|
||||
public CRUDObjectIdTests(MongoDbTestFixture<CoreObjectIdTestDocument, ObjectId> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDObjectIdTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
[CollectionName("CoreTestingCollectionNameAttributePartitionedTKey")]
|
||||
public class CorePartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
|
||||
{
|
||||
public CorePartitionedCollectionNameDoc()
|
||||
{
|
||||
PartitionKey = "CoreTestPartitionKeyCollectionName";
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
public class CRUDPartitionedCollectionNameAttributeTests : MongoDbDocumentTestBase<CorePartitionedCollectionNameDoc>
|
||||
{
|
||||
public CRUDPartitionedCollectionNameAttributeTests(MongoDbTestFixture<CorePartitionedCollectionNameDoc, Guid> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CoreCRUDPartitionedCollectionNameAttributeTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
public class CorePartitionedDoc : TestDoc, IPartitionedDocument
|
||||
{
|
||||
public CorePartitionedDoc()
|
||||
{
|
||||
PartitionKey = "CoreTestPartitionKey";
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
public class CRUDPartitionedTests : MongoDbDocumentTestBase<CorePartitionedDoc>
|
||||
{
|
||||
public CRUDPartitionedTests(MongoDbTestFixture<CorePartitionedDoc, Guid> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CoreCRUDPartitionedTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
[CollectionName("TestingCollectionNameAttributePartitionedTKey")]
|
||||
public class CoreTKeyPartitionedCollectionNameDoc : TestDoc<Guid>, IPartitionedDocument
|
||||
{
|
||||
public CoreTKeyPartitionedCollectionNameDoc()
|
||||
{
|
||||
PartitionKey = "CoreTestPartitionKey";
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
public class CRUDTKeyPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase<CoreTKeyPartitionedCollectionNameDoc, Guid>
|
||||
{
|
||||
public CRUDTKeyPartitionedCollectionNameAttributeTests(MongoDbTestFixture<CoreTKeyPartitionedCollectionNameDoc, Guid> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
public class CorePartitionedTKeyTestDocument : TestDoc<Guid>, IPartitionedDocument
|
||||
{
|
||||
public CorePartitionedTKeyTestDocument()
|
||||
{
|
||||
PartitionKey = "CoreTestPartitionKey";
|
||||
}
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
public class CRUDTKeyPartitionedTests : MongoDbTKeyDocumentTestBase<CorePartitionedTKeyTestDocument, Guid>
|
||||
{
|
||||
public CRUDTKeyPartitionedTests(MongoDbTestFixture<CorePartitionedTKeyTestDocument, Guid> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CoreCRUDTKeyPartitionedTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
public class CoreTKeyTestDocument : TestDoc<Guid>
|
||||
{
|
||||
}
|
||||
|
||||
public class CRUDTKeyTests : MongoDbTKeyDocumentTestBase<CoreTKeyTestDocument, Guid>
|
||||
{
|
||||
public CRUDTKeyTests(MongoDbTestFixture<CoreTKeyTestDocument, Guid> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CreateTKeyTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
public class CoreTestDocument : TestDoc
|
||||
{
|
||||
}
|
||||
|
||||
public class CRUDTests : MongoDbDocumentTestBase<CoreTestDocument>
|
||||
{
|
||||
public CRUDTests(MongoDbTestFixture<CoreTestDocument, Guid> fixture) : base(fixture)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,16 +5,21 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170810-02" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.4.4" />
|
||||
<PackageReference Include="xunit" Version="2.3.0-beta5-build3769" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.3.0-beta5-build3769" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta5-build3769" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.1" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.5.0" />
|
||||
<PackageReference Include="MongoDbGenericRepository" Version="1.3.6" />
|
||||
<PackageReference Include="xunit" Version="2.3.1" />
|
||||
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
|
||||
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
|
||||
<Reference Include="System.Configuration">
|
||||
<HintPath>..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll</HintPath>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public CreateTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 1;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePartitionedTests : BaseMongoDbRepositoryTests<CreateTestsPartitionedDocument>
|
||||
{
|
||||
private void PartitionedAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedDocument();
|
||||
// Act
|
||||
SUT.AddOne(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
Xunit.Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedAddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedAddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
|
||||
// Act
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedAddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTestsDocument : Document
|
||||
{
|
||||
public CreateTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class CreateTests : BaseMongoDbRepositoryTests<CreateTestsDocument>
|
||||
{
|
||||
[Fact]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsDocument();
|
||||
// Act
|
||||
SUT.AddOne(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
|
||||
// Act
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class DeleteTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public DeleteTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 1;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePartitionedTests : BaseMongoDbRepositoryTests<DeleteTestsPartitionedDocument>
|
||||
{
|
||||
[Fact]
|
||||
public void PartitionedDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne(document);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedDeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedDeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync(document);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedDeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedDeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedDeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync(documents);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedDeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyLinqContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany(documents);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class DeleteTestsDocument : Document
|
||||
{
|
||||
public DeleteTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteTests : BaseMongoDbRepositoryTests<DeleteTestsDocument>
|
||||
{
|
||||
[Fact]
|
||||
public void DeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne(document);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsDocument>(e => e.Id == document.Id);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync(document);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsDocument>(e => e.Id == document.Id);
|
||||
// Assert
|
||||
Assert.Equal(1, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent");
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync(documents);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyLinqContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsDocument>(e => e.SomeContent == content);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == content));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany(documents);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
Assert.False(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == content));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using CoreIntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Xunit;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CoreCoreIntegrationTests
|
||||
{
|
||||
public class MongoIdentityUser<TKey> : IdentityUser<TKey>, IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
public int Version { get; set; }
|
||||
}
|
||||
|
||||
public class IdentityUserTest : MongoIdentityUser<Guid>, IDocument<Guid>
|
||||
{
|
||||
public IdentityUserTest()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class IdentityUserTests : BaseMongoDbRepositoryTests<IdentityUserTest>
|
||||
{
|
||||
[Fact]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new IdentityUserTest();
|
||||
// Act
|
||||
SUT.AddOne<IdentityUserTest, Guid>(document);
|
||||
// Assert
|
||||
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == document.Id);
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new IdentityUserTest();
|
||||
// Act
|
||||
await SUT.AddOneAsync<IdentityUserTest, Guid>(document);
|
||||
// Assert
|
||||
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == document.Id);
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<IdentityUserTest> { new IdentityUserTest(), new IdentityUserTest() };
|
||||
// Act
|
||||
SUT.AddMany<IdentityUserTest, Guid>(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<IdentityUserTest> { new IdentityUserTest(), new IdentityUserTest() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<IdentityUserTest, Guid>(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<IdentityUserTest, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.Equal(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,10 +2,10 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
|
||||
public class BaseMongoDbRepositoryTests<T> : IDisposable where T : Document, new()
|
||||
public class BaseMongoDbRepositoryTests<T> : IDisposable where T : new()
|
||||
{
|
||||
public T CreateTestDocument()
|
||||
{
|
||||
@@ -44,6 +44,7 @@ namespace IntegrationTests.Infrastructure
|
||||
|
||||
public void Init()
|
||||
{
|
||||
MongoDbConfig.EnsureConfigured();
|
||||
SUT = TestRepository.Instance;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// A class holding global variables.
|
||||
/// </summary>
|
||||
public static class GlobalVariables
|
||||
{
|
||||
/// <summary>
|
||||
/// A random number generator.
|
||||
/// </summary>
|
||||
public static Random Random = new Random();
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using MongoDbGenericRepository;
|
||||
|
||||
namespace IntegrationTests
|
||||
namespace CoreIntegrationTests
|
||||
{
|
||||
public interface ITestRepository : IBaseMongoRepository
|
||||
{
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
using CoreCoreIntegrationTests;
|
||||
using MongoDB.Bson.Serialization;
|
||||
using MongoDB.Bson.Serialization.Conventions;
|
||||
using System.Threading;
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
internal static class MongoDbConfig
|
||||
{
|
||||
private static bool _initialized = false;
|
||||
private static object _initializationLock = new object();
|
||||
private static object _initializationTarget;
|
||||
|
||||
public static void EnsureConfigured()
|
||||
{
|
||||
EnsureConfiguredImpl();
|
||||
}
|
||||
|
||||
private static void EnsureConfiguredImpl()
|
||||
{
|
||||
LazyInitializer.EnsureInitialized(ref _initializationTarget, ref _initialized, ref _initializationLock, () =>
|
||||
{
|
||||
Configure();
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private static void Configure()
|
||||
{
|
||||
RegisterConventions();
|
||||
}
|
||||
|
||||
private static void RegisterConventions()
|
||||
{
|
||||
var pack = new ConventionPack
|
||||
{
|
||||
new IgnoreIfNullConvention(false),
|
||||
new CamelCaseElementNameConvention(),
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,727 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
public abstract class MongoDbDocumentTestBase<T> :
|
||||
IClassFixture<MongoDbTestFixture<T, Guid>>
|
||||
where T: TestDoc, new()
|
||||
{
|
||||
|
||||
private readonly MongoDbTestFixture<T, Guid> _fixture;
|
||||
|
||||
protected MongoDbDocumentTestBase(MongoDbTestFixture<T, Guid> fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
var type = CreateTestDocument();
|
||||
DocumentTypeName = type.GetType().FullName;
|
||||
if (type is IPartitionedDocument)
|
||||
{
|
||||
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
|
||||
}
|
||||
_fixture.PartitionKey = PartitionKey;
|
||||
TestClassName = GetClassName();
|
||||
MongoDbConfig.EnsureConfigured();
|
||||
SUT = TestRepository.Instance;
|
||||
}
|
||||
|
||||
protected T CreateTestDocument()
|
||||
{
|
||||
return _fixture.CreateTestDocument();
|
||||
}
|
||||
|
||||
public abstract string GetClassName();
|
||||
|
||||
protected List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
|
||||
{
|
||||
return _fixture.CreateTestDocuments(numberOfDocumentsToCreate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The partition key for the collection, if any
|
||||
/// </summary>
|
||||
protected string PartitionKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// the name of the test class
|
||||
/// </summary>
|
||||
protected string TestClassName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the document used for tests
|
||||
/// </summary>
|
||||
protected string DocumentTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// SUT: System Under Test
|
||||
/// </summary>
|
||||
protected static ITestRepository SUT { get; set; }
|
||||
|
||||
#region Add
|
||||
|
||||
[Fact]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
SUT.AddOne<T>(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);
|
||||
Assert.True(1 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
await SUT.AddOneAsync<T>(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);
|
||||
Assert.True (1 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
SUT.AddMany<T>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.True (2 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<T>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.True (2 == count, GetTestName());
|
||||
}
|
||||
|
||||
|
||||
#endregion Add
|
||||
|
||||
#region Read
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<T>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.True (1 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Count()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.Count<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Read
|
||||
|
||||
#region Update
|
||||
|
||||
[Fact]
|
||||
public void UpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T>(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T>(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateOneField()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneFieldAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateOneFieldWithFilter()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneFieldWithFilterAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T>(document, updateDef);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument);
|
||||
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T>(document, updateDef);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument);
|
||||
Assert.True(childrenToAdd[0].Type== updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[0].Value== updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Type== updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Value== updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Update
|
||||
|
||||
#region Delete
|
||||
|
||||
[Fact]
|
||||
public void DeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T>(document);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T>(document);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T>(documents);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T>(documents);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
#endregion Delete
|
||||
|
||||
#region Project
|
||||
|
||||
[Fact]
|
||||
public async Task ProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<T, MyTestProjection>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
Assert.True(someContent == result.SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<T, MyTestProjection>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
Assert.True(someContent == result.SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<T, MyTestProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
Assert.True(someContent == result.First().SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<T, MyTestProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
Assert.True(someContent == result.First().SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Project
|
||||
|
||||
#region Test Utils
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetCurrentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(1);
|
||||
|
||||
return sf.GetMethod().Name;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetParentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(2);
|
||||
var method = sf.GetMethod().DeclaringType.Name;
|
||||
return method;
|
||||
}
|
||||
|
||||
private string GetTestName()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
private string GetContent()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
private void Cleanup()
|
||||
{
|
||||
// We drop the collection at the end of each test session.
|
||||
if (!string.IsNullOrEmpty(PartitionKey))
|
||||
{
|
||||
SUT.DropTestCollection<T>(PartitionKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
SUT.DropTestCollection<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Test Utils
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,728 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
public abstract class MongoDbTKeyDocumentTestBase<T, TKey> :
|
||||
IClassFixture<MongoDbTestFixture<T, TKey>>
|
||||
where T: TestDoc<TKey>, new()
|
||||
where TKey : IEquatable<TKey>
|
||||
|
||||
{
|
||||
private readonly MongoDbTestFixture<T, TKey> _fixture;
|
||||
|
||||
protected MongoDbTKeyDocumentTestBase(MongoDbTestFixture<T, TKey> fixture)
|
||||
{
|
||||
_fixture = fixture;
|
||||
var type = CreateTestDocument();
|
||||
DocumentTypeName = type.GetType().FullName;
|
||||
if (type is IPartitionedDocument)
|
||||
{
|
||||
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
|
||||
}
|
||||
_fixture.PartitionKey = PartitionKey;
|
||||
TestClassName = GetClassName();
|
||||
MongoDbConfig.EnsureConfigured();
|
||||
SUT = TestRepository.Instance;
|
||||
}
|
||||
|
||||
public abstract string GetClassName();
|
||||
|
||||
public T CreateTestDocument()
|
||||
{
|
||||
return _fixture.CreateTestDocument();
|
||||
}
|
||||
|
||||
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
|
||||
{
|
||||
return _fixture.CreateTestDocuments(numberOfDocumentsToCreate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The partition key for the collection, if any
|
||||
/// </summary>
|
||||
protected string PartitionKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// the name of the test class
|
||||
/// </summary>
|
||||
protected string TestClassName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the document used for tests
|
||||
/// </summary>
|
||||
protected string DocumentTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// SUT: System Under Test
|
||||
/// </summary>
|
||||
protected static ITestRepository SUT { get; set; }
|
||||
|
||||
#region Add
|
||||
|
||||
[Fact]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
Assert.True(1 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
await SUT.AddOneAsync<T, TKey>(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
Assert.True (1 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.True (2 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<T, TKey>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.True (2 == count, GetTestName());
|
||||
}
|
||||
|
||||
|
||||
#endregion Add
|
||||
|
||||
#region Read
|
||||
|
||||
[Fact]
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<T, TKey>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.True (1 == count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T, TKey>(x => x.Id.Equals(document.Init<TKey>()), PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T, TKey>(x => x.Id.Equals(document.Init<TKey>()), PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAll()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountAsync()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Count()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Read
|
||||
|
||||
#region Update
|
||||
|
||||
[Fact]
|
||||
public void UpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateOneField()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneFieldAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateOneFieldWithFilter()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneFieldWithFilterAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument, GetTestName());
|
||||
Assert.True(content == updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey>(document, updateDef);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument);
|
||||
Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey>(document, updateDef);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.True(null != updatedDocument);
|
||||
Assert.True(childrenToAdd[0].Type== updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[0].Value== updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Type== updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.True(childrenToAdd[1].Value== updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Update
|
||||
|
||||
#region Delete
|
||||
|
||||
[Fact]
|
||||
public void DeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.True (1 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T, TKey>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task DeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T, TKey>(documents);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T, TKey>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T, TKey>(documents);
|
||||
// Assert
|
||||
Assert.True(5 == result);
|
||||
Assert.False(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
#endregion Delete
|
||||
|
||||
#region Project
|
||||
|
||||
[Fact]
|
||||
public async Task ProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<T, MyTestProjection, TKey>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
Assert.True(someContent == result.SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<T, MyTestProjection, TKey>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(null != result, GetTestName());
|
||||
Assert.True(someContent == result.SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<T, MyTestProjection, TKey>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
Assert.True(someContent == result.First().SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<T, MyTestProjection, TKey>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.True(5 == result.Count, GetTestName());
|
||||
Assert.True(someContent == result.First().SomeContent, GetTestName());
|
||||
Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Project
|
||||
|
||||
#region Test Utils
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetCurrentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(1);
|
||||
|
||||
return sf.GetMethod().Name;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetParentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(2);
|
||||
var method = sf.GetMethod().DeclaringType.Name;
|
||||
return method;
|
||||
}
|
||||
|
||||
private string GetTestName()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
private string GetContent()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
private void Cleanup()
|
||||
{
|
||||
// We drop the collection at the end of each test session.
|
||||
if (!string.IsNullOrEmpty(PartitionKey))
|
||||
{
|
||||
SUT.DropTestCollection<T>(PartitionKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
SUT.DropTestCollection<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#endregion Test Utils
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
|
||||
public class MongoDbTestFixture<T, TKey> : IDisposable
|
||||
where T : IDocument<TKey>, new()
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
|
||||
public IMongoDbContext Context;
|
||||
|
||||
public MongoDbTestFixture()
|
||||
{
|
||||
DocsToDelete = new ConcurrentBag<T>();
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
|
||||
public ConcurrentBag<T> DocsToDelete { get; set; }
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
var docIds = DocsToDelete.ToList().Select(e => e.Id);
|
||||
if (docIds.Any())
|
||||
{
|
||||
TestRepository.Instance.DeleteMany<T, TKey>(e => docIds.Contains(e.Id));
|
||||
}
|
||||
}
|
||||
|
||||
public T CreateTestDocument()
|
||||
{
|
||||
var doc = new T();
|
||||
DocsToDelete.Add(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
|
||||
{
|
||||
var docs = new List<T>();
|
||||
for (var i = 0; i < numberOfDocumentsToCreate; i++)
|
||||
{
|
||||
docs.Add(new T());
|
||||
DocsToDelete.Add(docs.Last());
|
||||
}
|
||||
return docs;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
// Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354
|
||||
/// <summary>
|
||||
/// Extensions for the random number generator <see cref="Random"/>
|
||||
/// </summary>
|
||||
public static class RandomExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from min (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="min">The inclusive minimum bound</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than min</param>
|
||||
public static long NextLong(this Random random, long min, long max)
|
||||
{
|
||||
if (max <= min)
|
||||
throw new ArgumentOutOfRangeException("max", "max must be > min!");
|
||||
|
||||
//Working with ulong so that modulo works correctly with values > long.MaxValue
|
||||
ulong uRange = (ulong)(max - min);
|
||||
|
||||
//Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419
|
||||
//for more information.
|
||||
//In the worst case, the expected number of calls is 2 (though usually it's
|
||||
//much closer to 1) so this loop doesn't really hurt performance at all.
|
||||
ulong ulongRand;
|
||||
do
|
||||
{
|
||||
byte[] buf = new byte[8];
|
||||
random.NextBytes(buf);
|
||||
ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
|
||||
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);
|
||||
|
||||
return (long)(ulongRand % uRange) + min;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from 0 (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than 0</param>
|
||||
public static long NextLong(this Random random, long max)
|
||||
{
|
||||
return random.NextLong(0, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long over all possible values of long (except long.MaxValue, similar to
|
||||
/// random.Next())
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
public static long NextLong(this Random random)
|
||||
{
|
||||
return random.NextLong(long.MinValue, long.MaxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
public class ProjectedGroup
|
||||
{
|
||||
public int Key { get; set; }
|
||||
public List<string> Content { get; set; }
|
||||
}
|
||||
|
||||
public class MyTestProjection
|
||||
{
|
||||
public string SomeContent { get; set; }
|
||||
public DateTime SomeDate { get; set; }
|
||||
}
|
||||
|
||||
public class Nested
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
}
|
||||
|
||||
public class Child
|
||||
{
|
||||
public Child(string type, string value)
|
||||
{
|
||||
Type = type;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public string Type { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
|
||||
public class TestDoc : Document
|
||||
{
|
||||
public TestDoc()
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
Children = new List<Child>();
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public int GroupingKey { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
|
||||
public List<Child> Children { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class TestDoc<TKey> : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
[BsonId]
|
||||
public TKey Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
|
||||
public TestDoc()
|
||||
{
|
||||
InitializeFields();
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
Children = new List<Child>();
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
|
||||
public List<Child> Children { get; set; }
|
||||
|
||||
public TId Init<TId>()
|
||||
{
|
||||
return IdGenerator.GetId<TId>();
|
||||
}
|
||||
|
||||
private void InitializeFields()
|
||||
{
|
||||
Id = Init<TKey>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
using MongoDbGenericRepository;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
namespace CoreIntegrationTests.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// A singleton implementation of the TestRepository
|
||||
|
||||
@@ -1,140 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ProjectTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public ProjectTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectPartitionedTests : BaseMongoDbRepositoryTests<ProjectTestsPartitionedDocument>
|
||||
{
|
||||
[Fact]
|
||||
public async Task PartitionedProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(someContent, result.SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(someContent, result.SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
Assert.Equal(someContent, result.First().SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
Assert.Equal(someContent, result.First().SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class Nested
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
}
|
||||
|
||||
public class MyProjection
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectTestsDocument : Document
|
||||
{
|
||||
public ProjectTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectTests : BaseMongoDbRepositoryTests<ProjectTestsDocument>
|
||||
{
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task ProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<ProjectTestsDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(someContent, result.SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<ProjectTestsDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.Equal(someContent, result.SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<ProjectTestsDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
Assert.Equal(someContent, result.First().SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<ProjectTestsDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
Assert.Equal(someContent, result.First().SomeContent);
|
||||
Assert.Equal(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.Equal(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ReadTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public ReadTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 1;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class ReadPartitionedTests : BaseMongoDbRepositoryTests<ReadTestsPartitionedDocument>
|
||||
{
|
||||
[Fact]
|
||||
public async Task PartitionedGetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<ReadTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedGetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetById<ReadTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedGetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedGetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedGetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedAnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedAnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsPartitionedDocument>(x => x.Id == Guid.NewGuid(), PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedAnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedAnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsPartitionedDocument>(x => x.Id == Guid.NewGuid(), PartitionKey);
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedGetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<ReadTestsPartitionedDocument>(x => x.SomeContent == "GetAllAsyncContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedGetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<ReadTestsPartitionedDocument>(x => x.SomeContent == "GetAllContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedCountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<ReadTestsPartitionedDocument>(x => x.SomeContent == "CountAsyncContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void PartitionedCount()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.Count<ReadTestsPartitionedDocument>(x => x.SomeContent == "CountContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ReadTestsDocument : Document
|
||||
{
|
||||
public ReadTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class ReadTests : BaseMongoDbRepositoryTests<ReadTestsDocument>
|
||||
{
|
||||
[Fact]
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<ReadTestsDocument>(document.Id);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetById<ReadTestsDocument>(document.Id);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.Equal(1, count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task AnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsDocument>(x => x.Id == Guid.NewGuid());
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsDocument>(x => x.Id == Guid.NewGuid());
|
||||
// Assert
|
||||
Assert.False(result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<ReadTestsDocument>(x => x.SomeContent == "GetAllAsyncContent");
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void GetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<ReadTestsDocument>(x => x.SomeContent == "GetAllContent");
|
||||
// Assert
|
||||
Assert.Equal(5, result.Count);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<ReadTestsDocument>(x => x.SomeContent == "CountAsyncContent");
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Count()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.Count<ReadTestsDocument>(x => x.SomeContent == "CountContent");
|
||||
// Assert
|
||||
Assert.Equal(5, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class UpdateTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public UpdateTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class UpdatePartitionedTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedDocument>
|
||||
{
|
||||
[Fact]
|
||||
public void PartitionedUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneContent";
|
||||
// Act
|
||||
var result = SUT.UpdateOne(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
Assert.NotNull(updatedDocument);
|
||||
Assert.Equal("UpdateOneContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task PartitionedUpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneAsyncContent";
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
Assert.NotNull(updatedDocument);
|
||||
Assert.Equal("UpdateOneAsyncContent", updatedDocument.SomeContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using Xunit;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class UpdateTestsDocument : Document
|
||||
{
|
||||
public UpdateTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTests : BaseMongoDbRepositoryTests<UpdateTestsDocument>
|
||||
{
|
||||
[Fact]
|
||||
public void UpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneContent";
|
||||
// Act
|
||||
var result = SUT.UpdateOne(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.NotNull(updatedDocument);
|
||||
Assert.Equal("UpdateOneContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task UpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneAsyncContent";
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync(document);
|
||||
// Assert
|
||||
Assert.True(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.NotNull(updatedDocument);
|
||||
Assert.Equal("UpdateOneAsyncContent", updatedDocument.SomeContent);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,11 @@
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Runtime.InteropServices.RuntimeInformation" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
|
||||
</dependentAssembly>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
|
||||
<bindingRedirect oldVersion="0.0.0.0-4.0.2.0" newVersion="4.0.2.0" />
|
||||
</dependentAssembly>
|
||||
</assemblyBinding>
|
||||
</runtime>
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ObjectIdTestDocument : TestDoc<ObjectId>
|
||||
{
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CRUDObjectIdTests : MongoDbTKeyDocumentTestBase<ObjectIdTestDocument, ObjectId>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDObjectIdTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
[CollectionName("TestingCollectionNameAttributePartitionedTKey")]
|
||||
public class PartitionedCollectionNameDoc : TestDoc, IPartitionedDocument
|
||||
{
|
||||
public PartitionedCollectionNameDoc()
|
||||
{
|
||||
PartitionKey = "TestPartitionKey";
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
public class CRUDPartitionedCollectionNameAttributeTests : MongoDbDocumentTestBase<PartitionedCollectionNameDoc>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDPartitionedCollectionNameAttributeTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class PartitionedDoc : TestDoc, IPartitionedDocument
|
||||
{
|
||||
public PartitionedDoc()
|
||||
{
|
||||
PartitionKey = "TestPartitionKey";
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
public class CRUDPartitionedTests : MongoDbDocumentTestBase<PartitionedDoc>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDPartitionedCollectionNameAttributeTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
[CollectionName("TestingCollectionNameAttributePartitionedTKey")]
|
||||
public class TKeyPartitionedCollectionNameDoc : TestDoc<Guid>, IPartitionedDocument
|
||||
{
|
||||
public TKeyPartitionedCollectionNameDoc()
|
||||
{
|
||||
PartitionKey = "TestPartitionKey";
|
||||
}
|
||||
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
public class CRUDTKeyPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase<TKeyPartitionedCollectionNameDoc, Guid>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDTKeyPartitionedCollectionNameAttributeTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class PartitionedTKeyTestDocument : TestDoc<Guid>, IPartitionedDocument
|
||||
{
|
||||
public PartitionedTKeyTestDocument()
|
||||
{
|
||||
PartitionKey = "TestPartitionKey";
|
||||
}
|
||||
public string PartitionKey { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CRUDTKeyPartitionedTests : MongoDbTKeyDocumentTestBase<PartitionedTKeyTestDocument, Guid>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDTKeyPartitionedTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class TKeyTestDocument : TestDoc<Guid>
|
||||
{
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CRUDTKeyTests : MongoDbTKeyDocumentTestBase<TKeyTestDocument, Guid>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CreateTKeyTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class TestDocument : TestDoc
|
||||
{
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CRUDTests : MongoDbDocumentTestBase<TestDocument>
|
||||
{
|
||||
public override string GetClassName()
|
||||
{
|
||||
return "CRUDTests";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,68 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public CreateTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 1;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePartitionedTests : BaseMongoDbRepositoryTests<CreateTestsPartitionedDocument>
|
||||
{
|
||||
[Test]
|
||||
public void PartitionedAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedDocument();
|
||||
// Act
|
||||
SUT.AddOne(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedAddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
|
||||
// Act
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedDocument> { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTestsPartitionedTKeyDocument : IDocument<Guid>, IPartitionedDocument
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public CreateTestsPartitionedTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
PartitionKey = "TestPartitionKey";
|
||||
}
|
||||
public string PartitionKey { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class CreatePartitionedTKeyTests : BaseMongoDbRepositoryTests<CreateTestsPartitionedTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public void PartitionedAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedTKeyDocument();
|
||||
// Act
|
||||
SUT.AddOne<CreateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsPartitionedTKeyDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync<CreateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedAddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedTKeyDocument> { new CreateTestsPartitionedTKeyDocument(), new CreateTestsPartitionedTKeyDocument() };
|
||||
// Act
|
||||
SUT.AddMany<CreateTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedTKeyDocument, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsPartitionedTKeyDocument> { new CreateTestsPartitionedTKeyDocument(), new CreateTestsPartitionedTKeyDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<CreateTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsPartitionedTKeyDocument, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTestsTKeyDocument : IDocument<Guid>
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public CreateTestsTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CreateTKeyTests : BaseMongoDbRepositoryTests<CreateTestsTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public void TKeyAddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsTKeyDocument();
|
||||
// Act
|
||||
SUT.AddOne<CreateTestsTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsTKeyDocument, Guid>(e => e.Id == document.Id);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TKeyAddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsTKeyDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync<CreateTestsTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsTKeyDocument, Guid>(e => e.Id == document.Id);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TKeyAddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsTKeyDocument> { new CreateTestsTKeyDocument(), new CreateTestsTKeyDocument() };
|
||||
// Act
|
||||
SUT.AddMany<CreateTestsTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsTKeyDocument, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TKeyAddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsTKeyDocument> { new CreateTestsTKeyDocument(), new CreateTestsTKeyDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<CreateTestsTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsTKeyDocument, Guid>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,69 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class CreateTestsDocument : Document
|
||||
{
|
||||
public CreateTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class CreateTests : BaseMongoDbRepositoryTests<CreateTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsDocument();
|
||||
// Act
|
||||
SUT.AddOne(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new CreateTestsDocument();
|
||||
// Act
|
||||
await SUT.AddOneAsync(document);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == document.Id);
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
|
||||
// Act
|
||||
SUT.AddMany(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<CreateTestsDocument> { new CreateTestsDocument(), new CreateTestsDocument() };
|
||||
// Act
|
||||
await SUT.AddManyAsync(documents);
|
||||
// Assert
|
||||
long count = SUT.Count<CreateTestsDocument>(e => e.Id == documents[0].Id || e.Id == documents[1].Id);
|
||||
Assert.AreEqual(2, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,137 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class DeleteTestsPartitionedTKeyDocument : IPartitionedDocument, IDocument<Guid>
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public DeleteTestsPartitionedTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
PartitionKey = "TestPartitionKey";
|
||||
}
|
||||
public string PartitionKey { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePartitionedTKeyTests : BaseMongoDbRepositoryTests<DeleteTestsPartitionedTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public void PartitionedDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedDeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany<DeleteTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany<DeleteTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedDeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyLinqContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<DeleteTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.SomeContent == content, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<DeleteTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedTKeyDocument, Guid>(e => e.SomeContent == content, PartitionKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class DeleteTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public DeleteTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 1;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class DeletePartitionedTests : BaseMongoDbRepositoryTests<DeleteTestsPartitionedDocument>
|
||||
{
|
||||
[Test]
|
||||
public void PartitionedDeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedDeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.Id == document.Id, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedDeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedDeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyLinqContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedDeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsPartitionedDocument>(e => e.SomeContent == content, PartitionKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class DeleteTestsTKeyDocument : IDocument<Guid>
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public DeleteTestsTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteTKeyTests : BaseMongoDbRepositoryTests<DeleteTestsTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public void DeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsTKeyDocument, Guid>(e => e.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<DeleteTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsTKeyDocument, Guid>(e => e.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany<DeleteTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsTKeyDocument, Guid>(e => e.SomeContent == "DeleteManyAsyncLinqContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany<DeleteTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyLinqContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<DeleteTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsTKeyDocument, Guid>(e => e.SomeContent == content);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.SomeContent == content));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<DeleteTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsTKeyDocument, Guid>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsTKeyDocument, Guid>(e => e.SomeContent == content));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,129 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class DeleteTestsDocument : Document
|
||||
{
|
||||
public DeleteTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteTests : BaseMongoDbRepositoryTests<DeleteTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public void DeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<DeleteTestsDocument>(e => e.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<DeleteTestsDocument>(e => e.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.Id == document.Id));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == "DeleteManyAsyncLinqContent"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyLinqContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<DeleteTestsDocument>(e => e.SomeContent == content);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == content));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var content = "DeleteManyContent";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<DeleteTestsDocument>(e => e.SomeContent == content));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,108 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace IntegrationTests.GroupTests
|
||||
{
|
||||
public class GroupingTestsDocument : Document
|
||||
{
|
||||
public GroupingTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
Children = new List<Child>();
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
public int GroupingKey { get; set; }
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectedGroup
|
||||
{
|
||||
public int Key { get; set; }
|
||||
public List<string> Content { get; set; }
|
||||
}
|
||||
|
||||
public class GroupingTests : BaseMongoDbRepositoryTests<GroupingTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public void GroupByTProjection()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
for(var i = 0; i < documents.Count - 2; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 1;
|
||||
documents[i].SomeContent = $"content-{i}";
|
||||
}
|
||||
for (var i = 3; i < documents.Count; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 2;
|
||||
documents[i].SomeContent = $"content-{i}";
|
||||
}
|
||||
SUT.AddMany(documents);
|
||||
|
||||
// Act
|
||||
|
||||
var result = SUT.GroupBy<GroupingTestsDocument, int, ProjectedGroup>(
|
||||
e => e.GroupingKey, g => new ProjectedGroup {
|
||||
Key = g.Key,
|
||||
Content = g.Select(doc => doc.SomeContent).ToList()
|
||||
});
|
||||
|
||||
// Assert
|
||||
var key1Group = result.First(e => e.Key == 1);
|
||||
Assert.NotNull(key1Group);
|
||||
Assert.AreEqual(3, key1Group.Content.Count);
|
||||
var key2Group = result.First(e => e.Key == 2);
|
||||
Assert.NotNull(key2Group);
|
||||
Assert.AreEqual(2, key2Group.Content.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FilteredGroupByTProjection()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
for (var i = 0; i < documents.Count - 2; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 4;
|
||||
documents[i].SomeContent = $"content-{i}";
|
||||
}
|
||||
for (var i = 3; i < documents.Count; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 5;
|
||||
documents[i].SomeContent = $"content-{i}";
|
||||
}
|
||||
var guid1 = Guid.NewGuid().ToString("n");
|
||||
var guid2 = Guid.NewGuid().ToString("n");
|
||||
for (var i = 0; i < documents.Count - 1; i++)
|
||||
{
|
||||
documents[i].Children = new List<Child> {
|
||||
new Child(guid1, guid2)
|
||||
};
|
||||
}
|
||||
|
||||
SUT.AddMany(documents);
|
||||
|
||||
// Act
|
||||
var result = SUT.GroupBy<GroupingTestsDocument, int, ProjectedGroup>(
|
||||
e => e.Children.Any(c => c.Type == guid1),
|
||||
e => e.GroupingKey, g => new ProjectedGroup
|
||||
{
|
||||
Key = g.Key,
|
||||
Content = g.Select(doc => doc.SomeContent).ToList()
|
||||
});
|
||||
|
||||
// Assert
|
||||
var key1Group = result.First(e => e.Key == 4);
|
||||
Assert.NotNull(key1Group);
|
||||
Assert.AreEqual(3, key1Group.Content.Count);
|
||||
var key2Group = result.First(e => e.Key == 5);
|
||||
Assert.NotNull(key2Group);
|
||||
Assert.AreEqual(1, key2Group.Content.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
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,15 @@
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
/// <summary>
|
||||
/// A class holding global variables.
|
||||
/// </summary>
|
||||
public static class GlobalVariables
|
||||
{
|
||||
/// <summary>
|
||||
/// A random number generator.
|
||||
/// </summary>
|
||||
public static Random Random = new Random();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,821 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
[TestFixture]
|
||||
public abstract class MongoDbDocumentTestBase<T>
|
||||
where T: TestDoc, new()
|
||||
{
|
||||
public T CreateTestDocument()
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
public abstract string GetClassName();
|
||||
|
||||
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
|
||||
{
|
||||
var docs = new List<T>();
|
||||
for (var i = 0; i < numberOfDocumentsToCreate; i++)
|
||||
{
|
||||
docs.Add(new T());
|
||||
}
|
||||
return docs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The partition key for the collection, if any
|
||||
/// </summary>
|
||||
protected string PartitionKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// the name of the test class
|
||||
/// </summary>
|
||||
protected string TestClassName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the document used for tests
|
||||
/// </summary>
|
||||
protected string DocumentTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// SUT: System Under Test
|
||||
/// </summary>
|
||||
protected static ITestRepository SUT { get; set; }
|
||||
|
||||
public MongoDbDocumentTestBase()
|
||||
{
|
||||
var type = CreateTestDocument();
|
||||
DocumentTypeName = type.GetType().FullName;
|
||||
if (type is IPartitionedDocument)
|
||||
{
|
||||
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
|
||||
}
|
||||
TestClassName = GetClassName();
|
||||
}
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Init()
|
||||
{
|
||||
var connectionString = ConfigurationManager.ConnectionStrings["MongoDbTests"].ConnectionString;
|
||||
SUT = new TestRepository(connectionString, "MongoDbTests");
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// We drop the collection at the end of each test session.
|
||||
if (!string.IsNullOrEmpty(PartitionKey))
|
||||
{
|
||||
SUT.DropTestCollection<T>(PartitionKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
SUT.DropTestCollection<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#region Add
|
||||
|
||||
[Test]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
SUT.AddOne<T>(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);
|
||||
Assert.AreEqual(1, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
await SUT.AddOneAsync<T>(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);
|
||||
Assert.AreEqual(1, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
SUT.AddMany<T>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.AreEqual(2, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<T>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.AreEqual(2, count, GetTestName());
|
||||
}
|
||||
|
||||
|
||||
#endregion Add
|
||||
|
||||
#region Read
|
||||
|
||||
[Test]
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<T>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.AreEqual(1, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T>(x => x.Id.Equals(Guid.NewGuid()), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Count()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
documents.ForEach(e => e.SomeContent = content);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.Count<T>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Read
|
||||
|
||||
#region Update
|
||||
|
||||
[Test]
|
||||
public void UpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneField()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneFieldAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneFieldWithFilter()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneFieldWithFilterAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Update
|
||||
|
||||
#region Delete
|
||||
|
||||
[Test]
|
||||
public void DeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
#endregion Delete
|
||||
|
||||
#region Project
|
||||
|
||||
[Test]
|
||||
public async Task ProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<T, MyTestProjection>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
Assert.AreEqual(someContent, result.SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<T, MyTestProjection>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
Assert.AreEqual(someContent, result.SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<T, MyTestProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
Assert.AreEqual(someContent, result.First().SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T>(documents);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<T, MyTestProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
Assert.AreEqual(someContent, result.First().SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Project
|
||||
|
||||
#region Group By
|
||||
|
||||
[Test]
|
||||
public void GroupByTProjection()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
for (var i = 0; i < documents.Count - 2; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 1;
|
||||
documents[i].SomeContent = $"{content}-{i}";
|
||||
}
|
||||
for (var i = 3; i < documents.Count; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 2;
|
||||
documents[i].SomeContent = $"{content}-{i}";
|
||||
}
|
||||
SUT.AddMany(documents);
|
||||
|
||||
// Act
|
||||
var result = SUT.GroupBy<T, int, ProjectedGroup>(
|
||||
e => e.GroupingKey, g => new ProjectedGroup
|
||||
{
|
||||
Key = g.Key,
|
||||
Content = g.Select(doc => doc.SomeContent).ToList()
|
||||
},
|
||||
PartitionKey);
|
||||
|
||||
// Assert
|
||||
var key1Group = result.First(e => e.Key == 1);
|
||||
Assert.NotNull(key1Group);
|
||||
Assert.AreEqual(3, key1Group.Content.Count);
|
||||
var key2Group = result.First(e => e.Key == 2);
|
||||
Assert.NotNull(key2Group);
|
||||
Assert.AreEqual(2, key2Group.Content.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void FilteredGroupByTProjection()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
var content = GetContent();
|
||||
for (var i = 0; i < documents.Count - 2; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 4;
|
||||
documents[i].SomeContent = $"{content}-{i}";
|
||||
}
|
||||
for (var i = 3; i < documents.Count; i++)
|
||||
{
|
||||
documents[i].GroupingKey = 5;
|
||||
documents[i].SomeContent = $"{content}-{i}";
|
||||
}
|
||||
var guid1 = Guid.NewGuid().ToString("n");
|
||||
var guid2 = Guid.NewGuid().ToString("n");
|
||||
for (var i = 0; i < documents.Count - 1; i++)
|
||||
{
|
||||
documents[i].Children = new List<Child> {
|
||||
new Child(guid1, guid2)
|
||||
};
|
||||
}
|
||||
|
||||
SUT.AddMany(documents);
|
||||
|
||||
// Act
|
||||
var result = SUT.GroupBy<T, int, ProjectedGroup>(
|
||||
e => e.Children.Any(c => c.Type == guid1),
|
||||
e => e.GroupingKey, g => new ProjectedGroup
|
||||
{
|
||||
Key = g.Key,
|
||||
Content = g.Select(doc => doc.SomeContent).ToList()
|
||||
}, PartitionKey);
|
||||
|
||||
// Assert
|
||||
var key1Group = result.First(e => e.Key == 4);
|
||||
Assert.NotNull(key1Group);
|
||||
Assert.AreEqual(3, key1Group.Content.Count);
|
||||
var key2Group = result.First(e => e.Key == 5);
|
||||
Assert.NotNull(key2Group);
|
||||
Assert.AreEqual(1, key2Group.Content.Count);
|
||||
}
|
||||
|
||||
#endregion Group By
|
||||
|
||||
#region Test Utils
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetCurrentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(1);
|
||||
|
||||
return sf.GetMethod().Name;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetParentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(2);
|
||||
var method = sf.GetMethod().DeclaringType.Name;
|
||||
return method;
|
||||
}
|
||||
|
||||
private string GetTestName()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
private string GetContent()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
#endregion Test Utils
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,735 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
[TestFixture]
|
||||
public abstract class MongoDbTKeyDocumentTestBase<T, TKey>
|
||||
where T: TestDoc<TKey>, new()
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
public T CreateTestDocument()
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
public abstract string GetClassName();
|
||||
|
||||
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
|
||||
{
|
||||
var docs = new List<T>();
|
||||
for (var i = 0; i < numberOfDocumentsToCreate; i++)
|
||||
{
|
||||
docs.Add(new T());
|
||||
}
|
||||
return docs;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The partition key for the collection, if any
|
||||
/// </summary>
|
||||
protected string PartitionKey { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// the name of the test class
|
||||
/// </summary>
|
||||
protected string TestClassName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The name of the document used for tests
|
||||
/// </summary>
|
||||
protected string DocumentTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// SUT: System Under Test
|
||||
/// </summary>
|
||||
protected static ITestRepository SUT { get; set; }
|
||||
|
||||
public MongoDbTKeyDocumentTestBase()
|
||||
{
|
||||
var type = CreateTestDocument();
|
||||
DocumentTypeName = type.GetType().FullName;
|
||||
if (type is IPartitionedDocument)
|
||||
{
|
||||
PartitionKey = ((IPartitionedDocument)type).PartitionKey;
|
||||
}
|
||||
TestClassName = GetClassName();
|
||||
}
|
||||
|
||||
[OneTimeSetUp]
|
||||
public void Init()
|
||||
{
|
||||
var connectionString = ConfigurationManager.ConnectionStrings["MongoDbTests"].ConnectionString;
|
||||
SUT = new TestRepository(connectionString, "MongoDbTests");
|
||||
}
|
||||
|
||||
[OneTimeTearDown]
|
||||
public void Cleanup()
|
||||
{
|
||||
// We drop the collection at the end of each test session.
|
||||
if (!string.IsNullOrEmpty(PartitionKey))
|
||||
{
|
||||
SUT.DropTestCollection<T>(PartitionKey);
|
||||
}
|
||||
else
|
||||
{
|
||||
SUT.DropTestCollection<T>();
|
||||
}
|
||||
}
|
||||
|
||||
#region Add
|
||||
|
||||
[Test]
|
||||
public void AddOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
Assert.AreEqual(1, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = new T();
|
||||
// Act
|
||||
await SUT.AddOneAsync<T, TKey>(document);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(document.Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
Assert.AreEqual(1, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AddMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.AreEqual(2, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AddManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = new List<T> { new T(), new T() };
|
||||
// Act
|
||||
await SUT.AddManyAsync<T, TKey>(documents);
|
||||
// Assert
|
||||
long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id))
|
||||
: SUT.Count<T, TKey>(e => e.Id.Equals(documents[0].Id)
|
||||
|| e.Id.Equals(documents[1].Id), PartitionKey);
|
||||
Assert.AreEqual(2, count, GetTestName());
|
||||
}
|
||||
|
||||
|
||||
#endregion Add
|
||||
|
||||
#region Read
|
||||
|
||||
[Test]
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<T, TKey>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.AreEqual(1, count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<T, TKey>(x => x.Id.Equals(document.Init<TKey>()), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T, TKey>(x => x.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.Any<T, TKey>(x => x.Id.Equals(document.Init<TKey>()), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllAsync()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAll()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CountAsync()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Count()
|
||||
{
|
||||
// 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>(x => x.SomeContent == content, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Read
|
||||
|
||||
#region Update
|
||||
|
||||
[Test]
|
||||
public void UpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
document.SomeContent = content;
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneField()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneFieldAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey, string>(document, x => x.SomeContent, content);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneFieldWithFilter()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneFieldWithFilterAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var content = GetContent();
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey, string>(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsTrue(result, GetTestName());
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument, GetTestName());
|
||||
Assert.AreEqual(content, updatedDocument.SomeContent, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<T, TKey>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = MongoDB.Driver.Builders<T>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<T, TKey>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<T, TKey>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type, GetTestName());
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Update
|
||||
|
||||
#region Delete
|
||||
|
||||
[Test]
|
||||
public void DeleteOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteOneLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.DeleteOne<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T, TKey>(document);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteOneAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.DeleteOneAsync<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(1, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.Id.Equals(document.Id), PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsyncLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T, TKey>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task DeleteManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.DeleteManyAsync<T, TKey>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteManyLinq()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T, TKey>(e => e.SomeContent == criteria, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DeleteMany()
|
||||
{
|
||||
// Arrange
|
||||
var criteria = $"{GetTestName()}.{DocumentTypeName}";
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = criteria);
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.DeleteMany<T, TKey>(documents);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
Assert.IsFalse(SUT.Any<T, TKey>(e => e.SomeContent == criteria, PartitionKey), GetTestName());
|
||||
}
|
||||
|
||||
#endregion Delete
|
||||
|
||||
#region Project
|
||||
|
||||
[Test]
|
||||
public async Task ProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<T, MyTestProjection, TKey>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
Assert.AreEqual(someContent, result.SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<T, TKey>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<T, MyTestProjection, TKey>(
|
||||
x => x.Id.Equals(document.Id),
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result, GetTestName());
|
||||
Assert.AreEqual(someContent, result.SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<T, MyTestProjection, TKey>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
Assert.AreEqual(someContent, result.First().SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
var someContent = GetContent();
|
||||
var someDate = DateTime.UtcNow;
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<T, TKey>(documents);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<T, MyTestProjection, TKey>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyTestProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count, GetTestName());
|
||||
Assert.AreEqual(someContent, result.First().SomeContent, GetTestName());
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute, GetTestName());
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second, GetTestName());
|
||||
}
|
||||
|
||||
#endregion Project
|
||||
|
||||
#region Test Utils
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetCurrentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(1);
|
||||
|
||||
return sf.GetMethod().Name;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private string GetParentMethod()
|
||||
{
|
||||
StackTrace st = new StackTrace();
|
||||
StackFrame sf = st.GetFrame(2);
|
||||
var method = sf.GetMethod().DeclaringType.Name;
|
||||
return method;
|
||||
}
|
||||
|
||||
private string GetTestName()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
private string GetContent()
|
||||
{
|
||||
return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}";
|
||||
}
|
||||
|
||||
#endregion Test Utils
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
using System;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
// Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354
|
||||
/// <summary>
|
||||
/// Extensions for the random number generator <see cref="Random"/>
|
||||
/// </summary>
|
||||
public static class RandomExtensions
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from min (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="min">The inclusive minimum bound</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than min</param>
|
||||
public static long NextLong(this Random random, long min, long max)
|
||||
{
|
||||
if (max <= min)
|
||||
throw new ArgumentOutOfRangeException("max", "max must be > min!");
|
||||
|
||||
//Working with ulong so that modulo works correctly with values > long.MaxValue
|
||||
ulong uRange = (ulong)(max - min);
|
||||
|
||||
//Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419
|
||||
//for more information.
|
||||
//In the worst case, the expected number of calls is 2 (though usually it's
|
||||
//much closer to 1) so this loop doesn't really hurt performance at all.
|
||||
ulong ulongRand;
|
||||
do
|
||||
{
|
||||
byte[] buf = new byte[8];
|
||||
random.NextBytes(buf);
|
||||
ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
|
||||
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);
|
||||
|
||||
return (long)(ulongRand % uRange) + min;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from 0 (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than 0</param>
|
||||
public static long NextLong(this Random random, long max)
|
||||
{
|
||||
return random.NextLong(0, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long over all possible values of long (except long.MaxValue, similar to
|
||||
/// random.Next())
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
public static long NextLong(this Random random)
|
||||
{
|
||||
return random.NextLong(long.MinValue, long.MaxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
public class ProjectedGroup
|
||||
{
|
||||
public int Key { get; set; }
|
||||
public List<string> Content { get; set; }
|
||||
}
|
||||
|
||||
public class MyTestProjection
|
||||
{
|
||||
public string SomeContent { get; set; }
|
||||
public DateTime SomeDate { get; set; }
|
||||
}
|
||||
|
||||
public class Nested
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
}
|
||||
|
||||
public class Child
|
||||
{
|
||||
public Child(string type, string value)
|
||||
{
|
||||
Type = type;
|
||||
Value = value;
|
||||
}
|
||||
|
||||
public string Type { get; set; }
|
||||
public string Value { get; set; }
|
||||
}
|
||||
|
||||
public class TestDoc : Document
|
||||
{
|
||||
public TestDoc()
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
Children = new List<Child>();
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public int GroupingKey { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
|
||||
public List<Child> Children { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class TestDoc<TKey> : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
[BsonId]
|
||||
public TKey Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
|
||||
public TestDoc()
|
||||
{
|
||||
InitializeFields();
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
Children = new List<Child>();
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
|
||||
public List<Child> Children { get; set; }
|
||||
|
||||
public TId Init<TId>()
|
||||
{
|
||||
return IdGenerator.GetId<TId>();
|
||||
}
|
||||
|
||||
private void InitializeFields()
|
||||
{
|
||||
Id = Init<TKey>();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,54 +30,53 @@
|
||||
<WarningLevel>4</WarningLevel>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="MongoDB.Bson, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Bson.2.4.4\lib\net45\MongoDB.Bson.dll</HintPath>
|
||||
<Reference Include="DnsClient, Version=1.0.7.0, Culture=neutral, PublicKeyToken=4574bb5573c51424, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.6\lib\net45\DnsClient.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Driver, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Driver.2.4.4\lib\net45\MongoDB.Driver.dll</HintPath>
|
||||
<Reference Include="MongoDB.Bson, Version=2.5.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.6\lib\net45\MongoDB.Bson.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDB.Driver.Core, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDB.Driver.Core.2.4.4\lib\net45\MongoDB.Driver.Core.dll</HintPath>
|
||||
<Reference Include="MongoDB.Driver, Version=2.5.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.6\lib\net45\MongoDB.Driver.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
|
||||
<Reference Include="MongoDB.Driver.Core, Version=2.5.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.6\lib\net45\MongoDB.Driver.Core.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="MongoDbGenericRepository, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.6\lib\net45\MongoDbGenericRepository.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="nunit.framework, Version=3.9.0.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\NUnit.3.9.0\lib\net45\nunit.framework.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Buffers, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.6\lib\net45\System.Buffers.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
|
||||
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\MongoDbGenericRepository.1.3.6\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
|
||||
<Private>True</Private>
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="CreateTKeyPartitionedTests.cs" />
|
||||
<Compile Include="CreateTKeyTests.cs" />
|
||||
<Compile Include="DeletePartitionedTKeyTests.cs" />
|
||||
<Compile Include="DeletePartitionedTests.cs" />
|
||||
<Compile Include="DeleteTKeyTests.cs" />
|
||||
<Compile Include="DeleteTests.cs" />
|
||||
<Compile Include="GroupTests\GroupingTests.cs" />
|
||||
<Compile Include="CRUDObjectIdTests.cs" />
|
||||
<Compile Include="CRUDPartitionedTests.cs" />
|
||||
<Compile Include="CRUDTKeyPartitionedCollectionNameAttributeTests.cs" />
|
||||
<Compile Include="CRUDTests.cs" />
|
||||
<Compile Include="CRUDPartitionedCollectionNameAttributeTests.cs" />
|
||||
<Compile Include="CRUDTKeyPartitionedTests.cs" />
|
||||
<Compile Include="CRUDTKeyTests.cs" />
|
||||
<Compile Include="Infrastructure\BaseMongoDbRepositoryTests.cs" />
|
||||
<Compile Include="CreatePartitionedTests.cs" />
|
||||
<Compile Include="Infrastructure\Child.cs" />
|
||||
<Compile Include="Infrastructure\GlobalVariables.cs" />
|
||||
<Compile Include="Infrastructure\ITestRepository.cs" />
|
||||
<Compile Include="Infrastructure\MongoDbDocumentTestBase.cs" />
|
||||
<Compile Include="Infrastructure\MongoDbTKeyDocumentTestBase.cs" />
|
||||
<Compile Include="Infrastructure\RandomExtensions.cs" />
|
||||
<Compile Include="Infrastructure\TestClasses.cs" />
|
||||
<Compile Include="Infrastructure\TestRepository.cs" />
|
||||
<Compile Include="CreateTests.cs" />
|
||||
<Compile Include="ProjectPartitionedTKeyTests.cs" />
|
||||
<Compile Include="ProjectPartitionedTests.cs" />
|
||||
<Compile Include="ProjectTKeyTests.cs" />
|
||||
<Compile Include="ProjectTests.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ReadPartitionedTKeyTests.cs" />
|
||||
<Compile Include="ReadPartitionedTests.cs" />
|
||||
<Compile Include="ReadTKeyTests.cs" />
|
||||
<Compile Include="ReadTests.cs" />
|
||||
<Compile Include="UpdatePartitionedTKeyTests.cs" />
|
||||
<Compile Include="UpdatePartitionedTests.cs" />
|
||||
<Compile Include="UpdateTKeyTests.cs" />
|
||||
<Compile Include="UpdateTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="App.config">
|
||||
@@ -88,11 +87,5 @@
|
||||
<ItemGroup>
|
||||
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj">
|
||||
<Project>{efc776c4-2af3-440c-be80-3fbe335817a5}</Project>
|
||||
<Name>MongoDbGenericRepository</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
</Project>
|
||||
@@ -1,143 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
|
||||
public class ProjectTestsPartitionedTKeyDocument : IDocument<Guid>, IPartitionedDocument
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public ProjectTestsPartitionedTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
PartitionKey = "TestPartitionKey";
|
||||
Nested = new NestedTKey();
|
||||
}
|
||||
public string PartitionKey { get; set; }
|
||||
public NestedTKey Nested { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectPartitionedTKeyTests : BaseMongoDbRepositoryTests<ProjectTestsPartitionedTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task PartitionedProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<ProjectTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<ProjectTestsPartitionedTKeyDocument, MyProjection, Guid>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<ProjectTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<ProjectTestsPartitionedTKeyDocument, MyProjection, Guid>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<ProjectTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<ProjectTestsPartitionedTKeyDocument, MyProjection, Guid>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<ProjectTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<ProjectTestsPartitionedTKeyDocument, MyProjection, Guid>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,140 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ProjectTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public ProjectTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectPartitionedTests : BaseMongoDbRepositoryTests<ProjectTestsPartitionedDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task PartitionedProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<ProjectTestsPartitionedDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
},
|
||||
PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,150 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class NestedTKey
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
}
|
||||
|
||||
public class MyProjectionTKey
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectTestsTKeyDocument : IDocument<Guid>
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public ProjectTestsTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
Nested = new NestedTKey
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
public NestedTKey Nested { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectTKeyTests : BaseMongoDbRepositoryTests<ProjectTestsTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task ProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<ProjectTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<ProjectTestsTKeyDocument, MyProjection, Guid>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne<ProjectTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<ProjectTestsTKeyDocument, MyProjection, Guid>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<ProjectTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<ProjectTestsTKeyDocument, MyProjection, Guid>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany<ProjectTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<ProjectTestsTKeyDocument, MyProjection, Guid>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,149 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class Nested
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
}
|
||||
|
||||
public class MyProjection
|
||||
{
|
||||
public DateTime SomeDate { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectTestsDocument : Document
|
||||
{
|
||||
public ProjectTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
Nested = new Nested
|
||||
{
|
||||
SomeDate = DateTime.UtcNow
|
||||
};
|
||||
}
|
||||
|
||||
public string SomeContent { get; set; }
|
||||
|
||||
public Nested Nested { get; set; }
|
||||
}
|
||||
|
||||
public class ProjectTests : BaseMongoDbRepositoryTests<ProjectTestsDocument>
|
||||
{
|
||||
|
||||
|
||||
[Test]
|
||||
public async Task ProjectOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectOneAsync<ProjectTestsDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectOne()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectOneContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocument();
|
||||
document.SomeContent = someContent;
|
||||
document.Nested.SomeDate = someDate;
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.ProjectOne<ProjectTestsDocument, MyProjection>(
|
||||
x => x.Id == document.Id,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
Assert.AreEqual(someContent, result.SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task ProjectManyAsync()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyAsyncContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = await SUT.ProjectManyAsync<ProjectTestsDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ProjectMany()
|
||||
{
|
||||
// Arrange
|
||||
const string someContent = "ProjectManyContent";
|
||||
var someDate = DateTime.UtcNow;
|
||||
var document = CreateTestDocuments(5);
|
||||
document.ForEach(e =>
|
||||
{
|
||||
e.SomeContent = someContent;
|
||||
e.Nested.SomeDate = someDate;
|
||||
});
|
||||
|
||||
SUT.AddMany(document);
|
||||
// Act
|
||||
var result = SUT.ProjectMany<ProjectTestsDocument, MyProjection>(
|
||||
x => x.SomeContent == someContent,
|
||||
x => new MyProjection
|
||||
{
|
||||
SomeContent = x.SomeContent,
|
||||
SomeDate = x.Nested.SomeDate
|
||||
});
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
Assert.AreEqual(someContent, result.First().SomeContent);
|
||||
Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute);
|
||||
Assert.AreEqual(someDate.Second, result.First().SomeDate.Second);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ReadTestsPartitionedTKeyDocument : IDocument<Guid>, IPartitionedDocument
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public ReadTestsPartitionedTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
PartitionKey = "TestPartitionKey";
|
||||
}
|
||||
public string PartitionKey { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class ReadPartitionedTKeyTests : BaseMongoDbRepositoryTests<ReadTestsPartitionedTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task PartitionedGetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<ReadTestsPartitionedTKeyDocument, Guid>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.GetById<ReadTestsPartitionedTKeyDocument, Guid>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedGetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<ReadTestsPartitionedTKeyDocument, Guid>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<ReadTestsPartitionedTKeyDocument, Guid>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<ReadTestsPartitionedTKeyDocument, Guid>(x => x.Id == document.Id, PartitionKey);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsPartitionedTKeyDocument, Guid>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsPartitionedTKeyDocument, Guid>(x => x.Id == Guid.NewGuid(), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedAnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsPartitionedTKeyDocument, Guid>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedAnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsPartitionedTKeyDocument, Guid>(x => x.Id == Guid.NewGuid(), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedGetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllAsyncContent");
|
||||
SUT.AddMany<ReadTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<ReadTestsPartitionedTKeyDocument, Guid>(x => x.SomeContent == "GetAllAsyncContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllContent");
|
||||
SUT.AddMany<ReadTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<ReadTestsPartitionedTKeyDocument, Guid>(x => x.SomeContent == "GetAllContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedCountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountAsyncContent");
|
||||
SUT.AddMany<ReadTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<ReadTestsPartitionedTKeyDocument, Guid>(x => x.SomeContent == "CountAsyncContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedCount()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountContent");
|
||||
SUT.AddMany<ReadTestsPartitionedTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.Count<ReadTestsPartitionedTKeyDocument, Guid>(x => x.SomeContent == "CountContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,181 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ReadTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public ReadTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 1;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
public class ReadPartitionedTests : BaseMongoDbRepositoryTests<ReadTestsPartitionedDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task PartitionedGetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<ReadTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetById<ReadTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedGetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedAnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsPartitionedDocument>(x => x.Id == Guid.NewGuid(), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedAnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsPartitionedDocument>(x => x.Id == document.Id, PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedAnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsPartitionedDocument>(x => x.Id == Guid.NewGuid(), PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedGetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<ReadTestsPartitionedDocument>(x => x.SomeContent == "GetAllAsyncContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedGetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<ReadTestsPartitionedDocument>(x => x.SomeContent == "GetAllContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedCountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<ReadTestsPartitionedDocument>(x => x.SomeContent == "CountAsyncContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void PartitionedCount()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.Count<ReadTestsPartitionedDocument>(x => x.SomeContent == "CountContent", PartitionKey);
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,187 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ReadTestsTKeyDocument : IDocument<Guid>
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public ReadTestsTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class ReadTKeyTests : BaseMongoDbRepositoryTests<ReadTestsTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<ReadTestsTKeyDocument, Guid>(document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.GetById<ReadTestsTKeyDocument, Guid>(document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<ReadTestsTKeyDocument, Guid>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<ReadTestsTKeyDocument, Guid>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<ReadTestsTKeyDocument, Guid>(x => x.Id == document.Id);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsTKeyDocument, Guid>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsTKeyDocument, Guid>(x => x.Id == Guid.NewGuid());
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsTKeyDocument, Guid>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<ReadTestsTKeyDocument, Guid>(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsTKeyDocument, Guid>(x => x.Id == Guid.NewGuid());
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllAsyncContent");
|
||||
SUT.AddMany<ReadTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<ReadTestsTKeyDocument, Guid>(x => x.SomeContent == "GetAllAsyncContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllContent");
|
||||
SUT.AddMany<ReadTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<ReadTestsTKeyDocument, Guid>(x => x.SomeContent == "GetAllContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountAsyncContent");
|
||||
SUT.AddMany<ReadTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<ReadTestsTKeyDocument, Guid>(x => x.SomeContent == "CountAsyncContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Count()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountContent");
|
||||
SUT.AddMany<ReadTestsTKeyDocument, Guid>(documents);
|
||||
// Act
|
||||
var result = SUT.Count<ReadTestsTKeyDocument, Guid>(x => x.SomeContent == "CountContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class ReadTestsDocument : Document
|
||||
{
|
||||
public ReadTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class ReadTests : BaseMongoDbRepositoryTests<ReadTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<ReadTestsDocument>(document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetById<ReadTestsDocument>(document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.GetOne<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetCursor()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var cursor = SUT.GetCursor<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
var count = cursor.Count();
|
||||
// Assert
|
||||
Assert.AreEqual(1, count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AnyAsyncReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.AnyAsync<ReadTestsDocument>(x => x.Id == Guid.NewGuid());
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsDocument>(x => x.Id == document.Id);
|
||||
// Assert
|
||||
Assert.AreEqual(true, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnyReturnsFalse()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = SUT.Any<ReadTestsDocument>(x => x.Id == Guid.NewGuid());
|
||||
// Assert
|
||||
Assert.AreEqual(false, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetAllAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.GetAllAsync<ReadTestsDocument>(x => x.SomeContent == "GetAllAsyncContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void GetAll()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "GetAllContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.GetAll<ReadTestsDocument>(x => x.SomeContent == "GetAllContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task CountAsync()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountAsyncContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = await SUT.CountAsync<ReadTestsDocument>(x => x.SomeContent == "CountAsyncContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Count()
|
||||
{
|
||||
// Arrange
|
||||
var documents = CreateTestDocuments(5);
|
||||
documents.ForEach(e => e.SomeContent = "CountContent");
|
||||
SUT.AddMany(documents);
|
||||
// Act
|
||||
var result = SUT.Count<ReadTestsDocument>(x => x.SomeContent == "CountContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,117 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
|
||||
public class UpdateTestsPartitionedTKeyDocument : IDocument<Guid>, IPartitionedDocument
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public UpdateTestsPartitionedTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
PartitionKey = "TestPartitionKey";
|
||||
Children = new List<Child>();
|
||||
}
|
||||
public string PartitionKey { get; set; }
|
||||
public string SomeContent { get; set; }
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class UpdatePartitionedTKeyTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public void PartitionedUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
document.SomeContent = "UpdateOneContent";
|
||||
// Act
|
||||
var result = SUT.UpdateOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedUpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
document.SomeContent = "UpdateOneAsyncContent";
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<UpdateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsPartitionedTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<UpdateTestsPartitionedTKeyDocument, Guid>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsPartitionedTKeyDocument, Guid>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsPartitionedTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<UpdateTestsPartitionedTKeyDocument, Guid>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedTKeyDocument, Guid>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,205 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class UpdateTestsPartitionedDocument : PartitionedDocument
|
||||
{
|
||||
public UpdateTestsPartitionedDocument() : base("TestPartitionKey")
|
||||
{
|
||||
Version = 2;
|
||||
Children = new List<Child>();
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
|
||||
public class UpdatePartitionedTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedDocument>
|
||||
{
|
||||
[Test]
|
||||
public void PartitionedUpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneContent";
|
||||
// Act
|
||||
var result = SUT.UpdateOne(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task PartitionedUpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneAsyncContent";
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsPartitionedDocument>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsPartitionedDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<UpdateTestsPartitionedDocument>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsPartitionedDocument>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsPartitionedDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<UpdateTestsPartitionedDocument>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne(document, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync(document, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithFilterAndFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var filter = Builders<UpdateTestsPartitionedDocument>.Filter.Eq("Id", document.Id);
|
||||
var result = SUT.UpdateOne(filter, x => x.Children, childrenToAdd, document.PartitionKey);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithFilterAndFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var filter = Builders<UpdateTestsPartitionedDocument>.Filter.Eq("Id", document.Id);
|
||||
var result = await SUT.UpdateOneAsync(filter, x => x.Children, childrenToAdd, document.PartitionKey);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsPartitionedDocument>(document.Id, document.PartitionKey);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,195 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
|
||||
public class UpdateTestsTKeyDocument : IDocument<Guid>
|
||||
{
|
||||
[BsonId]
|
||||
public Guid Id { get; set; }
|
||||
public int Version { get; set; }
|
||||
public UpdateTestsTKeyDocument()
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
Version = 2;
|
||||
Children = new List<Child>();
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
|
||||
[TestFixture]
|
||||
public class UpdateTKeyTests : BaseMongoDbRepositoryTests<UpdateTestsTKeyDocument>
|
||||
{
|
||||
[Test]
|
||||
public void UpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
document.SomeContent = "UpdateOneContent";
|
||||
// Act
|
||||
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
document.SomeContent = "UpdateOneAsyncContent";
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<UpdateTestsTKeyDocument, Guid>(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<UpdateTestsTKeyDocument, Guid>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsTKeyDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid>(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var filter = Builders<UpdateTestsTKeyDocument>.Filter.Eq("Id", document.Id);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid, List<Child>>(document, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var filter = Builders<UpdateTestsTKeyDocument>.Filter.Eq("Id", document.Id);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync<UpdateTestsTKeyDocument, Guid, List<Child>>(document, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithFilterAndFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne<UpdateTestsTKeyDocument, Guid>(document);
|
||||
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var filter = Builders<UpdateTestsTKeyDocument>.Filter.Eq("Id", document.Id);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne<UpdateTestsTKeyDocument, Guid, List<Child>>(filter, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsTKeyDocument, Guid>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,205 +0,0 @@
|
||||
using IntegrationTests.Infrastructure;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace IntegrationTests
|
||||
{
|
||||
public class UpdateTestsDocument : Document
|
||||
{
|
||||
public UpdateTestsDocument()
|
||||
{
|
||||
Version = 2;
|
||||
Children = new List<Child>();
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
public List<Child> Children { get; set; }
|
||||
}
|
||||
|
||||
public class UpdateTests : BaseMongoDbRepositoryTests<UpdateTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public void UpdateOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneContent";
|
||||
// Act
|
||||
var result = SUT.UpdateOne(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
document.SomeContent = "UpdateOneAsyncContent";
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync(document);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithUpdateDefinition()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
var updateDef = Builders<UpdateTestsDocument>.Update.AddToSetEach(p => p.Children, childrenToAdd);
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne(document, updateDef);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = SUT.UpdateOne(document, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var result = await SUT.UpdateOneAsync(document, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void UpdateOneWithFilterAndFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var filter = Builders<UpdateTestsDocument>.Filter.Eq("Id", document.Id);
|
||||
var result = SUT.UpdateOne(filter, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task UpdateOneAsyncWithFilterAndFieldSelector()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
var childrenToAdd = new List<Child>
|
||||
{
|
||||
new Child("testType1", "testValue1"),
|
||||
new Child("testType2", "testValue2")
|
||||
};
|
||||
|
||||
// Act
|
||||
var filter = Builders<UpdateTestsDocument>.Filter.Eq("Id", document.Id);
|
||||
var result = await SUT.UpdateOneAsync(filter, x => x.Children, childrenToAdd);
|
||||
// Assert
|
||||
Assert.IsTrue(result);
|
||||
var updatedDocument = SUT.GetById<UpdateTestsDocument>(document.Id);
|
||||
Assert.IsNotNull(updatedDocument);
|
||||
Assert.AreEqual(childrenToAdd[0].Type, updatedDocument.Children[0].Type);
|
||||
Assert.AreEqual(childrenToAdd[0].Value, updatedDocument.Children[0].Value);
|
||||
Assert.AreEqual(childrenToAdd[1].Type, updatedDocument.Children[1].Type);
|
||||
Assert.AreEqual(childrenToAdd[1].Value, updatedDocument.Children[1].Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<packages>
|
||||
<package id="MongoDB.Bson" version="2.4.4" targetFramework="net461" />
|
||||
<package id="MongoDB.Driver" version="2.4.4" targetFramework="net461" />
|
||||
<package id="MongoDB.Driver.Core" version="2.4.4" targetFramework="net461" />
|
||||
<package id="NUnit" version="3.7.1" targetFramework="net461" />
|
||||
<package id="DnsClient" version="1.0.7" targetFramework="net461" />
|
||||
<package id="MongoDB.Bson" version="2.5.0" targetFramework="net461" />
|
||||
<package id="MongoDB.Driver" version="2.5.0" targetFramework="net461" />
|
||||
<package id="MongoDB.Driver.Core" version="2.5.0" targetFramework="net461" />
|
||||
<package id="MongoDbGenericRepository" version="1.3.6" targetFramework="net461" />
|
||||
<package id="NUnit" version="3.9.0" targetFramework="net461" />
|
||||
<package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net461" />
|
||||
<package id="System.Runtime.InteropServices" version="4.3.0" targetFramework="net461" />
|
||||
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
|
||||
<package id="System.Buffers" version="4.3.0" targetFramework="net461" />
|
||||
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.0" targetFramework="net461" />
|
||||
</packages>
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2018 Alexandre SPIESER
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1,698 @@
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq.Expressions;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// The IBaseMongoRepository exposes the CRUD functionality of the BaseMongoRepository.
|
||||
/// </summary>
|
||||
public interface IBaseMongoRepository : IReadOnlyMongoRepository
|
||||
{
|
||||
#region Create
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a document to the collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
Task AddOneAsync<TDocument>(TDocument document) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
void AddOne<TDocument>(TDocument document) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documents">The document you want to add.</param>
|
||||
Task AddManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documents">The document you want to add.</param>
|
||||
void AddMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Create TKey
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
Task AddOneAsync<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
void AddOne<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="documents">The documents you want to add.</param>
|
||||
Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="documents">The documents you want to add.</param>
|
||||
void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Update
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
bool UpdateOne<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
bool UpdateOne<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
bool UpdateOne<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
bool UpdateOne<TDocument>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Update TKey
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument modifiedDocument)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
bool UpdateOne<TDocument, TKey>(TDocument modifiedDocument)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Takes a document you want to modify and applies the update you have defined in MongoDb.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="update">The update definition for the document.</param>
|
||||
bool UpdateOne<TDocument, TKey>(TDocument documentToModify, UpdateDefinition<TDocument> update)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value..
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
bool UpdateOne<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// For the entity selected by the filter, updates the property field with the given value.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The partition key for the document.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="documentToModify">The document you want to modify.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
bool UpdateOne<TDocument, TKey, TField>(TDocument documentToModify, Expression<Func<TDocument, TField>> field, TField value)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
Task<bool> UpdateOneAsync<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Updates the property field with the given value update a property field in entities.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TField">The type of the field.</typeparam>
|
||||
/// <param name="filter">The document filter.</param>
|
||||
/// <param name="field">The field selector.</param>
|
||||
/// <param name="value">The new value of the property field.</param>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
bool UpdateOne<TDocument, TKey, TField>(FilterDefinition<TDocument> filter, Expression<Func<TDocument, TField>> field, TField value, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Delete
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="document">The document you want to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteOneAsync<TDocument>(TDocument document) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="document">The document you want to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteOne<TDocument>(TDocument document) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a list of documents.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documents">The list of documents to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteManyAsync<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a list of documents.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="documents">The list of documents to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteMany<TDocument>(IEnumerable<TDocument> documents) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Delete TKey
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="document">The document you want to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteOne<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="document">The document you want to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a document matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a document matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes the documents matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously deletes a list of documents.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="documents">The list of documents to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes a list of documents.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="documents">The list of documents to delete.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Deletes the documents matching the condition of the LINQ expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Project
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a projected document matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<TProjection> ProjectOneAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
where TProjection : class;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a projected document matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<TProjection> ProjectOneAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
where TProjection : class;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a projected document matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
TProjection ProjectOne<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
where TProjection : class;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a projected document matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
TProjection ProjectOne<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
where TProjection : class;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
where TProjection : class;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<List<TProjection>> ProjectManyAsync<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
where TProjection : class;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
List<TProjection> ProjectMany<TDocument, TProjection>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
where TProjection : class;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of projected documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <typeparam name="TProjection">The type representing the model you want to project to.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="projection">The projection expression.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
List<TProjection> ProjectMany<TDocument, TProjection, TKey>(Expression<Func<TDocument, bool>> filter, Expression<Func<TDocument, TProjection>> projection, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
where TProjection : class;
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
|
||||
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<List<TDocument>> GetPaginatedAsync<TDocument>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a paginated list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="skipNumber">The number of documents you want to skip. Default value is 0.</param>
|
||||
/// <param name="takeNumber">The number of documents you want to take. Default value is 50.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<List<TDocument>> GetPaginatedAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// GetAndUpdateOne with filter
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="update"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
Task<TDocument> GetAndUpdateOne<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options)
|
||||
where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// GetAndUpdateOne with filter
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="update"></param>
|
||||
/// <param name="options"></param>
|
||||
/// <returns></returns>
|
||||
Task<TDocument> GetAndUpdateOne<TDocument, TKey>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update, FindOneAndUpdateOptions<TDocument, TDocument> options)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
#region Grouping
|
||||
|
||||
/// <summary>
|
||||
/// Groups a collection of documents given a grouping criteria,
|
||||
/// and returns a list of projected documents.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||
/// <param name="groupingCriteria">The grouping criteria.</param>
|
||||
/// <param name="groupProjection">The projected group result.</param>
|
||||
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(
|
||||
Expression<Func<TDocument, TGroupKey>> groupingCriteria,
|
||||
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> groupProjection,
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
where TProjection : class, new();
|
||||
|
||||
/// <summary>
|
||||
/// Groups filtered a collection of documents given a grouping criteria,
|
||||
/// and returns a dictionary of listed document groups with keys having the different values of the grouping criteria.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TGroupKey">The type of the grouping criteria.</typeparam>
|
||||
/// <typeparam name="TProjection">The type of the projected group.</typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="selector">The grouping criteria.</param>
|
||||
/// <param name="projection">The projected group result.</param>
|
||||
/// <param name="partitionKey">The partition key of your document, if any.</param>
|
||||
List<TProjection> GroupBy<TDocument, TGroupKey, TProjection>(Expression<Func<TDocument, bool>> filter,
|
||||
Expression<Func<TDocument, TGroupKey>> selector,
|
||||
Expression<Func<IGrouping<TGroupKey, TDocument>, TProjection>> projection,
|
||||
string partitionKey = null)
|
||||
where TDocument : IDocument
|
||||
where TProjection : class, new();
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
+8
-8
@@ -19,18 +19,12 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
IMongoDatabase Database { get; }
|
||||
|
||||
/// <summary>
|
||||
/// The private GetCollection method
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
IMongoCollection<TDocument> GetCollection<TDocument>();
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection for a document type that has a partition key.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument;
|
||||
IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection for a document type that has a partition key.
|
||||
@@ -38,7 +32,7 @@ namespace MongoDbGenericRepository
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey)
|
||||
IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -53,5 +47,11 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
void DropCollection<TDocument>(string partitionKey);
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Guid representation of the MongoDb Driver.
|
||||
/// </summary>
|
||||
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
||||
void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq.Expressions;
|
||||
using MongoDbGenericRepository.Models;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// The IReadOnlyMongoRepository exposes the readonly functionality of the BaseMongoRepository.
|
||||
/// </summary>
|
||||
public interface IReadOnlyMongoRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// The connection string.
|
||||
/// </summary>
|
||||
string ConnectionString { get; set; }
|
||||
/// <summary>
|
||||
/// The database name.
|
||||
/// </summary>
|
||||
string DatabaseName { get; set; }
|
||||
|
||||
#region Read
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection cursor.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Read TKey
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection cursor.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey</param>
|
||||
Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey</param>
|
||||
long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
|
||||
namespace MongoDbGenericRepository.Attributes
|
||||
{
|
||||
/// <summary>
|
||||
/// This attribute allows you to specify of the name of the collection.
|
||||
/// Added at commit c117bf2a7fee378f1e02199dea9b2023a7089ee2 by https://github.com/Etchelon
|
||||
/// who has included the CollectionName attribute into the repo to give another choice to the user on how
|
||||
/// to name their collections.
|
||||
/// The attribute takes precedence of course, and if not present the library will fall back to your Pluralize method.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
public class CollectionNameAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// The name of the collection in which your documents are stored.
|
||||
/// </summary>
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The constructor.
|
||||
/// </summary>
|
||||
/// <param name="name">The name of the collection.</param>
|
||||
public CollectionNameAttribute(string name)
|
||||
{
|
||||
this.Name = name;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -13,7 +13,6 @@ namespace MongoDbGenericRepository.Models
|
||||
/// The Primary Key, which must be decorated with the [BsonId] attribute
|
||||
/// if you want the MongoDb C# driver to consider it to be the document ID.
|
||||
/// </summary>
|
||||
[BsonId]
|
||||
TKey Id { get; set; }
|
||||
/// <summary>
|
||||
/// A version number, to indicate the version of the schema.
|
||||
@@ -25,7 +24,7 @@ namespace MongoDbGenericRepository.Models
|
||||
/// This class represents a basic document that can be stored in MongoDb.
|
||||
/// Your document must implement this class in order for the MongoDbRepository to handle them.
|
||||
/// </summary>
|
||||
public interface IDocument: IDocument<Guid>
|
||||
public interface IDocument : IDocument<Guid>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,10 @@
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository.Attributes;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
@@ -25,6 +29,15 @@ namespace MongoDbGenericRepository
|
||||
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the Guid representation of the MongoDb Driver.
|
||||
/// </summary>
|
||||
/// <param name="guidRepresentation">The new value of the GuidRepresentation</param>
|
||||
public void SetGuidRepresentation(MongoDB.Bson.GuidRepresentation guidRepresentation)
|
||||
{
|
||||
MongoDefaults.GuidRepresentation = guidRepresentation;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The constructor of the MongoDbContext, it needs a an object implementing <see cref="IMongoDatabase"/>.
|
||||
/// </summary>
|
||||
@@ -47,23 +60,29 @@ namespace MongoDbGenericRepository
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The private GetCollection method
|
||||
/// Extracts the CollectionName attribute from the entity type, if any.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <returns></returns>
|
||||
public IMongoCollection<TDocument> GetCollection<TDocument>()
|
||||
/// <returns>The name of the collection in which the TDocument is stored.</returns>
|
||||
private string GetAttributeCollectionName<TDocument>()
|
||||
{
|
||||
return Database.GetCollection<TDocument>(Pluralize<TDocument>());
|
||||
return (typeof(TDocument).GetTypeInfo()
|
||||
.GetCustomAttributes(typeof(CollectionNameAttribute))
|
||||
.FirstOrDefault() as CollectionNameAttribute)?.Name;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection for a document type that has a partition key.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
|
||||
/// <summary>
|
||||
/// Returns a collection for a document type that has a partition key.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="partitionKey">The value of the partition key.</param>
|
||||
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return Database.GetCollection<TDocument>(partitionKey +"-"+ Pluralize<TDocument>());
|
||||
if (string.IsNullOrEmpty(partitionKey))
|
||||
{
|
||||
return Database.GetCollection<TDocument>(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
||||
}
|
||||
return Database.GetCollection<TDocument>(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -76,16 +95,20 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return Database.GetCollection<TDocument>(partitionKey + "-" + Pluralize<TDocument>());
|
||||
if (string.IsNullOrEmpty(partitionKey))
|
||||
{
|
||||
return Database.GetCollection<TDocument>(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
||||
}
|
||||
return Database.GetCollection<TDocument>(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops a collection, use very carefully.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
public void DropCollection<TDocument>()
|
||||
/// <summary>
|
||||
/// Drops a collection, use very carefully.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
public void DropCollection<TDocument>()
|
||||
{
|
||||
Database.DropCollection(Pluralize<TDocument>());
|
||||
Database.DropCollection(GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -94,7 +117,7 @@ namespace MongoDbGenericRepository
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
public void DropCollection<TDocument>(string partitionKey)
|
||||
{
|
||||
Database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
|
||||
Database.DropCollection(partitionKey + "-" + GetAttributeCollectionName<TDocument>() ?? Pluralize<TDocument>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.4.4" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.5.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<package >
|
||||
<metadata>
|
||||
<id>MongoDbGenericRepository</id>
|
||||
<version>1.3</version>
|
||||
<version>1.3.6</version>
|
||||
<title>MongoDb Generic Repository</title>
|
||||
<authors>Alexandre Spieser</authors>
|
||||
<owners>Alexandre Spieser</owners>
|
||||
@@ -10,9 +10,12 @@
|
||||
<projectUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</projectUrl>
|
||||
<requireLicenseAcceptance>false</requireLicenseAcceptance>
|
||||
<description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</description>
|
||||
<releaseNotes>Added support for Documents that have an Id of type TKey, and implement the IDocument{TKey} interface.</releaseNotes>
|
||||
<releaseNotes>Adding support for Id of type ObjectId.</releaseNotes>
|
||||
<copyright>Copyright 2017 (c) Alexandre Spieser. All rights reserved.</copyright>
|
||||
<tags>MongoDb Repository Generic NoSql</tags>
|
||||
<dependencies>
|
||||
<dependency id="MongoDB.Driver" version="2.5.0" />
|
||||
</dependencies>
|
||||
</metadata>
|
||||
<files>
|
||||
<file src="lib\**" target="lib" />
|
||||
|
||||
@@ -0,0 +1,456 @@
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Linq.Expressions;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using System.Linq;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// The ReadOnlyMongoRepository implements the readonly functionality of the IReadOnlyMongoRepository.
|
||||
/// </summary>
|
||||
public class ReadOnlyMongoRepository : IReadOnlyMongoRepository
|
||||
{
|
||||
/// <summary>
|
||||
/// The connection string.
|
||||
/// </summary>
|
||||
public string ConnectionString { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The database name.
|
||||
/// </summary>
|
||||
public string DatabaseName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// The MongoDbContext
|
||||
/// </summary>
|
||||
protected IMongoDbContext MongoDbContext = null;
|
||||
|
||||
/// <summary>
|
||||
/// The constructor taking a connection string and a database name.
|
||||
/// </summary>
|
||||
/// <param name="connectionString">The connection string of the MongoDb server.</param>
|
||||
/// <param name="databaseName">The name of the database against which you want to perform operations.</param>
|
||||
protected ReadOnlyMongoRepository(string connectionString, string databaseName)
|
||||
{
|
||||
MongoDbContext = new MongoDbContext(connectionString, databaseName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The contructor taking a <see cref="IMongoDbContext"/>.
|
||||
/// </summary>
|
||||
/// <param name="mongoDbContext">A mongodb context implementing <see cref="IMongoDbContext"/></param>
|
||||
protected ReadOnlyMongoRepository(IMongoDbContext mongoDbContext)
|
||||
{
|
||||
MongoDbContext = mongoDbContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The contructor taking a <see cref="IMongoDatabase"/>.
|
||||
/// </summary>
|
||||
/// <param name="mongoDatabase">A mongodb context implementing <see cref="IMongoDatabase"/></param>
|
||||
protected ReadOnlyMongoRepository(IMongoDatabase mongoDatabase)
|
||||
{
|
||||
MongoDbContext = new MongoDbContext(mongoDatabase);
|
||||
}
|
||||
|
||||
#region Read
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection cursor.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var count = await HandlePartitioned<TDocument>(partitionKey).CountAsync(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var count = HandlePartitioned<TDocument>(partitionKey).Count(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey</param>
|
||||
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await HandlePartitioned<TDocument>(partitionKey).CountAsync(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey</param>
|
||||
public long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).Count();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Read TKey
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<TDocument> GetByIdAsync<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given its id.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="id">The Id of the document you want to get.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public TDocument GetById<TDocument, TKey>(TKey id, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var filter = Builders<TDocument>.Filter.Eq("Id", id);
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<TDocument> GetOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns one document given an expression filter.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public TDocument GetOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a collection cursor.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<bool> AnyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var count = await HandlePartitioned<TDocument, TKey>(partitionKey).CountAsync(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns true if any of the document of the collection matches the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public bool Any<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
var count = HandlePartitioned<TDocument, TKey>(partitionKey).Count(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<List<TDocument>> GetAllAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of the documents matching the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public List<TDocument> GetAll<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey</param>
|
||||
public async Task<long> CountAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return await HandlePartitioned<TDocument, TKey>(partitionKey).CountAsync(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Counts how many documents match the filter condition.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="filter">A LINQ expression filter.</param>
|
||||
/// <param name="partitionKey">An optional partitionKey</param>
|
||||
public long Count<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return HandlePartitioned<TDocument, TKey>(partitionKey).Find(filter).Count();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Utility Methods
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for the type TDocument with the matching partition key.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <param name="partitionKey">The partion key.</param>
|
||||
/// <returns>An <see cref="IMongoCollection{TDocument}"/></returns>
|
||||
protected IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
|
||||
{
|
||||
return MongoDbContext.GetCollection<TDocument>(partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for the type TDocument
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <returns></returns>
|
||||
protected IMongoCollection<TDocument> GetCollection<TDocument>() where TDocument : IDocument
|
||||
{
|
||||
return MongoDbContext.GetCollection<TDocument>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for the type TDocument
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <param name="document">The document.</param>
|
||||
/// <returns></returns>
|
||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument>(TDocument document) where TDocument : IDocument
|
||||
{
|
||||
if (document is IPartitionedDocument)
|
||||
{
|
||||
return GetCollection<TDocument>(((IPartitionedDocument)document).PartitionKey);
|
||||
}
|
||||
return GetCollection<TDocument>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for a potentially partitioned document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="document">The document.</param>
|
||||
/// <returns></returns>
|
||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(TDocument document)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (document is IPartitionedDocument)
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(((IPartitionedDocument)document).PartitionKey);
|
||||
}
|
||||
return GetCollection<TDocument, TKey>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for a potentially partitioned document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <param name="partitionKey">The collection partition key.</param>
|
||||
/// <returns></returns>
|
||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument>(string partitionKey) where TDocument : IDocument
|
||||
{
|
||||
if (!string.IsNullOrEmpty(partitionKey))
|
||||
{
|
||||
return GetCollection<TDocument>(partitionKey);
|
||||
}
|
||||
return GetCollection<TDocument>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for the type TDocument with a partition key.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="partitionKey">The collection partition key.</param>
|
||||
/// <returns></returns>
|
||||
protected IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey = null)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
return MongoDbContext.GetCollection<TDocument, TKey>(partitionKey);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a collections for a potentially partitioned document type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
/// <param name="partitionKey">The collection partition key.</param>
|
||||
/// <returns></returns>
|
||||
protected IMongoCollection<TDocument> HandlePartitioned<TDocument, TKey>(string partitionKey)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
if (!string.IsNullOrEmpty(partitionKey))
|
||||
{
|
||||
return GetCollection<TDocument, TKey>(partitionKey);
|
||||
}
|
||||
return GetCollection<TDocument, TKey>();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
|
||||
namespace MongoDbGenericRepository.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// The IdGenerator instance, used to generate Ids of different types.
|
||||
/// </summary>
|
||||
public static class IdGenerator
|
||||
{
|
||||
private static readonly Random Random = new Random();
|
||||
|
||||
/// <summary>
|
||||
/// Generates a random value of a given type.
|
||||
/// </summary>
|
||||
/// <typeparam name="TKey">The type of the value to generate.</typeparam>
|
||||
/// <returns>A value of type TKey.</returns>
|
||||
public static TKey GetId<TKey>()
|
||||
{
|
||||
var idTypeName = typeof(TKey).Name;
|
||||
switch (idTypeName)
|
||||
{
|
||||
case "Guid":
|
||||
return (TKey)(object)Guid.NewGuid();
|
||||
case "Int16":
|
||||
return (TKey)(object)Random.Next(1, short.MaxValue);
|
||||
case "Int32":
|
||||
return (TKey)(object)Random.Next(1, int.MaxValue);
|
||||
case "Int64":
|
||||
return (TKey)(object)(Random.NextLong(1, long.MaxValue));
|
||||
case "String":
|
||||
return (TKey)(object)Guid.NewGuid().ToString();
|
||||
case "ObjectId":
|
||||
return (TKey)(object)ObjectId.GenerateNewId();
|
||||
}
|
||||
throw new ArgumentException($"{idTypeName} is not a supported Id type, the Id of the document cannot be set.");
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -64,7 +64,7 @@ using System.Collections.Generic;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
|
||||
namespace MongoDbGenericRepository
|
||||
namespace MongoDbGenericRepository.Utils
|
||||
{
|
||||
/// <summary>
|
||||
/// Container for registered Vocabularies. At present, only a single vocabulary is supported: Default.
|
||||
@@ -0,0 +1,60 @@
|
||||
using System;
|
||||
|
||||
namespace MongoDbGenericRepository.Utils
|
||||
{
|
||||
// Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354
|
||||
/// <summary>
|
||||
/// Extensions for the random number generator <see cref="Random"/>
|
||||
/// </summary>
|
||||
public static class RandomExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns a random long from min (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="min">The inclusive minimum bound</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than min</param>
|
||||
public static long NextLong(this Random random, long min, long max)
|
||||
{
|
||||
if (max <= min)
|
||||
throw new ArgumentOutOfRangeException("max", "max must be > min!");
|
||||
|
||||
//Working with ulong so that modulo works correctly with values > long.MaxValue
|
||||
ulong uRange = (ulong)(max - min);
|
||||
|
||||
//Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419
|
||||
//for more information.
|
||||
//In the worst case, the expected number of calls is 2 (though usually it's
|
||||
//much closer to 1) so this loop doesn't really hurt performance at all.
|
||||
ulong ulongRand;
|
||||
do
|
||||
{
|
||||
byte[] buf = new byte[8];
|
||||
random.NextBytes(buf);
|
||||
ulongRand = (ulong)BitConverter.ToInt64(buf, 0);
|
||||
} while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange);
|
||||
|
||||
return (long)(ulongRand % uRange) + min;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long from 0 (inclusive) to max (exclusive)
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
/// <param name="max">The exclusive maximum bound. Must be greater than 0</param>
|
||||
public static long NextLong(this Random random, long max)
|
||||
{
|
||||
return random.NextLong(0, max);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a random long over all possible values of long (except long.MaxValue, similar to
|
||||
/// random.Next())
|
||||
/// </summary>
|
||||
/// <param name="random">The given random instance</param>
|
||||
public static long NextLong(this Random random)
|
||||
{
|
||||
return random.NextLong(long.MinValue, long.MaxValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
|
||||
<Relationship Type="http://schemas.microsoft.com/packaging/2010/07/manifest" Target="/MongoDbGenericRepository.nuspec" Id="Rd7ba7aa97a7d4fb2" />
|
||||
<Relationship Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="/package/services/metadata/core-properties/e5080d3094a649a39d247315f4dcd1ba.psmdcp" Id="Rcfffb2818c774b93" />
|
||||
</Relationships>
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
File diff suppressed because it is too large
Load Diff
Binary file not shown.
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"runtimeTarget": {
|
||||
"name": ".NETStandard,Version=v1.5/",
|
||||
"signature": "a194bb722afb9e376ea09b07a4832ba79352dd6a"
|
||||
"signature": "795a70ab1bbc13177859af16f1887befe3221c54"
|
||||
},
|
||||
"compilationOptions": {},
|
||||
"targets": {
|
||||
@@ -9,13 +9,36 @@
|
||||
".NETStandard,Version=v1.5/": {
|
||||
"MongoDbGenericRepository/1.0.0": {
|
||||
"dependencies": {
|
||||
"MongoDB.Driver": "2.4.4",
|
||||
"MongoDB.Driver": "2.5.0",
|
||||
"NETStandard.Library": "1.6.1"
|
||||
},
|
||||
"runtime": {
|
||||
"MongoDbGenericRepository.dll": {}
|
||||
}
|
||||
},
|
||||
"DnsClient/1.0.7": {
|
||||
"dependencies": {
|
||||
"Microsoft.Win32.Primitives": "4.3.0",
|
||||
"NETStandard.Library": "1.6.1",
|
||||
"System.Buffers": "4.3.0",
|
||||
"System.Collections": "4.3.0",
|
||||
"System.Collections.Concurrent": "4.3.0",
|
||||
"System.Globalization.Extensions": "4.3.0",
|
||||
"System.IO.FileSystem": "4.3.0",
|
||||
"System.Linq": "4.3.0",
|
||||
"System.Net.NameResolution": "4.3.0",
|
||||
"System.Net.NetworkInformation": "4.3.0",
|
||||
"System.Net.Sockets": "4.3.0",
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
"System.Runtime.InteropServices": "4.3.0",
|
||||
"System.Runtime.InteropServices.RuntimeInformation": "4.3.0",
|
||||
"System.Text.Encoding": "4.3.0",
|
||||
"System.Threading": "4.3.0"
|
||||
},
|
||||
"runtime": {
|
||||
"lib/netstandard1.3/DnsClient.dll": {}
|
||||
}
|
||||
},
|
||||
"Microsoft.NETCore.Platforms/1.1.0": {},
|
||||
"Microsoft.NETCore.Targets/1.1.0": {},
|
||||
"Microsoft.Win32.Primitives/4.3.0": {
|
||||
@@ -37,7 +60,7 @@
|
||||
"System.Runtime.InteropServices": "4.3.0"
|
||||
}
|
||||
},
|
||||
"MongoDB.Bson/2.4.4": {
|
||||
"MongoDB.Bson/2.5.0": {
|
||||
"dependencies": {
|
||||
"NETStandard.Library": "1.6.1",
|
||||
"System.Collections.NonGeneric": "4.0.1",
|
||||
@@ -49,24 +72,26 @@
|
||||
"lib/netstandard1.5/MongoDB.Bson.dll": {}
|
||||
}
|
||||
},
|
||||
"MongoDB.Driver/2.4.4": {
|
||||
"MongoDB.Driver/2.5.0": {
|
||||
"dependencies": {
|
||||
"MongoDB.Bson": "2.4.4",
|
||||
"MongoDB.Driver.Core": "2.4.4",
|
||||
"MongoDB.Bson": "2.5.0",
|
||||
"MongoDB.Driver.Core": "2.5.0",
|
||||
"NETStandard.Library": "1.6.1",
|
||||
"System.ComponentModel.TypeConverter": "4.1.0",
|
||||
"System.Linq.Queryable": "4.0.1"
|
||||
},
|
||||
"runtime": {
|
||||
"lib/netstandard1.5/MongoDB.Driver.dll": {}
|
||||
}
|
||||
},
|
||||
"MongoDB.Driver.Core/2.4.4": {
|
||||
"MongoDB.Driver.Core/2.5.0": {
|
||||
"dependencies": {
|
||||
"MongoDB.Bson": "2.4.4",
|
||||
"DnsClient": "1.0.7",
|
||||
"MongoDB.Bson": "2.5.0",
|
||||
"NETStandard.Library": "1.6.1",
|
||||
"System.Collections.Specialized": "4.0.1",
|
||||
"System.Diagnostics.TraceSource": "4.0.0",
|
||||
"System.Net.NameResolution": "4.0.0",
|
||||
"System.Net.NameResolution": "4.3.0",
|
||||
"System.Net.Security": "4.0.0",
|
||||
"System.Security.SecureString": "4.0.0"
|
||||
},
|
||||
@@ -216,7 +241,7 @@
|
||||
"dependencies": {
|
||||
"System.Collections.NonGeneric": "4.0.1",
|
||||
"System.Globalization": "4.3.0",
|
||||
"System.Globalization.Extensions": "4.0.1",
|
||||
"System.Globalization.Extensions": "4.3.0",
|
||||
"System.Resources.ResourceManager": "4.3.0",
|
||||
"System.Runtime": "4.3.0",
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
@@ -226,6 +251,46 @@
|
||||
"lib/netstandard1.3/System.Collections.Specialized.dll": {}
|
||||
}
|
||||
},
|
||||
"System.ComponentModel/4.0.1": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.3.0"
|
||||
},
|
||||
"runtime": {
|
||||
"lib/netstandard1.3/System.ComponentModel.dll": {}
|
||||
}
|
||||
},
|
||||
"System.ComponentModel.Primitives/4.1.0": {
|
||||
"dependencies": {
|
||||
"System.ComponentModel": "4.0.1",
|
||||
"System.Resources.ResourceManager": "4.3.0",
|
||||
"System.Runtime": "4.3.0"
|
||||
},
|
||||
"runtime": {
|
||||
"lib/netstandard1.0/System.ComponentModel.Primitives.dll": {}
|
||||
}
|
||||
},
|
||||
"System.ComponentModel.TypeConverter/4.1.0": {
|
||||
"dependencies": {
|
||||
"System.Collections": "4.3.0",
|
||||
"System.Collections.NonGeneric": "4.0.1",
|
||||
"System.Collections.Specialized": "4.0.1",
|
||||
"System.ComponentModel": "4.0.1",
|
||||
"System.ComponentModel.Primitives": "4.1.0",
|
||||
"System.Globalization": "4.3.0",
|
||||
"System.Linq": "4.3.0",
|
||||
"System.Reflection": "4.3.0",
|
||||
"System.Reflection.Extensions": "4.3.0",
|
||||
"System.Reflection.Primitives": "4.3.0",
|
||||
"System.Reflection.TypeExtensions": "4.1.0",
|
||||
"System.Resources.ResourceManager": "4.3.0",
|
||||
"System.Runtime": "4.3.0",
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
"System.Threading": "4.3.0"
|
||||
},
|
||||
"runtime": {
|
||||
"lib/netstandard1.5/System.ComponentModel.TypeConverter.dll": {}
|
||||
}
|
||||
},
|
||||
"System.Console/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
@@ -274,8 +339,8 @@
|
||||
"System.Text.Encoding.Extensions": "4.3.0",
|
||||
"System.Threading": "4.3.0",
|
||||
"System.Threading.Tasks": "4.3.0",
|
||||
"System.Threading.Thread": "4.0.0",
|
||||
"System.Threading.ThreadPool": "4.0.10",
|
||||
"System.Threading.Thread": "4.3.0",
|
||||
"System.Threading.ThreadPool": "4.3.0",
|
||||
"runtime.native.System": "4.3.0"
|
||||
}
|
||||
},
|
||||
@@ -343,7 +408,7 @@
|
||||
"System.Runtime": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Globalization.Extensions/4.0.1": {
|
||||
"System.Globalization.Extensions/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
"System.Globalization": "4.3.0",
|
||||
@@ -467,7 +532,7 @@
|
||||
"System.Threading.Tasks": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Net.NameResolution/4.0.0": {
|
||||
"System.Net.NameResolution/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
"System.Collections": "4.3.0",
|
||||
@@ -479,12 +544,39 @@
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
"System.Runtime.Handles": "4.3.0",
|
||||
"System.Runtime.InteropServices": "4.3.0",
|
||||
"System.Security.Principal.Windows": "4.0.0",
|
||||
"System.Security.Principal.Windows": "4.3.0",
|
||||
"System.Threading": "4.3.0",
|
||||
"System.Threading.Tasks": "4.3.0",
|
||||
"runtime.native.System": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Net.NetworkInformation/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
"Microsoft.Win32.Primitives": "4.3.0",
|
||||
"System.Collections": "4.3.0",
|
||||
"System.Diagnostics.Tracing": "4.3.0",
|
||||
"System.Globalization": "4.3.0",
|
||||
"System.IO": "4.3.0",
|
||||
"System.IO.FileSystem": "4.3.0",
|
||||
"System.IO.FileSystem.Primitives": "4.3.0",
|
||||
"System.Linq": "4.3.0",
|
||||
"System.Net.Primitives": "4.3.0",
|
||||
"System.Net.Sockets": "4.3.0",
|
||||
"System.Resources.ResourceManager": "4.3.0",
|
||||
"System.Runtime": "4.3.0",
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
"System.Runtime.Handles": "4.3.0",
|
||||
"System.Runtime.InteropServices": "4.3.0",
|
||||
"System.Security.Principal.Windows": "4.3.0",
|
||||
"System.Threading": "4.3.0",
|
||||
"System.Threading.Overlapped": "4.3.0",
|
||||
"System.Threading.Tasks": "4.3.0",
|
||||
"System.Threading.Thread": "4.3.0",
|
||||
"System.Threading.ThreadPool": "4.3.0",
|
||||
"runtime.native.System": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Net.Primitives/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
@@ -508,14 +600,14 @@
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
"System.Runtime.Handles": "4.3.0",
|
||||
"System.Runtime.InteropServices": "4.3.0",
|
||||
"System.Security.Claims": "4.0.1",
|
||||
"System.Security.Claims": "4.3.0",
|
||||
"System.Security.Cryptography.Primitives": "4.3.0",
|
||||
"System.Security.Cryptography.X509Certificates": "4.3.0",
|
||||
"System.Security.Principal": "4.0.1",
|
||||
"System.Security.Principal.Windows": "4.0.0",
|
||||
"System.Security.Principal": "4.3.0",
|
||||
"System.Security.Principal.Windows": "4.3.0",
|
||||
"System.Threading": "4.3.0",
|
||||
"System.Threading.Tasks": "4.3.0",
|
||||
"System.Threading.ThreadPool": "4.0.10"
|
||||
"System.Threading.ThreadPool": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Net.Sockets/4.3.0": {
|
||||
@@ -670,7 +762,7 @@
|
||||
"lib/netstandard1.3/System.Runtime.Numerics.dll": {}
|
||||
}
|
||||
},
|
||||
"System.Security.Claims/4.0.1": {
|
||||
"System.Security.Claims/4.3.0": {
|
||||
"dependencies": {
|
||||
"System.Collections": "4.3.0",
|
||||
"System.Globalization": "4.3.0",
|
||||
@@ -678,7 +770,7 @@
|
||||
"System.Resources.ResourceManager": "4.3.0",
|
||||
"System.Runtime": "4.3.0",
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
"System.Security.Principal": "4.0.1"
|
||||
"System.Security.Principal": "4.3.0"
|
||||
},
|
||||
"runtime": {
|
||||
"lib/netstandard1.3/System.Security.Claims.dll": {}
|
||||
@@ -729,7 +821,7 @@
|
||||
"System.Security.Cryptography.Encoding": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Security.Principal/4.0.1": {
|
||||
"System.Security.Principal/4.3.0": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.3.0"
|
||||
},
|
||||
@@ -737,7 +829,7 @@
|
||||
"lib/netstandard1.0/System.Security.Principal.dll": {}
|
||||
}
|
||||
},
|
||||
"System.Security.Principal.Windows/4.0.0": {
|
||||
"System.Security.Principal.Windows/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
"Microsoft.Win32.Primitives": "4.3.0",
|
||||
@@ -749,8 +841,8 @@
|
||||
"System.Runtime.Extensions": "4.3.0",
|
||||
"System.Runtime.Handles": "4.3.0",
|
||||
"System.Runtime.InteropServices": "4.3.0",
|
||||
"System.Security.Claims": "4.0.1",
|
||||
"System.Security.Principal": "4.0.1",
|
||||
"System.Security.Claims": "4.3.0",
|
||||
"System.Security.Principal": "4.3.0",
|
||||
"System.Text.Encoding": "4.3.0",
|
||||
"System.Threading": "4.3.0"
|
||||
}
|
||||
@@ -796,6 +888,14 @@
|
||||
"lib/netstandard1.3/System.Threading.dll": {}
|
||||
}
|
||||
},
|
||||
"System.Threading.Overlapped/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
"System.Resources.ResourceManager": "4.3.0",
|
||||
"System.Runtime": "4.3.0",
|
||||
"System.Runtime.Handles": "4.3.0"
|
||||
}
|
||||
},
|
||||
"System.Threading.Tasks/4.3.0": {
|
||||
"dependencies": {
|
||||
"Microsoft.NETCore.Platforms": "1.1.0",
|
||||
@@ -813,7 +913,7 @@
|
||||
"lib/netstandard1.0/System.Threading.Tasks.Extensions.dll": {}
|
||||
}
|
||||
},
|
||||
"System.Threading.Thread/4.0.0": {
|
||||
"System.Threading.Thread/4.3.0": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.3.0"
|
||||
},
|
||||
@@ -821,7 +921,7 @@
|
||||
"lib/netstandard1.3/System.Threading.Thread.dll": {}
|
||||
}
|
||||
},
|
||||
"System.Threading.ThreadPool/4.0.10": {
|
||||
"System.Threading.ThreadPool/4.3.0": {
|
||||
"dependencies": {
|
||||
"System.Runtime": "4.3.0",
|
||||
"System.Runtime.Handles": "4.3.0"
|
||||
@@ -886,6 +986,13 @@
|
||||
"serviceable": false,
|
||||
"sha512": ""
|
||||
},
|
||||
"DnsClient/1.0.7": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-f3k5ufhUSL658fy/ac1mReqRIr0jGMyOJFvBqJ+7cASq01NIysYbrrRDjboYKAGrd0Y2mc1A749uQvT8vAA91A==",
|
||||
"path": "dnsclient/1.0.7",
|
||||
"hashPath": "dnsclient.1.0.7.nupkg.sha512"
|
||||
},
|
||||
"Microsoft.NETCore.Platforms/1.1.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
@@ -914,26 +1021,26 @@
|
||||
"path": "microsoft.win32.registry/4.0.0",
|
||||
"hashPath": "microsoft.win32.registry.4.0.0.nupkg.sha512"
|
||||
},
|
||||
"MongoDB.Bson/2.4.4": {
|
||||
"MongoDB.Bson/2.5.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-BavFx+rmR5k+dx14tC23KTyjCPkASvTQ1WxzLYHt2w3Mkqel5lJV6+gWzWV3DX9hnTewCC09OKqNqckiJl4sIw==",
|
||||
"path": "mongodb.bson/2.4.4",
|
||||
"hashPath": "mongodb.bson.2.4.4.nupkg.sha512"
|
||||
"sha512": "sha512-b7zQAUdSdfJ4kmGzAA+hv89N2Q6jm1td9WfTimgp8xWAsN4qbtIjA/JkAY1HA0Z8xfXQE3EmdUcDEwT8bkXfXg==",
|
||||
"path": "mongodb.bson/2.5.0",
|
||||
"hashPath": "mongodb.bson.2.5.0.nupkg.sha512"
|
||||
},
|
||||
"MongoDB.Driver/2.4.4": {
|
||||
"MongoDB.Driver/2.5.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-sG+4H7732fG3XGCXWsxwuUQBvnbVO/bzzxBVZHtHa5R2UDsRXR7BfQxAS/d9Qk8FlNDHOjTjz+GzWTgzjgopQw==",
|
||||
"path": "mongodb.driver/2.4.4",
|
||||
"hashPath": "mongodb.driver.2.4.4.nupkg.sha512"
|
||||
"sha512": "sha512-VbHVV8Xdl3PcPU3XxdOUE/yc4BnPokg7k1XHU/3fEM/UdfCy0Ie0eXVE+U2HJXVcM3TQuuyVn+B1La2YY7X8dA==",
|
||||
"path": "mongodb.driver/2.5.0",
|
||||
"hashPath": "mongodb.driver.2.5.0.nupkg.sha512"
|
||||
},
|
||||
"MongoDB.Driver.Core/2.4.4": {
|
||||
"MongoDB.Driver.Core/2.5.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-fVjXuQE5Qe2P38xz9wz5V0QhT54+ZT78/JUKMMbIXOKYVFgkzEOE7UU6ZsbC/AbR4lwGIpRQZoiv7wW3rJb3xQ==",
|
||||
"path": "mongodb.driver.core/2.4.4",
|
||||
"hashPath": "mongodb.driver.core.2.4.4.nupkg.sha512"
|
||||
"sha512": "sha512-/JYwBTEoWZDHiSePk0AF775c0YkSGSsHTA2+hWt9/UOCkYV/QOFujAWDdpFzBMCDpmQewbLRR1knYj76YOxffA==",
|
||||
"path": "mongodb.driver.core/2.5.0",
|
||||
"hashPath": "mongodb.driver.core.2.5.0.nupkg.sha512"
|
||||
},
|
||||
"NETStandard.Library/1.6.1": {
|
||||
"type": "package",
|
||||
@@ -1075,6 +1182,27 @@
|
||||
"path": "system.collections.specialized/4.0.1",
|
||||
"hashPath": "system.collections.specialized.4.0.1.nupkg.sha512"
|
||||
},
|
||||
"System.ComponentModel/4.0.1": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-oBZFnm7seFiVfugsIyOvQCWobNZs7FzqDV/B7tx20Ep/l3UUFCPDkdTnCNaJZTU27zjeODmy2C/cP60u3D4c9w==",
|
||||
"path": "system.componentmodel/4.0.1",
|
||||
"hashPath": "system.componentmodel.4.0.1.nupkg.sha512"
|
||||
},
|
||||
"System.ComponentModel.Primitives/4.1.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-sc/7eVCdxPrp3ljpgTKVaQGUXiW05phNWvtv/m2kocXqrUQvTVWKou1Edas2aDjTThLPZOxPYIGNb/HN0QjURg==",
|
||||
"path": "system.componentmodel.primitives/4.1.0",
|
||||
"hashPath": "system.componentmodel.primitives.4.1.0.nupkg.sha512"
|
||||
},
|
||||
"System.ComponentModel.TypeConverter/4.1.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-MnDAlaeJZy9pdB5ZdOlwdxfpI+LJQ6e0hmH7d2+y2LkiD8DRJynyDYl4Xxf3fWFm7SbEwBZh4elcfzONQLOoQw==",
|
||||
"path": "system.componentmodel.typeconverter/4.1.0",
|
||||
"hashPath": "system.componentmodel.typeconverter.4.1.0.nupkg.sha512"
|
||||
},
|
||||
"System.Console/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
@@ -1145,12 +1273,12 @@
|
||||
"path": "system.globalization.calendars/4.3.0",
|
||||
"hashPath": "system.globalization.calendars.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Globalization.Extensions/4.0.1": {
|
||||
"System.Globalization.Extensions/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-KKo23iKeOaIg61SSXwjANN7QYDr/3op3OWGGzDzz7mypx0Za0fZSeG0l6cco8Ntp8YMYkIQcAqlk8yhm5/Uhcg==",
|
||||
"path": "system.globalization.extensions/4.0.1",
|
||||
"hashPath": "system.globalization.extensions.4.0.1.nupkg.sha512"
|
||||
"sha512": "sha512-FhKmdR6MPG+pxow6wGtNAWdZh7noIOpdD5TwQ3CprzgIE1bBBoim0vbR1+AWsWjQmU7zXHgQo4TWSP6lCeiWcQ==",
|
||||
"path": "system.globalization.extensions/4.3.0",
|
||||
"hashPath": "system.globalization.extensions.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.IO/4.3.0": {
|
||||
"type": "package",
|
||||
@@ -1215,12 +1343,19 @@
|
||||
"path": "system.net.http/4.3.0",
|
||||
"hashPath": "system.net.http.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Net.NameResolution/4.0.0": {
|
||||
"System.Net.NameResolution/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-JdqRdM1Qym3YehqdKIi5LHrpypP4JMfxKQSNCJ2z4WawkG0il+N3XfNeJOxll2XrTnG7WgYYPoeiu/KOwg0DQw==",
|
||||
"path": "system.net.nameresolution/4.0.0",
|
||||
"hashPath": "system.net.nameresolution.4.0.0.nupkg.sha512"
|
||||
"sha512": "sha512-AFYl08R7MrsrEjqpQWTZWBadqXyTzNDaWpMqyxhb0d6sGhV6xMDKueuBXlLL30gz+DIRY6MpdgnHWlCh5wmq9w==",
|
||||
"path": "system.net.nameresolution/4.3.0",
|
||||
"hashPath": "system.net.nameresolution.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Net.NetworkInformation/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-zNVmWVry0pAu7lcrRBhwwU96WUdbsrGL3azyzsbXmVNptae1+Za+UgOe9Z6s8iaWhPn7/l4wQqhC56HZWq7tkg==",
|
||||
"path": "system.net.networkinformation/4.3.0",
|
||||
"hashPath": "system.net.networkinformation.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Net.Primitives/4.3.0": {
|
||||
"type": "package",
|
||||
@@ -1348,12 +1483,12 @@
|
||||
"path": "system.runtime.numerics/4.3.0",
|
||||
"hashPath": "system.runtime.numerics.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Security.Claims/4.0.1": {
|
||||
"System.Security.Claims/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-4Jlp0OgJLS/Voj1kyFP6MJlIYp3crgfH8kNQk2p7+4JYfc1aAmh9PZyAMMbDhuoolGNtux9HqSOazsioRiDvCw==",
|
||||
"path": "system.security.claims/4.0.1",
|
||||
"hashPath": "system.security.claims.4.0.1.nupkg.sha512"
|
||||
"sha512": "sha512-P/+BR/2lnc4PNDHt/TPBAWHVMLMRHsyYZbU1NphW4HIWzCggz8mJbTQQ3MKljFE7LS3WagmVFuBgoLcFzYXlkA==",
|
||||
"path": "system.security.claims/4.3.0",
|
||||
"hashPath": "system.security.claims.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Security.Cryptography.Algorithms/4.3.0": {
|
||||
"type": "package",
|
||||
@@ -1383,19 +1518,19 @@
|
||||
"path": "system.security.cryptography.x509certificates/4.3.0",
|
||||
"hashPath": "system.security.cryptography.x509certificates.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Security.Principal/4.0.1": {
|
||||
"System.Security.Principal/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-On+SKhXY5rzxh/S8wlH1Rm0ogBlu7zyHNxeNBiXauNrhHRXAe9EuX8Yl5IOzLPGU5Z4kLWHMvORDOCG8iu9hww==",
|
||||
"path": "system.security.principal/4.0.1",
|
||||
"hashPath": "system.security.principal.4.0.1.nupkg.sha512"
|
||||
"sha512": "sha512-I1tkfQlAoMM2URscUtpcRo/hX0jinXx6a/KUtEQoz3owaYwl3qwsO8cbzYVVnjxrzxjHo3nJC+62uolgeGIS9A==",
|
||||
"path": "system.security.principal/4.3.0",
|
||||
"hashPath": "system.security.principal.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Security.Principal.Windows/4.0.0": {
|
||||
"System.Security.Principal.Windows/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-iFx15AF3RMEPZn3COh8+Bb2Thv2zsmLd93RchS1b8Mj5SNYeGqbYNCSn5AES1+gq56p4ujGZPrl0xN7ngkXOHg==",
|
||||
"path": "system.security.principal.windows/4.0.0",
|
||||
"hashPath": "system.security.principal.windows.4.0.0.nupkg.sha512"
|
||||
"sha512": "sha512-HVL1rvqYtnRCxFsYag/2le/ZfKLK4yMw79+s6FmKXbSCNN0JeAhrYxnRAHFoWRa0dEojsDcbBSpH3l22QxAVyw==",
|
||||
"path": "system.security.principal.windows/4.3.0",
|
||||
"hashPath": "system.security.principal.windows.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Security.SecureString/4.0.0": {
|
||||
"type": "package",
|
||||
@@ -1432,6 +1567,13 @@
|
||||
"path": "system.threading/4.3.0",
|
||||
"hashPath": "system.threading.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Threading.Overlapped/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-m3HQ2dPiX/DSTpf+yJt8B0c+SRvzfqAJKx+QDWi+VLhz8svLT23MVjEOHPF/KiSLeArKU/iHescrbLd3yVgyNg==",
|
||||
"path": "system.threading.overlapped/4.3.0",
|
||||
"hashPath": "system.threading.overlapped.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Threading.Tasks/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
@@ -1446,19 +1588,19 @@
|
||||
"path": "system.threading.tasks.extensions/4.3.0",
|
||||
"hashPath": "system.threading.tasks.extensions.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Threading.Thread/4.0.0": {
|
||||
"System.Threading.Thread/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-gIdJqDXlOr5W9zeqFErLw3dsOsiShSCYtF9SEHitACycmvNvY8odf9kiKvp6V7aibc8C4HzzNBkWXjyfn7plbQ==",
|
||||
"path": "system.threading.thread/4.0.0",
|
||||
"hashPath": "system.threading.thread.4.0.0.nupkg.sha512"
|
||||
"sha512": "sha512-OHmbT+Zz065NKII/ZHcH9XO1dEuLGI1L2k7uYss+9C1jLxTC9kTZZuzUOyXHayRk+dft9CiDf3I/QZ0t8JKyBQ==",
|
||||
"path": "system.threading.thread/4.3.0",
|
||||
"hashPath": "system.threading.thread.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Threading.ThreadPool/4.0.10": {
|
||||
"System.Threading.ThreadPool/4.3.0": {
|
||||
"type": "package",
|
||||
"serviceable": true,
|
||||
"sha512": "sha512-IMXgB5Vf/5Qw1kpoVgJMOvUO1l32aC+qC3OaIZjWJOjvcxuxNWOK2ZTWWYXfij22NHxT2j1yWX5vlAeQWld9vA==",
|
||||
"path": "system.threading.threadpool/4.0.10",
|
||||
"hashPath": "system.threading.threadpool.4.0.10.nupkg.sha512"
|
||||
"sha512": "sha512-k/+g4b7vjdd4aix83sTgC9VG6oXYKAktSfNIJUNGxPEj7ryEOfzHHhfnmsZvjxawwcD9HyWXKCXmPjX8U4zeSw==",
|
||||
"path": "system.threading.threadpool/4.3.0",
|
||||
"hashPath": "system.threading.threadpool.4.3.0.nupkg.sha512"
|
||||
},
|
||||
"System.Threading.Timer/4.3.0": {
|
||||
"type": "package",
|
||||
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,10 @@ An example of generic repository implementation using the MongoDB C# Sharp 2.0 d
|
||||
Now available as a nuget package:
|
||||
https://www.nuget.org/packages/MongoDbGenericRepository/
|
||||
|
||||
Covered by 200+ integration tests and counting.
|
||||
|
||||
The MongoDbGenericRepository is also used in [AspNetCore.Identity.MongoDbCore](https://github.com/alexandre-spieser/AspNetCore.Identity.MongoDbCore).
|
||||
|
||||
# Usage examples
|
||||
|
||||
This repository is meant to be inherited from.
|
||||
@@ -36,6 +40,7 @@ Here is an example of repository usage, where the TestRepository is implementing
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Instantiation
|
||||
|
||||
The repository can be instantiated like so:
|
||||
@@ -44,8 +49,16 @@ The repository can be instantiated like so:
|
||||
ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests");
|
||||
```
|
||||
|
||||
If you prefer to reuse the same MongoDb database across your application, you can use the `MongoDatabase` from the MongoDb driver implementing the `IMongoDatabase` interface:
|
||||
|
||||
```csharp
|
||||
var client = new MongoClient(connectionString);
|
||||
var mongoDbDatabase = Client.GetDatabase(databaseName);
|
||||
ITestRepository testRepository = new TestRepository(mongoDbDatabase);
|
||||
```
|
||||
|
||||
## Adding documents
|
||||
To add a document, its class must inherit from the `Document` class or implement the `IDocument` interface:
|
||||
To add a document, its class must inherit from the `Document` class, implement the `IDocument` or `IDocument<TKey>` interface:
|
||||
|
||||
```csharp
|
||||
public class MyDocument : Document
|
||||
@@ -58,7 +71,7 @@ To add a document, its class must inherit from the `Document` class or implement
|
||||
}
|
||||
```
|
||||
|
||||
The `IDocument` interface can be seen below:
|
||||
The `IDocument` and `IDocument<TKey>` interfaces can be seen below:
|
||||
|
||||
```csharp
|
||||
/// <summary>
|
||||
@@ -70,7 +83,26 @@ The `IDocument` interface can be seen below:
|
||||
Guid Id { get; set; }
|
||||
int Version { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This class represents a basic document that can be stored in MongoDb.
|
||||
/// Your document must implement this class in order for the MongoDbRepository to handle them.
|
||||
/// </summary>
|
||||
public interface IDocument<TKey> where TKey : IEquatable<TKey>
|
||||
{
|
||||
/// <summary>
|
||||
/// The Primary Key, which must be decorated with the [BsonId] attribute
|
||||
/// if you want the MongoDb C# driver to consider it to be the document ID.
|
||||
/// </summary>
|
||||
[BsonId]
|
||||
TKey Id { get; set; }
|
||||
/// <summary>
|
||||
/// A version number, to indicate the version of the schema.
|
||||
/// </summary>
|
||||
int Version { get; set; }
|
||||
}
|
||||
```
|
||||
|
||||
## Partitioned collections
|
||||
This repository also allows you to partition your document across multiple collections, this can be useful if you are running a SaaS application and want to keep good performance.
|
||||
|
||||
@@ -96,18 +128,27 @@ _testRepository.AddOne(myDoc);
|
||||
|
||||
The above code will generate a collection named `myPartitionKey-myPartitionedDocuments`.
|
||||
|
||||
## CollectionName Attribute
|
||||
It is now possible to change the collection name by using the `CollectionName` attribute:
|
||||
|
||||
```csharp
|
||||
[CollectionName("MyCollectionName")]
|
||||
public class MyDocument : Document
|
||||
{
|
||||
public MyDocument()
|
||||
{
|
||||
Version = 2;
|
||||
}
|
||||
public string SomeContent { get; set; }
|
||||
}
|
||||
```
|
||||
Documents of this type will be inserted into a collection named "MyCollectionName".
|
||||
|
||||
Please refer to the IntegrationTests (NET45) and CoreIntegrationTests (netstandard2.0) projects for more usage examples.
|
||||
|
||||
## Author
|
||||
**Alexandre Spieser**
|
||||
|
||||
## Donations
|
||||
Feeling like my work is worth a coffee?
|
||||
Donations are welcome and will go towards further development of this project as well as other MongoDb related projects. Use the wallet address below to donate.
|
||||
BTC Donations: 1Qc5ZpNA7g66KEEMcz7MXxwNyyoRyKJJZ
|
||||
|
||||
*Thank you for your support and generosity!*
|
||||
|
||||
## License
|
||||
mongodb-generic-repository is under MIT license - http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
|
||||
Reference in New Issue
Block a user