From a13800637f0f576cb1f9053f043dab6f87c6cd6c Mon Sep 17 00:00:00 2001 From: alexandre-spieser Date: Mon, 25 Sep 2017 21:44:10 +0000 Subject: [PATCH] added more tests for the documents implementing the IDocument interface --- .../CreateTKeyPartitionedTests.cs | 1 - IntegrationTests/IntegrationTests.csproj | 5 + .../ProjectPartitionedTKeyTests.cs | 143 +++++++++++++ IntegrationTests/ProjectTKeyTests.cs | 2 - IntegrationTests/ReadPartitionedTKeyTests.cs | 189 ++++++++++++++++++ IntegrationTests/ReadTKeyTests.cs | 187 +++++++++++++++++ .../UpdatePartitionedTKeyTests.cs | 61 ++++++ IntegrationTests/UpdateTKeyTests.cs | 59 ++++++ 8 files changed, 644 insertions(+), 3 deletions(-) create mode 100644 IntegrationTests/ProjectPartitionedTKeyTests.cs create mode 100644 IntegrationTests/ReadPartitionedTKeyTests.cs create mode 100644 IntegrationTests/ReadTKeyTests.cs create mode 100644 IntegrationTests/UpdatePartitionedTKeyTests.cs create mode 100644 IntegrationTests/UpdateTKeyTests.cs diff --git a/IntegrationTests/CreateTKeyPartitionedTests.cs b/IntegrationTests/CreateTKeyPartitionedTests.cs index 4cc8fa2..4d00dd5 100644 --- a/IntegrationTests/CreateTKeyPartitionedTests.cs +++ b/IntegrationTests/CreateTKeyPartitionedTests.cs @@ -8,7 +8,6 @@ using System.Threading.Tasks; namespace IntegrationTests { - public class CreateTestsPartitionedTKeyDocument : IDocument, IPartitionedDocument { [BsonId] diff --git a/IntegrationTests/IntegrationTests.csproj b/IntegrationTests/IntegrationTests.csproj index b2bef91..f608e8b 100644 --- a/IntegrationTests/IntegrationTests.csproj +++ b/IntegrationTests/IntegrationTests.csproj @@ -63,13 +63,18 @@ + + + + + diff --git a/IntegrationTests/ProjectPartitionedTKeyTests.cs b/IntegrationTests/ProjectPartitionedTKeyTests.cs new file mode 100644 index 0000000..9d80dde --- /dev/null +++ b/IntegrationTests/ProjectPartitionedTKeyTests.cs @@ -0,0 +1,143 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; +using System.Linq; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + + public class ProjectTestsPartitionedTKeyDocument : IDocument, IPartitionedDocument + { + [BsonId] + public Guid Id { get; set; } + public int Version { get; set; } + public ProjectTestsPartitionedTKeyDocument() + { + Id = Guid.NewGuid(); + Version = 2; + PartitionKey = "TestPartitionKey"; + Nested = new NestedTKey(); + } + public string PartitionKey { get; set; } + public NestedTKey Nested { get; set; } + public string SomeContent { get; set; } + } + + public class ProjectPartitionedTKeyTests : BaseMongoDbRepositoryTests + { + [Test] + public async Task PartitionedProjectOneAsync() + { + // Arrange + const string someContent = "ProjectOneAsyncContent"; + var someDate = DateTime.UtcNow; + var document = CreateTestDocument(); + document.SomeContent = someContent; + document.Nested.SomeDate = someDate; + SUT.AddOne(document); + // Act + var result = await SUT.ProjectOneAsync( + x => x.Id == document.Id, + x => new MyProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(someContent, result.SomeContent); + Assert.AreEqual(someDate.Minute, result.SomeDate.Minute); + Assert.AreEqual(someDate.Second, result.SomeDate.Second); + } + + [Test] + public void PartitionedProjectOne() + { + // Arrange + const string someContent = "ProjectOneContent"; + var someDate = DateTime.UtcNow; + var document = CreateTestDocument(); + document.SomeContent = someContent; + document.Nested.SomeDate = someDate; + SUT.AddOne(document); + // Act + var result = SUT.ProjectOne( + x => x.Id == document.Id, + x => new MyProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.IsNotNull(result); + Assert.AreEqual(someContent, result.SomeContent); + Assert.AreEqual(someDate.Minute, result.SomeDate.Minute); + Assert.AreEqual(someDate.Second, result.SomeDate.Second); + } + + [Test] + public async Task PartitionedProjectManyAsync() + { + // Arrange + const string someContent = "ProjectManyAsyncContent"; + var someDate = DateTime.UtcNow; + var document = CreateTestDocuments(5); + document.ForEach(e => + { + e.SomeContent = someContent; + e.Nested.SomeDate = someDate; + }); + + SUT.AddMany(document); + // Act + var result = await SUT.ProjectManyAsync( + x => x.SomeContent == someContent, + x => new MyProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.AreEqual(5, result.Count); + Assert.AreEqual(someContent, result.First().SomeContent); + Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute); + Assert.AreEqual(someDate.Second, result.First().SomeDate.Second); + } + + [Test] + public void PartitionedProjectMany() + { + // Arrange + const string someContent = "ProjectManyContent"; + var someDate = DateTime.UtcNow; + var document = CreateTestDocuments(5); + document.ForEach(e => + { + e.SomeContent = someContent; + e.Nested.SomeDate = someDate; + }); + + SUT.AddMany(document); + // Act + var result = SUT.ProjectMany( + x => x.SomeContent == someContent, + x => new MyProjection + { + SomeContent = x.SomeContent, + SomeDate = x.Nested.SomeDate + }, + PartitionKey); + // Assert + Assert.AreEqual(5, result.Count); + Assert.AreEqual(someContent, result.First().SomeContent); + Assert.AreEqual(someDate.Minute, result.First().SomeDate.Minute); + Assert.AreEqual(someDate.Second, result.First().SomeDate.Second); + } + } +} diff --git a/IntegrationTests/ProjectTKeyTests.cs b/IntegrationTests/ProjectTKeyTests.cs index 614892a..cec8606 100644 --- a/IntegrationTests/ProjectTKeyTests.cs +++ b/IntegrationTests/ProjectTKeyTests.cs @@ -39,8 +39,6 @@ namespace IntegrationTests public class ProjectTKeyTests : BaseMongoDbRepositoryTests { - - [Test] public async Task ProjectOneAsync() { diff --git a/IntegrationTests/ReadPartitionedTKeyTests.cs b/IntegrationTests/ReadPartitionedTKeyTests.cs new file mode 100644 index 0000000..ceab08e --- /dev/null +++ b/IntegrationTests/ReadPartitionedTKeyTests.cs @@ -0,0 +1,189 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class ReadTestsPartitionedTKeyDocument : IDocument, IPartitionedDocument + { + [BsonId] + public Guid Id { get; set; } + public int Version { get; set; } + public ReadTestsPartitionedTKeyDocument() + { + Id = Guid.NewGuid(); + Version = 2; + PartitionKey = "TestPartitionKey"; + } + public string PartitionKey { get; set; } + public string SomeContent { get; set; } + } + + [TestFixture] + public class ReadPartitionedTKeyTests : BaseMongoDbRepositoryTests + { + [Test] + public async Task PartitionedGetByIdAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetByIdAsync(document.Id, PartitionKey); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public void PartitionedGetById() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetById(document.Id, PartitionKey); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public async Task PartitionedGetOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetOneAsync(x => x.Id == document.Id, PartitionKey); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public void PartitionedGetOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetOne(x => x.Id == document.Id, PartitionKey); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public void PartitionedGetCursor() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var cursor = SUT.GetCursor(x => x.Id == document.Id, PartitionKey); + var count = cursor.Count(); + // Assert + Assert.AreEqual(1, count); + } + + [Test] + public async Task PartitionedAnyAsyncReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id == document.Id, PartitionKey); + // Assert + Assert.AreEqual(true, result); + } + + [Test] + public async Task PartitionedAnyAsyncReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id == Guid.NewGuid(), PartitionKey); + // Assert + Assert.AreEqual(false, result); + } + + [Test] + public void PartitionedAnyReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id == document.Id, PartitionKey); + // Assert + Assert.AreEqual(true, result); + } + + [Test] + public void PartitionedAnyReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id == Guid.NewGuid(), PartitionKey); + // Assert + Assert.AreEqual(false, result); + } + + [Test] + public async Task PartitionedGetAllAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "GetAllAsyncContent"); + SUT.AddMany(documents); + // Act + var result = await SUT.GetAllAsync(x => x.SomeContent == "GetAllAsyncContent", PartitionKey); + // Assert + Assert.AreEqual(5, result.Count); + } + + [Test] + public void PartitionedGetAll() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "GetAllContent"); + SUT.AddMany(documents); + // Act + var result = SUT.GetAll(x => x.SomeContent == "GetAllContent", PartitionKey); + // Assert + Assert.AreEqual(5, result.Count); + } + + [Test] + public async Task PartitionedCountAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "CountAsyncContent"); + SUT.AddMany(documents); + // Act + var result = await SUT.CountAsync(x => x.SomeContent == "CountAsyncContent", PartitionKey); + // Assert + Assert.AreEqual(5, result); + } + + [Test] + public void PartitionedCount() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "CountContent"); + SUT.AddMany(documents); + // Act + var result = SUT.Count(x => x.SomeContent == "CountContent", PartitionKey); + // Assert + Assert.AreEqual(5, result); + } + } +} diff --git a/IntegrationTests/ReadTKeyTests.cs b/IntegrationTests/ReadTKeyTests.cs new file mode 100644 index 0000000..e2e6330 --- /dev/null +++ b/IntegrationTests/ReadTKeyTests.cs @@ -0,0 +1,187 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + public class ReadTestsTKeyDocument : IDocument + { + [BsonId] + public Guid Id { get; set; } + public int Version { get; set; } + public ReadTestsTKeyDocument() + { + Id = Guid.NewGuid(); + Version = 2; + } + public string SomeContent { get; set; } + } + + [TestFixture] + public class ReadTKeyTests : BaseMongoDbRepositoryTests + { + [Test] + public async Task GetByIdAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetByIdAsync(document.Id); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public void GetById() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetById(document.Id); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public async Task GetOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetOneAsync(x => x.Id == document.Id); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public void GetOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.GetOne(x => x.Id == document.Id); + // Assert + Assert.IsNotNull(result); + } + + [Test] + public void GetCursor() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var cursor = SUT.GetCursor(x => x.Id == document.Id); + var count = cursor.Count(); + // Assert + Assert.AreEqual(1, count); + } + + [Test] + public async Task AnyAsyncReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id == document.Id); + // Assert + Assert.AreEqual(true, result); + } + + [Test] + public async Task AnyAsyncReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.AnyAsync(x => x.Id == Guid.NewGuid()); + // Assert + Assert.AreEqual(false, result); + } + + [Test] + public void AnyReturnsTrue() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id == document.Id); + // Assert + Assert.AreEqual(true, result); + } + + [Test] + public void AnyReturnsFalse() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = SUT.Any(x => x.Id == Guid.NewGuid()); + // Assert + Assert.AreEqual(false, result); + } + + [Test] + public async Task GetAllAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "GetAllAsyncContent"); + SUT.AddMany(documents); + // Act + var result = await SUT.GetAllAsync(x => x.SomeContent == "GetAllAsyncContent"); + // Assert + Assert.AreEqual(5, result.Count); + } + + [Test] + public void GetAll() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "GetAllContent"); + SUT.AddMany(documents); + // Act + var result = SUT.GetAll(x => x.SomeContent == "GetAllContent"); + // Assert + Assert.AreEqual(5, result.Count); + } + + [Test] + public async Task CountAsync() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "CountAsyncContent"); + SUT.AddMany(documents); + // Act + var result = await SUT.CountAsync(x => x.SomeContent == "CountAsyncContent"); + // Assert + Assert.AreEqual(5, result); + } + + [Test] + public void Count() + { + // Arrange + var documents = CreateTestDocuments(5); + documents.ForEach(e => e.SomeContent = "CountContent"); + SUT.AddMany(documents); + // Act + var result = SUT.Count(x => x.SomeContent == "CountContent"); + // Assert + Assert.AreEqual(5, result); + } + } +} diff --git a/IntegrationTests/UpdatePartitionedTKeyTests.cs b/IntegrationTests/UpdatePartitionedTKeyTests.cs new file mode 100644 index 0000000..8cfb58a --- /dev/null +++ b/IntegrationTests/UpdatePartitionedTKeyTests.cs @@ -0,0 +1,61 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + + public class UpdateTestsPartitionedTKeyDocument : IDocument, IPartitionedDocument + { + [BsonId] + public Guid Id { get; set; } + public int Version { get; set; } + public UpdateTestsPartitionedTKeyDocument() + { + Id = Guid.NewGuid(); + Version = 2; + PartitionKey = "TestPartitionKey"; + } + public string PartitionKey { get; set; } + public string SomeContent { get; set; } + } + + [TestFixture] + public class UpdatePartitionedTKeyTests : BaseMongoDbRepositoryTests + { + [Test] + public void PartitionedUpdateOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + document.SomeContent = "UpdateOneContent"; + // Act + var result = SUT.UpdateOne(document); + // Assert + Assert.IsTrue(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.IsNotNull(updatedDocument); + Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent); + } + + [Test] + public async Task PartitionedUpdateOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + document.SomeContent = "UpdateOneAsyncContent"; + // Act + var result = await SUT.UpdateOneAsync(document); + // Assert + Assert.IsTrue(result); + var updatedDocument = SUT.GetById(document.Id, PartitionKey); + Assert.IsNotNull(updatedDocument); + Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent); + } + } +} diff --git a/IntegrationTests/UpdateTKeyTests.cs b/IntegrationTests/UpdateTKeyTests.cs new file mode 100644 index 0000000..bcb77aa --- /dev/null +++ b/IntegrationTests/UpdateTKeyTests.cs @@ -0,0 +1,59 @@ +using IntegrationTests.Infrastructure; +using MongoDB.Bson.Serialization.Attributes; +using MongoDbGenericRepository.Models; +using NUnit.Framework; +using System; +using System.Threading.Tasks; + +namespace IntegrationTests +{ + + public class UpdateTestsTKeyDocument : IDocument + { + [BsonId] + public Guid Id { get; set; } + public int Version { get; set; } + public UpdateTestsTKeyDocument() + { + Id = Guid.NewGuid(); + Version = 2; + } + public string SomeContent { get; set; } + } + + [TestFixture] + public class UpdateTKeyTests : BaseMongoDbRepositoryTests + { + [Test] + public void UpdateOne() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + document.SomeContent = "UpdateOneContent"; + // Act + var result = SUT.UpdateOne(document); + // Assert + Assert.IsTrue(result); + var updatedDocument = SUT.GetById(document.Id); + Assert.IsNotNull(updatedDocument); + Assert.AreEqual("UpdateOneContent", updatedDocument.SomeContent); + } + + [Test] + public async Task UpdateOneAsync() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + document.SomeContent = "UpdateOneAsyncContent"; + // Act + var result = await SUT.UpdateOneAsync(document); + // Assert + Assert.IsTrue(result); + var updatedDocument = SUT.GetById(document.Id); + Assert.IsNotNull(updatedDocument); + Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent); + } + } +}