Skip to content

Commit

Permalink
Merge pull request #125 from bcgov/yj
Browse files Browse the repository at this point in the history
fix: unit tests
  • Loading branch information
ychung-mot authored Apr 15, 2024
2 parents 9e8738b + e9f11e0 commit 940e27e
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 44 deletions.
17 changes: 15 additions & 2 deletions server/StrDss.Data/UnitOfWork.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using StrDss.Data.Entities;
using Microsoft.EntityFrameworkCore.Storage;
using StrDss.Data.Entities;

namespace StrDss.Data
{
public interface IUnitOfWork
{
bool Commit();
DssDbContext GetDbContext();
IDbContextTransaction BeginTransaction();
void CommitTransaction(IDbContextTransaction transaction);
}

public class UnitOfWork : IUnitOfWork
Expand All @@ -26,5 +28,16 @@ public DssDbContext GetDbContext()
{
return _dbContext;
}

public IDbContextTransaction BeginTransaction()
{
return _dbContext.Database.BeginTransaction();
}

public void CommitTransaction(IDbContextTransaction transaction)
{
transaction.Commit();
}

}
}
6 changes: 2 additions & 4 deletions server/StrDss.Service/DelistingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,7 @@ private async Task ProcessTakedownRequestBatchEmailAsync(OrganizationDto platfor

emailEntity.ExternalMessageNo = await template.SendEmail();

var dbContext = _unitOfWork.GetDbContext();

using var transaction = dbContext.Database.BeginTransaction();
using var transaction = _unitOfWork.BeginTransaction();

_unitOfWork.Commit();

Expand All @@ -465,7 +463,7 @@ private async Task ProcessTakedownRequestBatchEmailAsync(OrganizationDto platfor

_unitOfWork.Commit();

transaction.Commit();
_unitOfWork.CommitTransaction(transaction);
}
}
}
6 changes: 2 additions & 4 deletions server/StrDss.Service/UserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,7 @@ public async Task<Dictionary<string, List<string>>> CreateAccessRequestAsync(Acc
await _userRepo.UpdateUserAsync(userDto);
}

var dbContext = _unitOfWork.GetDbContext();

using var transaction = dbContext.Database.BeginTransaction();
using var transaction = _unitOfWork.BeginTransaction();

_unitOfWork.Commit();

Expand Down Expand Up @@ -162,7 +160,7 @@ public async Task<Dictionary<string, List<string>>> CreateAccessRequestAsync(Acc
_unitOfWork.Commit();
}

transaction.Commit();
_unitOfWork.CommitTransaction(transaction);

return errors;
}
Expand Down
77 changes: 43 additions & 34 deletions server/StrDss.Test/UserServiceShould.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,39 @@
using StrDss.Common;
using Microsoft.EntityFrameworkCore;
using StrDss.Data.Entities;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace StrDss.Test
{
public class UserServiceShould
{
//[Theory]
//[AutoDomainData]
//public async Task CreateAccessRequestAsync_ValidDto_NoErrors(
// AccessRequestCreateDto dto,
// UserDto userDto,
// OrganizationDto organizationDto,
// [Frozen] Mock<IUserRepository> userRepoMock,
// [Frozen] Mock<ICurrentUser> currentUserMock,
// [Frozen] Mock<IUnitOfWork> unitOfWorkMock,
// [Frozen] Mock<IOrganizationRepository> organizationRepoMock,
// [Frozen] Mock<IEmailMessageService> emailServiceMock,
// [Frozen] Mock<DssDbContext> dbContextMock,
// UserService sut)
//{
// // Arrange
// SetupCurrentUser(currentUserMock);
// SetupUserRepository(userRepoMock, (UserDto)null);
// SetupUnitOfWork(unitOfWorkMock, dbContextMock);
// SetupOrganizationRepository(organizationRepoMock, organizationDto);

// // Act
// var result = await sut.CreateAccessRequestAsync(dto);

// // Assert
// Assert.Empty(result);
// unitOfWorkMock.Verify(x => x.Commit(), Times.AtLeastOnce);
//}
[Theory]
[AutoDomainData]
public async Task CreateAccessRequestAsync_ValidDto_NoErrors(
AccessRequestCreateDto dto,
UserDto userDto,
OrganizationDto organizationDto,
[Frozen] Mock<IUserRepository> userRepoMock,
[Frozen] Mock<ICurrentUser> currentUserMock,
[Frozen] Mock<IUnitOfWork> unitOfWorkMock,
[Frozen] Mock<IOrganizationRepository> organizationRepoMock,
[Frozen] Mock<IEmailMessageService> emailServiceMock,
UserService sut)
{
// Arrange
SetupCurrentUser(currentUserMock);
SetupUserRepository(userRepoMock, null, userDto);
SetupUnitOfWork(unitOfWorkMock);
SetupOrganizationRepository(organizationRepoMock, organizationDto);

// Act
var result = await sut.CreateAccessRequestAsync(dto);

// Assert
Assert.Empty(result);
unitOfWorkMock.Verify(x => x.Commit(), Times.AtLeastOnce);
}

[Theory]
[AutoDomainData]
Expand Down Expand Up @@ -283,7 +284,7 @@ public async Task ApproveAccessRequest_NonIdirWithBcGovOrgType_ReturnsError(
UserDto userDto,
OrganizationDto orgDto,
[Frozen] Mock<IUserRepository> userRepoMock,
[Frozen] Mock<IOrganizationRepository> orgRepoMock,
[Frozen] Mock<IOrganizationRepository> orgRepoMock,
UserService sut)
{
// Arrange
Expand All @@ -302,7 +303,7 @@ public async Task ApproveAccessRequest_NonIdirWithBcGovOrgType_ReturnsError(
}


public void SetupCurrentUser(Mock<ICurrentUser> currentUserMock)
private void SetupCurrentUser(Mock<ICurrentUser> currentUserMock)
{
currentUserMock.Setup(x => x.UserGuid).Returns(Guid.NewGuid());
currentUserMock.Setup(x => x.DisplayName).Returns("Test User");
Expand All @@ -313,19 +314,27 @@ public void SetupCurrentUser(Mock<ICurrentUser> currentUserMock)
currentUserMock.Setup(x => x.BusinessNm).Returns("Test Business");
}

public void SetupUserRepository(Mock<IUserRepository> userRepoMock, UserDto userDto)
private void SetupUserRepository(Mock<IUserRepository> userRepoMock, UserDto? userDto1, UserDto userDto2)
{
userRepoMock.Setup(x => x.GetUserByGuid(It.IsAny<Guid>())).ReturnsAsync(userDto);
int callCount = 0;

userRepoMock.Setup(x => x.GetUserByGuid(It.IsAny<Guid>()))
.ReturnsAsync(() =>
{
callCount++;
return callCount == 1 ? userDto1 : userDto2;
});

userRepoMock.Setup(x => x.CreateUserAsync(It.IsAny<UserCreateDto>())).Returns(Task.CompletedTask);
userRepoMock.Setup(x => x.GetAdminUsers()).ReturnsAsync(new List<UserDto> { userDto });
userRepoMock.Setup(x => x.GetAdminUsers()).ReturnsAsync(new List<UserDto> { userDto2 });
}

public void SetupUnitOfWork(Mock<IUnitOfWork> unitOfWorkMock, Mock<DssDbContext> dbContextMock)
private void SetupUnitOfWork(Mock<IUnitOfWork> unitOfWorkMock)
{
unitOfWorkMock.Setup(x => x.Commit()).Verifiable();
}

public void SetupOrganizationRepository(Mock<IOrganizationRepository> organizationRepoMock, OrganizationDto organizationDto)
private void SetupOrganizationRepository(Mock<IOrganizationRepository> organizationRepoMock, OrganizationDto organizationDto)
{
organizationRepoMock.Setup(x => x.GetOrganizationTypesAsnc()).ReturnsAsync(new List<OrganizationTypeDto>
{
Expand Down

0 comments on commit 940e27e

Please sign in to comment.