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 borrow document#208
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
3 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
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
354 changes: 354 additions & 0 deletions
354 tests/Application.Tests.Integration/Borrows/Commands/BorrowDocumentTests.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,354 @@ | ||
| using Application.Borrows.Commands; | ||
| using Application.Common.Exceptions; | ||
| using Application.Identity; | ||
| using Domain.Entities; | ||
| 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 BorrowDocumentTests : BaseClassFixture | ||
| { | ||
| public BorrowDocumentTests(CustomApiFactory apiFactory) : base(apiFactory) | ||
| { | ||
| } | ||
| [Fact] | ||
| public async Task ShouldCreateBorrowRequest_WhenDetailsAreValid() | ||
| { | ||
| // Arrange | ||
| using var scope = ScopeFactory.CreateScope(); | ||
| var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); | ||
| var department = CreateDepartment(); | ||
| await context.AddAsync(department); | ||
| var user = CreateUser(IdentityData.Roles.Employee, "aaaaaa"); | ||
| user.Department = department; | ||
| await context.AddAsync(user); | ||
| var document = CreateNDocuments(1).First(); | ||
| document.Status = DocumentStatus.Available; | ||
| document.Department = department; | ||
| await context.AddAsync(document); | ||
| await context.SaveChangesAsync(); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user.Id, | ||
| DocumentId = document.Id, | ||
| Reason = "Example", | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromHours(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| }; | ||
| // Act | ||
| var result = await SendAsync(command); | ||
| // Assert | ||
| result.DocumentId.Should().Be(command.DocumentId); | ||
| result.BorrowerId.Should().Be(command.BorrowerId); | ||
| result.Reason.Should().Be(command.Reason); | ||
| result.BorrowTime.Should().Be(command.BorrowFrom); | ||
| result.DueTime.Should().Be(command.BorrowTo); | ||
| result.Status.Should().Be(BorrowRequestStatus.Pending.ToString()); | ||
| // Cleanup | ||
| Remove(await FindAsync<Borrow>(result.Id)); | ||
| Remove(user); | ||
| Remove(document); | ||
| Remove(await FindAsync<Department>(department.Id)); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowKeyNotFoundException_WhenUserDoesNotExist() | ||
| { | ||
| // Arrange | ||
| var document = CreateNDocuments(1).First(); | ||
ChienNQuang marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| document.Status = DocumentStatus.Available; | ||
| await AddAsync(document); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = Guid.NewGuid(), | ||
| DocumentId = document.Id, | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<KeyNotFoundException>() | ||
| .WithMessage("User does not exist."); | ||
| // Cleanup | ||
| Remove(document); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowConflictException_WhenUserIsNotActive() | ||
| { | ||
| // Arrange | ||
| var document = CreateNDocuments(1).First(); | ||
ChienNQuang marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| document.Status = DocumentStatus.Available; | ||
| await AddAsync(document); | ||
| var user = CreateUser(IdentityData.Roles.Employee, "aaaaaa"); | ||
| user.IsActive = false; | ||
| await AddAsync(user); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user.Id, | ||
| DocumentId = document.Id, | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("User is not active."); | ||
| // Cleanup | ||
| Remove(document); | ||
| Remove(user); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowConflictException_WhenUserIsNotActivated() | ||
| { | ||
| // Arrange | ||
| var document = CreateNDocuments(1).First(); | ||
ChienNQuang marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| document.Status = DocumentStatus.Available; | ||
| await AddAsync(document); | ||
| var user = CreateUser(IdentityData.Roles.Employee, "aaaaaa"); | ||
| user.IsActivated = false; | ||
| await AddAsync(user); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user.Id, | ||
| DocumentId = document.Id, | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("User is not activated."); | ||
| // Cleanup | ||
| Remove(document); | ||
| Remove(user); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowKeyNotFoundException_WhenDocumentDoesNotExist() | ||
| { | ||
| // Arrange | ||
| var user = CreateUser(IdentityData.Roles.Employee, "bbbbbb"); | ||
| await AddAsync(user); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user.Id, | ||
| DocumentId = Guid.NewGuid(), | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<KeyNotFoundException>() | ||
| .WithMessage("Document does not exist."); | ||
| // Cleanup | ||
| Remove(user); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldConflictException_WhenDocumentIsNotAvailable() | ||
| { | ||
| // Arrange | ||
| var user = CreateUser(IdentityData.Roles.Employee, "bbbbbb"); | ||
| await AddAsync(user); | ||
| var document = CreateNDocuments(1).First(); | ||
| document.Status = DocumentStatus.Borrowed; | ||
| await AddAsync(document); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user.Id, | ||
| DocumentId = document.Id, | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("Document is not available."); | ||
| // Cleanup | ||
| Remove(user); | ||
| Remove(document); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldConflictException_WhenUserAndDocumentDoesNotBelongToTheSameDepartment() | ||
| { | ||
| // Arrange | ||
| using var scope = ScopeFactory.CreateScope(); | ||
| var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); | ||
| var department1 = CreateDepartment(); | ||
| var department2 = CreateDepartment(); | ||
| var user = CreateUser(IdentityData.Roles.Employee, "bbbbbb"); | ||
| user.Department = department1; | ||
| await context.AddAsync(user); | ||
| var document = CreateNDocuments(1).First(); | ||
| document.Status = DocumentStatus.Available; | ||
| document.Department = department2; | ||
| await context.AddAsync(document); | ||
| await context.SaveChangesAsync(); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user.Id, | ||
| DocumentId = document.Id, | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("User is not allowed to borrow this document."); | ||
| // Cleanup | ||
| Remove(user); | ||
| Remove(document); | ||
| Remove(department1); | ||
| Remove(department2); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowConflictException_WhenRequestWithSameUserAndDocumentAlreadyExists() | ||
| { | ||
| // Arrange | ||
| using var scope = ScopeFactory.CreateScope(); | ||
| var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); | ||
| var department = CreateDepartment(); | ||
| var user = CreateUser(IdentityData.Roles.Employee, "bbbbbb"); | ||
| user.Department = department; | ||
| var document = CreateNDocuments(1).First(); | ||
| document.Status = DocumentStatus.Available; | ||
| document.Department = department; | ||
| var borrow = CreateBorrowRequest(user, document, BorrowRequestStatus.Pending); | ||
| await context.AddAsync(borrow); | ||
| await context.SaveChangesAsync(); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user.Id, | ||
| DocumentId = document.Id, | ||
| BorrowFrom = DateTime.Now.Add(TimeSpan.FromDays(1)), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("This document is already requested borrow from the same user."); | ||
| // Cleanup; | ||
| Remove(borrow); | ||
| Remove(user); | ||
| Remove(document); | ||
| Remove(department); | ||
| } | ||
| [Fact] | ||
| public async Task ShouldThrowConflictException_WhenARequestIsMadeWhileDocumentIsAlreadyBeingBorrowed() | ||
| { | ||
| // Arrange | ||
| using var scope = ScopeFactory.CreateScope(); | ||
| var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); | ||
| var department = CreateDepartment(); | ||
| var user1 = CreateUser(IdentityData.Roles.Employee, "bbbbbb"); | ||
| user1.Department = department; | ||
| var user2 = CreateUser(IdentityData.Roles.Employee, "bbbbbb"); | ||
| user2.Department = department; | ||
| await context.AddAsync(user2); | ||
| var document = CreateNDocuments(1).First(); | ||
| document.Status = DocumentStatus.Available; | ||
| document.Department = department; | ||
| var borrow = CreateBorrowRequest(user1, document, BorrowRequestStatus.Approved); | ||
| await context.AddAsync(borrow); | ||
| await context.SaveChangesAsync(); | ||
| var command = new BorrowDocument.Command() | ||
| { | ||
| BorrowerId = user2.Id, | ||
| DocumentId = document.Id, | ||
| BorrowFrom = DateTime.Now.AddHours(1), | ||
| BorrowTo = DateTime.Now.Add(TimeSpan.FromDays(2)), | ||
| Reason = "Example", | ||
| }; | ||
| // Act | ||
| var result = async () => await SendAsync(command); | ||
| // Assert | ||
| await result.Should().ThrowAsync<ConflictException>() | ||
| .WithMessage("This document cannot be borrowed."); | ||
| // Cleanup; | ||
| Remove(borrow); | ||
| Remove(user1); | ||
| Remove(user2); | ||
| Remove(document); | ||
| Remove(department); | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really don't think using DateTime.Now is a good practice, but we'll investigate later