Compare commits

...

19 Commits

Author SHA1 Message Date
Alexandre SPIESER e91c89f054 Update README.md 2017-11-01 08:27:59 +00:00
alexandre-spieser 657beeac99 The MongoDbRepository now has a constructor that takes an IMongoDatabase. 2017-11-01 08:18:29 +00:00
alexandre-spieser fbb07475a1 Added tests for IdentityUsers 2017-10-29 19:29:43 +00:00
Alexandre SPIESER 30ea910b9c Update README.md 2017-10-22 01:06:26 +01:00
Alexandre SPIESER aae71cddc3 Update README.md 2017-10-02 00:20:50 +01:00
Alexandre SPIESER ad2cd66a7d Update README.md 2017-10-01 22:47:37 +01:00
alexandre-spieser bfe1652b9c Merge branch 'AddingTKey' 2017-10-01 22:36:05 +00:00
alexandre-spieser 9c0cd0fe47 dll added. 2017-10-01 22:35:51 +00:00
alexandre-spieser c07b1a5d7b Added grouping functionality and tests. 2017-10-01 22:30:30 +00:00
alexandre-spieser 76862b2caa new nuget package 2017-09-25 22:40:00 +00:00
alexandre-spieser a13800637f added more tests for the documents implementing the IDocument<TKey> interface 2017-09-25 21:44:10 +00:00
alexandre-spieser 1d985e0274 Added tests 2017-09-24 21:34:29 +00:00
Alexandre SPIESER 833263edbf Update README.md 2017-09-24 20:04:15 +01:00
alexandre-spieser 6552a01d28 TKey support, continued 2017-09-23 22:32:44 +00:00
alexandre-spieser 9b1048e318 Adding TKey support 2017-09-23 22:00:31 +00:00
alexandre-spieser 5e187e0b1f Exposed core MongoDb driver objects and removed the AddedAtUtc property constraint from the IDocument interface. 2017-09-23 18:57:56 +00:00
Alexandre SPIESER b843f2de7f Update README.md 2017-09-18 14:54:57 +01:00
Alexandre SPIESER 90ba1e4fc6 Update README.md 2017-09-10 13:07:22 +01:00
Alexandre SPIESER 926099626d Update README.md 2017-09-10 13:06:39 +01:00
49 changed files with 7356 additions and 1145 deletions
@@ -5,15 +5,20 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170810-02" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170810-02" />
<PackageReference Include="MongoDB.Driver" Version="2.4.4" /> <PackageReference Include="MongoDB.Driver" Version="2.4.4" />
<PackageReference Include="MongoDbGenericRepository" Version="1.2.0" /> <PackageReference Include="xunit" Version="2.3.0" />
<PackageReference Include="xunit" Version="2.3.0-beta5-build3769" /> <PackageReference Include="xunit.runner.console" Version="2.3.0" />
<PackageReference Include="xunit.runner.console" Version="2.3.0-beta5-build3769" /> <PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0-beta5-build3769" />
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" /> <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.0-beta4-build3742" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MongoDbGenericRepository\MongoDbGenericRepository.csproj" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<None Update="App.config"> <None Update="App.config">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class CreateTestsPartitionedDocument : PartitionedDocument public class CreateTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class CreateTestsDocument : Document public class CreateTestsDocument : Document
{ {
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class DeleteTestsPartitionedDocument : PartitionedDocument public class DeleteTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class DeleteTestsDocument : Document public class DeleteTestsDocument : Document
{ {
+77
View File
@@ -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.Collections.Generic;
using System; 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() public T CreateTestDocument()
{ {
@@ -44,6 +44,7 @@ namespace IntegrationTests.Infrastructure
public void Init() public void Init()
{ {
MongoDbConfig.EnsureConfigured();
SUT = TestRepository.Instance; SUT = TestRepository.Instance;
} }
@@ -1,6 +1,6 @@
using MongoDbGenericRepository; using MongoDbGenericRepository;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public interface ITestRepository : IBaseMongoRepository 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(),
};
}
}
}
@@ -1,6 +1,6 @@
using MongoDbGenericRepository; using MongoDbGenericRepository;
namespace IntegrationTests.Infrastructure namespace CoreIntegrationTests.Infrastructure
{ {
/// <summary> /// <summary>
/// A singleton implementation of the TestRepository /// A singleton implementation of the TestRepository
@@ -9,7 +9,7 @@ namespace IntegrationTests.Infrastructure
{ {
const string connectionString = "mongodb://localhost:27017"; const string connectionString = "mongodb://localhost:27017";
private static readonly ITestRepository instance = new TestRepository(connectionString, "MongoDbTests"); private static readonly ITestRepository _instance = new TestRepository(connectionString, "MongoDbTests");
// Explicit static constructor to tell C# compiler // Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit // not to mark type as beforefieldinit
@@ -26,7 +26,7 @@ namespace IntegrationTests.Infrastructure
{ {
get get
{ {
return instance; return _instance;
} }
} }
@@ -1,11 +1,11 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class ProjectTestsPartitionedDocument : PartitionedDocument public class ProjectTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,11 +1,11 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class Nested public class Nested
{ {
+2 -2
View File
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class ReadTestsPartitionedDocument : PartitionedDocument public class ReadTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,10 +1,10 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class ReadTestsDocument : Document public class ReadTestsDocument : Document
{ {
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class UpdateTestsPartitionedDocument : PartitionedDocument public class UpdateTestsPartitionedDocument : PartitionedDocument
{ {
+2 -2
View File
@@ -1,9 +1,9 @@
using IntegrationTests.Infrastructure; using CoreIntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using Xunit; using Xunit;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace CoreIntegrationTests
{ {
public class UpdateTestsDocument : Document public class UpdateTestsDocument : Document
{ {
+8
View File
@@ -7,4 +7,12 @@
<connectionStrings> <connectionStrings>
<add name="MongoDbTests" connectionString="mongodb://localhost:27017" /> <add name="MongoDbTests" connectionString="mongodb://localhost:27017" />
</connectionStrings> </connectionStrings>
<runtime>
<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" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration> </configuration>
@@ -0,0 +1,76 @@
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);
}
}
}
+75
View File
@@ -0,0 +1,75 @@
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);
}
}
}
@@ -0,0 +1,137 @@
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));
}
}
}
+135
View File
@@ -0,0 +1,135 @@
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));
}
}
}
@@ -0,0 +1,108 @@
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);
}
}
}
@@ -5,7 +5,7 @@ using System.Configuration;
namespace IntegrationTests.Infrastructure namespace IntegrationTests.Infrastructure
{ {
public class BaseMongoDbRepositoryTests<T> where T : Document, new() public class BaseMongoDbRepositoryTests<T> where T : class, new()
{ {
public T CreateTestDocument() public T CreateTestDocument()
{ {
+14
View File
@@ -0,0 +1,14 @@
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; }
}
}
+24 -8
View File
@@ -31,16 +31,13 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="MongoDB.Bson, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MongoDB.Bson, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDB.Bson.dll</HintPath> <HintPath>..\packages\MongoDB.Bson.2.4.4\lib\net45\MongoDB.Bson.dll</HintPath>
</Reference> </Reference>
<Reference Include="MongoDB.Driver, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MongoDB.Driver, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDB.Driver.dll</HintPath> <HintPath>..\packages\MongoDB.Driver.2.4.4\lib\net45\MongoDB.Driver.dll</HintPath>
</Reference> </Reference>
<Reference Include="MongoDB.Driver.Core, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="MongoDB.Driver.Core, Version=2.4.4.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDB.Driver.Core.dll</HintPath> <HintPath>..\packages\MongoDB.Driver.Core.2.4.4\lib\net45\MongoDB.Driver.Core.dll</HintPath>
</Reference>
<Reference Include="MongoDbGenericRepository, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\MongoDbGenericRepository.dll</HintPath>
</Reference> </Reference>
<Reference Include="nunit.framework, Version=3.7.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb, processorArchitecture=MSIL"> <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> <HintPath>..\packages\NUnit.3.7.1\lib\net45\nunit.framework.dll</HintPath>
@@ -49,24 +46,37 @@
<Reference Include="System.Configuration" /> <Reference Include="System.Configuration" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> <Reference Include="System.Runtime.InteropServices.RuntimeInformation, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\MongoDbGenericRepository.1.2.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath> <HintPath>..\packages\System.Runtime.InteropServices.RuntimeInformation.4.3.0\lib\net45\System.Runtime.InteropServices.RuntimeInformation.dll</HintPath>
<Private>True</Private>
</Reference> </Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="CreateTKeyPartitionedTests.cs" />
<Compile Include="CreateTKeyTests.cs" />
<Compile Include="DeletePartitionedTKeyTests.cs" />
<Compile Include="DeletePartitionedTests.cs" /> <Compile Include="DeletePartitionedTests.cs" />
<Compile Include="DeleteTKeyTests.cs" />
<Compile Include="DeleteTests.cs" /> <Compile Include="DeleteTests.cs" />
<Compile Include="GroupTests\GroupingTests.cs" />
<Compile Include="Infrastructure\BaseMongoDbRepositoryTests.cs" /> <Compile Include="Infrastructure\BaseMongoDbRepositoryTests.cs" />
<Compile Include="CreatePartitionedTests.cs" /> <Compile Include="CreatePartitionedTests.cs" />
<Compile Include="Infrastructure\Child.cs" />
<Compile Include="Infrastructure\ITestRepository.cs" /> <Compile Include="Infrastructure\ITestRepository.cs" />
<Compile Include="Infrastructure\TestRepository.cs" /> <Compile Include="Infrastructure\TestRepository.cs" />
<Compile Include="CreateTests.cs" /> <Compile Include="CreateTests.cs" />
<Compile Include="ProjectPartitionedTKeyTests.cs" />
<Compile Include="ProjectPartitionedTests.cs" /> <Compile Include="ProjectPartitionedTests.cs" />
<Compile Include="ProjectTKeyTests.cs" />
<Compile Include="ProjectTests.cs" /> <Compile Include="ProjectTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadPartitionedTKeyTests.cs" />
<Compile Include="ReadPartitionedTests.cs" /> <Compile Include="ReadPartitionedTests.cs" />
<Compile Include="ReadTKeyTests.cs" />
<Compile Include="ReadTests.cs" /> <Compile Include="ReadTests.cs" />
<Compile Include="UpdatePartitionedTKeyTests.cs" />
<Compile Include="UpdatePartitionedTests.cs" /> <Compile Include="UpdatePartitionedTests.cs" />
<Compile Include="UpdateTKeyTests.cs" />
<Compile Include="UpdateTests.cs" /> <Compile Include="UpdateTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -78,5 +88,11 @@
<ItemGroup> <ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" /> <Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup> </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" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>
@@ -0,0 +1,143 @@
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);
}
}
}
+150
View File
@@ -0,0 +1,150 @@
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);
}
}
}
@@ -0,0 +1,189 @@
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);
}
}
}
+187
View File
@@ -0,0 +1,187 @@
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);
}
}
}
@@ -0,0 +1,117 @@
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);
}
}
}
+154
View File
@@ -1,6 +1,8 @@
using IntegrationTests.Infrastructure; using IntegrationTests.Infrastructure;
using MongoDB.Driver;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using NUnit.Framework; using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace IntegrationTests
@@ -10,8 +12,10 @@ namespace IntegrationTests
public UpdateTestsPartitionedDocument() : base("TestPartitionKey") public UpdateTestsPartitionedDocument() : base("TestPartitionKey")
{ {
Version = 2; Version = 2;
Children = new List<Child>();
} }
public string SomeContent { get; set; } public string SomeContent { get; set; }
public List<Child> Children { get; set; }
} }
public class UpdatePartitionedTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedDocument> public class UpdatePartitionedTests : BaseMongoDbRepositoryTests<UpdateTestsPartitionedDocument>
@@ -47,5 +51,155 @@ namespace IntegrationTests
Assert.IsNotNull(updatedDocument); Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent); 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);
}
} }
} }
+195
View File
@@ -0,0 +1,195 @@
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);
}
}
}
+154
View File
@@ -1,6 +1,8 @@
using IntegrationTests.Infrastructure; using IntegrationTests.Infrastructure;
using MongoDB.Driver;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using NUnit.Framework; using NUnit.Framework;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace IntegrationTests namespace IntegrationTests
@@ -10,8 +12,10 @@ namespace IntegrationTests
public UpdateTestsDocument() public UpdateTestsDocument()
{ {
Version = 2; Version = 2;
Children = new List<Child>();
} }
public string SomeContent { get; set; } public string SomeContent { get; set; }
public List<Child> Children { get; set; }
} }
public class UpdateTests : BaseMongoDbRepositoryTests<UpdateTestsDocument> public class UpdateTests : BaseMongoDbRepositoryTests<UpdateTestsDocument>
@@ -47,5 +51,155 @@ namespace IntegrationTests
Assert.IsNotNull(updatedDocument); Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent); 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);
}
} }
} }
+2 -2
View File
@@ -3,8 +3,8 @@
<package id="MongoDB.Bson" version="2.4.4" targetFramework="net461" /> <package id="MongoDB.Bson" version="2.4.4" targetFramework="net461" />
<package id="MongoDB.Driver" 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="MongoDB.Driver.Core" version="2.4.4" targetFramework="net461" />
<package id="MongoDbGenericRepository" version="1.2.0" targetFramework="net461" />
<package id="NUnit" version="3.7.1" targetFramework="net461" /> <package id="NUnit" version="3.7.1" targetFramework="net461" />
<package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net461" /> <package id="NUnit.ConsoleRunner" version="3.7.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.0.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" />
</packages> </packages>
+1 -1
View File
@@ -7,7 +7,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntegrationTests", "Integra
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDbGenericRepository", "MongoDbGenericRepository\MongoDbGenericRepository.csproj", "{EFC776C4-2AF3-440C-BE80-3FBE335817A5}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDbGenericRepository", "MongoDbGenericRepository\MongoDbGenericRepository.csproj", "{EFC776C4-2AF3-440C-BE80-3FBE335817A5}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreIntegrationTests", "CoreIntegrationTests\CoreIntegrationTests.csproj", "{C640C106-7A25-4E49-A0CF-E4F248E5A97F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CoreIntegrationTests", "CoreIntegrationTests\CoreIntegrationTests.csproj", "{C640C106-7A25-4E49-A0CF-E4F248E5A97F}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
File diff suppressed because it is too large Load Diff
@@ -1,5 +1,6 @@
using MongoDB.Driver; using MongoDB.Driver;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using System;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
@@ -8,6 +9,16 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
public interface IMongoDbContext public interface IMongoDbContext
{ {
/// <summary>
/// The IMongoClient from the official MongoDb driver
/// </summary>
IMongoClient Client { get; }
/// <summary>
/// The IMongoDatabase from the official Mongodb driver
/// </summary>
IMongoDatabase Database { get; }
/// <summary> /// <summary>
/// The private GetCollection method /// The private GetCollection method
/// </summary> /// </summary>
@@ -21,6 +32,16 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">The value of the partition key.</param> /// <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) 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>
/// <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)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Drops a collection, use very carefully. /// Drops a collection, use very carefully.
/// </summary> /// </summary>
@@ -32,5 +53,11 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
void DropCollection<TDocument>(string partitionKey); 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);
} }
} }
+13 -8
View File
@@ -1,4 +1,5 @@
using System; using MongoDB.Bson.Serialization.Attributes;
using System;
namespace MongoDbGenericRepository.Models namespace MongoDbGenericRepository.Models
{ {
@@ -6,20 +7,24 @@ namespace MongoDbGenericRepository.Models
/// This class represents a basic document that can be stored in MongoDb. /// 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. /// Your document must implement this class in order for the MongoDbRepository to handle them.
/// </summary> /// </summary>
public interface IDocument public interface IDocument<TKey> where TKey : IEquatable<TKey>
{ {
/// <summary> /// <summary>
/// The date and UTC time at which the document was added to the collection. /// The Primary Key, which must be decorated with the [BsonId] attribute
/// </summary>
DateTime AddedAtUtc { get; set; }
/// <summary>
/// The Guid, which must be decorated with the [BsonId] attribute
/// if you want the MongoDb C# driver to consider it to be the document ID. /// if you want the MongoDb C# driver to consider it to be the document ID.
/// </summary> /// </summary>
Guid Id { get; set; } TKey Id { get; set; }
/// <summary> /// <summary>
/// A version number, to indicate the version of the schema. /// A version number, to indicate the version of the schema.
/// </summary> /// </summary>
int Version { 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 : IDocument<Guid>
{
}
} }
@@ -6,7 +6,7 @@
/// This can be useful if you are planning to build a Software as a Service (SaaS) Platform, or if you want to reduce indexing. /// This can be useful if you are planning to build a Software as a Service (SaaS) Platform, or if you want to reduce indexing.
/// You could for example insert Logs in different collections based on the week and year they where created, or their Log category/source. /// You could for example insert Logs in different collections based on the week and year they where created, or their Log category/source.
/// </summary> /// </summary>
public interface IPartitionedDocument : IDocument public interface IPartitionedDocument
{ {
/// <summary> /// <summary>
/// The partition key used to partition your collection. /// The partition key used to partition your collection.
+55 -15
View File
@@ -1,5 +1,6 @@
using MongoDB.Driver; using MongoDB.Driver;
using MongoDbGenericRepository.Models; using MongoDbGenericRepository.Models;
using System;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
@@ -8,8 +9,15 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
public class MongoDbContext : IMongoDbContext public class MongoDbContext : IMongoDbContext
{ {
private readonly IMongoClient _client; /// <summary>
private readonly IMongoDatabase _database; /// The IMongoClient from the official MongoDb driver
/// </summary>
public IMongoClient Client { get; }
/// <summary>
/// The IMongoDatabase from the official Mongodb driver
/// </summary>
public IMongoDatabase Database { get; }
static MongoDbContext() static MongoDbContext()
{ {
@@ -17,59 +25,91 @@ namespace MongoDbGenericRepository
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard; 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>
/// <param name="mongoDatabase">An object implementing IMongoDatabase</param>
public MongoDbContext(IMongoDatabase mongoDatabase)
{
Database = mongoDatabase;
Client = Database.Client;
}
/// <summary> /// <summary>
/// The constructor of the MongoDbContext, it needs a connection string and a database name. /// The constructor of the MongoDbContext, it needs a connection string and a database name.
/// </summary> /// </summary>
/// <param name="connectionString"></param> /// <param name="connectionString">The connections string.</param>
/// <param name="databaseName"></param> /// <param name="databaseName">The name of your database.</param>
public MongoDbContext(string connectionString, string databaseName) public MongoDbContext(string connectionString, string databaseName)
{ {
_client = new MongoClient(connectionString); Client = new MongoClient(connectionString);
_database = _client.GetDatabase(databaseName); Database = Client.GetDatabase(databaseName);
} }
/// <summary> /// <summary>
/// The private GetCollection method /// The private GetCollection method
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns></returns> /// <returns></returns>
public IMongoCollection<TDocument> GetCollection<TDocument>() public IMongoCollection<TDocument> GetCollection<TDocument>()
{ {
return _database.GetCollection<TDocument>(Pluralize<TDocument>()); return Database.GetCollection<TDocument>(Pluralize<TDocument>());
} }
/// <summary> /// <summary>
/// Returns a collection for a document type that has a partition key. /// Returns a collection for a document type that has a partition key.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="partitionKey">The value of the partition key.</param> /// <param name="partitionKey">The value of the partition key.</param>
public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument public IMongoCollection<TDocument> GetCollection<TDocument>(string partitionKey) where TDocument : IDocument
{ {
return _database.GetCollection<TDocument>(partitionKey +"-"+ Pluralize<TDocument>()); return Database.GetCollection<TDocument>(partitionKey +"-"+ Pluralize<TDocument>());
}
/// <summary>
/// Returns a collection for a document type that has a partition key.
/// </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="partitionKey">The value of the partition key.</param>
public IMongoCollection<TDocument> GetCollection<TDocument, TKey>(string partitionKey)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return Database.GetCollection<TDocument>(partitionKey + "-" + Pluralize<TDocument>());
} }
/// <summary> /// <summary>
/// Drops a collection, use very carefully. /// Drops a collection, use very carefully.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
public void DropCollection<TDocument>() public void DropCollection<TDocument>()
{ {
_database.DropCollection(Pluralize<TDocument>()); Database.DropCollection(Pluralize<TDocument>());
} }
/// <summary> /// <summary>
/// Drops a collection having a partitionkey, use very carefully. /// Drops a collection having a partitionkey, use very carefully.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
public void DropCollection<TDocument>(string partitionKey) public void DropCollection<TDocument>(string partitionKey)
{ {
_database.DropCollection(partitionKey + "-" + Pluralize<TDocument>()); Database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
} }
/// <summary> /// <summary>
/// Very naively pluralizes a TDocument type name. /// Very naively pluralizes a TDocument type name.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <returns></returns> /// <returns></returns>
private string Pluralize<TDocument>() private string Pluralize<TDocument>()
{ {
@@ -2,7 +2,7 @@
<package > <package >
<metadata> <metadata>
<id>MongoDbGenericRepository</id> <id>MongoDbGenericRepository</id>
<version>1.2</version> <version>1.3</version>
<title>MongoDb Generic Repository</title> <title>MongoDb Generic Repository</title>
<authors>Alexandre Spieser</authors> <authors>Alexandre Spieser</authors>
<owners>Alexandre Spieser</owners> <owners>Alexandre Spieser</owners>
@@ -10,7 +10,7 @@
<projectUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</projectUrl> <projectUrl>https://github.com/alexandre-spieser/mongodb-generic-repository</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance> <requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</description> <description>A generic repository implementation using the MongoDB C# Sharp 2.0 driver.</description>
<releaseNotes>.NET Core support added.</releaseNotes> <releaseNotes>Added support for Documents that have an Id of type TKey, and implement the IDocument{TKey} interface.</releaseNotes>
<copyright>Copyright 2017 (c) Alexandre Spieser. All rights reserved.</copyright> <copyright>Copyright 2017 (c) Alexandre Spieser. All rights reserved.</copyright>
<tags>MongoDb Repository Generic NoSql</tags> <tags>MongoDb Repository Generic NoSql</tags>
</metadata> </metadata>
+5
View File
@@ -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>
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+41 -13
View File
@@ -4,6 +4,8 @@ An example of generic repository implementation using the MongoDB C# Sharp 2.0 d
Now available as a nuget package: Now available as a nuget package:
https://www.nuget.org/packages/MongoDbGenericRepository/ https://www.nuget.org/packages/MongoDbGenericRepository/
Covered by 200+ integration tests and counting.
# Usage examples # Usage examples
This repository is meant to be inherited from. This repository is meant to be inherited from.
@@ -12,7 +14,7 @@ You are responsible for managing its lifetime, it is advised to setup this repos
Here is an example of repository usage, where the TestRepository is implementing 2 custom methods: Here is an example of repository usage, where the TestRepository is implementing 2 custom methods:
``` ```csharp
public interface ITestRepository : IBaseMongoRepository public interface ITestRepository : IBaseMongoRepository
{ {
void DropTestCollection<TDocument>(); void DropTestCollection<TDocument>();
@@ -40,17 +42,25 @@ Here is an example of repository usage, where the TestRepository is implementing
The repository can be instantiated like so: The repository can be instantiated like so:
``` ```csharp
ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests"); ITestRepository testRepository = new TestRepository(connectionString, "MongoDbTests");
``` ```
## Adding documents If you prefer to reuse the same MongoDb database across you application, you can use the `MongoDatabase` from the MongoDb driver implementing the `IMongoDatabase` interface:
To add a document, its class must inherit from the `Document` class or implement the `IDocument` 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, implement the `IDocument` or `IDocument<TKey>` interface:
```csharp
public class MyDocument : Document public class MyDocument : Document
{ {
public ReadTestsDocument() public MyDocument()
{ {
Version = 2; // you can bump the version of the document schema if you change it over time Version = 2; // you can bump the version of the document schema if you change it over time
} }
@@ -58,28 +68,46 @@ 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> /// <summary>
/// This class represents a basic document that can be stored in MongoDb. /// 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. /// Your document must implement this class in order for the MongoDbRepository to handle them.
/// </summary> /// </summary>
public interface IDocument public interface IDocument
{ {
DateTime AddedAtUtc { get; set; }
Guid Id { get; set; } Guid Id { get; set; }
int Version { 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 ## 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. 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.
To use partitioned collections, you must define your documents using the PartitionedDocument class, which implements the IPartitionedDocument interface: To use partitioned collections, you must define your documents using the PartitionedDocument class, which implements the IPartitionedDocument interface:
``` ```csharp
public class MyPartitionedDocument : PartitionedDocument public class MyPartitionedDocument : PartitionedDocument
{ {
public CreateTestsPartitionedDocument(string myPartitionKey) : base(myPartitionKey) public MyPartitionedDocument(string myPartitionKey) : base(myPartitionKey)
{ {
Version = 1; Version = 1;
} }
@@ -90,14 +118,14 @@ To use partitioned collections, you must define your documents using the Partiti
This partitioned key will be used as a prefix to your collection name. This partitioned key will be used as a prefix to your collection name.
The collection name is derived from the name of the type of your document, is set to camel case, and is pluralized using a class taken from Humanizer (https://github.com/Humanizr/Humanizer). The collection name is derived from the name of the type of your document, is set to camel case, and is pluralized using a class taken from Humanizer (https://github.com/Humanizr/Humanizer).
``` ```csharp
var myDoc = new MyPartitionedDocument("myPartitionKey"); var myDoc = new MyPartitionedDocument("myPartitionKey");
_testRepository.AddOne(myDoc); _testRepository.AddOne(myDoc);
``` ```
The above code will generate a collection named `myPartitionKey-myPartitionedDocuments`. The above code will generate a collection named `myPartitionKey-myPartitionedDocuments`.
Please refer to the IntegrationTests project for more usage examples. Please refer to the IntegrationTests (NET45) and CoreIntegrationTests (netstandard2.0) projects for more usage examples.
## Author ## Author
**Alexandre Spieser** **Alexandre Spieser**
@@ -105,7 +133,7 @@ Please refer to the IntegrationTests project for more usage examples.
## Donations ## Donations
Feeling like my work is worth a coffee? 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. 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 BTC Donations: 1FDMWqSK8SHXDGKKp7gyZc4rknynWJ7qbj
*Thank you for your support and generosity!* *Thank you for your support and generosity!*