From 40ff874cf62bf3875e8d095776ecedd02d65d371 Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Sat, 10 Feb 2018 18:16:14 +0000 Subject: [PATCH] CoreIntegrationTests refactored and passing. --- ...PartitionedCollectionNameAttributeTests.cs | 31 + CoreIntegrationTests/CRUDPartitionedTests.cs | 29 + ...PartitionedCollectionNameAttributeTests.cs | 31 + .../CRUDTKeyPartitionedTests.cs | 28 + CoreIntegrationTests/CRUDTKeyTests.cs | 22 + CoreIntegrationTests/CRUDTests.cs | 22 + .../CoreIntegrationTests.csproj | 11 +- .../CreatePartitionedTests.cs | 67 -- CoreIntegrationTests/CreateTests.cs | 68 -- .../DeletePartitionedTests.cs | 129 ---- CoreIntegrationTests/DeleteTests.cs | 129 ---- .../Infrastructure/GlobalVariables.cs | 15 + .../Infrastructure/MongoDbDocumentTestBase.cs | 727 +++++++++++++++++ .../MongoDbTKeyDocumentTestBase.cs | 728 ++++++++++++++++++ .../Infrastructure/MongoDbTestFixture.cs | 55 ++ .../Infrastructure/RandomExtensions.cs | 61 ++ .../Infrastructure/TestClasses.cs | 108 +++ .../ProjectPartitionedTests.cs | 140 ---- CoreIntegrationTests/ProjectTests.cs | 149 ---- CoreIntegrationTests/ReadPartitionedTests.cs | 181 ----- CoreIntegrationTests/ReadTests.cs | 181 ----- .../UpdatePartitionedTests.cs | 51 -- CoreIntegrationTests/UpdateTests.cs | 51 -- IntegrationTests/GroupTests/GroupingTests.cs | 108 --- IntegrationTests/Infrastructure/Child.cs | 14 - .../Infrastructure/MongoDBDocumentTestBase.cs | 116 ++- .../MongoDbTKeyDocumentTestBase.cs | 51 -- .../Infrastructure/TestClasses.cs | 108 +++ IntegrationTests/IntegrationTests.csproj | 3 +- 29 files changed, 2061 insertions(+), 1353 deletions(-) create mode 100644 CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs create mode 100644 CoreIntegrationTests/CRUDPartitionedTests.cs create mode 100644 CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs create mode 100644 CoreIntegrationTests/CRUDTKeyPartitionedTests.cs create mode 100644 CoreIntegrationTests/CRUDTKeyTests.cs create mode 100644 CoreIntegrationTests/CRUDTests.cs delete mode 100644 CoreIntegrationTests/CreatePartitionedTests.cs delete mode 100644 CoreIntegrationTests/CreateTests.cs delete mode 100644 CoreIntegrationTests/DeletePartitionedTests.cs delete mode 100644 CoreIntegrationTests/DeleteTests.cs create mode 100644 CoreIntegrationTests/Infrastructure/GlobalVariables.cs create mode 100644 CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs create mode 100644 CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs create mode 100644 CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs create mode 100644 CoreIntegrationTests/Infrastructure/RandomExtensions.cs create mode 100644 CoreIntegrationTests/Infrastructure/TestClasses.cs delete mode 100644 CoreIntegrationTests/ProjectPartitionedTests.cs delete mode 100644 CoreIntegrationTests/ProjectTests.cs delete mode 100644 CoreIntegrationTests/ReadPartitionedTests.cs delete mode 100644 CoreIntegrationTests/ReadTests.cs delete mode 100644 CoreIntegrationTests/UpdatePartitionedTests.cs delete mode 100644 CoreIntegrationTests/UpdateTests.cs delete mode 100644 IntegrationTests/GroupTests/GroupingTests.cs delete mode 100644 IntegrationTests/Infrastructure/Child.cs create mode 100644 IntegrationTests/Infrastructure/TestClasses.cs diff --git a/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs b/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs new file mode 100644 index 0000000..a6381a9 --- /dev/null +++ b/CoreIntegrationTests/CRUDPartitionedCollectionNameAttributeTests.cs @@ -0,0 +1,31 @@ +using CoreIntegrationTests.Infrastructure; +using MongoDbGenericRepository.Attributes; +using MongoDbGenericRepository.Models; +using System; + +namespace CoreIntegrationTests +{ + [CollectionName("CoreTestingCollectionNameAttributePartitionedTKey")] + public class CorePartitionedCollectionNameDoc : TestDoc, IPartitionedDocument + { + public CorePartitionedCollectionNameDoc() + { + PartitionKey = "CoreTestPartitionKeyCollectionName"; + } + + public string PartitionKey { get; set; } + } + + public class CRUDPartitionedCollectionNameAttributeTests : MongoDbDocumentTestBase + { + public CRUDPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture) + { + + } + + public override string GetClassName() + { + return "CoreCRUDPartitionedCollectionNameAttributeTests"; + } + } +} diff --git a/CoreIntegrationTests/CRUDPartitionedTests.cs b/CoreIntegrationTests/CRUDPartitionedTests.cs new file mode 100644 index 0000000..7e0beb1 --- /dev/null +++ b/CoreIntegrationTests/CRUDPartitionedTests.cs @@ -0,0 +1,29 @@ +using CoreIntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using System; + +namespace CoreIntegrationTests +{ + public class CorePartitionedDoc : TestDoc, IPartitionedDocument + { + public CorePartitionedDoc() + { + PartitionKey = "CoreTestPartitionKey"; + } + + public string PartitionKey { get; set; } + } + + public class CRUDPartitionedTests : MongoDbDocumentTestBase + { + public CRUDPartitionedTests(MongoDbTestFixture fixture) : base(fixture) + { + + } + + public override string GetClassName() + { + return "CoreCRUDPartitionedTests"; + } + } +} diff --git a/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs b/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs new file mode 100644 index 0000000..71d982c --- /dev/null +++ b/CoreIntegrationTests/CRUDTKeyPartitionedCollectionNameAttributeTests.cs @@ -0,0 +1,31 @@ +using CoreIntegrationTests.Infrastructure; +using MongoDbGenericRepository.Attributes; +using MongoDbGenericRepository.Models; +using System; + +namespace CoreIntegrationTests +{ + [CollectionName("TestingCollectionNameAttributePartitionedTKey")] + public class CoreTKeyPartitionedCollectionNameDoc : TestDoc, IPartitionedDocument + { + public CoreTKeyPartitionedCollectionNameDoc() + { + PartitionKey = "CoreTestPartitionKey"; + } + + public string PartitionKey { get; set; } + } + + public class CRUDTKeyPartitionedCollectionNameAttributeTests : MongoDbTKeyDocumentTestBase + { + public CRUDTKeyPartitionedCollectionNameAttributeTests(MongoDbTestFixture fixture) : base(fixture) + { + + } + + public override string GetClassName() + { + return "CoreCRUDTKeyPartitionedCollectionNameAttributeTests"; + } + } +} diff --git a/CoreIntegrationTests/CRUDTKeyPartitionedTests.cs b/CoreIntegrationTests/CRUDTKeyPartitionedTests.cs new file mode 100644 index 0000000..b2cbe10 --- /dev/null +++ b/CoreIntegrationTests/CRUDTKeyPartitionedTests.cs @@ -0,0 +1,28 @@ +using CoreIntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using System; + +namespace CoreIntegrationTests +{ + public class CorePartitionedTKeyTestDocument : TestDoc, IPartitionedDocument + { + public CorePartitionedTKeyTestDocument() + { + PartitionKey = "CoreTestPartitionKey"; + } + public string PartitionKey { get; set; } + } + + public class CRUDTKeyPartitionedTests : MongoDbTKeyDocumentTestBase + { + public CRUDTKeyPartitionedTests(MongoDbTestFixture fixture) : base(fixture) + { + + } + + public override string GetClassName() + { + return "CoreCRUDTKeyPartitionedTests"; + } + } +} diff --git a/CoreIntegrationTests/CRUDTKeyTests.cs b/CoreIntegrationTests/CRUDTKeyTests.cs new file mode 100644 index 0000000..32c15a6 --- /dev/null +++ b/CoreIntegrationTests/CRUDTKeyTests.cs @@ -0,0 +1,22 @@ +using CoreIntegrationTests.Infrastructure; +using System; + +namespace CoreIntegrationTests +{ + public class CoreTKeyTestDocument : TestDoc + { + } + + public class CRUDTKeyTests : MongoDbTKeyDocumentTestBase + { + public CRUDTKeyTests(MongoDbTestFixture fixture) : base(fixture) + { + + } + + public override string GetClassName() + { + return "CreateTKeyTests"; + } + } +} diff --git a/CoreIntegrationTests/CRUDTests.cs b/CoreIntegrationTests/CRUDTests.cs new file mode 100644 index 0000000..bdc73f6 --- /dev/null +++ b/CoreIntegrationTests/CRUDTests.cs @@ -0,0 +1,22 @@ +using CoreIntegrationTests.Infrastructure; +using System; + +namespace CoreIntegrationTests +{ + public class CoreTestDocument : TestDoc + { + } + + public class CRUDTests : MongoDbDocumentTestBase + { + public CRUDTests(MongoDbTestFixture fixture) : base(fixture) + { + + } + + public override string GetClassName() + { + return "CRUDTests"; + } + } +} diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj index abf2b72..fdc9f8d 100644 --- a/CoreIntegrationTests/CoreIntegrationTests.csproj +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -9,13 +9,22 @@ - + + + + + + + ..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.6.1\System.Configuration.dll + + + Always diff --git a/CoreIntegrationTests/CreatePartitionedTests.cs b/CoreIntegrationTests/CreatePartitionedTests.cs deleted file mode 100644 index bfad21e..0000000 --- a/CoreIntegrationTests/CreatePartitionedTests.cs +++ /dev/null @@ -1,67 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class CreateTestsPartitionedDocument : PartitionedDocument - { - public CreateTestsPartitionedDocument() : base("TestPartitionKey") - { - Version = 1; - } - public string SomeContent { get; set; } - } - - public class CreatePartitionedTests : BaseMongoDbRepositoryTests - { - private void PartitionedAddOne() - { - // Arrange - var document = new CreateTestsPartitionedDocument(); - // Act - SUT.AddOne(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id, PartitionKey); - Xunit.Assert.Equal(1, count); - } - - [Fact] - public async Task PartitionedAddOneAsync() - { - // Arrange - var document = new CreateTestsPartitionedDocument(); - // Act - await SUT.AddOneAsync(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id, PartitionKey); - Assert.Equal(1, count); - } - - [Fact] - public void PartitionedAddMany() - { - // Arrange - var documents = new List { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() }; - // Act - SUT.AddMany(documents); - // Assert - long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey); - Assert.Equal(2, count); - } - - [Fact] - public async Task PartitionedAddManyAsync() - { - // Arrange - var documents = new List { new CreateTestsPartitionedDocument(), new CreateTestsPartitionedDocument() }; - // Act - await SUT.AddManyAsync(documents); - // Assert - long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id, PartitionKey); - Assert.Equal(2, count); - } - } -} diff --git a/CoreIntegrationTests/CreateTests.cs b/CoreIntegrationTests/CreateTests.cs deleted file mode 100644 index e54be04..0000000 --- a/CoreIntegrationTests/CreateTests.cs +++ /dev/null @@ -1,68 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System.Collections.Generic; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class CreateTestsDocument : Document - { - public CreateTestsDocument() - { - Version = 2; - } - public string SomeContent { get; set; } - } - - public class CreateTests : BaseMongoDbRepositoryTests - { - [Fact] - public void AddOne() - { - // Arrange - var document = new CreateTestsDocument(); - // Act - SUT.AddOne(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id); - Assert.Equal(1, count); - } - - [Fact] - public async Task AddOneAsync() - { - // Arrange - var document = new CreateTestsDocument(); - // Act - await SUT.AddOneAsync(document); - // Assert - long count = SUT.Count(e => e.Id == document.Id); - Assert.Equal(1, count); - } - - [Fact] - public void AddMany() - { - // Arrange - var documents = new List { new CreateTestsDocument(), new CreateTestsDocument() }; - // Act - SUT.AddMany(documents); - // Assert - long count = SUT.Count(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 { new CreateTestsDocument(), new CreateTestsDocument() }; - // Act - await SUT.AddManyAsync(documents); - // Assert - long count = SUT.Count(e => e.Id == documents[0].Id || e.Id == documents[1].Id); - Assert.Equal(2, count); - } - } -} diff --git a/CoreIntegrationTests/DeletePartitionedTests.cs b/CoreIntegrationTests/DeletePartitionedTests.cs deleted file mode 100644 index 486c462..0000000 --- a/CoreIntegrationTests/DeletePartitionedTests.cs +++ /dev/null @@ -1,129 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class DeleteTestsPartitionedDocument : PartitionedDocument - { - public DeleteTestsPartitionedDocument() : base("TestPartitionKey") - { - Version = 1; - } - public string SomeContent { get; set; } - } - - public class DeletePartitionedTests : BaseMongoDbRepositoryTests - { - [Fact] - public void PartitionedDeleteOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(document); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Fact] - public void PartitionedDeleteOneLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(e => e.Id == document.Id, PartitionKey); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Fact] - public async Task PartitionedDeleteOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(document); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Fact] - public async Task PartitionedDeleteOneAsyncLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(e => e.Id == document.Id, PartitionKey); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id, PartitionKey)); - } - - [Fact] - public async Task PartitionedDeleteManyAsyncLinq() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey)); - } - - [Fact] - public async Task PartitionedDeleteManyAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(documents); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent", PartitionKey)); - } - - [Fact] - public void PartitionedDeleteManyLinq() - { - // Arrange - var content = "DeleteManyLinqContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(e => e.SomeContent == content, PartitionKey); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == content, PartitionKey)); - } - - [Fact] - public void PartitionedDeleteMany() - { - // Arrange - var content = "DeleteManyContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(documents); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == content, PartitionKey)); - } - } -} diff --git a/CoreIntegrationTests/DeleteTests.cs b/CoreIntegrationTests/DeleteTests.cs deleted file mode 100644 index 249b1b7..0000000 --- a/CoreIntegrationTests/DeleteTests.cs +++ /dev/null @@ -1,129 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class DeleteTestsDocument : Document - { - public DeleteTestsDocument() - { - Version = 2; - } - public string SomeContent { get; set; } - } - - public class DeleteTests : BaseMongoDbRepositoryTests - { - [Fact] - public void DeleteOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(document); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id)); - } - - [Fact] - public void DeleteOneLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.DeleteOne(e => e.Id == document.Id); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id)); - } - - [Fact] - public async Task DeleteOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(document); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id)); - } - - [Fact] - public async Task DeleteOneAsyncLinq() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.DeleteOneAsync(e => e.Id == document.Id); - // Assert - Assert.Equal(1, result); - Assert.False(SUT.Any(e => e.Id == document.Id)); - } - - [Fact] - public async Task DeleteManyAsyncLinq() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(e => e.SomeContent == "DeleteManyAsyncLinqContent"); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent")); - } - - [Fact] - public async Task DeleteManyAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "DeleteManyAsyncLinqContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.DeleteManyAsync(documents); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == "DeleteManyAsyncLinqContent")); - } - - [Fact] - public void DeleteManyLinq() - { - // Arrange - var content = "DeleteManyLinqContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(e => e.SomeContent == content); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == content)); - } - - [Fact] - public void DeleteMany() - { - // Arrange - var content = "DeleteManyContent"; - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = content); - SUT.AddMany(documents); - // Act - var result = SUT.DeleteMany(documents); - // Assert - Assert.Equal(5, result); - Assert.False(SUT.Any(e => e.SomeContent == content)); - } - } -} diff --git a/CoreIntegrationTests/Infrastructure/GlobalVariables.cs b/CoreIntegrationTests/Infrastructure/GlobalVariables.cs new file mode 100644 index 0000000..907f944 --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/GlobalVariables.cs @@ -0,0 +1,15 @@ +using System; + +namespace CoreIntegrationTests.Infrastructure +{ + /// + /// A class holding global variables. + /// + public static class GlobalVariables + { + /// + /// A random number generator. + /// + public static Random Random = new Random(); + } +} diff --git a/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs new file mode 100644 index 0000000..be584e7 --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/MongoDbDocumentTestBase.cs @@ -0,0 +1,727 @@ +using MongoDbGenericRepository.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using Xunit; + +namespace CoreIntegrationTests.Infrastructure +{ + public abstract class MongoDbDocumentTestBase : + IClassFixture> + where T: TestDoc, new() + { + + private readonly MongoDbTestFixture _fixture; + + protected MongoDbDocumentTestBase(MongoDbTestFixture fixture) + { + _fixture = fixture; + var type = CreateTestDocument(); + DocumentTypeName = type.GetType().FullName; + if (type is IPartitionedDocument) + { + PartitionKey = ((IPartitionedDocument)type).PartitionKey; + } + _fixture.PartitionKey = PartitionKey; + TestClassName = GetClassName(); + MongoDbConfig.EnsureConfigured(); + SUT = TestRepository.Instance; + } + + protected T CreateTestDocument() + { + return _fixture.CreateTestDocument(); + } + + public abstract string GetClassName(); + + protected List CreateTestDocuments(int numberOfDocumentsToCreate) + { + return _fixture.CreateTestDocuments(numberOfDocumentsToCreate); + } + + /// + /// The partition key for the collection, if any + /// + protected string PartitionKey { get; set; } + + /// + /// the name of the test class + /// + protected string TestClassName { get; set; } + + /// + /// The name of the document used for tests + /// + protected string DocumentTypeName { get; set; } + + /// + /// SUT: System Under Test + /// + protected static ITestRepository SUT { get; set; } + + #region Add + + [Fact] + public void AddOne() + { + // Arrange + var document = new T(); + // Act + SUT.AddOne(document); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(document.Id)) + : SUT.Count(e => e.Id.Equals(document.Id), PartitionKey); + Assert.True(1 == count, GetTestName()); + } + + [Fact] + public async Task AddOneAsync() + { + // Arrange + var document = new T(); + // Act + await SUT.AddOneAsync(document); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(document.Id)) + : SUT.Count(e => e.Id.Equals(document.Id), PartitionKey); + Assert.True (1 == count, GetTestName()); + } + + [Fact] + public void AddMany() + { + // Arrange + var documents = new List { new T(), new T() }; + // Act + SUT.AddMany(documents); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id)) + : SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id), PartitionKey); + Assert.True (2 == count, GetTestName()); + } + + [Fact] + public async Task AddManyAsync() + { + // Arrange + var documents = new List { new T(), new T() }; + // Act + await SUT.AddManyAsync(documents); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id)) + : SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id), PartitionKey); + Assert.True (2 == count, GetTestName()); + } + + + #endregion Add + + #region Read + + [Fact] + public async Task GetByIdAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetByIdAsync(document.Id, PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public void GetById() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetById(document.Id, PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public async Task GetOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetOneAsync(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public void GetOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetOne(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public void GetCursor() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var cursor = SUT.GetCursor(x => x.Id.Equals(document.Id), PartitionKey); + var count = cursor.Count(); + // Assert + Assert.True (1 == count, GetTestName()); + } + + [Fact] + public async Task AnyAsyncReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(result, GetTestName()); + } + + [Fact] + public async Task AnyAsyncReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id.Equals(Guid.NewGuid()), PartitionKey); + // Assert + Assert.False(result, GetTestName()); + } + + [Fact] + public void AnyReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(result, GetTestName()); + } + + [Fact] + public void AnyReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id.Equals(Guid.NewGuid()), PartitionKey); + // Assert + Assert.False(result, GetTestName()); + } + + [Fact] + public async Task GetAllAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = await SUT.GetAllAsync(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + } + + [Fact] + public void GetAll() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = SUT.GetAll(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + } + + [Fact] + public async Task CountAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = await SUT.CountAsync(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result, GetTestName()); + } + + [Fact] + public void Count() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = SUT.Count(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result, GetTestName()); + } + + #endregion Read + + #region Update + + [Fact] + public void UpdateOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = SUT.UpdateOne(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = await SUT.UpdateOneAsync(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneField() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneFieldWithFilter() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldWithFilterAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsyncWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = await SUT.UpdateOneAsync(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + } + + [Fact] + public void UpdateOneWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = SUT.UpdateOne(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type== updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value== updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type== updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value== updatedDocument.Children[1].Value, GetTestName()); + } + + #endregion Update + + #region Delete + + [Fact] + public void DeleteOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.DeleteOne(document); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public void DeleteOneLinq() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.DeleteOne(e => e.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.DeleteOneAsync(document); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteOneAsyncLinq() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.DeleteOneAsync(e => e.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteManyAsyncLinq() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = await SUT.DeleteManyAsync(e => e.SomeContent == criteria, PartitionKey); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteManyAsync() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = await SUT.DeleteManyAsync(documents); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Fact] + public void DeleteManyLinq() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = SUT.DeleteMany(e => e.SomeContent == criteria, PartitionKey); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Fact] + public void DeleteMany() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = SUT.DeleteMany(documents); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + #endregion Delete + + #region Project + + [Fact] + public async Task ProjectOneAsync() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var document = CreateTestDocument(); + document.SomeContent = someContent; + document.Nested.SomeDate = someDate; + SUT.AddOne(document); + // Act + var result = await SUT.ProjectOneAsync( + x => x.Id.Equals(document.Id), + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + Assert.True(someContent == result.SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.SomeDate.Second, GetTestName()); + } + + [Fact] + public void ProjectOne() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var document = CreateTestDocument(); + document.SomeContent = someContent; + document.Nested.SomeDate = someDate; + SUT.AddOne(document); + // Act + var result = SUT.ProjectOne( + x => x.Id.Equals(document.Id), + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + Assert.True(someContent == result.SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.SomeDate.Second, GetTestName()); + } + + [Fact] + public async Task ProjectManyAsync() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var documents = CreateTestDocuments(5); + documents.ForEach(e => + { + e.SomeContent = someContent; + e.Nested.SomeDate = someDate; + }); + + SUT.AddMany(documents); + // Act + var result = await SUT.ProjectManyAsync( + x => x.SomeContent == someContent, + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + Assert.True(someContent == result.First().SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName()); + } + + [Fact] + public void ProjectMany() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var documents = CreateTestDocuments(5); + documents.ForEach(e => + { + e.SomeContent = someContent; + e.Nested.SomeDate = someDate; + }); + + SUT.AddMany(documents); + // Act + var result = SUT.ProjectMany( + x => x.SomeContent == someContent, + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + Assert.True(someContent == result.First().SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName()); + } + + #endregion Project + + #region Test Utils + [MethodImpl(MethodImplOptions.NoInlining)] + private string GetCurrentMethod() + { + StackTrace st = new StackTrace(); + StackFrame sf = st.GetFrame(1); + + return sf.GetMethod().Name; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private string GetParentMethod() + { + StackTrace st = new StackTrace(); + StackFrame sf = st.GetFrame(2); + var method = sf.GetMethod().DeclaringType.Name; + return method; + } + + private string GetTestName() + { + return $"{TestClassName}{PartitionKey}.{GetParentMethod()}"; + } + + private string GetContent() + { + return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}"; + } + + private void Cleanup() + { + // We drop the collection at the end of each test session. + if (!string.IsNullOrEmpty(PartitionKey)) + { + SUT.DropTestCollection(PartitionKey); + } + else + { + SUT.DropTestCollection(); + } + } + + #endregion Test Utils + + } +} diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs new file mode 100644 index 0000000..cb5888c --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs @@ -0,0 +1,728 @@ +using MongoDbGenericRepository.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading.Tasks; +using Xunit; + +namespace CoreIntegrationTests.Infrastructure +{ + public abstract class MongoDbTKeyDocumentTestBase : + IClassFixture> + where T: TestDoc, new() + where TKey : IEquatable + + { + private readonly MongoDbTestFixture _fixture; + + protected MongoDbTKeyDocumentTestBase(MongoDbTestFixture fixture) + { + _fixture = fixture; + var type = CreateTestDocument(); + DocumentTypeName = type.GetType().FullName; + if (type is IPartitionedDocument) + { + PartitionKey = ((IPartitionedDocument)type).PartitionKey; + } + _fixture.PartitionKey = PartitionKey; + TestClassName = GetClassName(); + MongoDbConfig.EnsureConfigured(); + SUT = TestRepository.Instance; + } + + public abstract string GetClassName(); + + public T CreateTestDocument() + { + return _fixture.CreateTestDocument(); + } + + public List CreateTestDocuments(int numberOfDocumentsToCreate) + { + return _fixture.CreateTestDocuments(numberOfDocumentsToCreate); + } + + /// + /// The partition key for the collection, if any + /// + protected string PartitionKey { get; set; } + + /// + /// the name of the test class + /// + protected string TestClassName { get; set; } + + /// + /// The name of the document used for tests + /// + protected string DocumentTypeName { get; set; } + + /// + /// SUT: System Under Test + /// + protected static ITestRepository SUT { get; set; } + + #region Add + + [Fact] + public void AddOne() + { + // Arrange + var document = new T(); + // Act + SUT.AddOne(document); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(document.Id)) + : SUT.Count(e => e.Id.Equals(document.Id), PartitionKey); + Assert.True(1 == count, GetTestName()); + } + + [Fact] + public async Task AddOneAsync() + { + // Arrange + var document = new T(); + // Act + await SUT.AddOneAsync(document); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(document.Id)) + : SUT.Count(e => e.Id.Equals(document.Id), PartitionKey); + Assert.True (1 == count, GetTestName()); + } + + [Fact] + public void AddMany() + { + // Arrange + var documents = new List { new T(), new T() }; + // Act + SUT.AddMany(documents); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id)) + : SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id), PartitionKey); + Assert.True (2 == count, GetTestName()); + } + + [Fact] + public async Task AddManyAsync() + { + // Arrange + var documents = new List { new T(), new T() }; + // Act + await SUT.AddManyAsync(documents); + // Assert + long count = string.IsNullOrEmpty(PartitionKey) ? SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id)) + : SUT.Count(e => e.Id.Equals(documents[0].Id) + || e.Id.Equals(documents[1].Id), PartitionKey); + Assert.True (2 == count, GetTestName()); + } + + + #endregion Add + + #region Read + + [Fact] + public async Task GetByIdAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetByIdAsync(document.Id, PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public void GetById() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetById(document.Id, PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public async Task GetOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetOneAsync(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public void GetOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetOne(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + } + + [Fact] + public void GetCursor() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var cursor = SUT.GetCursor(x => x.Id.Equals(document.Id), PartitionKey); + var count = cursor.Count(); + // Assert + Assert.True (1 == count, GetTestName()); + } + + [Fact] + public async Task AnyAsyncReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(result, GetTestName()); + } + + [Fact] + public async Task AnyAsyncReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id.Equals(document.Init()), PartitionKey); + // Assert + Assert.False(result, GetTestName()); + } + + [Fact] + public void AnyReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True(result, GetTestName()); + } + + [Fact] + public void AnyReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id.Equals(document.Init()), PartitionKey); + // Assert + Assert.False(result, GetTestName()); + } + + [Fact] + public async Task GetAllAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = await SUT.GetAllAsync(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + } + + [Fact] + public void GetAll() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = SUT.GetAll(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + } + + [Fact] + public async Task CountAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = await SUT.CountAsync(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result, GetTestName()); + } + + [Fact] + public void Count() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + documents.ForEach(e => e.SomeContent = content); + SUT.AddMany(documents); + // Act + var result = SUT.Count(x => x.SomeContent == content, PartitionKey); + // Assert + Assert.True(5 == result, GetTestName()); + } + + #endregion Read + + #region Update + + [Fact] + public void UpdateOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = SUT.UpdateOne(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + document.SomeContent = content; + // Act + var result = await SUT.UpdateOneAsync(document); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneField() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(document, x => x.SomeContent, content); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public void UpdateOneFieldWithFilter() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = SUT.UpdateOne(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneFieldWithFilterAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var content = GetContent(); + // Act + var result = await SUT.UpdateOneAsync(x => x.Id.Equals(document.Id), x => x.SomeContent, content, PartitionKey); + // Assert + Assert.True(result, GetTestName()); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument, GetTestName()); + Assert.True(content == updatedDocument.SomeContent, GetTestName()); + } + + [Fact] + public async Task UpdateOneAsyncWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = await SUT.UpdateOneAsync(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type == updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value == updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type == updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value == updatedDocument.Children[1].Value, GetTestName()); + } + + [Fact] + public void UpdateOneWithUpdateDefinition() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + var childrenToAdd = new List + { + new Child("testType1", "testValue1"), + new Child("testType2", "testValue2") + }; + + var updateDef = MongoDB.Driver.Builders.Update.AddToSetEach(p => p.Children, childrenToAdd); + + // Act + var result = SUT.UpdateOne(document, updateDef); + // Assert + Assert.True(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.True(null != updatedDocument); + Assert.True(childrenToAdd[0].Type== updatedDocument.Children[0].Type, GetTestName()); + Assert.True(childrenToAdd[0].Value== updatedDocument.Children[0].Value, GetTestName()); + Assert.True(childrenToAdd[1].Type== updatedDocument.Children[1].Type, GetTestName()); + Assert.True(childrenToAdd[1].Value== updatedDocument.Children[1].Value, GetTestName()); + } + + #endregion Update + + #region Delete + + [Fact] + public void DeleteOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.DeleteOne(document); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public void DeleteOneLinq() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.DeleteOne(e => e.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.DeleteOneAsync(document); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteOneAsyncLinq() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.DeleteOneAsync(e => e.Id.Equals(document.Id), PartitionKey); + // Assert + Assert.True (1 == result); + Assert.False(SUT.Any(e => e.Id.Equals(document.Id), PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteManyAsyncLinq() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = await SUT.DeleteManyAsync(e => e.SomeContent == criteria, PartitionKey); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Fact] + public async Task DeleteManyAsync() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = await SUT.DeleteManyAsync(documents); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Fact] + public void DeleteManyLinq() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = SUT.DeleteMany(e => e.SomeContent == criteria, PartitionKey); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + [Fact] + public void DeleteMany() + { + // Arrange + var criteria = $"{GetTestName()}.{DocumentTypeName}"; + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = criteria); + SUT.AddMany(documents); + // Act + var result = SUT.DeleteMany(documents); + // Assert + Assert.True(5 == result); + Assert.False(SUT.Any(e => e.SomeContent == criteria, PartitionKey), GetTestName()); + } + + #endregion Delete + + #region Project + + [Fact] + public async Task ProjectOneAsync() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var document = CreateTestDocument(); + document.SomeContent = someContent; + document.Nested.SomeDate = someDate; + SUT.AddOne(document); + // Act + var result = await SUT.ProjectOneAsync( + x => x.Id.Equals(document.Id), + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + Assert.True(someContent == result.SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.SomeDate.Second, GetTestName()); + } + + [Fact] + public void ProjectOne() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var document = CreateTestDocument(); + document.SomeContent = someContent; + document.Nested.SomeDate = someDate; + SUT.AddOne(document); + // Act + var result = SUT.ProjectOne( + x => x.Id.Equals(document.Id), + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(null != result, GetTestName()); + Assert.True(someContent == result.SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.SomeDate.Second, GetTestName()); + } + + [Fact] + public async Task ProjectManyAsync() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var documents = CreateTestDocuments(5); + documents.ForEach(e => + { + e.SomeContent = someContent; + e.Nested.SomeDate = someDate; + }); + + SUT.AddMany(documents); + // Act + var result = await SUT.ProjectManyAsync( + x => x.SomeContent == someContent, + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + Assert.True(someContent == result.First().SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName()); + } + + [Fact] + public void ProjectMany() + { + // Arrange + var someContent = GetContent(); + var someDate = DateTime.UtcNow; + var documents = CreateTestDocuments(5); + documents.ForEach(e => + { + e.SomeContent = someContent; + e.Nested.SomeDate = someDate; + }); + + SUT.AddMany(documents); + // Act + var result = SUT.ProjectMany( + x => x.SomeContent == someContent, + x => new MyTestProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.True(5 == result.Count, GetTestName()); + Assert.True(someContent == result.First().SomeContent, GetTestName()); + Assert.True(someDate.Minute == result.First().SomeDate.Minute, GetTestName()); + Assert.True(someDate.Second == result.First().SomeDate.Second, GetTestName()); + } + + #endregion Project + + #region Test Utils + [MethodImpl(MethodImplOptions.NoInlining)] + private string GetCurrentMethod() + { + StackTrace st = new StackTrace(); + StackFrame sf = st.GetFrame(1); + + return sf.GetMethod().Name; + } + + [MethodImpl(MethodImplOptions.NoInlining)] + private string GetParentMethod() + { + StackTrace st = new StackTrace(); + StackFrame sf = st.GetFrame(2); + var method = sf.GetMethod().DeclaringType.Name; + return method; + } + + private string GetTestName() + { + return $"{TestClassName}{PartitionKey}.{GetParentMethod()}"; + } + + private string GetContent() + { + return $"{TestClassName}{PartitionKey}.{Guid.NewGuid()}.{GetParentMethod()}"; + } + + private void Cleanup() + { + // We drop the collection at the end of each test session. + if (!string.IsNullOrEmpty(PartitionKey)) + { + SUT.DropTestCollection(PartitionKey); + } + else + { + SUT.DropTestCollection(); + } + } + + #endregion Test Utils + + } +} diff --git a/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs b/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs new file mode 100644 index 0000000..1b28bb1 --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/MongoDbTestFixture.cs @@ -0,0 +1,55 @@ +using MongoDbGenericRepository; +using MongoDbGenericRepository.Models; +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Linq; + + +namespace CoreIntegrationTests.Infrastructure +{ + + public class MongoDbTestFixture : IDisposable + where T : IDocument, new() + where TKey : IEquatable + { + + public IMongoDbContext Context; + + public MongoDbTestFixture() + { + DocsToDelete = new ConcurrentBag(); + } + + public string PartitionKey { get; set; } + + public ConcurrentBag DocsToDelete { get; set; } + + public virtual void Dispose() + { + var docIds = DocsToDelete.ToList().Select(e => e.Id); + if (docIds.Any()) + { + TestRepository.Instance.DeleteMany(e => docIds.Contains(e.Id)); + } + } + + public T CreateTestDocument() + { + var doc = new T(); + DocsToDelete.Add(doc); + return doc; + } + + public List CreateTestDocuments(int numberOfDocumentsToCreate) + { + var docs = new List(); + for (var i = 0; i < numberOfDocumentsToCreate; i++) + { + docs.Add(new T()); + DocsToDelete.Add(docs.Last()); + } + return docs; + } + } +} diff --git a/CoreIntegrationTests/Infrastructure/RandomExtensions.cs b/CoreIntegrationTests/Infrastructure/RandomExtensions.cs new file mode 100644 index 0000000..3b38c9f --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/RandomExtensions.cs @@ -0,0 +1,61 @@ +using System; + +namespace CoreIntegrationTests.Infrastructure +{ + // Thanks BlueRaja - Danny Pflughoeft https://stackoverflow.com/a/13095144/5103354 + /// + /// Extensions for the random number generator + /// + public static class RandomExtensions + { + + /// + /// Returns a random long from min (inclusive) to max (exclusive) + /// + /// The given random instance + /// The inclusive minimum bound + /// The exclusive maximum bound. Must be greater than min + public static long NextLong(this Random random, long min, long max) + { + if (max <= min) + throw new ArgumentOutOfRangeException("max", "max must be > min!"); + + //Working with ulong so that modulo works correctly with values > long.MaxValue + ulong uRange = (ulong)(max - min); + + //Prevent a modulo bias; see https://stackoverflow.com/a/10984975/238419 + //for more information. + //In the worst case, the expected number of calls is 2 (though usually it's + //much closer to 1) so this loop doesn't really hurt performance at all. + ulong ulongRand; + do + { + byte[] buf = new byte[8]; + random.NextBytes(buf); + ulongRand = (ulong)BitConverter.ToInt64(buf, 0); + } while (ulongRand > ulong.MaxValue - ((ulong.MaxValue % uRange) + 1) % uRange); + + return (long)(ulongRand % uRange) + min; + } + + /// + /// Returns a random long from 0 (inclusive) to max (exclusive) + /// + /// The given random instance + /// The exclusive maximum bound. Must be greater than 0 + public static long NextLong(this Random random, long max) + { + return random.NextLong(0, max); + } + + /// + /// Returns a random long over all possible values of long (except long.MaxValue, similar to + /// random.Next()) + /// + /// The given random instance + public static long NextLong(this Random random) + { + return random.NextLong(long.MinValue, long.MaxValue); + } + } +} diff --git a/CoreIntegrationTests/Infrastructure/TestClasses.cs b/CoreIntegrationTests/Infrastructure/TestClasses.cs new file mode 100644 index 0000000..4b27bc9 --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/TestClasses.cs @@ -0,0 +1,108 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using System; +using System.Collections.Generic; + +namespace CoreIntegrationTests.Infrastructure +{ + public class ProjectedGroup + { + public int Key { get; set; } + public List Content { get; set; } + } + + public class MyTestProjection + { + public string SomeContent { get; set; } + public DateTime SomeDate { get; set; } + } + + public class Nested + { + public DateTime SomeDate { get; set; } + } + + public class Child + { + public Child(string type, string value) + { + Type = type; + Value = value; + } + + public string Type { get; set; } + public string Value { get; set; } + } + + public class TestDoc : Document + { + public TestDoc() + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public string SomeContent { get; set; } + + public int GroupingKey { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } + + } + + public class TestDoc : IDocument + where TKey : IEquatable + { + [BsonId] + public TKey Id { get; set; } + public int Version { get; set; } + + public TestDoc() + { + InitializeFields(); + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public string SomeContent { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } + + public TId Init() + { + var idTypeName = typeof(TKey).Name; + switch (idTypeName) + { + case "Guid": + return (TId)(object)Guid.NewGuid(); + case "Int16": + return (TId)(object)GlobalVariables.Random.Next(1, short.MaxValue); + case "Int32": + return (TId)(object)GlobalVariables.Random.Next(1, int.MaxValue); + case "Int64": + return (TId)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue)); + case "String": + return (TId)(object)Guid.NewGuid().ToString(); + default: + throw new NotSupportedException($"{idTypeName} is not supported."); + } + } + + private void InitializeFields() + { + Id = Init(); + } + } +} diff --git a/CoreIntegrationTests/ProjectPartitionedTests.cs b/CoreIntegrationTests/ProjectPartitionedTests.cs deleted file mode 100644 index 4d0caac..0000000 --- a/CoreIntegrationTests/ProjectPartitionedTests.cs +++ /dev/null @@ -1,140 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System; -using System.Linq; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class ProjectTestsPartitionedDocument : PartitionedDocument - { - public ProjectTestsPartitionedDocument() : base("TestPartitionKey") - { - Version = 2; - Nested = new Nested - { - SomeDate = DateTime.UtcNow - }; - } - - public string SomeContent { get; set; } - - public Nested Nested { get; set; } - } - - public class ProjectPartitionedTests : BaseMongoDbRepositoryTests - { - [Fact] - public async Task PartitionedProjectOneAsync() - { - // Arrange - const string someContent = "ProjectOneAsyncContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocument(); - document.SomeContent = someContent; - document.Nested.SomeDate = someDate; - SUT.AddOne(document); - // Act - var result = await SUT.ProjectOneAsync( - x => x.Id == document.Id, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }, - PartitionKey); - // Assert - Assert.NotNull(result); - Assert.Equal(someContent, result.SomeContent); - Assert.Equal(someDate.Minute, result.SomeDate.Minute); - Assert.Equal(someDate.Second, result.SomeDate.Second); - } - - [Fact] - public void PartitionedProjectOne() - { - // Arrange - const string someContent = "ProjectOneContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocument(); - document.SomeContent = someContent; - document.Nested.SomeDate = someDate; - SUT.AddOne(document); - // Act - var result = SUT.ProjectOne( - x => x.Id == document.Id, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }, - PartitionKey); - // Assert - Assert.NotNull(result); - Assert.Equal(someContent, result.SomeContent); - Assert.Equal(someDate.Minute, result.SomeDate.Minute); - Assert.Equal(someDate.Second, result.SomeDate.Second); - } - - [Fact] - public async Task PartitionedProjectManyAsync() - { - // Arrange - const string someContent = "ProjectManyAsyncContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocuments(5); - document.ForEach(e => - { - e.SomeContent = someContent; - e.Nested.SomeDate = someDate; - }); - - SUT.AddMany(document); - // Act - var result = await SUT.ProjectManyAsync( - x => x.SomeContent == someContent, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }, - PartitionKey); - // Assert - Assert.Equal(5, result.Count); - Assert.Equal(someContent, result.First().SomeContent); - Assert.Equal(someDate.Minute, result.First().SomeDate.Minute); - Assert.Equal(someDate.Second, result.First().SomeDate.Second); - } - - [Fact] - public void PartitionedProjectMany() - { - // Arrange - const string someContent = "ProjectManyContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocuments(5); - document.ForEach(e => - { - e.SomeContent = someContent; - e.Nested.SomeDate = someDate; - }); - - SUT.AddMany(document); - // Act - var result = SUT.ProjectMany( - x => x.SomeContent == someContent, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }, - PartitionKey); - // Assert - Assert.Equal(5, result.Count); - Assert.Equal(someContent, result.First().SomeContent); - Assert.Equal(someDate.Minute, result.First().SomeDate.Minute); - Assert.Equal(someDate.Second, result.First().SomeDate.Second); - } - } -} diff --git a/CoreIntegrationTests/ProjectTests.cs b/CoreIntegrationTests/ProjectTests.cs deleted file mode 100644 index fc30c46..0000000 --- a/CoreIntegrationTests/ProjectTests.cs +++ /dev/null @@ -1,149 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System; -using System.Linq; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class Nested - { - public DateTime SomeDate { get; set; } - } - - public class MyProjection - { - public DateTime SomeDate { get; set; } - public string SomeContent { get; set; } - } - - public class ProjectTestsDocument : Document - { - public ProjectTestsDocument() - { - Version = 2; - Nested = new Nested - { - SomeDate = DateTime.UtcNow - }; - } - - public string SomeContent { get; set; } - - public Nested Nested { get; set; } - } - - public class ProjectTests : BaseMongoDbRepositoryTests - { - - - [Fact] - public async Task ProjectOneAsync() - { - // Arrange - const string someContent = "ProjectOneAsyncContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocument(); - document.SomeContent = someContent; - document.Nested.SomeDate = someDate; - SUT.AddOne(document); - // Act - var result = await SUT.ProjectOneAsync( - x => x.Id == document.Id, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }); - // Assert - Assert.NotNull(result); - Assert.Equal(someContent, result.SomeContent); - Assert.Equal(someDate.Minute, result.SomeDate.Minute); - Assert.Equal(someDate.Second, result.SomeDate.Second); - } - - [Fact] - public void ProjectOne() - { - // Arrange - const string someContent = "ProjectOneContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocument(); - document.SomeContent = someContent; - document.Nested.SomeDate = someDate; - SUT.AddOne(document); - // Act - var result = SUT.ProjectOne( - x => x.Id == document.Id, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }); - // Assert - Assert.NotNull(result); - Assert.Equal(someContent, result.SomeContent); - Assert.Equal(someDate.Minute, result.SomeDate.Minute); - Assert.Equal(someDate.Second, result.SomeDate.Second); - } - - [Fact] - public async Task ProjectManyAsync() - { - // Arrange - const string someContent = "ProjectManyAsyncContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocuments(5); - document.ForEach(e => - { - e.SomeContent = someContent; - e.Nested.SomeDate = someDate; - }); - - SUT.AddMany(document); - // Act - var result = await SUT.ProjectManyAsync( - x => x.SomeContent == someContent, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }); - // Assert - Assert.Equal(5, result.Count); - Assert.Equal(someContent, result.First().SomeContent); - Assert.Equal(someDate.Minute, result.First().SomeDate.Minute); - Assert.Equal(someDate.Second, result.First().SomeDate.Second); - } - - [Fact] - public void ProjectMany() - { - // Arrange - const string someContent = "ProjectManyContent"; - var someDate = DateTime.UtcNow; - var document = CreateTestDocuments(5); - document.ForEach(e => - { - e.SomeContent = someContent; - e.Nested.SomeDate = someDate; - }); - - SUT.AddMany(document); - // Act - var result = SUT.ProjectMany( - x => x.SomeContent == someContent, - x => new MyProjection - { - SomeContent = x.SomeContent, - SomeDate = x.Nested.SomeDate - }); - // Assert - Assert.Equal(5, result.Count); - Assert.Equal(someContent, result.First().SomeContent); - Assert.Equal(someDate.Minute, result.First().SomeDate.Minute); - Assert.Equal(someDate.Second, result.First().SomeDate.Second); - } - } -} diff --git a/CoreIntegrationTests/ReadPartitionedTests.cs b/CoreIntegrationTests/ReadPartitionedTests.cs deleted file mode 100644 index b9e8c05..0000000 --- a/CoreIntegrationTests/ReadPartitionedTests.cs +++ /dev/null @@ -1,181 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class ReadTestsPartitionedDocument : PartitionedDocument - { - public ReadTestsPartitionedDocument() : base("TestPartitionKey") - { - Version = 1; - } - public string SomeContent { get; set; } - } - - public class ReadPartitionedTests : BaseMongoDbRepositoryTests - { - [Fact] - public async Task PartitionedGetByIdAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.GetByIdAsync(document.Id, PartitionKey); - // Assert - Assert.NotNull(result); - } - - [Fact] - public void PartitionedGetById() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.GetById(document.Id, PartitionKey); - // Assert - Assert.NotNull(result); - } - - [Fact] - public async Task PartitionedGetOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.GetOneAsync(x => x.Id == document.Id, PartitionKey); - // Assert - Assert.NotNull(result); - } - - [Fact] - public void PartitionedGetOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.GetOne(x => x.Id == document.Id, PartitionKey); - // Assert - Assert.NotNull(result); - } - - [Fact] - public void PartitionedGetCursor() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var cursor = SUT.GetCursor(x => x.Id == document.Id, PartitionKey); - var count = cursor.Count(); - // Assert - Assert.Equal(1, count); - } - - [Fact] - public async Task PartitionedAnyAsyncReturnsTrue() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.AnyAsync(x => x.Id == document.Id, PartitionKey); - // Assert - Assert.True(result); - } - - [Fact] - public async Task PartitionedAnyAsyncReturnsFalse() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.AnyAsync(x => x.Id == Guid.NewGuid(), PartitionKey); - // Assert - Assert.False(result); - } - - [Fact] - public void PartitionedAnyReturnsTrue() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.Any(x => x.Id == document.Id, PartitionKey); - // Assert - Assert.True(result); - } - - [Fact] - public void PartitionedAnyReturnsFalse() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.Any(x => x.Id == Guid.NewGuid(), PartitionKey); - // Assert - Assert.False(result); - } - - [Fact] - public async Task PartitionedGetAllAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "GetAllAsyncContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.GetAllAsync(x => x.SomeContent == "GetAllAsyncContent", PartitionKey); - // Assert - Assert.Equal(5, result.Count); - } - - [Fact] - public void PartitionedGetAll() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "GetAllContent"); - SUT.AddMany(documents); - // Act - var result = SUT.GetAll(x => x.SomeContent == "GetAllContent", PartitionKey); - // Assert - Assert.Equal(5, result.Count); - } - - [Fact] - public async Task PartitionedCountAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "CountAsyncContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.CountAsync(x => x.SomeContent == "CountAsyncContent", PartitionKey); - // Assert - Assert.Equal(5, result); - } - - [Fact] - public void PartitionedCount() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "CountContent"); - SUT.AddMany(documents); - // Act - var result = SUT.Count(x => x.SomeContent == "CountContent", PartitionKey); - // Assert - Assert.Equal(5, result); - } - } -} diff --git a/CoreIntegrationTests/ReadTests.cs b/CoreIntegrationTests/ReadTests.cs deleted file mode 100644 index 66ccea7..0000000 --- a/CoreIntegrationTests/ReadTests.cs +++ /dev/null @@ -1,181 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class ReadTestsDocument : Document - { - public ReadTestsDocument() - { - Version = 2; - } - public string SomeContent { get; set; } - } - - public class ReadTests : BaseMongoDbRepositoryTests - { - [Fact] - public async Task GetByIdAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.GetByIdAsync(document.Id); - // Assert - Assert.NotNull(result); - } - - [Fact] - public void GetById() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.GetById(document.Id); - // Assert - Assert.NotNull(result); - } - - [Fact] - public async Task GetOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.GetOneAsync(x => x.Id == document.Id); - // Assert - Assert.NotNull(result); - } - - [Fact] - public void GetOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.GetOne(x => x.Id == document.Id); - // Assert - Assert.NotNull(result); - } - - [Fact] - public void GetCursor() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var cursor = SUT.GetCursor(x => x.Id == document.Id); - var count = cursor.Count(); - // Assert - Assert.Equal(1, count); - } - - [Fact] - public async Task AnyAsyncReturnsTrue() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.AnyAsync(x => x.Id == document.Id); - // Assert - Assert.True(result); - } - - [Fact] - public async Task AnyAsyncReturnsFalse() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = await SUT.AnyAsync(x => x.Id == Guid.NewGuid()); - // Assert - Assert.False(result); - } - - [Fact] - public void AnyReturnsTrue() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.Any(x => x.Id == document.Id); - // Assert - Assert.True(result); - } - - [Fact] - public void AnyReturnsFalse() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - // Act - var result = SUT.Any(x => x.Id == Guid.NewGuid()); - // Assert - Assert.False(result); - } - - [Fact] - public async Task GetAllAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "GetAllAsyncContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.GetAllAsync(x => x.SomeContent == "GetAllAsyncContent"); - // Assert - Assert.Equal(5, result.Count); - } - - [Fact] - public void GetAll() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "GetAllContent"); - SUT.AddMany(documents); - // Act - var result = SUT.GetAll(x => x.SomeContent == "GetAllContent"); - // Assert - Assert.Equal(5, result.Count); - } - - [Fact] - public async Task CountAsync() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "CountAsyncContent"); - SUT.AddMany(documents); - // Act - var result = await SUT.CountAsync(x => x.SomeContent == "CountAsyncContent"); - // Assert - Assert.Equal(5, result); - } - - [Fact] - public void Count() - { - // Arrange - var documents = CreateTestDocuments(5); - documents.ForEach(e => e.SomeContent = "CountContent"); - SUT.AddMany(documents); - // Act - var result = SUT.Count(x => x.SomeContent == "CountContent"); - // Assert - Assert.Equal(5, result); - } - } -} diff --git a/CoreIntegrationTests/UpdatePartitionedTests.cs b/CoreIntegrationTests/UpdatePartitionedTests.cs deleted file mode 100644 index 65658b6..0000000 --- a/CoreIntegrationTests/UpdatePartitionedTests.cs +++ /dev/null @@ -1,51 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class UpdateTestsPartitionedDocument : PartitionedDocument - { - public UpdateTestsPartitionedDocument() : base("TestPartitionKey") - { - Version = 2; - } - public string SomeContent { get; set; } - } - - public class UpdatePartitionedTests : BaseMongoDbRepositoryTests - { - [Fact] - public void PartitionedUpdateOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - document.SomeContent = "UpdateOneContent"; - // Act - var result = SUT.UpdateOne(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.NotNull(updatedDocument); - Assert.Equal("UpdateOneContent", updatedDocument.SomeContent); - } - - [Fact] - public async Task PartitionedUpdateOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - document.SomeContent = "UpdateOneAsyncContent"; - // Act - var result = await SUT.UpdateOneAsync(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id, PartitionKey); - Assert.NotNull(updatedDocument); - Assert.Equal("UpdateOneAsyncContent", updatedDocument.SomeContent); - } - } -} diff --git a/CoreIntegrationTests/UpdateTests.cs b/CoreIntegrationTests/UpdateTests.cs deleted file mode 100644 index f8bd2da..0000000 --- a/CoreIntegrationTests/UpdateTests.cs +++ /dev/null @@ -1,51 +0,0 @@ -using CoreIntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using Xunit; -using System.Threading.Tasks; - -namespace CoreIntegrationTests -{ - public class UpdateTestsDocument : Document - { - public UpdateTestsDocument() - { - Version = 2; - } - public string SomeContent { get; set; } - } - - public class UpdateTests : BaseMongoDbRepositoryTests - { - [Fact] - public void UpdateOne() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - document.SomeContent = "UpdateOneContent"; - // Act - var result = SUT.UpdateOne(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id); - Assert.NotNull(updatedDocument); - Assert.Equal("UpdateOneContent", updatedDocument.SomeContent); - } - - [Fact] - public async Task UpdateOneAsync() - { - // Arrange - var document = CreateTestDocument(); - SUT.AddOne(document); - document.SomeContent = "UpdateOneAsyncContent"; - // Act - var result = await SUT.UpdateOneAsync(document); - // Assert - Assert.True(result); - var updatedDocument = SUT.GetById(document.Id); - Assert.NotNull(updatedDocument); - Assert.Equal("UpdateOneAsyncContent", updatedDocument.SomeContent); - } - } -} diff --git a/IntegrationTests/GroupTests/GroupingTests.cs b/IntegrationTests/GroupTests/GroupingTests.cs deleted file mode 100644 index 751af45..0000000 --- a/IntegrationTests/GroupTests/GroupingTests.cs +++ /dev/null @@ -1,108 +0,0 @@ -using IntegrationTests.Infrastructure; -using MongoDbGenericRepository.Models; -using NUnit.Framework; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace IntegrationTests.GroupTests -{ - public class GroupingTestsDocument : Document - { - public GroupingTestsDocument() - { - Version = 2; - Children = new List(); - } - public string SomeContent { get; set; } - public int GroupingKey { get; set; } - public List Children { get; set; } - } - - public class ProjectedGroup - { - public int Key { get; set; } - public List Content { get; set; } - } - - public class GroupingTests : BaseMongoDbRepositoryTests - { - [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( - 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 { - new Child(guid1, guid2) - }; - } - - SUT.AddMany(documents); - - // Act - var result = SUT.GroupBy( - 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); - } - } -} diff --git a/IntegrationTests/Infrastructure/Child.cs b/IntegrationTests/Infrastructure/Child.cs deleted file mode 100644 index cf9d1ec..0000000 --- a/IntegrationTests/Infrastructure/Child.cs +++ /dev/null @@ -1,14 +0,0 @@ -namespace IntegrationTests.Infrastructure -{ - public class Child - { - public Child(string type, string value) - { - Type = type; - Value = value; - } - - public string Type { get; set; } - public string Value { get; set; } - } -} diff --git a/IntegrationTests/Infrastructure/MongoDBDocumentTestBase.cs b/IntegrationTests/Infrastructure/MongoDBDocumentTestBase.cs index d50463a..ebd66e2 100644 --- a/IntegrationTests/Infrastructure/MongoDBDocumentTestBase.cs +++ b/IntegrationTests/Infrastructure/MongoDBDocumentTestBase.cs @@ -10,37 +10,6 @@ using System.Threading.Tasks; namespace IntegrationTests.Infrastructure { - public class MyTestProjection - { - public string SomeContent { get; set; } - public DateTime SomeDate { get; set; } - } - - public class TestDoc : Document - { - public TestDoc() - { - Version = 2; - Nested = new Nested - { - SomeDate = DateTime.UtcNow - }; - Children = new List(); - } - - public string SomeContent { get; set; } - - public Nested Nested { get; set; } - - public List Children { get; set; } - - } - - public class Nested - { - public DateTime SomeDate { get; set; } - } - [TestFixture] public abstract class MongoDbDocumentTestBase where T: TestDoc, new() @@ -730,6 +699,91 @@ namespace IntegrationTests.Infrastructure #endregion Project + #region Group By + + [Test] + public void GroupByTProjection() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + for (var i = 0; i < documents.Count - 2; i++) + { + documents[i].GroupingKey = 1; + documents[i].SomeContent = $"{content}-{i}"; + } + for (var i = 3; i < documents.Count; i++) + { + documents[i].GroupingKey = 2; + documents[i].SomeContent = $"{content}-{i}"; + } + SUT.AddMany(documents); + + // Act + var result = SUT.GroupBy( + e => e.GroupingKey, g => new ProjectedGroup + { + Key = g.Key, + Content = g.Select(doc => doc.SomeContent).ToList() + }, + PartitionKey); + + // Assert + var key1Group = result.First(e => e.Key == 1); + Assert.NotNull(key1Group); + Assert.AreEqual(3, key1Group.Content.Count); + var key2Group = result.First(e => e.Key == 2); + Assert.NotNull(key2Group); + Assert.AreEqual(2, key2Group.Content.Count); + } + + [Test] + public void FilteredGroupByTProjection() + { + // Arrange + var documents = CreateTestDocuments(5); + var content = GetContent(); + for (var i = 0; i < documents.Count - 2; i++) + { + documents[i].GroupingKey = 4; + documents[i].SomeContent = $"{content}-{i}"; + } + for (var i = 3; i < documents.Count; i++) + { + documents[i].GroupingKey = 5; + documents[i].SomeContent = $"{content}-{i}"; + } + var guid1 = Guid.NewGuid().ToString("n"); + var guid2 = Guid.NewGuid().ToString("n"); + for (var i = 0; i < documents.Count - 1; i++) + { + documents[i].Children = new List { + new Child(guid1, guid2) + }; + } + + SUT.AddMany(documents); + + // Act + var result = SUT.GroupBy( + e => e.Children.Any(c => c.Type == guid1), + e => e.GroupingKey, g => new ProjectedGroup + { + Key = g.Key, + Content = g.Select(doc => doc.SomeContent).ToList() + }, PartitionKey); + + // Assert + var key1Group = result.First(e => e.Key == 4); + Assert.NotNull(key1Group); + Assert.AreEqual(3, key1Group.Content.Count); + var key2Group = result.First(e => e.Key == 5); + Assert.NotNull(key2Group); + Assert.AreEqual(1, key2Group.Content.Count); + } + + #endregion Group By + #region Test Utils [MethodImpl(MethodImplOptions.NoInlining)] diff --git a/IntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs b/IntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs index 01605b5..3d0cf8c 100644 --- a/IntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs +++ b/IntegrationTests/Infrastructure/MongoDbTKeyDocumentTestBase.cs @@ -11,57 +11,6 @@ using System.Threading.Tasks; namespace IntegrationTests.Infrastructure { - public class TestDoc : IDocument - where TKey : IEquatable - { - [BsonId] - public TKey Id { get; set; } - public int Version { get; set; } - - public TestDoc() - { - InitializeFields(); - Version = 2; - Nested = new Nested - { - SomeDate = DateTime.UtcNow - }; - Children = new List(); - } - - public string SomeContent { get; set; } - - public Nested Nested { get; set; } - - public List Children { get; set; } - - public TId Init() - { - var idTypeName = typeof(TKey).Name; - switch (idTypeName) - { - case "Guid": - return (TId)(object)Guid.NewGuid(); - case "Int16": - return (TId)(object)GlobalVariables.Random.Next(1, short.MaxValue); - case "Int32": - return (TId)(object)GlobalVariables.Random.Next(1, int.MaxValue); - case "Int64": - return (TId)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue)); - case "String": - return (TId)(object)Guid.NewGuid().ToString(); - default: - throw new NotSupportedException($"{idTypeName} is not supported."); - } - } - - private void InitializeFields() - { - Id = Init(); - } - } - - [TestFixture] public abstract class MongoDbTKeyDocumentTestBase where T: TestDoc, new() diff --git a/IntegrationTests/Infrastructure/TestClasses.cs b/IntegrationTests/Infrastructure/TestClasses.cs new file mode 100644 index 0000000..9d2ec7f --- /dev/null +++ b/IntegrationTests/Infrastructure/TestClasses.cs @@ -0,0 +1,108 @@ +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using System; +using System.Collections.Generic; + +namespace IntegrationTests.Infrastructure +{ + public class ProjectedGroup + { + public int Key { get; set; } + public List Content { get; set; } + } + + public class MyTestProjection + { + public string SomeContent { get; set; } + public DateTime SomeDate { get; set; } + } + + public class Nested + { + public DateTime SomeDate { get; set; } + } + + public class Child + { + public Child(string type, string value) + { + Type = type; + Value = value; + } + + public string Type { get; set; } + public string Value { get; set; } + } + + public class TestDoc : Document + { + public TestDoc() + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public string SomeContent { get; set; } + + public int GroupingKey { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } + + } + + public class TestDoc : IDocument + where TKey : IEquatable + { + [BsonId] + public TKey Id { get; set; } + public int Version { get; set; } + + public TestDoc() + { + InitializeFields(); + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + Children = new List(); + } + + public string SomeContent { get; set; } + + public Nested Nested { get; set; } + + public List Children { get; set; } + + public TId Init() + { + var idTypeName = typeof(TKey).Name; + switch (idTypeName) + { + case "Guid": + return (TId)(object)Guid.NewGuid(); + case "Int16": + return (TId)(object)GlobalVariables.Random.Next(1, short.MaxValue); + case "Int32": + return (TId)(object)GlobalVariables.Random.Next(1, int.MaxValue); + case "Int64": + return (TId)(object)(GlobalVariables.Random.NextLong(1, long.MaxValue)); + case "String": + return (TId)(object)Guid.NewGuid().ToString(); + default: + throw new NotSupportedException($"{idTypeName} is not supported."); + } + } + + private void InitializeFields() + { + Id = Init(); + } + } +} diff --git a/IntegrationTests/IntegrationTests.csproj b/IntegrationTests/IntegrationTests.csproj index 41ce31e..586079d 100644 --- a/IntegrationTests/IntegrationTests.csproj +++ b/IntegrationTests/IntegrationTests.csproj @@ -54,14 +54,13 @@ - - +