From bb9139f9dc882c5ed5847c07b3c2cd79e3487f09 Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Mon, 29 May 2023 21:18:04 +0700 Subject: [PATCH 1/2] add: integration test --- .../Folders/Queries/GetFolderByIdTests.cs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 tests/Application.Tests.Integration/Folders/Queries/GetFolderByIdTests.cs diff --git a/tests/Application.Tests.Integration/Folders/Queries/GetFolderByIdTests.cs b/tests/Application.Tests.Integration/Folders/Queries/GetFolderByIdTests.cs new file mode 100644 index 00000000..d4d078b6 --- /dev/null +++ b/tests/Application.Tests.Integration/Folders/Queries/GetFolderByIdTests.cs @@ -0,0 +1,59 @@ +using Application.Folders.Queries; +using Domain.Entities; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Folders.Queries; + +public class GetFolderByIdTests : BaseClassFixture +{ + public GetFolderByIdTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldReturnFolder_WhenThatFolderExists() + { + // Arrange + var department = CreateDepartment(); + var folder = CreateFolder(); + var locker = CreateLocker(folder); + var room = CreateRoom(department, locker); + await AddAsync(room); + + var query = new GetFolderById.Query() + { + FolderId = folder.Id, + }; + + // Act + var result = await SendAsync(query); + + // Assert + result.Id.Should().Be(folder.Id); + result.Name.Should().Be(folder.Name); + + // Cleanup + Remove(folder); + Remove(locker); + Remove(room); + Remove(await FindAsync(department.Id)); + } + + [Fact] + public async Task ShouldThrowKeyNotFoundException_WhenThatFolderDoesNotExist() + { + // Arrange + var query = new GetFolderById.Query() + { + FolderId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Folder does not exist."); + } +} \ No newline at end of file From 4fb3e02442c273aebc12b7cca6852ff682a18b8e Mon Sep 17 00:00:00 2001 From: kaitoz11 <43519768+kaitoz11@users.noreply.github.com> Date: Thu, 1 Jun 2023 13:37:57 +0700 Subject: [PATCH 2/2] feat(GetFolderById.cs): implementation for get folder by id --- .../Folders/Queries/GetFolderById.cs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/Application/Folders/Queries/GetFolderById.cs b/src/Application/Folders/Queries/GetFolderById.cs index fd0d0b57..9b1d07df 100644 --- a/src/Application/Folders/Queries/GetFolderById.cs +++ b/src/Application/Folders/Queries/GetFolderById.cs @@ -1,5 +1,8 @@ +using Application.Common.Interfaces; using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Folders.Queries; @@ -9,4 +12,28 @@ public record Query : IRequest { public Guid FolderId { get; init; } } + + public class QueryHandler : IRequestHandler + { + private readonly IApplicationDbContext _context; + private readonly IMapper _mapper; + + public QueryHandler(IApplicationDbContext context, IMapper mapper) + { + _context = context; + _mapper = mapper; + } + + public async Task Handle(Query request, CancellationToken cancellationToken) + { + var folder = await _context.Folders.FirstOrDefaultAsync(x => x.Id.Equals(request.FolderId), cancellationToken); + + if (folder is null) + { + throw new KeyNotFoundException("Folder does not exist."); + } + + return _mapper.Map(folder); + } + } } \ No newline at end of file