added cancellation tokens to sync deletes and added more unit tests

This commit is contained in:
Sean Garrett
2023-06-17 17:49:49 +01:00
parent b285c7d919
commit 1b8f795e00
13 changed files with 861 additions and 192 deletions
@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using AutoFixture; using AutoFixture;
using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model; using CoreUnitTests.Infrastructure.Model;
@@ -23,7 +24,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<IEnumerable<TestDocument>>())) .Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<IEnumerable<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(count); .Returns(count);
// Act // Act
@@ -31,7 +32,28 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(documents), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public void WithDocumentsAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var documents = Fixture.CreateMany<TestDocument>().ToList();
var count = Fixture.Create<long>();
var token = new CancellationToken(true);
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<IEnumerable<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany(documents, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(documents, token), Times.Once);
} }
[Fact] [Fact]
@@ -46,7 +68,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), null)) .Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(count); .Returns(count);
// Act // Act
@@ -54,7 +76,31 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, null), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, null, CancellationToken.None), Times.Once);
}
[Fact]
public void WithFilterAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var content = Fixture.Create<string>();
var count = Fixture.Create<long>();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany(filter, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, null, token), Times.Once);
} }
[Fact] [Fact]
@@ -70,7 +116,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>())) .Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(count); .Returns(count);
// Act // Act
@@ -78,7 +124,32 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, partitionKey), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, partitionKey, CancellationToken.None), Times.Once);
}
[Fact]
public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var content = Fixture.Create<string>();
var count = Fixture.Create<long>();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
Expression<Func<TestDocument, bool>> filter = x => x.SomeContent == content;
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocument, Guid>(It.IsAny<Expression<Func<TestDocument, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany(filter, partitionKey, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocument, Guid>(filter, partitionKey, token), Times.Once);
} }
#region Keyed #region Keyed
@@ -92,7 +163,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>())) .Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
.Returns(count); .Returns(count);
// Act // Act
@@ -100,7 +171,28 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public void Keyed_WithDocumentsAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
var count = Fixture.Create<long>();
var token = new CancellationToken(true);
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(documents, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
} }
[Fact] [Fact]
@@ -115,7 +207,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), null)) .Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), null, CancellationToken.None))
.Returns(count); .Returns(count);
// Act // Act
@@ -123,7 +215,31 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
}
[Fact]
public void Keyed_WithFilterAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var content = Fixture.Create<string>();
var count = Fixture.Create<long>();
var token = new CancellationToken(true);
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(filter, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
} }
[Fact] [Fact]
@@ -139,7 +255,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>())) .Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(count); .Returns(count);
// Act // Act
@@ -147,8 +263,32 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
} }
[Fact]
public void Keyed_WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var content = Fixture.Create<string>();
var count = Fixture.Create<long>();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>(), It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
}
#endregion #endregion
} }
@@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Delete;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
public class DeleteManyAsyncTests : GenericTestContext<MongoDbEraser>
{
[Fact]
public async Task WithEmptyDocuments_DeletesNothing()
{
// Arrange
var documents = new List<TestDocument>(0);
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(collection.Object);
// Act
var result = await Sut.DeleteManyAsync<TestDocument, Guid>(documents, token);
// Assert
result.Should().Be(0);
var idsToDelete = documents.Select(e => e.Id).ToArray();
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
collection.Verify(
x => x.DeleteManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
token),
Times.Never);
}
[Fact]
public async Task WithDocumentsAndCancellationToken_DeletesMany()
{
// Arrange
var count = Fixture.Create<long>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(collection.Object);
// Act
var result = await Sut.DeleteManyAsync<TestDocument, Guid>(documents, token);
// Assert
result.Should().Be(count);
var idsToDelete = documents.Select(e => e.Id).ToArray();
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
collection.Verify(
x => x.DeleteManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
token),
Times.Once);
}
[Fact]
public async Task WithPartitionDocumentsAndCancellationToken_DeletesMany()
{
// Arrange
var count = Fixture.Create<long>();
var partitionKey = Fixture.Create<string>();
var documents = Fixture
.Build<PartitionedTestDocument>()
.With(x => x.PartitionKey, partitionKey)
.CreateMany()
.ToList();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
collection
.Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<PartitionedTestDocument>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
.Returns(collection.Object);
// Act
var result = await Sut.DeleteManyAsync<PartitionedTestDocument, Guid>(documents, token);
// Assert
result.Should().Be(count);
var idsToDelete = documents.Select(e => e.Id).ToArray();
Expression<Func<PartitionedTestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
collection.Verify(
x => x.DeleteManyAsync(
It.Is<FilterDefinition<PartitionedTestDocument>>(f => f.EquivalentTo(expectedFilter)),
token),
Times.Once);
dbContext.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once);
}
[Fact]
public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var id = Fixture.Create<Guid>();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteManyAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
.Returns(collection.Object);
Expression<Func<TestDocument, bool>> filter = d => d.Id == id;
// Act
var result = await Sut.DeleteManyAsync<TestDocument, Guid>(filter, partitionKey, token);
// Assert
result.Should().Be(count);
collection.Verify(
x => x.DeleteManyAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
token),
Times.Once);
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
}
@@ -0,0 +1,157 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Delete;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
public class DeleteManyTests : GenericTestContext<MongoDbEraser>
{
[Fact]
public void WithEmptyDocuments_DeletesNothing()
{
// Arrange
var documents = new List<TestDocument>(0);
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(collection.Object);
// Act
var result = Sut.DeleteMany<TestDocument, Guid>(documents, token);
// Assert
result.Should().Be(0);
var idsToDelete = documents.Select(e => e.Id).ToArray();
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
collection.Verify(
x => x.DeleteMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
token),
Times.Never);
}
[Fact]
public void WithDocumentsAndCancellationToken_DeletesMany()
{
// Arrange
var count = Fixture.Create<long>();
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteMany(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(collection.Object);
// Act
var result = Sut.DeleteMany<TestDocument, Guid>(documents, token);
// Assert
result.Should().Be(count);
var idsToDelete = documents.Select(e => e.Id).ToArray();
Expression<Func<TestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
collection.Verify(
x => x.DeleteMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
token),
Times.Once);
}
[Fact]
public void WithPartitionDocumentsAndCancellationToken_DeletesMany()
{
// Arrange
var count = Fixture.Create<long>();
var partitionKey = Fixture.Create<string>();
var documents = Fixture
.Build<PartitionedTestDocument>()
.With(x => x.PartitionKey, partitionKey)
.CreateMany()
.ToList();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
collection
.Setup(x => x.DeleteMany(It.IsAny<FilterDefinition<PartitionedTestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
.Returns(collection.Object);
// Act
var result = Sut.DeleteMany<PartitionedTestDocument, Guid>(documents, token);
// Assert
result.Should().Be(count);
var idsToDelete = documents.Select(e => e.Id).ToArray();
Expression<Func<PartitionedTestDocument, bool>> expectedFilter = x => idsToDelete.Contains(x.Id);
collection.Verify(
x => x.DeleteMany(
It.Is<FilterDefinition<PartitionedTestDocument>>(f => f.EquivalentTo(expectedFilter)),
token),
Times.Once);
dbContext.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once);
}
[Fact]
public void WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var id = Fixture.Create<Guid>();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteMany(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
.Returns(collection.Object);
Expression<Func<TestDocument, bool>> filter = d => d.Id == id;
// Act
var result = Sut.DeleteMany<TestDocument, Guid>(filter, partitionKey, token);
// Assert
result.Should().Be(count);
collection.Verify(
x => x.DeleteMany(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
token),
Times.Once);
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey), Times.Once);
}
}
@@ -0,0 +1,85 @@
using System;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using FluentAssertions;
using MongoDB.Driver;
using MongoDbGenericRepository;
using MongoDbGenericRepository.DataAccess.Delete;
using Moq;
using Xunit;
namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
public class DeleteOneAsyncTests : GenericTestContext<MongoDbEraser>
{
[Fact]
public async Task WithDocumentAndCancellationToken_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteOneAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(collection.Object);
// Act
var result = await Sut.DeleteOneAsync<TestDocument, Guid>(document, token);
// Assert
result.Should().Be(count);
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection.Verify(
x => x.DeleteOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
token),
Times.Once());
}
[Fact]
public async Task WithFilterAndPartitionKeyAndCancellationToken_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var document = Fixture.Create<TestDocument>();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteOneAsync(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
.Returns(collection.Object);
Expression<Func<TestDocument, bool>> filter = d => d.Id == document.Id;
// Act
var result = await Sut.DeleteOneAsync<TestDocument, Guid>(filter, partitionKey, token);
// Assert
result.Should().Be(count);
collection.Verify(
x => x.DeleteOneAsync(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
token),
Times.Once());
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey));
}
}
@@ -15,36 +15,6 @@ namespace CoreUnitTests.DataAccessTests.MongoDbEraserTests;
public class DeleteOneTests : GenericTestContext<MongoDbEraser> public class DeleteOneTests : GenericTestContext<MongoDbEraser>
{ {
[Fact]
public void WithDocument_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var document = Fixture.Create<TestDocument>();
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteOne(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(Fixture.Create<IMongoCollection<TestDocument>>());
// Act
var result = Sut.DeleteOne<TestDocument, Guid>(document);
// Assert
result.Should().Be(count);
var expectedFilter = Builders<TestDocument>.Filter.Eq("Id", document.Id);
collection.Verify(
x => x.DeleteOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
CancellationToken.None));
}
[Fact] [Fact]
public void WithDocumentAndCancellationToken_DeletesOne() public void WithDocumentAndCancellationToken_DeletesOne()
{ {
@@ -61,7 +31,7 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
var dbContext = MockOf<IMongoDbContext>(); var dbContext = MockOf<IMongoDbContext>();
dbContext dbContext
.Setup(x => x.GetCollection<TestDocument>(null)) .Setup(x => x.GetCollection<TestDocument>(null))
.Returns(Fixture.Create<IMongoCollection<TestDocument>>()); .Returns(collection.Object);
// Act // Act
var result = Sut.DeleteOne<TestDocument, Guid>(document, token); var result = Sut.DeleteOne<TestDocument, Guid>(document, token);
@@ -73,98 +43,8 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
collection.Verify( collection.Verify(
x => x.DeleteOne( x => x.DeleteOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)), It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
token)); token),
} Times.Once());
[Fact]
public void WithFilter_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var document = Fixture.Create<TestDocument>();
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteOne(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(Fixture.Create<IMongoCollection<TestDocument>>());
Expression<Func<TestDocument, bool>> filter = d => d.SomeContent == document.SomeContent;
// Act
var result = Sut.DeleteOne<TestDocument, Guid>(filter);
// Assert
result.Should().Be(count);
collection.Verify(
x => x.DeleteOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)), CancellationToken.None));
}
[Fact]
public void WithFilterAndCancellationToken_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteOne(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(Fixture.Create<IMongoCollection<TestDocument>>());
Expression<Func<TestDocument, bool>> filter = d => d.Id == document.Id;
// Act
var result = Sut.DeleteOne<TestDocument, Guid>(filter, cancellationToken: token);
// Assert
result.Should().Be(count);
collection.Verify(
x => x.DeleteOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)), token));
}
[Fact]
public void WithFilterAndPartitionKey_DeletesOne()
{
// Arrange
var count = Fixture.Create<long>();
var document = Fixture.Create<TestDocument>();
var partitionKey = Fixture.Create<string>();
var collection = MockOf<IMongoCollection<TestDocument>>();
collection
.Setup(x => x.DeleteOne(It.IsAny<FilterDefinition<TestDocument>>(), It.IsAny<CancellationToken>()))
.Returns(new DeleteResult.Acknowledged(count));
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
.Returns(Fixture.Create<IMongoCollection<TestDocument>>());
Expression<Func<TestDocument, bool>> filter = d => d.Id == document.Id;
// Act
var result = Sut.DeleteOne<TestDocument, Guid>(filter, partitionKey);
// Assert
result.Should().Be(count);
collection.Verify(
x => x.DeleteOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)), CancellationToken.None));
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey));
} }
[Fact] [Fact]
@@ -184,7 +64,7 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
var dbContext = MockOf<IMongoDbContext>(); var dbContext = MockOf<IMongoDbContext>();
dbContext dbContext
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>())) .Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
.Returns(Fixture.Create<IMongoCollection<TestDocument>>()); .Returns(collection.Object);
Expression<Func<TestDocument, bool>> filter = d => d.Id == document.Id; Expression<Func<TestDocument, bool>> filter = d => d.Id == document.Id;
@@ -195,7 +75,9 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
result.Should().Be(count); result.Should().Be(count);
collection.Verify( collection.Verify(
x => x.DeleteOne( x => x.DeleteOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)), token)); It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(filter)),
token),
Times.Once());
dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey)); dbContext.Verify(x => x.GetCollection<TestDocument>(partitionKey));
} }
@@ -6,4 +6,4 @@ public class PartitionedTestDocument : TestDocument, IPartitionedDocument
{ {
/// <inheritdoc /> /// <inheritdoc />
public string PartitionKey { get; set; } = "PartitionedTestDocument"; public string PartitionKey { get; set; } = "PartitionedTestDocument";
} }
@@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading;
using AutoFixture; using AutoFixture;
using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model; using CoreUnitTests.Infrastructure.Model;
@@ -23,7 +24,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>())) .Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), CancellationToken.None))
.Returns(count); .Returns(count);
// Act // Act
@@ -31,7 +32,29 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public void WithDocumentsAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
var count = Fixture.Create<long>();
var token = new CancellationToken(true);
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<IEnumerable<TestDocumentWithKey<int>>>(), It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany(documents, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
} }
[Fact] [Fact]
@@ -46,7 +69,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), null)) .Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), null, CancellationToken.None))
.Returns(count); .Returns(count);
// Act // Act
@@ -54,7 +77,35 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, CancellationToken.None), Times.Once);
}
[Fact]
public void WithFilterAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var content = Fixture.Create<string>();
var count = Fixture.Create<long>();
var token = new CancellationToken(true);
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(
x => x.DeleteMany<TestDocumentWithKey<int>, int>(
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany(filter, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, null, token), Times.Once);
} }
[Fact] [Fact]
@@ -70,7 +121,11 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
Eraser = new Mock<IMongoDbEraser>(); Eraser = new Mock<IMongoDbEraser>();
Eraser Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(), It.IsAny<string>())) .Setup(
x => x.DeleteMany<TestDocumentWithKey<int>, int>(
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
It.IsAny<string>(),
CancellationToken.None))
.Returns(count); .Returns(count);
// Act // Act
@@ -78,6 +133,34 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
// Assert // Assert
result.Should().Be(count); result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey), Times.Once); Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, CancellationToken.None), Times.Once);
} }
}
[Fact]
public void WithFilterAndPartitionKeyAndCancellationToken_ShouldDeleteMany()
{
// Arrange
var content = Fixture.Create<string>();
var count = Fixture.Create<long>();
var partitionKey = Fixture.Create<string>();
var token = new CancellationToken(true);
Expression<Func<TestDocumentWithKey<int>, bool>> filter = x => x.SomeContent == content;
Eraser = new Mock<IMongoDbEraser>();
Eraser
.Setup(x => x.DeleteMany<TestDocumentWithKey<int>, int>(
It.IsAny<Expression<Func<TestDocumentWithKey<int>, bool>>>(),
It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.Returns(count);
// Act
var result = Sut.DeleteMany(filter, partitionKey, token);
// Assert
result.Should().Be(count);
Eraser.Verify(x => x.DeleteMany<TestDocumentWithKey<int>, int>(filter, partitionKey, token), Times.Once);
}
}
@@ -47,10 +47,9 @@ namespace MongoDbGenericRepository
public virtual long DeleteOne<TDocument>(TDocument document, CancellationToken cancellationToken) public virtual long DeleteOne<TDocument>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
return MongoDbEraser.DeleteOne<TDocument, Guid>(document, cancellationToken); return DeleteOne<TDocument, Guid>(document, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter) public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
@@ -76,7 +75,7 @@ namespace MongoDbGenericRepository
public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken) public virtual long DeleteOne<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
return MongoDbEraser.DeleteOne<TDocument, Guid>(filter, partitionKey, cancellationToken); return DeleteOne<TDocument, Guid>(filter, partitionKey, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -90,10 +89,9 @@ namespace MongoDbGenericRepository
public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken) public virtual async Task<long> DeleteOneAsync<TDocument>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(document, cancellationToken); return await DeleteOneAsync<TDocument, Guid>(document, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter) public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
@@ -119,7 +117,7 @@ namespace MongoDbGenericRepository
public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken) public virtual async Task<long> DeleteOneAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
return await MongoDbEraser.DeleteOneAsync<TDocument, Guid>(filter, partitionKey, cancellationToken); return await DeleteOneAsync<TDocument, Guid>(filter, partitionKey, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -147,7 +145,7 @@ namespace MongoDbGenericRepository
public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken) public async Task<long> DeleteManyAsync<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
return await MongoDbEraser.DeleteManyAsync<TDocument, Guid>(filter, partitionKey, cancellationToken); return await DeleteManyAsync<TDocument, Guid>(filter, partitionKey, cancellationToken);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -168,14 +166,42 @@ namespace MongoDbGenericRepository
public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents) public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
return DeleteMany<TDocument, Guid>(documents); return DeleteMany<TDocument, Guid>(documents, CancellationToken.None);
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<Guid> where TDocument : IDocument<Guid>
{ {
return MongoDbEraser.DeleteMany<TDocument, Guid>(filter, partitionKey); return DeleteMany<TDocument, Guid>(documents, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<Guid>
{
return DeleteMany(filter, null, CancellationToken.None);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<Guid>
{
return DeleteMany(filter, null, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<Guid>
{
return DeleteMany(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<Guid>
{
return DeleteMany<TDocument, Guid>(filter, partitionKey, cancellationToken);
} }
#endregion Delete #endregion Delete
@@ -331,15 +357,47 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return MongoDbEraser.DeleteMany<TDocument, TKey>(documents); return DeleteMany<TDocument, TKey>(documents, CancellationToken.None);
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return MongoDbEraser.DeleteMany<TDocument, TKey>(filter, partitionKey); return MongoDbEraser.DeleteMany<TDocument, TKey>(documents, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return DeleteMany<TDocument, TKey>(filter, null, CancellationToken.None);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return DeleteMany<TDocument, TKey>(filter, null, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return DeleteMany<TDocument, TKey>(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>
{
return MongoDbEraser.DeleteMany<TDocument, TKey>(filter, partitionKey, cancellationToken);
} }
#endregion #endregion
@@ -21,7 +21,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <param name="document">The document you want to delete.</param> /// <param name="document">The document you want to delete.</param>
/// <param name="cancellationToken">An optional cancellation token</param> /// <param name="cancellationToken">An optional cancellation token</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default) long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -34,7 +34,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">An optional cancellation token</param> /// <param name="cancellationToken">An optional cancellation token</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null, CancellationToken cancellationToken = default) long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -94,8 +94,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TDocument">The type representing a Document.</typeparam> /// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> /// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param> /// <param name="documents">The list of documents to delete.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents) long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
@@ -106,8 +107,9 @@ namespace MongoDbGenericRepository.DataAccess.Delete
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam> /// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The Cancellation Token</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
} }
@@ -24,7 +24,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
#region Delete TKey #region Delete TKey
/// <inheritdoc /> /// <inheritdoc />
public virtual long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default) public virtual long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -33,7 +33,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null, CancellationToken cancellationToken = default) public virtual long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -41,7 +41,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken) public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -68,7 +68,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter)).DeletedCount; return (await HandlePartitioned<TDocument, TKey>(partitionKey).DeleteManyAsync(filter, cancellationToken)).DeletedCount;
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -103,14 +103,8 @@ namespace MongoDbGenericRepository.DataAccess.Delete
.DeletedCount; .DeletedCount;
} }
/// <summary> /// <inheritdoc />
/// Deletes a list of documents. public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
@@ -128,29 +122,22 @@ namespace MongoDbGenericRepository.DataAccess.Delete
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey)) foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
{ {
var groupIdsToDelete = group.Select(e => e.Id).ToArray(); var groupIdsToDelete = group.Select(e => e.Id).ToArray();
deleteCount += HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id)).DeletedCount; deleteCount += HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).DeleteMany(x => groupIdsToDelete.Contains(x.Id), cancellationToken).DeletedCount;
} }
return deleteCount; return deleteCount;
} }
var idsToDelete = documentList.Select(e => e.Id).ToArray(); var idsToDelete = documentList.Select(e => e.Id).ToArray();
return HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id)).DeletedCount; return HandlePartitioned<TDocument, TKey>(documentList.FirstOrDefault()).DeleteMany(x => idsToDelete.Contains(x.Id), cancellationToken).DeletedCount;
} }
/// <summary> /// <inheritdoc />
/// Deletes the documents matching the condition of the LINQ expression filter. public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns>
public virtual long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey> where TKey : IEquatable<TKey>
{ {
return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteMany(filter).DeletedCount; return HandlePartitioned<TDocument, TKey>(partitionKey).DeleteMany(filter, cancellationToken).DeletedCount;
} }
#endregion #endregion
@@ -236,6 +236,42 @@ namespace MongoDbGenericRepository
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Deletes a list of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
/// <summary> /// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter. /// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary> /// </summary>
@@ -244,8 +280,22 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>; where TKey : IEquatable<TKey>;
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
where TKey : IEquatable<TKey>;
} }
} }
@@ -164,14 +164,42 @@ namespace MongoDbGenericRepository
public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents) public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
return MongoDbEraser.DeleteMany<TDocument, TKey>(documents); return DeleteMany(documents, CancellationToken.None);
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) public virtual long DeleteMany<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey> where TDocument : IDocument<TKey>
{ {
return MongoDbEraser.DeleteMany<TDocument, TKey>(filter, partitionKey); return MongoDbEraser.DeleteMany<TDocument, TKey>(documents, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>
{
return DeleteMany<TDocument>(filter, null, CancellationToken.None);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{
return DeleteMany<TDocument>(filter, null, cancellationToken);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>
{
return DeleteMany<TDocument>(filter, partitionKey, CancellationToken.None);
}
/// <inheritdoc />
public virtual long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>
{
return MongoDbEraser.DeleteMany<TDocument, TKey>(filter, partitionKey, cancellationToken);
} }
} }
} }
@@ -201,6 +201,35 @@ namespace MongoDbGenericRepository
long DeleteMany<TDocument>(IEnumerable<TDocument> documents) long DeleteMany<TDocument>(IEnumerable<TDocument> documents)
where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
/// <summary>
/// Deletes a list of documents.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="documents">The list of documents to delete.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter)
where TDocument : IDocument<TKey>;
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="cancellationToken">The cancellation token</param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>;
/// <summary> /// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter. /// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary> /// </summary>
@@ -208,7 +237,18 @@ namespace MongoDbGenericRepository
/// <param name="filter">A LINQ expression filter.</param> /// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param> /// <param name="partitionKey">An optional partition key.</param>
/// <returns>The number of documents deleted.</returns> /// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey = null) long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey)
where TDocument : IDocument<TKey>;
/// <summary>
/// Deletes the documents matching the condition of the LINQ expression filter.
/// </summary>
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
/// <param name="filter">A LINQ expression filter.</param>
/// <param name="partitionKey">An optional partition key.</param>
/// <param name="cancellationToken"></param>
/// <returns>The number of documents deleted.</returns>
long DeleteMany<TDocument>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
where TDocument : IDocument<TKey>; where TDocument : IDocument<TKey>;
} }
} }