Files
2026-03-17 09:53:09 -05:00

116 lines
4.2 KiB
C#

using LCU.API.Internal.Operations.IncentiveApi.Services;
using LCU.Library.Internal.IncentiveApiModels.Entities;
using LCU.Library.Internal.IncentiveApiModels.Enums;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;
using System;
using System.Linq;
using System.Threading.Tasks;
using LCU.API.Internal.Operations.IncentiveApiLib.Persistence;
using LCU.API.Internal.Operations.IncentiveApiLib.Repositories;
namespace Application.UnitTests.Repository
{
public class UserAssignmentRepositoryTest
{
private ApplicationDbContext _context;
private UserAssignmentRepository _repository;
private Mock<IDateTime> _mockDateTime;
[SetUp]
public void Setup()
{
_mockDateTime = new Mock<IDateTime>();
var mockCurrentUserService = Mock.Of<ICurrentUserService>();
Mock.Get(mockCurrentUserService).Setup(s => s.GetCurrentUser()).Returns(new ApplicationUser()
{
Email = "[email protected]",
});
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.EnableSensitiveDataLogging(true)
.Options;
_context = new ApplicationDbContext(options, mockCurrentUserService, _mockDateTime.Object);
_repository = new UserAssignmentRepository(_context);
}
[Test]
public async Task Basic_Flow_test()
{
var mockDate = new DateTime(2021, 8, 17, 1, 2, 3, 456);
_mockDateTime.SetupGet(mock => mock.Now).Returns(mockDate);
var saved = await _repository.GetAllUserAssignmentsAsync();
Assert.NotNull(saved);
Assert.IsEmpty(saved);
var newAssignment = new UserAssignment
{
Id = 1,
Key = "key",
Type = AssignmentType.RegionalManager,
AppUser = new AppUser {
BaseRole = "Role",
FullName = "Test",
HomeDepartment = "Test",
Role = Role.Admin,
EmployeeId = "123",
AdEmail = "[email protected]",
PersonId = 456
}
};
var newId = await _context.AddAsync(newAssignment);
await _context.SaveChangesAsync();
saved = await _repository.GetAllUserAssignmentsAsync();
Assert.NotNull(saved);
Assert.IsNotEmpty(saved);
var first = saved.First();
}
[Test]
public async Task FindExistingAssignment_Match()
{
var mockDate = new DateTime(2021, 8, 17, 1, 2, 3, 456);
_mockDateTime.SetupGet(mock => mock.Now).Returns(mockDate);
var saved = await _repository.GetAllUserAssignmentsAsync();
Assert.NotNull(saved);
Assert.IsEmpty(saved);
var newAssignment = new UserAssignment
{
Id = 1,
Key = "NORTH",
Type = AssignmentType.RegionalManager,
AppUser = new AppUser
{
BaseRole = "Role",
FullName = "Test",
HomeDepartment = "Test",
Role = Role.Admin,
EmployeeId = "123",
AdEmail = "[email protected]",
PersonId = 456
}
};
var newId = await _context.AddAsync(newAssignment);
await _context.SaveChangesAsync();
var notFound = await _repository.FindExistingAssignmentAsync(AssignmentType.RegionalManager, "CENTRAL");
Assert.Null(notFound);
var found = await _repository.FindExistingAssignmentAsync(AssignmentType.RegionalManager, "NORTH");
Assert.NotNull(found);
Assert.AreEqual(1, found.Id);
Assert.AreEqual("NORTH", found.Key);
Assert.AreEqual(AssignmentType.RegionalManager, found.Type);
Assert.AreEqual("123", found.AppUser.EmployeeId);
Assert.AreEqual("[email protected]", found.AppUser.AdEmail);
Assert.AreEqual(456, found.AppUser.PersonId);
}
}
}