Skip to content

Commit

Permalink
Merge pull request #37 from DFE-Digital/notes-unit-tests
Browse files Browse the repository at this point in the history
notes unit tests
  • Loading branch information
paullocknimble authored Jan 14, 2025
2 parents b706c35 + 600006c commit a3d4808
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using AutoFixture;
using Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.CreateSupportProjectNote;
using Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.UpdateSupportProject;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Interfaces.Repositories;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.ValueObjects;
using Moq;
using System.Linq.Expressions;
using Dfe.RegionalImprovementForStandardsAndExcellence.Utils;

namespace Dfe.RegionalImprovementForStandardsAndExcellence.Application.Tests.CommandHandlers.SupportProject.Notes;

public class CreateSupportProjectNoteCommandHandlerTests
{
private readonly Mock<ISupportProjectRepository> _mockSupportProjectRepository;
private readonly Domain.Entities.SupportProject.SupportProject _mockSupportProject;
private readonly CancellationToken _cancellationToken;
private readonly Mock<IDateTimeProvider> _iDateTimeProvider;

public CreateSupportProjectNoteCommandHandlerTests()
{
_mockSupportProjectRepository = new Mock<ISupportProjectRepository>();
var fixture = new Fixture();
_mockSupportProject = fixture.Create<Domain.Entities.SupportProject.SupportProject>();
_cancellationToken = new CancellationToken();
_cancellationToken = CancellationToken.None;
_iDateTimeProvider = new Mock<IDateTimeProvider>();
}

[Fact]
public async Task Handle_ValidCommand_CreateNote()
{

var command = new CreateSupportProjectNote.CreateSupportProjectNoteCommand(
_mockSupportProject.Id,
"Note",
"[email protected]"

);

_mockSupportProjectRepository.Setup(repo => repo.FindAsync(It.IsAny<Expression<Func<Domain.Entities.SupportProject.SupportProject, bool>>>(), It.IsAny<CancellationToken>())).ReturnsAsync(_mockSupportProject);
var CreateNoteCommandHandler =
new CreateSupportProjectNote.CreateSupportProjectNoteCommandHandler(_mockSupportProjectRepository.Object,_iDateTimeProvider.Object);

var result = await CreateNoteCommandHandler.Handle(command, _cancellationToken);
Assert.IsType<SupportProjectNoteId>(result);

Assert.IsType<Guid>(result.Value);

_mockSupportProjectRepository.Verify(repo => repo.UpdateAsync(It.IsAny<Domain.Entities.SupportProject.SupportProject>(), It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task Handle_EmptyCommand_CreateNote()
{
var command = new CreateSupportProjectNote.CreateSupportProjectNoteCommand(
null,
null,
null
);

_mockSupportProjectRepository.Setup(repo => repo.FindAsync(It.IsAny<Expression<Func<Domain.Entities.SupportProject.SupportProject, bool>>>(), It.IsAny<CancellationToken>())).ReturnsAsync(_mockSupportProject);
var createNoteCommandHandler =
new CreateSupportProjectNote.CreateSupportProjectNoteCommandHandler(_mockSupportProjectRepository.Object,_iDateTimeProvider.Object);

var result = await createNoteCommandHandler.Handle(command, _cancellationToken);
Assert.IsType<SupportProjectNoteId>(result);

Assert.IsType<Guid>(result.Value);

_mockSupportProjectRepository.Verify(repo => repo.UpdateAsync(It.IsAny<Domain.Entities.SupportProject.SupportProject>(), It.IsAny<CancellationToken>()), Times.Once);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using AutoFixture;
using Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.CreateSupportProjectNote;
using Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.UpdateSupportProject;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.Interfaces.Repositories;
using Dfe.RegionalImprovementForStandardsAndExcellence.Domain.ValueObjects;
using Moq;
using System.Linq.Expressions;
using Dfe.RegionalImprovementForStandardsAndExcellence.Application.SupportProject.Commands.EditSupportProjectNote;
using Dfe.RegionalImprovementForStandardsAndExcellence.Utils;

namespace Dfe.RegionalImprovementForStandardsAndExcellence.Application.Tests.CommandHandlers.SupportProject.Notes;

public class EditSupportProjectNoteCommandHandlerTests
{
private readonly Mock<ISupportProjectRepository> _mockSupportProjectRepository;
private readonly Domain.Entities.SupportProject.SupportProject _mockSupportProject;
private readonly Domain.Entities.SupportProject.SupportProjectNote _mockSupportProjectNote;
private readonly CancellationToken _cancellationToken;
private readonly Mock<IDateTimeProvider> _iDateTimeProvider;

public EditSupportProjectNoteCommandHandlerTests()
{
_mockSupportProjectRepository = new Mock<ISupportProjectRepository>();
var fixture = new Fixture();
_mockSupportProject = fixture.Create<Domain.Entities.SupportProject.SupportProject>();
_mockSupportProjectNote = fixture.Create<Domain.Entities.SupportProject.SupportProjectNote>();
_cancellationToken = new CancellationToken();
_cancellationToken = CancellationToken.None;
_iDateTimeProvider = new Mock<IDateTimeProvider>();
}

[Fact]
public async Task Handle_ValidCommand_EditNote()
{

var command = new EditSupportProjectNote.EditSupportProjectNoteCommand(
_mockSupportProjectNote.SupportProjectId,
"Note",_mockSupportProjectNote.Id,_mockSupportProjectNote.CreatedBy);

_mockSupportProject.AddNote(_mockSupportProjectNote.Id,
_mockSupportProjectNote.Note,
_mockSupportProjectNote.CreatedBy,
_mockSupportProjectNote.CreatedOn,
_mockSupportProjectNote.SupportProjectId);

_mockSupportProjectRepository.Setup(repo => repo.GetSupportProjectById(It.IsAny<SupportProjectId>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(_mockSupportProject);

var editSupportProjectNoteCommandHandler =
new EditSupportProjectNote.EditSupportProjectNoteCommandHandler(_mockSupportProjectRepository.Object,_iDateTimeProvider.Object);

var result = await editSupportProjectNoteCommandHandler.Handle(command, _cancellationToken);
Assert.IsType<SupportProjectNoteId>(result);

Assert.Equal(result,_mockSupportProjectNote.Id);

_mockSupportProjectRepository.Verify(repo => repo.UpdateAsync(It.IsAny<Domain.Entities.SupportProject.SupportProject>(), It.IsAny<CancellationToken>()), Times.Once);
}

[Fact]
public async Task Handle_InvalidValidCommand_EditNote()
{

var command = new EditSupportProjectNote.EditSupportProjectNoteCommand(
null,
"Note",null,_mockSupportProjectNote.CreatedBy);

_mockSupportProjectRepository.Setup(repo => repo.GetSupportProjectById(It.IsAny<SupportProjectId>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(_mockSupportProject);

var editSupportProjectNoteCommandHandler =
new EditSupportProjectNote.EditSupportProjectNoteCommandHandler(_mockSupportProjectRepository.Object,_iDateTimeProvider.Object);

var result = await editSupportProjectNoteCommandHandler.Handle(command, _cancellationToken);
Assert.IsNotType<SupportProjectNoteId>(result);

_mockSupportProjectRepository.Verify(repo => repo.UpdateAsync(It.IsAny<Domain.Entities.SupportProject.SupportProject>(), It.IsAny<CancellationToken>()), Times.Once);
}


}

0 comments on commit a3d4808

Please sign in to comment.