more tests, in the ReadTests
This commit is contained in:
@@ -1,19 +1,30 @@
|
||||
using MongoDbGenericRepository.Models;
|
||||
using NUnit.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
|
||||
namespace IntegrationTests.Infrastructure
|
||||
{
|
||||
public class BaseMongoDbRepositoryTests<T> where T : Document, new()
|
||||
{
|
||||
public T GetDocumentInstance()
|
||||
public T CreateTestDocument()
|
||||
{
|
||||
return new T();
|
||||
}
|
||||
|
||||
public List<T> CreateTestDocuments(int numberOfDocumentsToCreate)
|
||||
{
|
||||
var docs = new List<T>();
|
||||
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;
|
||||
|
||||
@@ -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<ReadTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task GetById()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetByIdAsync<ReadTestsDocument>(document.Id);
|
||||
// Assert
|
||||
Assert.IsNotNull(result);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task GetOneAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
SUT.AddOne(document);
|
||||
// Act
|
||||
var result = await SUT.GetOneAsync<ReadTestsDocument>(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<ReadTestsDocument>(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<ReadTestsDocument>(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<ReadTestsDocument>(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<ReadTestsDocument>(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<ReadTestsDocument>(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<ReadTestsDocument>(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<ReadTestsDocument>(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<ReadTestsDocument>(x => x.SomeContent == "GetAllContent");
|
||||
// Assert
|
||||
Assert.AreEqual(5, result.Count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -205,11 +205,15 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
public void AddMany<TDocument>(IEnumerable<TDocument> 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
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<TDocument> GetOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await GetCollection<TDocument>().Find(filter).FirstOrDefaultAsync();
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -247,7 +251,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public TDocument GetOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return GetCollection<TDocument>().Find(filter).FirstOrDefault();
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -258,7 +262,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public IFindFluent<TDocument, TDocument> GetCursor<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return GetCollection<TDocument>().Find(filter);
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -269,7 +273,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<bool> AnyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var count = await GetCollection<TDocument>().CountAsync(filter);
|
||||
var count = await HandlePartitioned<TDocument>(partitionKey).CountAsync(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
@@ -281,7 +285,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public bool Any<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
var count = GetCollection<TDocument>().Count(filter);
|
||||
var count = HandlePartitioned<TDocument>(partitionKey).Count(filter);
|
||||
return (count > 0);
|
||||
}
|
||||
|
||||
@@ -293,7 +297,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<List<TDocument>> GetAllAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await GetCollection<TDocument>().Find(filter).ToListAsync();
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).ToListAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -304,7 +308,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public List<TDocument> GetAll<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return GetCollection<TDocument>().Find(filter).ToList();
|
||||
return HandlePartitioned<TDocument>(partitionKey).Find(filter).ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -315,7 +319,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partitionKey</param>
|
||||
public async Task<long> CountAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await GetCollection<TDocument>().CountAsync(filter);
|
||||
return await HandlePartitioned<TDocument>(partitionKey).CountAsync(filter);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -339,7 +343,7 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
public async Task<List<TDocument>> GetPaginatedAsync<TDocument>(Expression<Func<TDocument, bool>> filter, int skipNumber = 0, int takeNumber = 50, string partitionKey = null) where TDocument : IDocument
|
||||
{
|
||||
return await GetCollection<TDocument>().Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync();
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).Skip(skipNumber).Limit(takeNumber).ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
@@ -353,9 +357,9 @@ namespace MongoDbGenericRepository
|
||||
where TDocument : IDocument
|
||||
where TProjection : class, new()
|
||||
{
|
||||
return await GetCollection<TDocument>().Find(Builders<TDocument>.Filter.Where(filter))
|
||||
.Project(projection)
|
||||
.FirstOrDefaultAsync();
|
||||
return await HandlePartitioned<TDocument>(partitionKey).Find(filter)
|
||||
.Project(projection)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
#endregion Get
|
||||
|
||||
Reference in New Issue
Block a user