unit tests for add

This commit is contained in:
Sean Garrett
2023-06-18 18:37:30 +01:00
parent 39e93dc968
commit ed2eddd33e
9 changed files with 451 additions and 27 deletions
@@ -0,0 +1,77 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create;
using Moq;
using Xunit;
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
public class AddManyAsyncTests : TestMongoRepositoryContext
{
[Fact]
public async Task WithDocument_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocument>().ToList();
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddManyAsync(documents);
// Assert
Creator.Verify(x => x.AddManyAsync<TestDocument, Guid>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddManyAsync(documents, token);
// Assert
Creator.Verify(x => x.AddManyAsync<TestDocument, Guid>(documents, token), Times.Once);
}
#region Keyed
[Fact]
public async Task Keyed_WithDocument_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddManyAsync<TestDocumentWithKey<int>, int>(documents);
// Assert
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddManyAsync<TestDocumentWithKey<int>, int>(documents, token);
// Assert
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
}
#endregion
}
@@ -1,7 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using AutoFixture;
using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model; using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create; using MongoDbGenericRepository.DataAccess.Create;
@@ -14,20 +14,64 @@ public class AddManyTests : TestMongoRepositoryContext
{ {
[Fact] [Fact]
public async Task AddManyAsync_EnsureTokenPassed() public void WithDocument_ShouldAddOne()
{ {
// Arrange // Arrange
var documents = Fixture.CreateMany<TestDocument>().ToList();
Creator = new Mock<IMongoDbCreator>(); Creator = new Mock<IMongoDbCreator>();
var token = new CancellationToken(true);
var documents = new List<TestDocument>
{
new(), new(), new()
};
// Act // Act
await Sut.AddManyAsync(documents, token); Sut.AddMany(documents);
// Assert // Assert
Creator.Verify(x => x.AddManyAsync<TestDocument, Guid>(documents, token)); Creator.Verify(x => x.AddMany<TestDocument, Guid>(documents, CancellationToken.None), Times.Once);
} }
[Fact]
public void WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocument>().ToList();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddMany(documents, token);
// Assert
Creator.Verify(x => x.AddMany<TestDocument, Guid>(documents, token), Times.Once);
}
#region Keyed
[Fact]
public void Keyed_WithDocument_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddMany<TestDocumentWithKey<int>, int>(documents);
// Assert
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddMany<TestDocumentWithKey<int>, int>(documents, token);
// Assert
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
}
#endregion
} }
@@ -0,0 +1,76 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create;
using Moq;
using Xunit;
namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
public class AddOneAsyncTests : TestMongoRepositoryContext
{
[Fact]
public async Task WithDocument_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddOneAsync(document);
// Assert
Creator.Verify(x => x.AddOneAsync<TestDocument, Guid>(document, CancellationToken.None), Times.Once);
}
[Fact]
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddOneAsync(document, token);
// Assert
Creator.Verify(x => x.AddOneAsync<TestDocument, Guid>(document, token), Times.Once);
}
#region Keyed
[Fact]
public async Task Keyed_WithDocument_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddOneAsync<TestDocumentWithKey<int>, int>(document);
// Assert
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
}
[Fact]
public async Task Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddOneAsync<TestDocumentWithKey<int>, int>(document, token);
// Assert
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, token), Times.Once);
}
#endregion
}
@@ -1,6 +1,6 @@
using System; using System;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using AutoFixture;
using CoreUnitTests.Infrastructure; using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model; using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create; using MongoDbGenericRepository.DataAccess.Create;
@@ -12,17 +12,64 @@ namespace CoreUnitTests.BaseMongoRepositoryTests.AddTests;
public class AddOneTests : TestMongoRepositoryContext public class AddOneTests : TestMongoRepositoryContext
{ {
[Fact] [Fact]
public async Task AddOneAsync_EnsureTokenPassed() public void WithDocument_ShouldAddOne()
{ {
// Arrange // Arrange
var document = Fixture.Create<TestDocument>();
Creator = new Mock<IMongoDbCreator>(); Creator = new Mock<IMongoDbCreator>();
var token = new CancellationToken(true);
var document = new TestDocument();
// Act // Act
await Sut.AddOneAsync<TestDocument>(document, token); Sut.AddOne(document);
// Assert // Assert
Creator.Verify(x => x.AddOneAsync<TestDocument, Guid>(document, token)); Creator.Verify(x => x.AddOne<TestDocument, Guid>(document, CancellationToken.None), Times.Once);
} }
[Fact]
public void WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocument>();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddOne(document, token);
// Assert
Creator.Verify(x => x.AddOne<TestDocument, Guid>(document, token), Times.Once);
}
#region Keyed
[Fact]
public void Keyed_WithDocument_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddOne<TestDocumentWithKey<int>, int>(document);
// Assert
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
}
[Fact]
public void Keyed_WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddOne<TestDocumentWithKey<int>, int>(document, token);
// Assert
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, token), Times.Once);
}
#endregion
} }
@@ -0,0 +1,43 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create;
using Moq;
using Xunit;
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
public class AddManyAsyncTests : TestKeyedMongoRepositoryContext<int>
{
[Fact]
public async Task WithDocument_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddManyAsync(documents);
// Assert
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddManyAsync(documents, token);
// Assert
Creator.Verify(x => x.AddManyAsync<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
}
}
@@ -0,0 +1,43 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create;
using Moq;
using Xunit;
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
public class AddManyTests : TestKeyedMongoRepositoryContext<int>
{
[Fact]
public void WithDocument_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddMany(documents);
// Assert
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, CancellationToken.None), Times.Once);
}
[Fact]
public void WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var documents = Fixture.CreateMany<TestDocumentWithKey<int>>().ToList();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddMany(documents, token);
// Assert
Creator.Verify(x => x.AddMany<TestDocumentWithKey<int>, int>(documents, token), Times.Once);
}
}
@@ -0,0 +1,42 @@
using System.Threading;
using System.Threading.Tasks;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create;
using Moq;
using Xunit;
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
public class AddOneAsyncTests : TestKeyedMongoRepositoryContext<int>
{
[Fact]
public async Task WithDocument_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddOneAsync(document);
// Assert
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
}
[Fact]
public async Task WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
await Sut.AddOneAsync(document, token);
// Assert
Creator.Verify(x => x.AddOneAsync<TestDocumentWithKey<int>, int>(document, token), Times.Once);
}
}
@@ -0,0 +1,41 @@
using System.Threading;
using AutoFixture;
using CoreUnitTests.Infrastructure;
using CoreUnitTests.Infrastructure.Model;
using MongoDbGenericRepository.DataAccess.Create;
using Moq;
using Xunit;
namespace CoreUnitTests.KeyTypedRepositoryTests.AddTests;
public class AddOneTests : TestKeyedMongoRepositoryContext<int>
{
[Fact]
public void WithDocument_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddOne(document);
// Assert
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, CancellationToken.None), Times.Once);
}
[Fact]
public void WithDocumentAndCancellationToken_ShouldAddOne()
{
// Arrange
var document = Fixture.Create<TestDocumentWithKey<int>>();
var token = new CancellationToken(true);
Creator = new Mock<IMongoDbCreator>();
// Act
Sut.AddOne(document, token);
// Assert
Creator.Verify(x => x.AddOne<TestDocumentWithKey<int>, int>(document, token), Times.Once);
}
}
@@ -1,10 +1,10 @@
using MongoDbGenericRepository.DataAccess.Delete; using System;
using MongoDbGenericRepository.Models;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using MongoDbGenericRepository.DataAccess.Delete;
using MongoDbGenericRepository.Models;
namespace MongoDbGenericRepository namespace MongoDbGenericRepository
{ {
@@ -19,7 +19,10 @@ namespace MongoDbGenericRepository
{ {
get get
{ {
if (_mongoDbEraser != null) { return _mongoDbEraser; } if (_mongoDbEraser != null)
{
return _mongoDbEraser;
}
lock (_initLock) lock (_initLock)
{ {
@@ -31,7 +34,7 @@ namespace MongoDbGenericRepository
return _mongoDbEraser; return _mongoDbEraser;
} }
set { _mongoDbEraser = value; } set => _mongoDbEraser = value;
} }
#region Delete #region Delete
@@ -114,7 +117,10 @@ namespace MongoDbGenericRepository
} }
/// <inheritdoc /> /// <inheritdoc />
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 DeleteOneAsync<TDocument, Guid>(filter, partitionKey, cancellationToken); return await DeleteOneAsync<TDocument, Guid>(filter, partitionKey, cancellationToken);
@@ -297,7 +303,10 @@ namespace MongoDbGenericRepository
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual async Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken) public virtual async Task<long> DeleteOneAsync<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>
{ {
@@ -329,7 +338,10 @@ namespace MongoDbGenericRepository
} }
/// <inheritdoc /> /// <inheritdoc />
public virtual async Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken) public virtual async Task<long> DeleteManyAsync<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>
{ {
@@ -401,6 +413,5 @@ namespace MongoDbGenericRepository
} }
#endregion #endregion
} }
} }