Added tests for Updating methods
This commit is contained in:
@@ -64,6 +64,7 @@
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="ReadPartitionedTests.cs" />
|
||||
<Compile Include="ReadTests.cs" />
|
||||
<Compile Include="UpdatePartitionedTests.cs" />
|
||||
<Compile Include="UpdateTests.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
||||
@@ -18,7 +18,7 @@ namespace IntegrationTests
|
||||
public class ReadPartitionedTests : BaseMongoDbRepositoryTests<ReadTestsPartitionedDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task PartitionedGetById()
|
||||
public async Task PartitionedGetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
@@ -29,6 +29,18 @@ namespace IntegrationTests
|
||||
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]
|
||||
public async Task PartitionedGetOneAsync()
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace IntegrationTests
|
||||
public class ReadTests : BaseMongoDbRepositoryTests<ReadTestsDocument>
|
||||
{
|
||||
[Test]
|
||||
public async Task GetById()
|
||||
public async Task GetByIdAsync()
|
||||
{
|
||||
// Arrange
|
||||
var document = CreateTestDocument();
|
||||
@@ -30,6 +30,18 @@ namespace IntegrationTests
|
||||
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]
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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,6 +8,9 @@ namespace MongoDbGenericRepository
|
||||
/// </summary>
|
||||
public class MongoDbContext : IMongoDbContext
|
||||
{
|
||||
private readonly IMongoClient _client;
|
||||
private readonly IMongoDatabase _database;
|
||||
|
||||
static MongoDbContext()
|
||||
{
|
||||
// Avoid legacy UUID representation: use Binary 0x04 subtype.
|
||||
@@ -16,14 +19,10 @@ namespace MongoDbGenericRepository
|
||||
|
||||
public MongoDbContext(string connectionString, string databaseName)
|
||||
{
|
||||
|
||||
_client = new MongoClient(connectionString);
|
||||
_database = _client.GetDatabase(databaseName);
|
||||
}
|
||||
|
||||
private readonly IMongoClient _client;
|
||||
private readonly IMongoDatabase _database;
|
||||
|
||||
/// <summary>
|
||||
/// The private GetCollection method
|
||||
/// </summary>
|
||||
@@ -62,6 +61,11 @@ namespace MongoDbGenericRepository
|
||||
_database.DropCollection(partitionKey + "-" + Pluralize<TDocument>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Very naively pluralizes a TDocument type name.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <returns></returns>
|
||||
private string Pluralize<TDocument>()
|
||||
{
|
||||
return typeof(TDocument).Name.ToLower() + "s";
|
||||
|
||||
@@ -55,6 +55,14 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
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>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
@@ -127,7 +135,27 @@ namespace MongoDbGenericRepository
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
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();
|
||||
}
|
||||
|
||||
/// <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>
|
||||
/// Asynchronously returns one document given an expression filter.
|
||||
/// </summary>
|
||||
@@ -472,43 +512,27 @@ namespace MongoDbGenericRepository
|
||||
#region Update
|
||||
|
||||
/// <summary>
|
||||
/// Updates a document
|
||||
/// Asynchronously Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<bool> UpdateOneAsync<TDocument>(TDocument entity) where TDocument : IDocument
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public async Task<bool> UpdateOneAsync<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
|
||||
{
|
||||
var updateRes = await GetCollection<TDocument>().ReplaceOneAsync(x => x.Id == entity.Id, entity);
|
||||
return updateRes.ModifiedCount < 1;
|
||||
var updateRes = await HandlePartitioned(modifiedDocument).ReplaceOneAsync(x => x.Id == modifiedDocument.Id, modifiedDocument);
|
||||
return updateRes.ModifiedCount == 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// UpdateOne with filter
|
||||
/// Updates a document.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument"></typeparam>
|
||||
/// <param name="filter"></param>
|
||||
/// <param name="update"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<long> UpdateOne<TDocument>(FilterDefinition<TDocument> filter, UpdateDefinition<TDocument> update) where TDocument : IDocument
|
||||
/// <param name="modifiedDocument">The document with the modifications you want to persist.</param>
|
||||
public bool UpdateOne<TDocument>(TDocument modifiedDocument) where TDocument : IDocument
|
||||
{
|
||||
var updateRes = await GetCollection<TDocument>().UpdateOneAsync(filter, update);
|
||||
return updateRes.ModifiedCount;
|
||||
var updateRes = HandlePartitioned(modifiedDocument).ReplaceOne(x => x.Id == modifiedDocument.Id, modifiedDocument);
|
||||
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
|
||||
|
||||
#region Find And Update
|
||||
|
||||
Reference in New Issue
Block a user