Skip to content

Commit

Permalink
fix(aait.backend.1a):testing added
Browse files Browse the repository at this point in the history
  • Loading branch information
tans1 committed Sep 2, 2023
1 parent 56e423b commit 20c42d7
Show file tree
Hide file tree
Showing 72 changed files with 652 additions and 623 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
using Application.Contracts;
using Application.DTO.CommentDTO.DTO;
using Application.Profiles;
using Application.Response;
using Application.Tests.Mocks;
using AutoMapper;
using Moq;
using Shouldly;
using Application.Features.CommentFeatures.Handlers.Commands;
using Application.Features.CommentFeatures.Handlers.Queries;
using Application.Features.CommentFeatures.Requests.Commands;
using Application.Exceptions;
using MediatR;
using Moq;
using Application.Contracts;
using Application.Features.NotificationFeaure.Requests.Commands;

namespace Application.Tests.Features.CommentFeatureTest.Commands
{
public class CreateCommentHandlerTest
{
private readonly IMapper _mapper;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

private readonly Mock<IMediator> _mediator;
private CommentCreateDTO testComment;
public CreateCommentHandlerTest()
{

var mapperConfig = new MapperConfiguration(c =>
_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();

var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});

_mapper = mapperConfig.CreateMapper();
_mapper = mapperConfig.CreateMapper();
_mediator = new Mock<IMediator>();

testComment = new CommentCreateDTO()
{
PostId = 1,
Expand All @@ -36,33 +44,50 @@ public CreateCommentHandlerTest()
[Fact]
public async Task ValidCreateCommentTest()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var mocPostRepository = MockPostRepository.GetPostRepository().Object;
var handler = new CommentCreateHandler(_mapper,mocCommentRepository,mocPostRepository );
var handler = new CommentCreateHandler(_mockUnitOfWork.Object,_mapper,_mediator.Object );

// act
var result = await handler.Handle(new CommentCreateCommand() {NewCommentData = testComment,userId = 1}, CancellationToken.None);

// assert
result.ShouldBeOfType<BaseResponse<CommentResponseDTO>>();
}

[Fact]
public async Task CreateCommentForNonExistentPostTest()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var mocPostRepository = MockPostRepository.GetPostRepository().Object;
var handler = new CommentCreateHandler(_mapper,mocCommentRepository,mocPostRepository );
var handler = new CommentCreateHandler(_mockUnitOfWork.Object,_mapper,_mediator.Object );
testComment.PostId = 100;

// act and assert
await Should.ThrowAsync<NotFoundException>(async () =>
await handler.Handle(new CommentCreateCommand() {NewCommentData = testComment,userId = 1}, CancellationToken.None));
}
[Fact]
public async Task CreateCommentWithNoContentTest()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var mocPostRepository = MockPostRepository.GetPostRepository().Object;
var handler = new CommentCreateHandler(_mapper,mocCommentRepository,mocPostRepository );
var handler = new CommentCreateHandler(_mockUnitOfWork.Object,_mapper,_mediator.Object );
testComment.Content = "";

// act and arrange

await Should.ThrowAsync<ValidationException>(async () =>
await handler.Handle(new CommentCreateCommand() {NewCommentData = testComment,userId = 1}, CancellationToken.None));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Contracts;
using Application.DTO.CommentDTO.DTO;
using Application.Exceptions;
using Application.Features.CommentFeatures.Handlers.Commands;
using Application.Features.CommentFeatures.Requests.Commands;
using Application.Profiles;
using Application.Features.NotificationFeaure.Requests.Commands;
using Application.Response;
using Application.Tests.Mocks;
using AutoMapper;
using MediatR;
using Moq;
using Shouldly;
using Xunit;

namespace Application.Tests.Features.CommentFeatureTest.Commands
{
public class DeleteCommentHandlerTest
{
private readonly IMapper _mapper;
private readonly Mock<ICommentRepository> _mockRepo;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

private readonly Mock<IMediator> _mediator;
public DeleteCommentHandlerTest()
{
_mockRepo = MockCommentRepository.GetCommentRepository();

var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});
_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();
_mediator = new Mock<IMediator>();

_mapper = mapperConfig.CreateMapper();
}

[Fact]
public async Task DeleteCommentValidTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var handler = new CommentDeleteHandler(mocCommentRepository);
var handler = new CommentDeleteHandler(_mockUnitOfWork.Object, _mediator.Object);

var result = await handler.Handle(new CommentDeleteCommand() { Id = 1 }, CancellationToken.None);

Expand All @@ -46,8 +39,11 @@ public async Task DeleteCommentValidTest()
[Fact]
public async Task DeleteCommentWithInValidIdTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var handler = new CommentDeleteHandler(mocCommentRepository);
var handler = new CommentDeleteHandler(_mockUnitOfWork.Object, _mediator.Object);

await Should.ThrowAsync<NotFoundException>(async () =>
await handler.Handle(new CommentDeleteCommand(), CancellationToken.None));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Contracts;
using Application.DTO.CommentDTO.DTO;
using Application.Exceptions;
using Application.Features.CommentFeatures.Handlers.Commands;
using Application.Features.CommentFeatures.Requests.Commands;
using Application.Features.NotificationFeaure.Requests.Commands;
using Application.Profiles;
using Application.Response;
using Application.Tests.Mocks;
using AutoMapper;
using MediatR;
using Moq;
using Shouldly;
using Xunit;

namespace Application.Tests.Features.CommentFeatureTest.Commands
{
public class UpdateCommentHandlerTest
{
private readonly IMapper _mapper;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

private readonly Mock<IMediator> _mediator;
CommentUpdateDTO testComment;
public UpdateCommentHandlerTest()
{

var mapperConfig = new MapperConfiguration(c =>
_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();

var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});

_mapper = mapperConfig.CreateMapper();
_mapper = mapperConfig.CreateMapper();
_mediator = new Mock<IMediator>();

testComment = new CommentUpdateDTO()
{
Content = "Test comment"
Expand All @@ -39,9 +43,12 @@ public UpdateCommentHandlerTest()
[Fact]
public async Task UpdateCommentValidTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new UpdateCommentHandler(_mockRepo.Object, _mapper);
var handler = new UpdateCommentHandler(_mockUnitOfWork.Object, _mapper, _mediator.Object);

testComment.Content = "Updated comment";
var result = await handler.Handle(new UpdateCommentCommand()
Expand All @@ -58,9 +65,12 @@ public async Task UpdateCommentValidTest()
[Fact]
public async Task UpdateCommentWithNoContentTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new UpdateCommentHandler(_mockRepo.Object, _mapper);
var handler = new UpdateCommentHandler(_mockUnitOfWork.Object, _mapper, _mediator.Object);

testComment.Content = "";
await Should.ThrowAsync<ValidationException>(async () => await handler.Handle(new UpdateCommentCommand()
Expand All @@ -75,9 +85,12 @@ await Should.ThrowAsync<ValidationException>(async () => await handler.Handle(ne
[Fact]
public async Task UpdateNonExistentCommentTest()
{
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new UpdateCommentHandler(_mockRepo.Object, _mapper);
var handler = new UpdateCommentHandler(_mockUnitOfWork.Object, _mapper, _mediator.Object);

testComment.Content = "Updated comment";
await Should.ThrowAsync<NotFoundException>(async () => await handler.Handle(new UpdateCommentCommand()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,26 @@ namespace Application.Tests.Features.CommentFeatureTest.Queries
public class GetCommentHandlerTest
{
private readonly IMapper _mapper;
private readonly Mock<ICommentRepository> _mockRepo;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

public GetCommentHandlerTest()
{
_mockRepo = MockCommentRepository.GetCommentRepository();
_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();

var mapperConfig = new MapperConfiguration(c =>
var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});

_mapper = mapperConfig.CreateMapper();
_mapper = mapperConfig.CreateMapper();
}

[Fact]
public async Task GetValidCommentTest()
{
var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new GetCommentQueryHandler(_mockRepo.Object, _mapper);
var handler = new GetCommentQueryHandler(_mockUnitOfWork.Object, _mapper);
var result = await handler.Handle(new GetSingleCommentQuery()
{
Id = 1
Expand All @@ -52,7 +52,7 @@ public async Task GetInValidCommentTest()
{
var _mockRepo = MockCommentRepository.GetCommentRepository();

var handler = new GetCommentQueryHandler(_mockRepo.Object, _mapper);
var handler = new GetCommentQueryHandler(_mockUnitOfWork.Object, _mapper);

await Should.ThrowAsync<BadRequestException>(async () => await handler.Handle(new GetSingleCommentQuery()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Application.Contracts;
using Application.DTO.Common;
using Application.Exceptions;
using Application.Features.CommentReactionFeature.Handlers.Commands;
using Application.Features.CommentReactionFeature.Requests.Commands;
using Application.Features.NotificationFeaure.Requests.Commands;
using Application.Profiles;
using Application.Response;
using Application.Tests.Mocks;
using Application.Tests.Mocs;
using AutoMapper;
using Domain.Entites;
using MediatR;
using Moq;
using Shouldly;

Expand All @@ -20,19 +18,20 @@ namespace Application.Tests.Features.CommentReactionFeature.Commands
public class CreateCommentReactionCommandTest
{
private readonly IMapper _mapper;
private readonly Mock<IUnitOfWork> _mockUnitOfWork;

private readonly Mock<IMediator> _mediator;
private ReactionDTO testReaction;
public CreateCommentReactionCommandTest()
{

_mockUnitOfWork = MockUnitOfWork.GetUnitOfWork();
var mapperConfig = new MapperConfiguration(c =>
{
c.AddProfile<MappingProfile>();
});

_mapper = mapperConfig.CreateMapper();



_mapper = mapperConfig.CreateMapper();
_mediator = new Mock<IMediator>();
testReaction = new ReactionDTO
{
ReactedId = 1,
Expand All @@ -43,21 +42,32 @@ public CreateCommentReactionCommandTest()
[Fact]
public async Task Valid_CommentReaction_Added()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);

var mocCommentReactionRepository = MockCommentReactionRepository.GetCommentReactionRepository().Object;
var mockCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var _handler = new MakeCommentReactionHandler(mocCommentReactionRepository,_mapper,mockCommentRepository);

var _handler = new MakeCommentReactionHandler(_mapper,_mockUnitOfWork.Object,_mediator.Object);

// act
var result = await _handler.Handle(new MakeReactionOnComment() { UserId = 1,ReactionData = testReaction}, CancellationToken.None);
// assert
result.ShouldBeOfType<BaseResponse<int>>();
}

[Fact]
public async Task IValid_CommentReaction_Added()
{
// arrange
_mediator.Setup(
x => x.Send(It.IsAny<CreateNotification>(), It.IsAny<CancellationToken>())).ReturnsAsync(true);
var mocCommentReactionRepository = MockCommentReactionRepository.GetCommentReactionRepository().Object;
var mockCommentRepository = MockCommentRepository.GetCommentRepository().Object;
var _handler = new MakeCommentReactionHandler(mocCommentReactionRepository,_mapper,mockCommentRepository);
var _handler = new MakeCommentReactionHandler(_mapper,_mockUnitOfWork.Object,_mediator.Object);
testReaction.ReactionType = "Like";

// act and assert
await Should.ThrowAsync<ValidationException>(async () =>
await _handler.Handle(new MakeReactionOnComment() { UserId = 1,ReactionData = testReaction}, CancellationToken.None));
}
Expand Down
Loading

0 comments on commit 20c42d7

Please sign in to comment.