diff --git a/IntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs b/IntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs index fae98d9..74d358e 100644 --- a/IntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs +++ b/IntegrationTests/Infrastructure/BaseMongoDbRepositoryTests.cs @@ -1,19 +1,30 @@ using MongoDbGenericRepository.Models; using NUnit.Framework; +using System.Collections.Generic; using System.Configuration; namespace IntegrationTests.Infrastructure { public class BaseMongoDbRepositoryTests where T : Document, new() { - public T GetDocumentInstance() + 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; + } + public BaseMongoDbRepositoryTests() { - var type = GetDocumentInstance(); + var type = CreateTestDocument(); if (type is IPartitionedDocument) { PartitionKey = ((IPartitionedDocument)type).PartitionKey; diff --git a/IntegrationTests/ReadTests.cs b/IntegrationTests/ReadTests.cs index 1e2ec8d..3ef6031 100644 --- a/IntegrationTests/ReadTests.cs +++ b/IntegrationTests/ReadTests.cs @@ -1,5 +1,8 @@ -using MongoDbGenericRepository.Models; +using IntegrationTests.Infrastructure; +using MongoDbGenericRepository.Models; using NUnit.Framework; +using System; +using System.Threading.Tasks; namespace IntegrationTests { @@ -13,7 +16,134 @@ namespace IntegrationTests } [TestFixture] - public class ReadTests + public class ReadTests : BaseMongoDbRepositoryTests { + [Test] + public async Task GetById() + { + // Arrange + var document = CreateTestDocument(); + SUT.AddOne(document); + // Act + var result = await SUT.GetByIdAsync(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); + } + + private class ProjectClass + { + public string Content { get; set; } + } + + [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); + } } } diff --git a/MongoDbGenericRepository/MongoDbRepository.cs b/MongoDbGenericRepository/MongoDbRepository.cs index bf7563a..64bae7e 100644 --- a/MongoDbGenericRepository/MongoDbRepository.cs +++ b/MongoDbGenericRepository/MongoDbRepository.cs @@ -205,11 +205,15 @@ namespace MongoDbGenericRepository /// The document you want to add. public void AddMany(IEnumerable documents) where TDocument : IDocument { + if (!documents.Any()) + { + return; + } foreach (var document in documents) { FormatDocument(document); } - HandlePartitioned(documents.FirstOrDefault()).InsertMany(documents); + HandlePartitioned(documents.FirstOrDefault()).InsertMany(documents.ToList()); } #endregion Create @@ -236,7 +240,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public async Task GetOneAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { - return await GetCollection().Find(filter).FirstOrDefaultAsync(); + return await HandlePartitioned(partitionKey).Find(filter).FirstOrDefaultAsync(); } /// @@ -247,7 +251,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public TDocument GetOne(Expression> filter, string partitionKey = null) where TDocument : IDocument { - return GetCollection().Find(filter).FirstOrDefault(); + return HandlePartitioned(partitionKey).Find(filter).FirstOrDefault(); } /// @@ -258,7 +262,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public IFindFluent GetCursor(Expression> filter, string partitionKey = null) where TDocument : IDocument { - return GetCollection().Find(filter); + return HandlePartitioned(partitionKey).Find(filter); } /// @@ -269,7 +273,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public async Task AnyAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { - var count = await GetCollection().CountAsync(filter); + var count = await HandlePartitioned(partitionKey).CountAsync(filter); return (count > 0); } @@ -281,7 +285,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public bool Any(Expression> filter, string partitionKey = null) where TDocument : IDocument { - var count = GetCollection().Count(filter); + var count = HandlePartitioned(partitionKey).Count(filter); return (count > 0); } @@ -293,7 +297,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public async Task> GetAllAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { - return await GetCollection().Find(filter).ToListAsync(); + return await HandlePartitioned(partitionKey).Find(filter).ToListAsync(); } /// @@ -304,7 +308,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public List GetAll(Expression> filter, string partitionKey = null) where TDocument : IDocument { - return GetCollection().Find(filter).ToList(); + return HandlePartitioned(partitionKey).Find(filter).ToList(); } /// @@ -315,7 +319,7 @@ namespace MongoDbGenericRepository /// An optional partitionKey public async Task CountAsync(Expression> filter, string partitionKey = null) where TDocument : IDocument { - return await GetCollection().CountAsync(filter); + return await HandlePartitioned(partitionKey).CountAsync(filter); } /// @@ -339,7 +343,7 @@ namespace MongoDbGenericRepository /// An optional partition key. public async Task> GetPaginatedAsync(Expression> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument { - return await GetCollection().Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); + return await HandlePartitioned(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync(); } @@ -353,9 +357,9 @@ namespace MongoDbGenericRepository where TDocument : IDocument where TProjection : class, new() { - return await GetCollection().Find(Builders.Filter.Where(filter)) - .Project(projection) - .FirstOrDefaultAsync(); + return await HandlePartitioned(partitionKey).Find(filter) + .Project(projection) + .FirstOrDefaultAsync(); } #endregion Get