added unit tests for MongoDBCreator and missing CancellationTokens
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddManyAsyncTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocuments_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentsHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture
|
||||
.Build<TestDocumentWithKey<string>>()
|
||||
.Without(x => x.Id)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocumentWithKey<string>, string>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<TestDocumentWithKey<string>>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
documents.Should().AllSatisfy(d => d.Id.Should().NotBeNull());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocument_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<PartitionedTestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentsAndCancellationToken_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddManyAsync<PartitionedTestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertManyAsync(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
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.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddManyTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocuments_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentsHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture
|
||||
.Build<TestDocumentWithKey<string>>()
|
||||
.Without(x => x.Id)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocumentWithKey<string>, string>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<TestDocumentWithKey<string>>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
documents.Should().AllSatisfy(d => d.Id.Should().NotBeNull());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocument_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<PartitionedTestDocument, Guid>(documents);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentsAndCancellationToken_AddsMany()
|
||||
{
|
||||
// Arrange
|
||||
var documents = Fixture.CreateMany<TestDocument>().ToList();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<TestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<TestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var documents = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.CreateMany()
|
||||
.ToList();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddMany<PartitionedTestDocument, Guid>(documents, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertMany(
|
||||
It.Is<List<PartitionedTestDocument>>(l => l.All(d => documents.Contains(d))),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
using System;
|
||||
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.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddOneAsyncTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public async Task WithDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocumentWithKey<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocumentWithKey<string>, string>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<TestDocumentWithKey<string>>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
document.Id.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<PartitionedTestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
await Sut.AddOneAsync<PartitionedTestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOneAsync(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using AutoFixture;
|
||||
using CoreUnitTests.Infrastructure;
|
||||
using CoreUnitTests.Infrastructure.Model;
|
||||
using FluentAssertions;
|
||||
using MongoDB.Driver;
|
||||
using MongoDbGenericRepository;
|
||||
using MongoDbGenericRepository.DataAccess.Create;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
namespace CoreUnitTests.DataAccessTests.MongoDbCreatorTests;
|
||||
|
||||
public class AddOneTests : GenericTestContext<MongoDbCreator>
|
||||
{
|
||||
[Fact]
|
||||
public void WithDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentHavingNoId_SetsId()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocumentWithKey<string>();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocumentWithKey<string>>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocumentWithKey<string>>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocumentWithKey<string>, string>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<TestDocumentWithKey<string>>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
document.Id.Should().NotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocument_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<PartitionedTestDocument, Guid>(document);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
default),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var document = new TestDocument();
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<TestDocument>>();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<TestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<TestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<TestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void WithPartitionedDocumentAndCancellationToken_AddsOne()
|
||||
{
|
||||
// Arrange
|
||||
var partitionKey = Fixture.Create<string>();
|
||||
var document = Fixture.Build<PartitionedTestDocument>()
|
||||
.With(x => x.PartitionKey, partitionKey)
|
||||
.Create();
|
||||
var token = new CancellationToken(true);
|
||||
|
||||
var context = MockOf<IMongoDbContext>();
|
||||
var collection = MockOf<IMongoCollection<PartitionedTestDocument>>();
|
||||
|
||||
context
|
||||
.Setup(x => x.GetCollection<PartitionedTestDocument>(It.IsAny<string>()))
|
||||
.Returns(collection.Object);
|
||||
|
||||
// Act
|
||||
Sut.AddOne<PartitionedTestDocument, Guid>(document, token);
|
||||
|
||||
// Assert
|
||||
collection.Verify(
|
||||
x => x.InsertOne(
|
||||
It.Is<PartitionedTestDocument>(d => d == document),
|
||||
null,
|
||||
token),
|
||||
Times.Once());
|
||||
|
||||
context.Verify(x => x.GetCollection<PartitionedTestDocument>(partitionKey), Times.Once());
|
||||
}
|
||||
}
|
||||
@@ -31,7 +31,8 @@ namespace MongoDbGenericRepository.DataAccess.Create
|
||||
/// <typeparam name="TDocument">The type representing a Document.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key for a Document.</typeparam>
|
||||
/// <param name="document">The document you want to add.</param>
|
||||
void AddOne<TDocument, TKey>(TDocument document)
|
||||
/// <param name="cancellationToken">An optional cancellation Token.</param>
|
||||
void AddOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -54,7 +55,8 @@ namespace MongoDbGenericRepository.DataAccess.Create
|
||||
/// <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 documents you want to add.</param>
|
||||
void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
/// <param name="cancellationToken">An optional cancellation Token.</param>
|
||||
void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
}
|
||||
|
||||
@@ -1,68 +1,48 @@
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using MongoDbGenericRepository.DataAccess.Base;
|
||||
using MongoDbGenericRepository.Models;
|
||||
using MongoDbGenericRepository.Utils;
|
||||
|
||||
namespace MongoDbGenericRepository.DataAccess.Create
|
||||
{
|
||||
/// <summary>
|
||||
/// A class to insert MongoDb document.
|
||||
/// A class to insert MongoDb document.
|
||||
/// </summary>
|
||||
public class MongoDbCreator : DataAccessBase, IMongoDbCreator
|
||||
{
|
||||
/// <summary>
|
||||
/// The construct of the MongoDbCreator class.
|
||||
/// The construct of the MongoDbCreator class.
|
||||
/// </summary>
|
||||
/// <param name="mongoDbContext">A <see cref="IMongoDbContext"/> instance.</param>
|
||||
/// <param name="mongoDbContext">A <see cref="IMongoDbContext" /> instance.</param>
|
||||
public MongoDbCreator(IMongoDbContext mongoDbContext) : base(mongoDbContext)
|
||||
{
|
||||
}
|
||||
|
||||
#region Create TKey
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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="document">The document you want to add.</param>
|
||||
/// <param name="cancellationToken">An optional cancellation Token.</param>
|
||||
/// <inheritdoc />
|
||||
public virtual async Task AddOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
await HandlePartitioned<TDocument, TKey>(document).InsertOneAsync(document, null, cancellationToken);
|
||||
await HandlePartitioned<TDocument, TKey>(document)
|
||||
.InsertOneAsync(document, null, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a document to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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="document">The document you want to add.</param>
|
||||
public virtual void AddOne<TDocument, TKey>(TDocument document)
|
||||
/// <inheritdoc />
|
||||
public virtual void AddOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
FormatDocument<TDocument, TKey>(document);
|
||||
HandlePartitioned<TDocument, TKey>(document).InsertOne(document);
|
||||
HandlePartitioned<TDocument, TKey>(document)
|
||||
.InsertOne(document, null, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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 documents you want to add.</param>
|
||||
/// <param name="cancellationToken">An optional cancellation Token.</param>
|
||||
/// <inheritdoc />
|
||||
public virtual async Task AddManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
@@ -82,7 +62,7 @@ namespace MongoDbGenericRepository.DataAccess.Create
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documentsList.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documentsList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
foreach (var group in documentsList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
|
||||
{
|
||||
await HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertManyAsync(group.ToList(), null, cancellationToken);
|
||||
}
|
||||
@@ -93,14 +73,8 @@ namespace MongoDbGenericRepository.DataAccess.Create
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a list of documents to the collection.
|
||||
/// Populates the Id and AddedAtUtc fields if necessary.
|
||||
/// </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 documents you want to add.</param>
|
||||
public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents)
|
||||
/// <inheritdoc />
|
||||
public virtual void AddMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
@@ -119,21 +93,19 @@ namespace MongoDbGenericRepository.DataAccess.Create
|
||||
// cannot use typeof(IPartitionedDocument).IsAssignableFrom(typeof(TDocument)), not available in netstandard 1.5
|
||||
if (documentList.Any(e => e is IPartitionedDocument))
|
||||
{
|
||||
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument)e).PartitionKey))
|
||||
foreach (var group in documentList.GroupBy(e => ((IPartitionedDocument) e).PartitionKey))
|
||||
{
|
||||
HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertMany(group.ToList());
|
||||
HandlePartitioned<TDocument, TKey>(group.FirstOrDefault()).InsertMany(group.ToList(), cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
GetCollection<TDocument, TKey>().InsertMany(documentList.ToList());
|
||||
GetCollection<TDocument, TKey>().InsertMany(documentList.ToList(), cancellationToken: cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the document Id if it is not set already.
|
||||
/// Sets the value of the document Id if it is not set already.
|
||||
/// </summary>
|
||||
/// <typeparam name="TDocument">The document type.</typeparam>
|
||||
/// <typeparam name="TKey">The type of the primary key.</typeparam>
|
||||
@@ -146,6 +118,7 @@ namespace MongoDbGenericRepository.DataAccess.Create
|
||||
{
|
||||
throw new ArgumentNullException(nameof(document));
|
||||
}
|
||||
|
||||
var defaultTKey = default(TKey);
|
||||
if (document.Id == null
|
||||
|| (defaultTKey != null
|
||||
|
||||
@@ -21,7 +21,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <param name="document">The document you want to delete.</param>
|
||||
/// <param name="cancellationToken">An optional cancellation token</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
|
||||
long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <param name="cancellationToken">An optional cancellation token</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
|
||||
long DeleteOne<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = default, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <param name="document">The document you want to delete.</param>
|
||||
/// <param name="cancellationToken">The cancellation token</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
|
||||
Task<long> DeleteOneAsync<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <param name="cancellationToken">The cancellation token</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
|
||||
Task<long> DeleteOneAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = default, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <param name="partitionKey">An optional partition key.</param>
|
||||
/// <param name="cancellationToken">The cancellation token</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey, CancellationToken cancellationToken)
|
||||
Task<long> DeleteManyAsync<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = default, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -84,7 +84,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <param name="documents">The list of documents to delete.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The number of documents deleted.</returns>
|
||||
Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken)
|
||||
Task<long> DeleteManyAsync<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -96,7 +96,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <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)
|
||||
long DeleteMany<TDocument, TKey>(IEnumerable<TDocument> documents, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
|
||||
@@ -109,7 +109,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
/// <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)
|
||||
long DeleteMany<TDocument, TKey>(Expression<Func<TDocument, bool>> filter, string partitionKey = default, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>;
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace MongoDbGenericRepository.DataAccess.Delete
|
||||
#region Delete TKey
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken)
|
||||
public virtual long DeleteOne<TDocument, TKey>(TDocument document, CancellationToken cancellationToken = default)
|
||||
where TDocument : IDocument<TKey>
|
||||
where TKey : IEquatable<TKey>
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user