Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
test: add integration tests for approve borrow request#204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
168 changes: 168 additions & 0 deletions
168 tests/Application.Tests.Integration/Borrows/Commands/ApproveBorrowRequestTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| using Application.Borrows.Commands; | ||
| using Application.Common.Exceptions; | ||
| using Application.Identity; | ||
| using Domain.Entities.Physical; | ||
| using Domain.Statuses; | ||
| using FluentAssertions; | ||
| using Infrastructure.Persistence; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using NodaTime; | ||
| using Xunit; | ||
| namespace Application.Tests.Integration.Borrows.Commands; | ||
| public class ApproveBorrowRequestTests : BaseClassFixture | ||
| { | ||
| public ApproveBorrowRequestTests(CustomApiFactory apiFactory) : base(apiFactory) | ||
| { | ||
| } | ||
| [Fact] | ||
| public async Task ShouldApproveRequest_WhenRequestIsValid() | ||
| { | ||
| // Arrange | ||
| var document = CreateNDocuments(1).First(); | ||
| var user = CreateUser(IdentityData.Roles.Employee, "abcdef"); | ||
| var request = CreateBorrowRequest(user, document, BorrowRequestStatus.Pending); | ||
| await AddAsync(request); | ||
| var command = new ApproveBorrowRequest.Command() | ||
| { | ||
| BorrowId = request.Id, | ||
| }; | ||
| // Act | ||
| var result = await SendAsync(command); | ||
| // Assert | ||
| result.Status.Should().Be(BorrowRequestStatus.Approved.ToString()); | ||
| // Cleanup | ||
| Remove(request); | ||
| Remove(user); | ||
| Remove(document); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowKeyNotFoundException_WhenRequestDoesNotExist() | ||
| { | ||
| // Arrange | ||
| var command = new ApproveBorrowRequest.Command() | ||
| { | ||
| BorrowId = Guid.NewGuid(), | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<KeyNotFoundException>() | ||
| .WithMessage("Borrow request does not exist."); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowConflictException_WhenDocumentIsLost() | ||
| { | ||
| // Arrange | ||
| var document = CreateNDocuments(1).First(); | ||
| document.Status = DocumentStatus.Lost; | ||
| var user = CreateUser(IdentityData.Roles.Employee, "abcdef"); | ||
| var request = CreateBorrowRequest(user, document, BorrowRequestStatus.Pending); | ||
| await AddAsync(request); | ||
| var command = new ApproveBorrowRequest.Command() | ||
| { | ||
| BorrowId = request.Id, | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("Document is lost. Request is unprocessable."); | ||
| (await FindAsync<Borrow>(request.Id))!.Status.Should().Be(BorrowRequestStatus.NotProcessable); | ||
| // Cleanup | ||
| Remove(request); | ||
| Remove(user); | ||
| Remove(document); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowConflictException_WhenRequestStatusIsNotPendingAndRejected() | ||
| { | ||
| var document = CreateNDocuments(1).First(); | ||
| var user = CreateUser(IdentityData.Roles.Employee, "abcdef"); | ||
| var request = CreateBorrowRequest(user, document, BorrowRequestStatus.CheckedOut); | ||
| await AddAsync(request); | ||
| var command = new ApproveBorrowRequest.Command() | ||
| { | ||
| BorrowId = request.Id, | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("Request cannot be approved."); | ||
| // Cleanup | ||
| Remove(request); | ||
| Remove(user); | ||
| Remove(document); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowConflictException_WhenRequestTimespanOverlapAnApprovedOrCheckedOutRequestTimespan() | ||
| { | ||
| using var scope = ScopeFactory.CreateScope(); | ||
| var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); | ||
| var document = CreateNDocuments(1).First(); | ||
| var user1 = CreateUser(IdentityData.Roles.Employee, "abcdef"); | ||
| var user2 = CreateUser(IdentityData.Roles.Employee, "aaaaaa"); | ||
| var request1 = CreateBorrowRequest(user1, document, BorrowRequestStatus.Approved); | ||
| var request2 = CreateBorrowRequest(user2, document, BorrowRequestStatus.Pending); | ||
| request2.BorrowTime = request2.BorrowTime.Plus(Period.FromMinutes(30)); | ||
| request2.DueTime = request2.DueTime.Plus(Period.FromHours(1)); | ||
| await context.AddAsync(request1); | ||
| await context.AddAsync(request2); | ||
| await context.SaveChangesAsync(); | ||
| var command = new ApproveBorrowRequest.Command() | ||
| { | ||
| BorrowId = request2.Id, | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("This document cannot be borrowed."); | ||
| // Cleanup | ||
| Remove(request1); | ||
| Remove(request2); | ||
| Remove(user1); | ||
| Remove(user2); | ||
| Remove(document); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.