From 54efe5e520fe239ed095f011d281cadccc87689a Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Sat, 9 Sep 2017 17:52:12 +0000 Subject: [PATCH] .NET Core tests with xunit --- CoreIntegrationTests/App.config | 10 + .../CoreIntegrationTests.csproj | 27 +++ .../CreatePartitionedTests.cs | 67 +++++++ CoreIntegrationTests/CreateTests.cs | 68 +++++++ .../DeletePartitionedTests.cs | 129 +++++++++++++ CoreIntegrationTests/DeleteTests.cs | 129 +++++++++++++ .../BaseMongoDbRepositoryTests.cs | 89 +++++++++ .../Infrastructure/ITestRepository.cs | 10 + .../Infrastructure/TestRepository.cs | 43 +++++ .../ProjectPartitionedTests.cs | 140 ++++++++++++++ CoreIntegrationTests/ProjectTests.cs | 149 ++++++++++++++ CoreIntegrationTests/ReadPartitionedTests.cs | 181 ++++++++++++++++++ CoreIntegrationTests/ReadTests.cs | 181 ++++++++++++++++++ .../UpdatePartitionedTests.cs | 51 +++++ CoreIntegrationTests/UpdateTests.cs | 51 +++++ MongoDbGenericRepository.sln | 12 +- 16 files changed, 1332 insertions(+), 5 deletions(-) create mode 100644 CoreIntegrationTests/App.config create mode 100644 CoreIntegrationTests/CoreIntegrationTests.csproj create mode 100644 CoreIntegrationTests/CreatePartitionedTests.cs create mode 100644 CoreIntegrationTests/CreateTests.cs create mode 100644 CoreIntegrationTests/DeletePartitionedTests.cs create mode 100644 CoreIntegrationTests/DeleteTests.cs create mode 100644 CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs create mode 100644 CoreIntegrationTests/Infrastructure/ITestRepository.cs create mode 100644 CoreIntegrationTests/Infrastructure/TestRepository.cs create mode 100644 CoreIntegrationTests/ProjectPartitionedTests.cs create mode 100644 CoreIntegrationTests/ProjectTests.cs create mode 100644 CoreIntegrationTests/ReadPartitionedTests.cs create mode 100644 CoreIntegrationTests/ReadTests.cs create mode 100644 CoreIntegrationTests/UpdatePartitionedTests.cs create mode 100644 CoreIntegrationTests/UpdateTests.cs diff --git a/CoreIntegrationTests/App.config b/CoreIntegrationTests/App.config new file mode 100644 index 0000000..139995c --- /dev/null +++ b/CoreIntegrationTests/App.config @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/CoreIntegrationTests/CoreIntegrationTests.csproj b/CoreIntegrationTests/CoreIntegrationTests.csproj new file mode 100644 index 0000000..0ed4f08 --- /dev/null +++ b/CoreIntegrationTests/CoreIntegrationTests.csproj @@ -0,0 +1,27 @@ + + + + netcoreapp2.0 + + + + + + + + + + + + + + + + + + + Always + + + + diff --git a/CoreIntegrationTests/CreatePartitionedTests.cs b/CoreIntegrationTests/CreatePartitionedTests.cs new file mode 100644 index 0000000..5f11707 --- /dev/null +++ b/CoreIntegrationTests/CreatePartitionedTests.cs @@ -0,0 +1,67 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class CreateTestsPartitionedDocument : PartitionedDocument + { + public CreateTestsPartitionedDocument() : base("TestPartitionKey") + { + Version = 1; + } + public string SomeContent { get; set; } + } + + public class CreatePartitionedTests : BaseMongoDbRepositoryTests + { + 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 new file mode 100644 index 0000000..1437d87 --- /dev/null +++ b/CoreIntegrationTests/CreateTests.cs @@ -0,0 +1,68 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System.Collections.Generic; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class CreateTestsDocument : Document + { + public CreateTestsDocument() + { + Version = 2; + } + public string SomeContent { get; set; } + } + + public class CreateTests : BaseMongoDbRepositoryTests + { + [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 new file mode 100644 index 0000000..823f853 --- /dev/null +++ b/CoreIntegrationTests/DeletePartitionedTests.cs @@ -0,0 +1,129 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class DeleteTestsPartitionedDocument : PartitionedDocument + { + public DeleteTestsPartitionedDocument() : base("TestPartitionKey") + { + Version = 1; + } + public string SomeContent { get; set; } + } + + public class DeletePartitionedTests : BaseMongoDbRepositoryTests + { + [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 new file mode 100644 index 0000000..4db073f --- /dev/null +++ b/CoreIntegrationTests/DeleteTests.cs @@ -0,0 +1,129 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class DeleteTestsDocument : Document + { + public DeleteTestsDocument() + { + Version = 2; + } + public string SomeContent { get; set; } + } + + public class DeleteTests : BaseMongoDbRepositoryTests + { + [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/BaseMongoDbRepositoryTests.cs b/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs new file mode 100644 index 0000000..a4cdaa3 --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs @@ -0,0 +1,89 @@ +using MongoDbGenericRepository.Models; +using Xunit; +using System.Collections.Generic; +using System.Configuration; +using System; + +namespace IntegrationTests.Infrastructure +{ + + public class BaseMongoDbRepositoryTests : IDisposable where T : Document, new() + { + public T CreateTestDocument() + { + return new T(); + } + + public List CreateTestDocuments(int numberOfDocumentsToCreate) + { + var docs = new List(); + for(var i = 0; i < numberOfDocumentsToCreate; i++) + { + docs.Add(new T()); + } + return docs; + } + + /// + /// Constructor, init code + /// + public BaseMongoDbRepositoryTests() + { + Init(); + var type = CreateTestDocument(); + if (type is IPartitionedDocument) + { + PartitionKey = ((IPartitionedDocument)type).PartitionKey; + } + } + + public string PartitionKey { get; set; } + + /// + /// SUT: System Under Test + /// + protected static ITestRepository SUT { get; set; } + + public void Init() + { + SUT = TestRepository.Instance; + } + + public void Cleanup() + { + // We drop the collection at the end of each test session. + if (!string.IsNullOrEmpty(PartitionKey)) + { + SUT.DropTestCollection(PartitionKey); + } + else + { + SUT.DropTestCollection(); + } + } + + #region IDisposable Support + private bool disposedValue = false; // Pour détecter les appels redondants + + protected virtual void Dispose(bool disposing) + { + if (!disposedValue) + { + if (disposing) + { + Cleanup(); + } + + disposedValue = true; + } + } + + // Ce code est ajouté pour implémenter correctement le modèle supprimable. + public void Dispose() + { + // Ne modifiez pas ce code. Placez le code de nettoyage dans Dispose(bool disposing) ci-dessus. + Dispose(true); + } + #endregion + } +} diff --git a/CoreIntegrationTests/Infrastructure/ITestRepository.cs b/CoreIntegrationTests/Infrastructure/ITestRepository.cs new file mode 100644 index 0000000..c2d672c --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/ITestRepository.cs @@ -0,0 +1,10 @@ +using MongoDbGenericRepository; + +namespace IntegrationTests +{ + public interface ITestRepository : IBaseMongoRepository + { + void DropTestCollection(); + void DropTestCollection(string partitionKey); + } +} \ No newline at end of file diff --git a/CoreIntegrationTests/Infrastructure/TestRepository.cs b/CoreIntegrationTests/Infrastructure/TestRepository.cs new file mode 100644 index 0000000..aaec153 --- /dev/null +++ b/CoreIntegrationTests/Infrastructure/TestRepository.cs @@ -0,0 +1,43 @@ +using MongoDbGenericRepository; + +namespace IntegrationTests.Infrastructure +{ + /// + /// A singleton implementation of the TestRepository + /// + public sealed class TestRepository : BaseMongoRepository, ITestRepository + { + + const string connectionString = "mongodb://localhost:27017"; + private static readonly ITestRepository instance = new TestRepository(connectionString, "MongoDbTests"); + + // Explicit static constructor to tell C# compiler + // not to mark type as beforefieldinit + static TestRepository() + { + } + + /// + private TestRepository(string connectionString, string databaseName) : base(connectionString, databaseName) + { + } + + public static ITestRepository Instance + { + get + { + return instance; + } + } + + public void DropTestCollection() + { + MongoDbContext.DropCollection(); + } + + public void DropTestCollection(string partitionKey) + { + MongoDbContext.DropCollection(partitionKey); + } + } +} diff --git a/CoreIntegrationTests/ProjectPartitionedTests.cs b/CoreIntegrationTests/ProjectPartitionedTests.cs new file mode 100644 index 0000000..c3bb73e --- /dev/null +++ b/CoreIntegrationTests/ProjectPartitionedTests.cs @@ -0,0 +1,140 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class ProjectTestsPartitionedDocument : PartitionedDocument + { + public ProjectTestsPartitionedDocument() : base("TestPartitionKey") + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + } + + public string SomeContent { get; set; } + + public Nested Nested { get; set; } + } + + public class ProjectPartitionedTests : BaseMongoDbRepositoryTests + { + [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 new file mode 100644 index 0000000..901d93e --- /dev/null +++ b/CoreIntegrationTests/ProjectTests.cs @@ -0,0 +1,149 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class Nested + { + public DateTime SomeDate { get; set; } + } + + public class MyProjection + { + public DateTime SomeDate { get; set; } + public string SomeContent { get; set; } + } + + public class ProjectTestsDocument : Document + { + public ProjectTestsDocument() + { + Version = 2; + Nested = new Nested + { + SomeDate = DateTime.UtcNow + }; + } + + public string SomeContent { get; set; } + + public Nested Nested { get; set; } + } + + public class ProjectTests : BaseMongoDbRepositoryTests + { + + + [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 new file mode 100644 index 0000000..abe20c5 --- /dev/null +++ b/CoreIntegrationTests/ReadPartitionedTests.cs @@ -0,0 +1,181 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class ReadTestsPartitionedDocument : PartitionedDocument + { + public ReadTestsPartitionedDocument() : base("TestPartitionKey") + { + Version = 1; + } + public string SomeContent { get; set; } + } + + public class ReadPartitionedTests : BaseMongoDbRepositoryTests + { + [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 new file mode 100644 index 0000000..3dfaf14 --- /dev/null +++ b/CoreIntegrationTests/ReadTests.cs @@ -0,0 +1,181 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class ReadTestsDocument : Document + { + public ReadTestsDocument() + { + Version = 2; + } + public string SomeContent { get; set; } + } + + public class ReadTests : BaseMongoDbRepositoryTests + { + [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 new file mode 100644 index 0000000..3f1c0a7 --- /dev/null +++ b/CoreIntegrationTests/UpdatePartitionedTests.cs @@ -0,0 +1,51 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class UpdateTestsPartitionedDocument : PartitionedDocument + { + public UpdateTestsPartitionedDocument() : base("TestPartitionKey") + { + Version = 2; + } + public string SomeContent { get; set; } + } + + public class UpdatePartitionedTests : BaseMongoDbRepositoryTests + { + [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 new file mode 100644 index 0000000..69f7f9b --- /dev/null +++ b/CoreIntegrationTests/UpdateTests.cs @@ -0,0 +1,51 @@ +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; +using Xunit; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class UpdateTestsDocument : Document + { + public UpdateTestsDocument() + { + Version = 2; + } + public string SomeContent { get; set; } + } + + public class UpdateTests : BaseMongoDbRepositoryTests + { + [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/MongoDbGenericRepository.sln b/MongoDbGenericRepository.sln index baa72b9..29387f9 100644 --- a/MongoDbGenericRepository.sln +++ b/MongoDbGenericRepository.sln @@ -5,7 +5,9 @@ VisualStudioVersion = 15.0.26730.12 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IntegrationTests", "IntegrationTests\IntegrationTests.csproj", "{A484A355-A015-40CC-9B35-A4E872421128}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDbGenericRepository", "MongoDbGenericRepository\MongoDbGenericRepository.csproj", "{EFC776C4-2AF3-440C-BE80-3FBE335817A5}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MongoDbGenericRepository", "MongoDbGenericRepository\MongoDbGenericRepository.csproj", "{EFC776C4-2AF3-440C-BE80-3FBE335817A5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CoreIntegrationTests", "CoreIntegrationTests\CoreIntegrationTests.csproj", "{C640C106-7A25-4E49-A0CF-E4F248E5A97F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -13,10 +15,6 @@ Global Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {D154E7D0-9A3C-43AB-8E90-ED92BC4343F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {D154E7D0-9A3C-43AB-8E90-ED92BC4343F0}.Debug|Any CPU.Build.0 = Debug|Any CPU - {D154E7D0-9A3C-43AB-8E90-ED92BC4343F0}.Release|Any CPU.ActiveCfg = Release|Any CPU - {D154E7D0-9A3C-43AB-8E90-ED92BC4343F0}.Release|Any CPU.Build.0 = Release|Any CPU {A484A355-A015-40CC-9B35-A4E872421128}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {A484A355-A015-40CC-9B35-A4E872421128}.Debug|Any CPU.Build.0 = Debug|Any CPU {A484A355-A015-40CC-9B35-A4E872421128}.Release|Any CPU.ActiveCfg = Release|Any CPU @@ -25,6 +23,10 @@ Global {EFC776C4-2AF3-440C-BE80-3FBE335817A5}.Debug|Any CPU.Build.0 = Debug|Any CPU {EFC776C4-2AF3-440C-BE80-3FBE335817A5}.Release|Any CPU.ActiveCfg = Release|Any CPU {EFC776C4-2AF3-440C-BE80-3FBE335817A5}.Release|Any CPU.Build.0 = Release|Any CPU + {C640C106-7A25-4E49-A0CF-E4F248E5A97F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C640C106-7A25-4E49-A0CF-E4F248E5A97F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C640C106-7A25-4E49-A0CF-E4F248E5A97F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C640C106-7A25-4E49-A0CF-E4F248E5A97F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE