Added tests for Updating methods

This commit is contained in:
alexandre-spieser
2017-08-27 20:10:52 +00:00
parent bd69797d57
commit af5ba2e98e
7 changed files with 184 additions and 35 deletions
+1
View File
@@ -64,6 +64,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReadPartitionedTests.cs" /> <Compile Include="ReadPartitionedTests.cs" />
<Compile Include="ReadTests.cs" /> <Compile Include="ReadTests.cs" />
<Compile Include="UpdatePartitionedTests.cs" />
<Compile Include="UpdateTests.cs" /> <Compile Include="UpdateTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+13 -1
View File
@@ -18,7 +18,7 @@ namespace IntegrationTests
public class ReadPartitionedTests : BaseMongoDbRepositoryTests<ReadTestsPartitionedDocument> public class ReadPartitionedTests : BaseMongoDbRepositoryTests<ReadTestsPartitionedDocument>
{ {
[Test] [Test]
public async Task PartitionedGetById() public async Task PartitionedGetByIdAsync()
{ {
// Arrange // Arrange
var document = CreateTestDocument(); var document = CreateTestDocument();
@@ -29,6 +29,18 @@ namespace IntegrationTests
Assert.IsNotNull(result); Assert.IsNotNull(result);
} }
[Test]
public void PartitionedGetById()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.GetById<ReadTestsPartitionedDocument>(document.Id, PartitionKey);
// Assert
Assert.IsNotNull(result);
}
[Test] [Test]
public async Task PartitionedGetOneAsync() public async Task PartitionedGetOneAsync()
{ {
+13 -1
View File
@@ -19,7 +19,7 @@ namespace IntegrationTests
public class ReadTests : BaseMongoDbRepositoryTests<ReadTestsDocument> public class ReadTests : BaseMongoDbRepositoryTests<ReadTestsDocument>
{ {
[Test] [Test]
public async Task GetById() public async Task GetByIdAsync()
{ {
// Arrange // Arrange
var document = CreateTestDocument(); var document = CreateTestDocument();
@@ -30,6 +30,18 @@ namespace IntegrationTests
Assert.IsNotNull(result); Assert.IsNotNull(result);
} }
[Test]
public void GetById()
{
// Arrange
var document = CreateTestDocument();
SUT.AddOne(document);
// Act
var result = SUT.GetById<ReadTestsDocument>(document.Id);
// Assert
Assert.IsNotNull(result);
}
[Test] [Test]
public async Task GetOneAsync() public async Task GetOneAsync()
{ {
@@ -0,0 +1,51 @@
using IntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
using NUnit.Framework;
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<UpdateTestsPartitionedDocument>
{
[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<UpdateTestsPartitionedDocument>(document.Id, PartitionKey);
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<UpdateTestsPartitionedDocument>(document.Id, PartitionKey);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
}
}
}
+47 -2
View File
@@ -1,6 +1,51 @@
namespace IntegrationTests using IntegrationTests.Infrastructure;
using MongoDbGenericRepository.Models;
using NUnit.Framework;
using System.Threading.Tasks;
namespace IntegrationTests
{ {
class UpdateTests public class UpdateTestsDocument : Document
{ {
public UpdateTestsDocument()
{
Version = 2;
}
public string SomeContent { get; set; }
}
public class UpdateTests : BaseMongoDbRepositoryTests<UpdateTestsDocument>
{
[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<UpdateTestsDocument>(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<UpdateTestsDocument>(document.Id);
Assert.IsNotNull(updatedDocument);
Assert.AreEqual("UpdateOneAsyncContent", updatedDocument.SomeContent);
}
} }
} }
+8 -4
View File
@@ -8,6 +8,9 @@ namespace MongoDbGenericRepository
/// </summary> /// </summary>
public class MongoDbContext : IMongoDbContext public class MongoDbContext : IMongoDbContext
{ {
private readonly IMongoClient _client;
private readonly IMongoDatabase _database;
static MongoDbContext() static MongoDbContext()
{ {
// Avoid legacy UUID representation: use Binary 0x04 subtype. // Avoid legacy UUID representation: use Binary 0x04 subtype.
@@ -16,14 +19,10 @@ namespace MongoDbGenericRepository
public MongoDbContext(string connectionString, string databaseName) public MongoDbContext(string connectionString, string databaseName)
{ {
_client = new MongoClient(connectionString); _client = new MongoClient(connectionString);
_database = _client.GetDatabase(databaseName); _database = _client.GetDatabase(databaseName);
} }
private readonly IMongoClient _client;
private readonly IMongoDatabase _database;
/// <summary> /// <summary>
/// The private GetCollection method /// The private GetCollection method
/// </summary> /// </summary>
@@ -62,6 +61,11 @@ namespace MongoDbGenericRepository
_database.DropCollection(partitionKey + "-" + Pluralize<TDocument>()); _database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
} }
/// <summary>
/// Very naively pluralizes a TDocument type name.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <returns></returns>
private string Pluralize<TDocument>() private string Pluralize<TDocument>()
{ {
return typeof(TDocument).Name.ToLower() + "s"; return typeof(TDocument).Name.ToLower() + "s";
+51 -27
View File
@@ -55,6 +55,14 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument; Task<TDocument> GetByIdAsync<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
/// <summary>
/// Returns one document given its id.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="id">The Id of the document you want to get.</param>
/// <param name="partitionKey">An optional partition key.</param>
TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument;
/// <summary> /// <summary>
/// Asynchronously returns one document given an expression filter. /// Asynchronously returns one document given an expression filter.
/// </summary> /// </summary>
@@ -127,7 +135,27 @@ namespace MongoDbGenericRepository
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument; long Count<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) where TDocument : IDocument;
#endregion Get #endregion
#region Update
/// <summary>
/// Asynchronously Updates a document.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
/// <summary>
/// Updates a document.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument;
#endregion
} }
@@ -232,6 +260,18 @@ namespace MongoDbGenericRepository
return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync(); return await HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefaultAsync();
} }
/// <summary>
/// Returns one document given its id.
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="id">The Id of the document you want to get.</param>
/// <param name="partitionKey">An optional partition key.</param>
public TDocument GetById<TDocument>(Guid id, string partitionKey = null) where TDocument : IDocument
{
var filter = Builders<TDocument>.Filter.Eq("Id", id);
return HandlePartitioned<TDocument>(partitionKey).Find(filter).FirstOrDefault();
}
/// <summary> /// <summary>
/// Asynchronously returns one document given an expression filter. /// Asynchronously returns one document given an expression filter.
/// </summary> /// </summary>
@@ -472,43 +512,27 @@ namespace MongoDbGenericRepository
#region Update #region Update
/// <summary> /// <summary>
/// Updates a document /// Asynchronously Updates a document.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="entity"></param> /// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
/// <returns></returns> public async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
public async Task<bool> UpdateOneAsync<TDocument>(TDocument entity) where TDocument : IDocument
{ {
var updateRes = await GetCollection<TDocument>().ReplaceOneAsync(x => x.Id == entity.Id, entity); var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(x => x.Id == modifiedDocument.Id, modifiedDocument);
return updateRes.ModifiedCount < 1; return updateRes.ModifiedCount == 1;
} }
/// <summary> /// <summary>
/// UpdateOne with filter /// Updates a document.
/// </summary> /// </summary>
/// <typeparam name="TDocument"></typeparam> /// <typeparam name="TDocument"></typeparam>
/// <param name="filter"></param> /// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
/// <param name="update"></param> public bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
/// <returns></returns>
private async Task<long> UpdateOne<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update) where TDocument : IDocument
{ {
var updateRes = await GetCollection<TDocument>().UpdateOneAsync(filter, update); var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(x => x.Id == modifiedDocument.Id, modifiedDocument);
return updateRes.ModifiedCount; return updateRes.ModifiedCount == 1;
} }
/// <summary>
/// UpdateMany with filter
/// </summary>
/// <typeparam name="TDocument"></typeparam>
/// <param name="filter"></param>
/// <param name="update"></param>
/// <returns></returns>
public async Task<long> UpdateMany<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update) where TDocument : IDocument
{
var collection = GetCollection<TDocument>();
var updateRes = await collection.UpdateManyAsync(filter, update);
return updateRes.ModifiedCount;
}
#endregion Update #endregion Update
#region Find And Update #region Find And Update