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 { [Fact] public void WithDocument_AddsOne() { // Arrange var document = new TestDocument(); var context = MockOf(); var collection = MockOf>(); context .Setup(x => x.GetCollection(It.IsAny())) .Returns(collection.Object); // Act Sut.AddOne(document); // Assert collection.Verify( x => x.InsertOne( It.Is(d => d == document), null, default), Times.Once()); } [Fact] public void WithDocumentHavingNoId_SetsId() { // Arrange var document = new TestDocumentWithKey(); var context = MockOf(); var collection = MockOf>>(); context .Setup(x => x.GetCollection>(It.IsAny())) .Returns(collection.Object); // Act Sut.AddOne, string>(document); // Assert collection.Verify( x => x.InsertOne( It.Is>(d => d == document), null, default), Times.Once()); document.Id.Should().NotBeNull(); } [Fact] public void WithPartitionedDocument_AddsOne() { // Arrange var partitionKey = Fixture.Create(); var document = Fixture.Build() .With(x => x.PartitionKey, partitionKey) .Create(); var context = MockOf(); var collection = MockOf>(); context .Setup(x => x.GetCollection(It.IsAny())) .Returns(collection.Object); // Act Sut.AddOne(document); // Assert collection.Verify( x => x.InsertOne( It.Is(d => d == document), null, default), Times.Once()); context.Verify(x => x.GetCollection(partitionKey), Times.Once()); } [Fact] public void WithDocumentAndCancellationToken_AddsOne() { // Arrange var document = new TestDocument(); var context = MockOf(); var collection = MockOf>(); var token = new CancellationToken(true); context .Setup(x => x.GetCollection(It.IsAny())) .Returns(collection.Object); // Act Sut.AddOne(document, token); // Assert collection.Verify( x => x.InsertOne( It.Is(d => d == document), null, token), Times.Once()); } [Fact] public void WithPartitionedDocumentAndCancellationToken_AddsOne() { // Arrange var partitionKey = Fixture.Create(); var document = Fixture.Build() .With(x => x.PartitionKey, partitionKey) .Create(); var token = new CancellationToken(true); var context = MockOf(); var collection = MockOf>(); context .Setup(x => x.GetCollection(It.IsAny())) .Returns(collection.Object); // Act Sut.AddOne(document, token); // Assert collection.Verify( x => x.InsertOne( It.Is(d => d == document), null, token), Times.Once()); context.Verify(x => x.GetCollection(partitionKey), Times.Once()); } }