From 3632b2bde02b94fb95ebcb8d244fb81b9c7d288c Mon Sep 17 00:00:00 2001 From: Nguyen Quang Chien Date: Mon, 29 May 2023 12:54:05 +0700 Subject: [PATCH 1/2] add: integration test --- .../Queries/GetDepartmentByIdTests.cs | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs diff --git a/tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs b/tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs new file mode 100644 index 00000000..3da615d4 --- /dev/null +++ b/tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs @@ -0,0 +1,50 @@ +using Application.Departments.Queries; +using FluentAssertions; +using Xunit; + +namespace Application.Tests.Integration.Departments.Queries; + +public class GetDepartmentByIdTests : BaseClassFixture +{ + public GetDepartmentByIdTests(CustomApiFactory apiFactory) : base(apiFactory) + { + } + + [Fact] + public async Task ShouldReturnDepartment_WhenThatDepartmentExists() + { + // Arrange + var department = CreateDepartment(); + + var query = new GetDepartmentById.Query() + { + DepartmentId = department.Id, + }; + + // Act + var result = await SendAsync(query); + + // Assert + result.Name.Should().Be(department.Name); + + // Cleanup + Remove(department); + } + + [Fact] + public async Task ShouldThrowNotFoundException_WhenThatDepartmentDoesNotExist() + { + // Arrange + var query = new GetDepartmentById.Query() + { + DepartmentId = Guid.NewGuid(), + }; + + // Act + var action = async () => await SendAsync(query); + + // Assert + await action.Should().ThrowAsync() + .WithMessage("Department does not exist."); + } +} \ No newline at end of file From 9a8a3f4b6fda7faecd0b7c6be1c149bcfa6d66fb Mon Sep 17 00:00:00 2001 From: StarryFolf Date: Thu, 1 Jun 2023 21:09:28 +0700 Subject: [PATCH 2/2] feat: implement get department by id --- .../Departments/Queries/GetDepartmentById.cs | 28 +++++++++++++++++++ .../Queries/GetDepartmentByIdTests.cs | 2 ++ 2 files changed, 30 insertions(+) diff --git a/src/Application/Departments/Queries/GetDepartmentById.cs b/src/Application/Departments/Queries/GetDepartmentById.cs index e4120c64..83f3f725 100644 --- a/src/Application/Departments/Queries/GetDepartmentById.cs +++ b/src/Application/Departments/Queries/GetDepartmentById.cs @@ -1,5 +1,9 @@ +using Application.Common.Interfaces; using Application.Common.Models.Dtos; +using Application.Common.Models.Dtos.Physical; +using AutoMapper; using MediatR; +using Microsoft.EntityFrameworkCore; namespace Application.Departments.Queries; @@ -9,4 +13,28 @@ public record Query : IRequest { public Guid DepartmentId { 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 department = await _context.Departments.FirstOrDefaultAsync(x => x.Id.Equals(request.DepartmentId), cancellationToken); + + if (department is null) + { + throw new KeyNotFoundException("Department does not exist."); + } + + return _mapper.Map(department); + } + } + } \ No newline at end of file diff --git a/tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs b/tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs index 3da615d4..af7d9df8 100644 --- a/tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs +++ b/tests/Application.Tests.Integration/Departments/Queries/GetDepartmentByIdTests.cs @@ -16,6 +16,8 @@ public async Task ShouldReturnDepartment_WhenThatDepartmentExists() // Arrange var department = CreateDepartment(); + await AddAsync(department); + var query = new GetDepartmentById.Query() { DepartmentId = department.Id,