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.Linq;
using System.Linq.Expressions;
using System.Threading;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
@@ -23,7 +24,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -31,7 +32,28 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert
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]
@@ -46,7 +68,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -54,7 +76,31 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert
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]
@@ -70,7 +116,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -78,7 +124,32 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert
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
@@ -92,7 +163,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -100,7 +171,28 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert
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]
@@ -115,7 +207,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -123,7 +215,31 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert
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]
@@ -139,7 +255,7 @@ public class DeleteManyTests : TestMongoRepositoryContext
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -147,8 +263,32 @@ public class DeleteManyTests : TestMongoRepositoryContext
// Assert
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
}
}
@@ -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>
{
[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]
public void WithDocumentAndCancellationToken_DeletesOne()
{
@@ -61,7 +31,7 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
var dbContext = MockOf<IMongoDbContext>();
dbContext
.Setup(x => x.GetCollection<TestDocument>(null))
.Returns(Fixture.Create<IMongoCollection<TestDocument>>());
.Returns(collection.Object);
// Act
var result = Sut.DeleteOne<TestDocument, Guid>(document, token);
@@ -73,98 +43,8 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
collection.Verify(
x => x.DeleteOne(
It.Is<FilterDefinition<TestDocument>>(f => f.EquivalentTo(expectedFilter)),
token));
}
[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));
token),
Times.Once());
}
[Fact]
@@ -184,7 +64,7 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
var dbContext = MockOf<IMongoDbContext>();
dbContext
.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;
@@ -195,7 +75,9 @@ public class DeleteOneTests : GenericTestContext<MongoDbEraser>
result.Should().Be(count);
collection.Verify(
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));
}
@@ -6,4 +6,4 @@ public class PartitionedTestDocument : TestDocument, IPartitionedDocument
{
/// <inheritdoc />
public string PartitionKey { get; set; } = "PartitionedTestDocument";
}
}
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
@@ -23,7 +24,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -31,7 +32,29 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
// Assert
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]
@@ -46,7 +69,7 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -54,7 +77,35 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
// Assert
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]
@@ -70,7 +121,11 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
Eraser = new Mock<IMongoDbEraser>();
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);
// Act
@@ -78,6 +133,34 @@ public class DeleteManyTests : TestKeyedMongoRepositoryContext<int>
// Assert
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);
}
}